Skip to content

Commit 786e6be

Browse files
committed
review comments: assign channelz IDs even when channelz is turned OFF
1 parent 3e5c516 commit 786e6be

File tree

5 files changed

+101
-59
lines changed

5 files changed

+101
-59
lines changed

call_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"time"
3232

3333
"google.golang.org/grpc/codes"
34+
"google.golang.org/grpc/internal/channelz"
3435
"google.golang.org/grpc/internal/transport"
3536
"google.golang.org/grpc/status"
3637
)
@@ -123,12 +124,16 @@ type server struct {
123124
startedErr chan error // sent nil or an error after server starts
124125
mu sync.Mutex
125126
conns map[transport.ServerTransport]bool
127+
channelzID *channelz.Identifier
126128
}
127129

128130
type ctxKey string
129131

130132
func newTestServer() *server {
131-
return &server{startedErr: make(chan error, 1)}
133+
return &server{
134+
startedErr: make(chan error, 1),
135+
channelzID: channelz.NewIdentifierForTesting(channelz.RefServer, time.Now().Unix(), nil),
136+
}
132137
}
133138

134139
// start starts server. Other goroutines should block on s.startedErr for further operations.
@@ -158,10 +163,12 @@ func (s *server) start(t *testing.T, port int, maxStreams uint32) {
158163
return
159164
}
160165
config := &transport.ServerConfig{
161-
MaxStreams: maxStreams,
166+
MaxStreams: maxStreams,
167+
ChannelzParentID: s.channelzID,
162168
}
163169
st, err := transport.NewServerTransport(conn, config)
164170
if err != nil {
171+
t.Errorf("failed to create server transport: %v", err)
165172
continue
166173
}
167174
s.mu.Lock()

