Skip to content

Commit 92f6dd0

Browse files
committed
channelz: pass parent pointer instead of parent ID to RegisterSubChannel (#7101)
1 parent 0f6ef0f commit 92f6dd0

File tree

6 files changed

+34
-31
lines changed

6 files changed

+34
-31
lines changed

Diff for: channelz/service/service_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func (s) TestGetChannel(t *testing.T) {
334334
},
335335
})
336336

337-
subChan := channelz.RegisterSubChannel(cids[0].ID, refNames[2])
337+
subChan := channelz.RegisterSubChannel(cids[0], refNames[2])
338338
channelz.AddTraceEvent(logger, subChan, 0, &channelz.TraceEvent{
339339
Desc: "SubChannel Created",
340340
Severity: channelz.CtInfo,
@@ -432,7 +432,7 @@ func (s) TestGetSubChannel(t *testing.T) {
432432
Desc: "Channel Created",
433433
Severity: channelz.CtInfo,
434434
})
435-
subChan := channelz.RegisterSubChannel(chann.ID, refNames[1])
435+
subChan := channelz.RegisterSubChannel(chann, refNames[1])
436436
defer channelz.RemoveEntry(subChan.ID)
437437
channelz.AddTraceEvent(logger, subChan, 0, &channelz.TraceEvent{
438438
Desc: subchanCreated,

Diff for: clientconn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ func (cc *ClientConn) newAddrConnLocked(addrs []resolver.Address, opts balancer.
833833
addrs: copyAddressesWithoutBalancerAttributes(addrs),
834834
scopts: opts,
835835
dopts: cc.dopts,
836-
channelz: channelz.RegisterSubChannel(cc.channelz.ID, ""),
836+
channelz: channelz.RegisterSubChannel(cc.channelz, ""),
837837
resetBackoff: make(chan struct{}),
838838
stateChan: make(chan struct{}),
839839
}

Diff for: internal/channelz/funcs.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,21 @@ func RegisterChannel(parent *Channel, target string) *Channel {
143143
// Returns a unique channelz identifier assigned to this subChannel.
144144
//
145145
// If channelz is not turned ON, the channelz database is not mutated.
146-
func RegisterSubChannel(pid int64, ref string) *SubChannel {
146+
func RegisterSubChannel(parent *Channel, ref string) *SubChannel {
147147
id := IDGen.genID()
148-
if !IsOn() {
149-
return &SubChannel{ID: id}
150-
}
151-
152148
sc := &SubChannel{
153-
RefName: ref,
154149
ID: id,
155-
sockets: make(map[int64]string),
156-
parent: db.getChannel(pid),
157-
trace: &ChannelTrace{CreationTime: time.Now(), Events: make([]*traceEvent, 0, getMaxTraceEntry())},
150+
RefName: ref,
151+
parent: parent,
158152
}
159-
db.addSubChannel(id, sc, pid)
153+
154+
if !IsOn() {
155+
return sc
156+
}
157+
158+
sc.sockets = make(map[int64]string)
159+
sc.trace = &ChannelTrace{CreationTime: time.Now(), Events: make([]*traceEvent, 0, getMaxTraceEntry())}
160+
db.addSubChannel(id, sc, parent.ID)
160161
return sc
161162
}
162163

Diff for: internal/transport/keepalive_test.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ func (s) TestKeepaliveServerWithResponsiveClient(t *testing.T) {
249249
}
250250
}
251251

252+
func channelzSubChannel(t *testing.T) *channelz.SubChannel {
253+
ch := channelz.RegisterChannel(nil, "test chan")
254+
sc := channelz.RegisterSubChannel(ch, "test subchan")
255+
t.Cleanup(func() {
256+
channelz.RemoveEntry(sc.ID)
257+
channelz.RemoveEntry(ch.ID)
258+
})
259+
return sc
260+
}
261+
252262
// TestKeepaliveClientClosesUnresponsiveServer creates a server which does not
253263
// respond to keepalive pings, and makes sure that the client closes the
254264
// transport once the keepalive logic kicks in. Here, we set the
@@ -257,14 +267,13 @@ func (s) TestKeepaliveServerWithResponsiveClient(t *testing.T) {
257267
func (s) TestKeepaliveClientClosesUnresponsiveServer(t *testing.T) {
258268
connCh := make(chan net.Conn, 1)
259269
copts := ConnectOptions{
260-
ChannelzParent: channelz.RegisterSubChannel(-1, "test subchan"),
270+
ChannelzParent: channelzSubChannel(t),
261271
KeepaliveParams: keepalive.ClientParameters{
262272
Time: 10 * time.Millisecond,
263273
Timeout: 10 * time.Millisecond,
264274
PermitWithoutStream: true,
265275
},
266276
}
267-
defer channelz.RemoveEntry(copts.ChannelzParent.ID)
268277
client, cancel := setUpWithNoPingServer(t, copts, connCh)
269278
defer cancel()
270279
defer client.Close(fmt.Errorf("closed manually by test"))
@@ -288,13 +297,12 @@ func (s) TestKeepaliveClientClosesUnresponsiveServer(t *testing.T) {
288297
func (s) TestKeepaliveClientOpenWithUnresponsiveServer(t *testing.T) {
289298
connCh := make(chan net.Conn, 1)
290299
copts := ConnectOptions{
291-
ChannelzParent: channelz.RegisterSubChannel(-1, "test subchan"),
300+
ChannelzParent: channelzSubChannel(t),
292301
KeepaliveParams: keepalive.ClientParameters{
293302
Time: 10 * time.Millisecond,
294303
Timeout: 10 * time.Millisecond,
295304
},
296305
}
297-
defer channelz.RemoveEntry(copts.ChannelzParent.ID)
298306
client, cancel := setUpWithNoPingServer(t, copts, connCh)
299307
defer cancel()
300308
defer client.Close(fmt.Errorf("closed manually by test"))
@@ -319,13 +327,12 @@ func (s) TestKeepaliveClientOpenWithUnresponsiveServer(t *testing.T) {
319327
func (s) TestKeepaliveClientClosesWithActiveStreams(t *testing.T) {
320328
connCh := make(chan net.Conn, 1)
321329
copts := ConnectOptions{
322-
ChannelzParent: channelz.RegisterSubChannel(-1, "test subchan"),
330+
ChannelzParent: channelzSubChannel(t),
323331
KeepaliveParams: keepalive.ClientParameters{
324332
Time: 500 * time.Millisecond,
325333
Timeout: 500 * time.Millisecond,
326334
},
327335
}
328-
defer channelz.RemoveEntry(copts.ChannelzParent.ID)
329336
// TODO(i/6099): Setup a server which can ping and no-ping based on a flag to
330337
// reduce the flakiness in this test.
331338
client, cancel := setUpWithNoPingServer(t, copts, connCh)

Diff for: internal/transport/transport_test.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,7 @@ func setUp(t *testing.T, port int, ht hType) (*server, *http2Client, func()) {
434434
func setUpWithOptions(t *testing.T, port int, sc *ServerConfig, ht hType, copts ConnectOptions) (*server, *http2Client, func()) {
435435
server := setUpServerOnly(t, port, sc, ht)
436436
addr := resolver.Address{Addr: "localhost:" + server.port}
437-
copts.ChannelzParent = channelz.RegisterSubChannel(-1, "test channel")
438-
t.Cleanup(func() { channelz.RemoveEntry(copts.ChannelzParent.ID) })
437+
copts.ChannelzParent = channelzSubChannel(t)
439438

440439
connectCtx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
441440
ct, connErr := NewClientTransport(connectCtx, context.Background(), addr, copts, func(GoAwayReason) {})
@@ -1321,9 +1320,8 @@ func (s) TestClientHonorsConnectContext(t *testing.T) {
13211320
connectCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
13221321
time.AfterFunc(100*time.Millisecond, cancel)
13231322

1324-
parent := channelz.RegisterSubChannel(-1, "test channel")
1323+
parent := channelzSubChannel(t)
13251324
copts := ConnectOptions{ChannelzParent: parent}
1326-
defer channelz.RemoveEntry(parent.ID)
13271325
_, err = NewClientTransport(connectCtx, context.Background(), resolver.Address{Addr: lis.Addr().String()}, copts, func(GoAwayReason) {})
13281326
if err == nil {
13291327
t.Fatalf("NewClientTransport() returned successfully; wanted error")
@@ -1414,8 +1412,7 @@ func (s) TestClientWithMisbehavedServer(t *testing.T) {
14141412
connectCtx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
14151413
defer cancel()
14161414

1417-
parent := channelz.RegisterSubChannel(-1, "test channel")
1418-
defer channelz.RemoveEntry(parent.ID)
1415+
parent := channelzSubChannel(t)
14191416
copts := ConnectOptions{ChannelzParent: parent}
14201417
ct, err := NewClientTransport(connectCtx, context.Background(), resolver.Address{Addr: lis.Addr().String()}, copts, func(GoAwayReason) {})
14211418
if err != nil {
@@ -2425,9 +2422,8 @@ func (s) TestClientHandshakeInfo(t *testing.T) {
24252422

24262423
copts := ConnectOptions{
24272424
TransportCredentials: creds,
2428-
ChannelzParent: channelz.RegisterSubChannel(-1, "test subchannel"),
2425+
ChannelzParent: channelzSubChannel(t),
24292426
}
2430-
defer channelz.RemoveEntry(copts.ChannelzParent.ID)
24312427
tr, err := NewClientTransport(ctx, context.Background(), addr, copts, func(GoAwayReason) {})
24322428
if err != nil {
24332429
t.Fatalf("NewClientTransport(): %v", err)
@@ -2467,9 +2463,8 @@ func (s) TestClientHandshakeInfoDialer(t *testing.T) {
24672463

24682464
copts := ConnectOptions{
24692465
Dialer: dialer,
2470-
ChannelzParent: channelz.RegisterSubChannel(-1, "test subchannel"),
2466+
ChannelzParent: channelzSubChannel(t),
24712467
}
2472-
defer channelz.RemoveEntry(copts.ChannelzParent.ID)
24732468
tr, err := NewClientTransport(ctx, context.Background(), addr, copts, func(GoAwayReason) {})
24742469
if err != nil {
24752470
t.Fatalf("NewClientTransport(): %v", err)

Diff for: test/channelz_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ func (s) TestCZRecusivelyDeletionOfEntry(t *testing.T) {
554554
// Socket1 Socket2
555555

556556
topChan := channelz.RegisterChannel(nil, "")
557-
subChan1 := channelz.RegisterSubChannel(topChan.ID, "")
558-
subChan2 := channelz.RegisterSubChannel(topChan.ID, "")
557+
subChan1 := channelz.RegisterSubChannel(topChan, "")
558+
subChan2 := channelz.RegisterSubChannel(topChan, "")
559559
skt1 := channelz.RegisterSocket(&channelz.Socket{SocketType: channelz.SocketTypeNormal, Parent: subChan1})
560560
skt2 := channelz.RegisterSocket(&channelz.Socket{SocketType: channelz.SocketTypeNormal, Parent: subChan1})
561561

0 commit comments

Comments
 (0)