Skip to content

Commit 07ec1d6

Browse files
committed
comments etc
1 parent 535e619 commit 07ec1d6

File tree

8 files changed

+72
-84
lines changed

8 files changed

+72
-84
lines changed

balancer/grpclb/grpclb_remote_balancer.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"google.golang.org/grpc/connectivity"
3636
"google.golang.org/grpc/credentials/insecure"
3737
"google.golang.org/grpc/internal/backoff"
38-
"google.golang.org/grpc/internal/channelz"
3938
imetadata "google.golang.org/grpc/internal/metadata"
4039
"google.golang.org/grpc/keepalive"
4140
"google.golang.org/grpc/metadata"
@@ -240,9 +239,7 @@ func (lb *lbBalancer) newRemoteBalancerCCWrapper() {
240239
// Explicitly set pickfirst as the balancer.
241240
dopts = append(dopts, grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy":"pick_first"}`))
242241
dopts = append(dopts, grpc.WithResolvers(lb.manualResolver))
243-
if channelz.IsOn() {
244-
dopts = append(dopts, grpc.WithChannelzParentID(lb.opt.ChannelzParentID))
245-
}
242+
dopts = append(dopts, grpc.WithChannelzParentID(lb.opt.ChannelzParentID))
246243

247244
// Enable Keepalive for grpclb client.
248245
dopts = append(dopts, grpc.WithKeepaliveParams(keepalive.ClientParameters{

channelz/channelz.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ package channelz
3131

3232
import "google.golang.org/grpc/internal/channelz"
3333

34-
// TODO(easwars): Add comment.
34+
// Identifier is an opaque identifier which uniquely identifies an entity in the
35+
// channelz database.
3536
type Identifier = channelz.Identifier

clientconn.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"google.golang.org/grpc/internal/backoff"
3939
"google.golang.org/grpc/internal/channelz"
4040
"google.golang.org/grpc/internal/grpcsync"
41+
"google.golang.org/grpc/internal/pretty"
4142
iresolver "google.golang.org/grpc/internal/resolver"
4243
"google.golang.org/grpc/internal/transport"
4344
"google.golang.org/grpc/keepalive"
@@ -1320,7 +1321,7 @@ func (ac *addrConn) createTransport(addr resolver.Address, copts transport.Conne
13201321
newTr, err := transport.NewClientTransport(connectCtx, ac.cc.ctx, addr, copts, func() { prefaceReceived.Fire() }, onGoAway, onClose)
13211322
if err != nil {
13221323
// newTr is either nil, or closed.
1323-
channelz.Warningf(logger, ac.channelzID, "grpc: addrConn.createTransport failed to connect to %v. Err: %v", addr, err)
1324+
channelz.Warningf(logger, ac.channelzID, "grpc: addrConn.createTransport failed to connect to %s. Err: %v", pretty.ToJSON(addr), err)
13241325
return err
13251326
}
13261327

@@ -1333,7 +1334,7 @@ func (ac *addrConn) createTransport(addr resolver.Address, copts transport.Conne
13331334
newTr.Close(transport.ErrConnClosing)
13341335
if connectCtx.Err() == context.DeadlineExceeded {
13351336
err := errors.New("failed to receive server preface within timeout")
1336-
channelz.Warningf(logger, ac.channelzID, "grpc: addrConn.createTransport failed to connect to %v: %v", addr, err)
1337+
channelz.Warningf(logger, ac.channelzID, "grpc: addrConn.createTransport failed to connect to %s: %v", pretty.ToJSON(addr), err)
13371338
return err
13381339
}
13391340
return nil

internal/channelz/id.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,45 @@ package channelz
2020

2121
import "fmt"
2222

23+
// Identifier is an opaque identifier which uniquely identifies an entity in the
24+
// channelz database.
2325
type Identifier struct {
2426
typ RefChannelType
2527
id int64
2628
str string
2729
pid *Identifier
2830
}
2931

32+
// Type returns the entity type corresponding to id.
3033
func (id *Identifier) Type() RefChannelType {
3134
if id == nil {
3235
return RefUnknown
3336
}
3437
return id.typ
3538
}
3639

40+
// Int returns the integer identifier corresponding to id.
3741
func (id *Identifier) Int() int64 {
3842
if id == nil {
3943
return 0
4044
}
4145
return id.id
4246
}
4347

48+
// String returns a string representation of the entity corresponding to id.
49+
50+
// This includes some information about the parent as well. Examples:
51+
// Top-level channel: [Channel #channel-number]
52+
// Nested channel: [Channel #parent-channel-number Channel #channel-number]
53+
// Sub channel: [Channel #parent-channel SubChannel #subchannel-number]
4454
func (id *Identifier) String() string {
4555
if id == nil {
4656
return ""
4757
}
4858
return id.str
4959
}
5060

61+
// Equal returns true if other is the same as id.
5162
func (id *Identifier) Equal(other *Identifier) bool {
5263
if (id != nil) != (other != nil) {
5364
return false
@@ -58,6 +69,8 @@ func (id *Identifier) Equal(other *Identifier) bool {
5869
return id.typ == other.typ && id.id == other.id && id.pid == other.pid
5970
}
6071

72+
// NewIdentifierForTesting returns a new opaque identifier to be used only for
73+
// testing purposes.
6174
func NewIdentifierForTesting(typ RefChannelType, id int64, pid *Identifier) *Identifier {
6275
return newIdentifer(typ, id, pid)
6376
}

internal/channelz/logging.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ import (
2626

2727
var logger = grpclog.Component("channelz")
2828

29+
func withParens(id *Identifier) string {
30+
return "[" + id.String() + "] "
31+
}
32+
2933
// Info logs and adds a trace event if channelz is on.
3034
func Info(l grpclog.DepthLoggerV2, id *Identifier, args ...interface{}) {
31-
msg := "[" + id.String() + "] " + fmt.Sprint(args...)
35+
msg := withParens(id) + fmt.Sprint(args...)
3236
if IsOn() {
3337
AddTraceEvent(l, id, 1, &TraceEventDesc{
3438
Desc: msg,
@@ -41,7 +45,7 @@ func Info(l grpclog.DepthLoggerV2, id *Identifier, args ...interface{}) {
4145

4246
// Infof logs and adds a trace event if channelz is on.
4347
func Infof(l grpclog.DepthLoggerV2, id *Identifier, format string, args ...interface{}) {
44-
msg := "[" + id.String() + "] " + fmt.Sprintf(format, args...)
48+
msg := withParens(id) + fmt.Sprintf(format, args...)
4549
if IsOn() {
4650
AddTraceEvent(l, id, 1, &TraceEventDesc{
4751
Desc: msg,
@@ -54,7 +58,7 @@ func Infof(l grpclog.DepthLoggerV2, id *Identifier, format string, args ...inter
5458

5559
// Warning logs and adds a trace event if channelz is on.
5660
func Warning(l grpclog.DepthLoggerV2, id *Identifier, args ...interface{}) {
57-
msg := "[" + id.String() + "] " + fmt.Sprint(args...)
61+
msg := withParens(id) + fmt.Sprint(args...)
5862
if IsOn() {
5963
AddTraceEvent(l, id, 1, &TraceEventDesc{
6064
Desc: msg,
@@ -67,7 +71,7 @@ func Warning(l grpclog.DepthLoggerV2, id *Identifier, args ...interface{}) {
6771

6872
// Warningf logs and adds a trace event if channelz is on.
6973
func Warningf(l grpclog.DepthLoggerV2, id *Identifier, format string, args ...interface{}) {
70-
msg := "[" + id.String() + "] " + fmt.Sprintf(format, args...)
74+
msg := withParens(id) + fmt.Sprintf(format, args...)
7175
if IsOn() {
7276
AddTraceEvent(l, id, 1, &TraceEventDesc{
7377
Desc: msg,
@@ -80,7 +84,7 @@ func Warningf(l grpclog.DepthLoggerV2, id *Identifier, format string, args ...in
8084

8185
// Error logs and adds a trace event if channelz is on.
8286
func Error(l grpclog.DepthLoggerV2, id *Identifier, args ...interface{}) {
83-
msg := "[" + id.String() + "] " + fmt.Sprint(args...)
87+
msg := withParens(id) + fmt.Sprint(args...)
8488
if IsOn() {
8589
AddTraceEvent(l, id, 1, &TraceEventDesc{
8690
Desc: msg,
@@ -93,7 +97,7 @@ func Error(l grpclog.DepthLoggerV2, id *Identifier, args ...interface{}) {
9397

9498
// Errorf logs and adds a trace event if channelz is on.
9599
func Errorf(l grpclog.DepthLoggerV2, id *Identifier, format string, args ...interface{}) {
96-
msg := "[" + id.String() + "] " + fmt.Sprintf(format, args...)
100+
msg := withParens(id) + fmt.Sprintf(format, args...)
97101
if IsOn() {
98102
AddTraceEvent(l, id, 1, &TraceEventDesc{
99103
Desc: msg,

internal/transport/http2_client.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,9 @@ func newHTTP2Client(connectCtx, ctx context.Context, addr resolver.Address, opts
351351
}
352352
t.statsHandler.HandleConn(t.ctx, connBegin)
353353
}
354-
if channelz.IsOn() {
355-
var err error
356-
t.channelzID, err = channelz.RegisterNormalSocket(t, opts.ChannelzParentID, fmt.Sprintf("%s -> %s", t.localAddr, t.remoteAddr))
357-
if err != nil {
358-
// TODO(easwars): See if you need to return a more meaningful error.
359-
// TODO(easwars): Also, check if the transport needs to be closed before returning.
360-
return nil, err
361-
}
354+
t.channelzID, err = channelz.RegisterNormalSocket(t, opts.ChannelzParentID, fmt.Sprintf("%s -> %s", t.localAddr, t.remoteAddr))
355+
if err != nil {
356+
return nil, err
362357
}
363358
if t.keepaliveEnabled {
364359
t.kpDormancyCond = sync.NewCond(&t.mu)
@@ -904,9 +899,7 @@ func (t *http2Client) Close(err error) {
904899
t.controlBuf.finish()
905900
t.cancel()
906901
t.conn.Close()
907-
if channelz.IsOn() {
908-
channelz.RemoveEntry(t.channelzID)
909-
}
902+
channelz.RemoveEntry(t.channelzID)
910903
// Append info about previous goaways if there were any, since this may be important
911904
// for understanding the root cause for this connection to be closed.
912905
_, goAwayDebugMessage := t.GetGoAwayReason()

internal/transport/http2_server.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,9 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
275275
connBegin := &stats.ConnBegin{}
276276
t.stats.HandleConn(t.ctx, connBegin)
277277
}
278-
if channelz.IsOn() {
279-
var err error
280-
t.channelzID, err = channelz.RegisterNormalSocket(t, config.ChannelzParentID, fmt.Sprintf("%s -> %s", t.remoteAddr, t.localAddr))
281-
if err != nil {
282-
// TODO(easwars): See if you need to return a more meaningful error.
283-
// TODO(easwars): Also, check if the transport needs to be closed before returning.
284-
return nil, err
285-
}
278+
t.channelzID, err = channelz.RegisterNormalSocket(t, config.ChannelzParentID, fmt.Sprintf("%s -> %s", t.remoteAddr, t.localAddr))
279+
if err != nil {
280+
return nil, err
286281
}
287282

288283
t.connectionID = atomic.AddUint64(&serverConnectionCounter, 1)
@@ -1215,9 +1210,7 @@ func (t *http2Server) Close() {
12151210
if err := t.conn.Close(); err != nil && logger.V(logLevel) {
12161211
logger.Infof("transport: error closing conn during Close: %v", err)
12171212
}
1218-
if channelz.IsOn() {
1219-
channelz.RemoveEntry(t.channelzID)
1220-
}
1213+
channelz.RemoveEntry(t.channelzID)
12211214
// Cancel all active streams.
12221215
for _, s := range streams {
12231216
s.cancel()

server.go

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,7 @@ func NewServer(opt ...ServerOption) *Server {
584584
s.initServerWorkers()
585585
}
586586

587-
if channelz.IsOn() {
588-
s.channelzID = channelz.RegisterServer(&channelzServer{s}, "")
589-
}
587+
s.channelzID = channelz.RegisterServer(&channelzServer{s}, "")
590588
return s
591589
}
592590

@@ -724,9 +722,7 @@ func (l *listenSocket) ChannelzMetric() *channelz.SocketInternalMetric {
724722

725723
func (l *listenSocket) Close() error {
726724
err := l.Listener.Close()
727-
if channelz.IsOn() {
728-
channelz.RemoveEntry(l.channelzID)
729-
}
725+
channelz.RemoveEntry(l.channelzID)
730726
return err
731727
}
732728

@@ -737,52 +733,50 @@ func (l *listenSocket) Close() error {
737733
// this method returns.
738734
// Serve will return a non-nil error unless Stop or GracefulStop is called.
739735
func (s *Server) Serve(lis net.Listener) error {
740-
s.mu.Lock()
741-
s.printf("serving")
742-
s.serve = true
743-
if s.lis == nil {
744-
// Serve called after Stop or GracefulStop.
745-
s.mu.Unlock()
746-
lis.Close()
747-
return ErrServerStopped
748-
}
736+
if err := func() error { // Anonymous func to be able to defer the unlock.
737+
s.mu.Lock()
738+
defer s.mu.Unlock()
749739

750-
s.serveWG.Add(1)
751-
defer func() {
752-
s.serveWG.Done()
753-
if s.quit.HasFired() {
754-
// Stop or GracefulStop called; block until done and return nil.
755-
<-s.done.Done()
740+
s.printf("serving")
741+
s.serve = true
742+
if s.lis == nil {
743+
lis.Close()
744+
return ErrServerStopped
756745
}
757-
}()
758746

759-
ls := &listenSocket{Listener: lis}
760-
s.lis[ls] = true
747+
s.serveWG.Add(1)
748+
defer func() {
749+
s.serveWG.Done()
750+
if s.quit.HasFired() {
751+
// Stop or GracefulStop called; block until done and return nil.
752+
<-s.done.Done()
753+
}
754+
}()
755+
756+
ls := &listenSocket{Listener: lis}
757+
s.lis[ls] = true
761758

762-
if channelz.IsOn() {
763759
var err error
764760
ls.channelzID, err = channelz.RegisterListenSocket(ls, s.channelzID, lis.Addr().String())
765761
if err != nil {
766-
// TODO(easwars): See if you can defer this unlock somehow.
767-
s.mu.Unlock()
768762
lis.Close()
769-
// TODO(easwars): return a more meaningful error.
770763
return err
771764
}
772-
}
773-
s.mu.Unlock()
774765

775-
defer func() {
776-
s.mu.Lock()
777-
if s.lis != nil && s.lis[ls] {
778-
ls.Close()
779-
delete(s.lis, ls)
780-
}
781-
s.mu.Unlock()
782-
}()
766+
defer func() {
767+
s.mu.Lock()
768+
if s.lis != nil && s.lis[ls] {
769+
ls.Close()
770+
delete(s.lis, ls)
771+
}
772+
s.mu.Unlock()
773+
}()
774+
return nil
775+
}(); err != nil {
776+
return err
777+
}
783778

784779
var tempDelay time.Duration // how long to sleep on accept failure
785-
786780
for {
787781
rawConn, err := lis.Accept()
788782
if err != nil {
@@ -1717,11 +1711,7 @@ func (s *Server) Stop() {
17171711
s.done.Fire()
17181712
}()
17191713

1720-
s.channelzRemoveOnce.Do(func() {
1721-
if channelz.IsOn() {
1722-
channelz.RemoveEntry(s.channelzID)
1723-
}
1724-
})
1714+
s.channelzRemoveOnce.Do(func() { channelz.RemoveEntry(s.channelzID) })
17251715

17261716
s.mu.Lock()
17271717
listeners := s.lis
@@ -1759,11 +1749,7 @@ func (s *Server) GracefulStop() {
17591749
s.quit.Fire()
17601750
defer s.done.Fire()
17611751

1762-
s.channelzRemoveOnce.Do(func() {
1763-
if channelz.IsOn() {
1764-
channelz.RemoveEntry(s.channelzID)
1765-
}
1766-
})
1752+
s.channelzRemoveOnce.Do(func() { channelz.RemoveEntry(s.channelzID) })
17671753
s.mu.Lock()
17681754
if s.conns == nil {
17691755
s.mu.Unlock()

0 commit comments

Comments
 (0)