clientconn.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,9 +1314,7 @@ func (ac *addrConn) createTransport(addr resolver.Address, copts transport.Conne
13141314

13151315
connectCtx, cancel := context.WithDeadline(ac.ctx, connectDeadline)
13161316
defer cancel()
1317-
if channelz.IsOn() {
1318-
copts.ChannelzParentID = ac.channelzID
1319-
}
1317+
copts.ChannelzParentID = ac.channelzID
13201318

13211319
newTr, err := transport.NewClientTransport(connectCtx, ac.cc.ctx, addr, copts, func() { prefaceReceived.Fire() }, onGoAway, onClose)
13221320
if err != nil {

internal/channelz/funcs.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,8 @@ func GetServer(id int64) *ServerMetric {
191191
//
192192
// Returns a unique channelz identifier assigned to this channel.
193193
//
194-
// If channelz is not turned ON, this function is a no-op.
194+
// If channelz is not turned ON, the channelz database is not mutated.
195195
func RegisterChannel(c Channel, pid *Identifier, ref string) *Identifier {
196-
if !IsOn() {
197-
return nil
198-
}
199-
200196
id := idGen.genID()
201197
parent := int64(0)
202198
isTopChannel := true
@@ -205,6 +201,10 @@ func RegisterChannel(c Channel, pid *Identifier, ref string) *Identifier {
205201
parent = pid.Int()
206202
}
207203

204+
if !IsOn() {
205+
return newIdentifer(RefChannel, id, pid)
206+
}
207+
208208
cn := &channel{
209209
refName: ref,
210210
c: c,
@@ -224,16 +224,16 @@ func RegisterChannel(c Channel, pid *Identifier, ref string) *Identifier {
224224
//
225225
// Returns a unique channelz identifier assigned to this subChannel.
226226
//
227-
// If channelz is not turned ON, this function is a no-op.
227+
// If channelz is not turned ON, the channelz database is not mutated.
228228
func RegisterSubChannel(c Channel, pid *Identifier, ref string) (*Identifier, error) {
229-
if !IsOn() {
230-
return nil, nil
231-
}
232-
233229
if pid == nil {
234230
return nil, errors.New("a SubChannel's parent id cannot be nil")
235231
}
236232
id := idGen.genID()
233+
if !IsOn() {
234+
return newIdentifer(RefSubChannel, id, pid), nil
235+
}
236+
237237
sc := &subChannel{
238238
refName: ref,
239239
c: c,
@@ -249,13 +249,13 @@ func RegisterSubChannel(c Channel, pid *Identifier, ref string) (*Identifier, er
249249
// RegisterServer registers the given server s in channelz database. It returns
250250
// the unique channelz tracking id assigned to this server.
251251
//
252-
// If channelz is not turned ON, this function is a no-op.
252+
// If channelz is not turned ON, the channelz database is not mutated.
253253
func RegisterServer(s Server, ref string) *Identifier {
254+
id := idGen.genID()
254255
if !IsOn() {
255-
return nil
256+
return newIdentifer(RefServer, id, nil)
256257
}
257258

258-
id := idGen.genID()
259259
svr := &server{
260260
refName: ref,
261261
s: s,
@@ -272,16 +272,16 @@ func RegisterServer(s Server, ref string) *Identifier {
272272
// (identified by pid). It returns the unique channelz tracking id assigned to
273273
// this listen socket.
274274
//
275-
// If channelz is not turned ON, this function is a no-op.
275+
// If channelz is not turned ON, the channelz database is not mutated.
276276
func RegisterListenSocket(s Socket, pid *Identifier, ref string) (*Identifier, error) {
277-
if !IsOn() {
278-
return nil, nil
279-
}
280-
281277
if pid == nil {
282278
return nil, errors.New("a ListenSocket's parent id cannot be 0")
283279
}
284280
id := idGen.genID()
281+
if !IsOn() {
282+
return newIdentifer(RefListenSocket, id, pid), nil
283+
}
284+
285285
ls := &listenSocket{refName: ref, s: s, id: id, pid: pid.Int()}
286286
db.get().addListenSocket(id, ls, pid.Int())
287287
return newIdentifer(RefListenSocket, id, pid), nil
@@ -292,16 +292,16 @@ func RegisterListenSocket(s Socket, pid *Identifier, ref string) (*Identifier, e
292292
// (identified by pid). It returns the unique channelz tracking id assigned to
293293
// this normal socket.
294294
//
295-
// If channelz is not turned ON, this function is a no-op.
295+
// If channelz is not turned ON, the channelz database is not mutated.
296296
func RegisterNormalSocket(s Socket, pid *Identifier, ref string) (*Identifier, error) {
297-
if !IsOn() {
298-
return nil, nil
299-
}
300-
301297
if pid == nil {
302298
return nil, errors.New("a NormalSocket's parent id cannot be 0")
303299
}
304300
id := idGen.genID()
301+
if !IsOn() {
302+
return newIdentifer(RefNormalSocket, id, pid), nil
303+
}
304+
305305
ns := &normalSocket{refName: ref, s: s, id: id, pid: pid.Int()}
306306
db.get().addNormalSocket(id, ns, pid.Int())
307307
return newIdentifer(RefNormalSocket, id, pid), nil

internal/transport/keepalive_test.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"time"
3232

3333
"golang.org/x/net/http2"
34+
"google.golang.org/grpc/internal/channelz"
3435
"google.golang.org/grpc/internal/syscall"
3536
"google.golang.org/grpc/keepalive"
3637
)
@@ -252,11 +253,15 @@ func (s) TestKeepaliveServerWithResponsiveClient(t *testing.T) {
252253
// logic is running even without any active streams.
253254
func (s) TestKeepaliveClientClosesUnresponsiveServer(t *testing.T) {
254255
connCh := make(chan net.Conn, 1)
255-
client, cancel := setUpWithNoPingServer(t, ConnectOptions{KeepaliveParams: keepalive.ClientParameters{
256-
Time: 1 * time.Second,
257-
Timeout: 1 * time.Second,
258-
PermitWithoutStream: true,
259-
}}, connCh)
256+
copts := ConnectOptions{
257+
ChannelzParentID: channelz.NewIdentifierForTesting(channelz.RefSubChannel, time.Now().Unix(), nil),
258+
KeepaliveParams: keepalive.ClientParameters{
259+
Time: 1 * time.Second,
260+
Timeout: 1 * time.Second,
261+
PermitWithoutStream: true,
262+
},
263+
}
264+
client, cancel := setUpWithNoPingServer(t, copts, connCh)
260265
defer cancel()
261266
defer client.Close(fmt.Errorf("closed manually by test"))
262267

@@ -284,10 +289,14 @@ func (s) TestKeepaliveClientClosesUnresponsiveServer(t *testing.T) {
284289
// active streams, and therefore the transport stays open.
285290
func (s) TestKeepaliveClientOpenWithUnresponsiveServer(t *testing.T) {
286291
connCh := make(chan net.Conn, 1)
287-
client, cancel := setUpWithNoPingServer(t, ConnectOptions{KeepaliveParams: keepalive.ClientParameters{
288-
Time: 1 * time.Second,
289-
Timeout: 1 * time.Second,
290-
}}, connCh)
292+
copts := ConnectOptions{
293+
ChannelzParentID: channelz.NewIdentifierForTesting(channelz.RefSubChannel, time.Now().Unix(), nil),
294+
KeepaliveParams: keepalive.ClientParameters{
295+
Time: 1 * time.Second,
296+
Timeout: 1 * time.Second,
297+
},
298+
}
299+
client, cancel := setUpWithNoPingServer(t, copts, connCh)
291300
defer cancel()
292301
defer client.Close(fmt.Errorf("closed manually by test"))
293302

@@ -313,10 +322,14 @@ func (s) TestKeepaliveClientOpenWithUnresponsiveServer(t *testing.T) {
313322
// transport even when there is an active stream.
314323
func (s) TestKeepaliveClientClosesWithActiveStreams(t *testing.T) {
315324
connCh := make(chan net.Conn, 1)
316-
client, cancel := setUpWithNoPingServer(t, ConnectOptions{KeepaliveParams: keepalive.ClientParameters{
317-
Time: 1 * time.Second,
318-
Timeout: 1 * time.Second,
319-
}}, connCh)
325+
copts := ConnectOptions{
326+
ChannelzParentID: channelz.NewIdentifierForTesting(channelz.RefSubChannel, time.Now().Unix(), nil),
327+
KeepaliveParams: keepalive.ClientParameters{
328+
Time: 1 * time.Second,
329+
Timeout: 1 * time.Second,
330+
},
331+
}
332+
client, cancel := setUpWithNoPingServer(t, copts, connCh)
320333
defer cancel()
321334
defer client.Close(fmt.Errorf("closed manually by test"))
322335

internal/transport/transport_test.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"google.golang.org/grpc/attributes"
4141
"google.golang.org/grpc/codes"
4242
"google.golang.org/grpc/credentials"
43+
"google.golang.org/grpc/internal/channelz"
4344
"google.golang.org/grpc/internal/grpctest"
4445
"google.golang.org/grpc/internal/leakcheck"
4546
"google.golang.org/grpc/internal/testutils"
@@ -55,16 +56,6 @@ func Test(t *testing.T) {
5556
grpctest.RunSubTests(t, s{})
5657
}
5758

58-
type server struct {
59-
lis net.Listener
60-
port string
61-
startedErr chan error // error (or nil) with server start value
62-
mu sync.Mutex
63-
conns map[ServerTransport]bool
64-
h *testStreamHandler
65-
ready chan struct{}
66-
}
67-
6859
var (
6960
expectedRequest = []byte("ping")
7061
expectedResponse = []byte("pong")
@@ -298,6 +289,25 @@ func (h *testStreamHandler) handleStreamDelayRead(t *testing.T, s *Stream) {
298289
}
299290
}
300291

292+
type server struct {
293+
lis net.Listener
294+
port string
295+
startedErr chan error // error (or nil) with server start value
296+
mu sync.Mutex
297+
conns map[ServerTransport]bool
298+
h *testStreamHandler
299+
ready chan struct{}
300+
channelzID *channelz.Identifier
301+
}
302+
303+
func newTestServer() *server {
304+
return &server{
305+
startedErr: make(chan error, 1),
306+
ready: make(chan struct{}),
307+
channelzID: channelz.NewIdentifierForTesting(channelz.RefServer, time.Now().Unix(), nil),
308+
}
309+
}
310+
301311
// start starts server. Other goroutines should block on s.readyChan for further operations.
302312
func (s *server) start(t *testing.T, port int, serverConfig *ServerConfig, ht hType) {
303313
var err error
@@ -421,9 +431,10 @@ func (s *server) addr() string {
421431
return s.lis.Addr().String()
422432
}
423433

424-
func setUpServerOnly(t *testing.T, port int, serverConfig *ServerConfig, ht hType) *server {
425-
server := &server{startedErr: make(chan error, 1), ready: make(chan struct{})}
426-
go server.start(t, port, serverConfig, ht)
434+
func setUpServerOnly(t *testing.T, port int, sc *ServerConfig, ht hType) *server {
435+
server := newTestServer()
436+
sc.ChannelzParentID = server.channelzID
437+
go server.start(t, port, sc, ht)
427438
server.wait(t, 2*time.Second)
428439
return server
429440
}
@@ -432,9 +443,11 @@ func setUp(t *testing.T, port int, maxStreams uint32, ht hType) (*server, *http2
432443
return setUpWithOptions(t, port, &ServerConfig{MaxStreams: maxStreams}, ht, ConnectOptions{})
433444
}
434445

435-
func setUpWithOptions(t *testing.T, port int, serverConfig *ServerConfig, ht hType, copts ConnectOptions) (*server, *http2Client, func()) {
436-
server := setUpServerOnly(t, port, serverConfig, ht)
446+
func setUpWithOptions(t *testing.T, port int, sc *ServerConfig, ht hType, copts ConnectOptions) (*server, *http2Client, func()) {
447+
server := setUpServerOnly(t, port, sc, ht)
437448
addr := resolver.Address{Addr: "localhost:" + server.port}
449+
copts.ChannelzParentID = channelz.NewIdentifierForTesting(channelz.RefSubChannel, time.Now().Unix(), nil)
450+
438451
connectCtx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
439452
ct, connErr := NewClientTransport(connectCtx, context.Background(), addr, copts, func() {}, func(GoAwayReason) {}, func() {})
440453
if connErr != nil {
@@ -1298,11 +1311,14 @@ func (s) TestClientWithMisbehavedServer(t *testing.T) {
12981311
}()
12991312
connectCtx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
13001313
defer cancel()
1301-
ct, err := NewClientTransport(connectCtx, context.Background(), resolver.Address{Addr: lis.Addr().String()}, ConnectOptions{}, func() {}, func(GoAwayReason) {}, func() {})
1314+
1315+
copts := ConnectOptions{ChannelzParentID: channelz.NewIdentifierForTesting(channelz.RefSubChannel, time.Now().Unix(), nil)}
1316+
ct, err := NewClientTransport(connectCtx, context.Background(), resolver.Address{Addr: lis.Addr().String()}, copts, func() {}, func(GoAwayReason) {}, func() {})
13021317
if err != nil {
13031318
t.Fatalf("Error while creating client transport: %v", err)
13041319
}
13051320
defer ct.Close(fmt.Errorf("closed manually by test"))
1321+
13061322
str, err := ct.NewStream(connectCtx, &CallHdr{})
13071323
if err != nil {
13081324
t.Fatalf("Error while creating stream: %v", err)
@@ -2180,7 +2196,11 @@ func (s) TestClientHandshakeInfo(t *testing.T) {
21802196
defer cancel()
21812197
creds := &attrTransportCreds{}
21822198

2183-
tr, err := NewClientTransport(ctx, context.Background(), addr, ConnectOptions{TransportCredentials: creds}, func() {}, func(GoAwayReason) {}, func() {})
2199+
copts := ConnectOptions{
2200+
TransportCredentials: creds,
2201+
ChannelzParentID: channelz.NewIdentifierForTesting(channelz.RefSubChannel, time.Now().Unix(), nil),
2202+
}
2203+
tr, err := NewClientTransport(ctx, context.Background(), addr, copts, func() {}, func(GoAwayReason) {}, func() {})
21842204
if err != nil {
21852205
t.Fatalf("NewClientTransport(): %v", err)
21862206
}
@@ -2217,7 +2237,11 @@ func (s) TestClientHandshakeInfoDialer(t *testing.T) {
22172237
return (&net.Dialer{}).DialContext(ctx, "tcp", addr)
22182238
}
22192239

2220-
tr, err := NewClientTransport(ctx, context.Background(), addr, ConnectOptions{Dialer: dialer}, func() {}, func(GoAwayReason) {}, func() {})
2240+
copts := ConnectOptions{
2241+
Dialer: dialer,
2242+
ChannelzParentID: channelz.NewIdentifierForTesting(channelz.RefSubChannel, time.Now().Unix(), nil),
2243+
}
2244+
tr, err := NewClientTransport(ctx, context.Background(), addr, copts, func() {}, func(GoAwayReason) {}, func() {})
22212245
if err != nil {
22222246
t.Fatalf("NewClientTransport(): %v", err)
22232247
}

0 commit comments

Comments
 (0)