Skip to content

Commit 800a8e0

Browse files
authored
channelz: re-add state for subchannels (#7046)
1 parent dadbbfa commit 800a8e0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

clientconn.go

+1
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,7 @@ func (ac *addrConn) updateConnectivityState(s connectivity.State, lastErr error)
12041204
close(ac.stateChan)
12051205
ac.stateChan = make(chan struct{})
12061206
ac.state = s
1207+
ac.channelz.ChannelMetrics.State.Store(&s)
12071208
if lastErr == nil {
12081209
channelz.Infof(logger, ac.channelz, "Subchannel Connectivity change to %v", s)
12091210
} else {

test/channelz_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,43 @@ func (s) TestCZGetChannel(t *testing.T) {
131131
}
132132
}
133133

134+
func (s) TestCZGetSubChannel(t *testing.T) {
135+
e := tcpClearRREnv
136+
e.balancer = ""
137+
te := newTest(t, e)
138+
te.startServer(&testServer{security: e.security})
139+
r := manual.NewBuilderWithScheme("whatever")
140+
addrs := []resolver.Address{{Addr: te.srvAddr}}
141+
r.InitialState(resolver.State{Addresses: addrs})
142+
te.resolverScheme = r.Scheme()
143+
te.clientConn(grpc.WithResolvers(r))
144+
defer te.tearDown()
145+
if err := verifyResultWithDelay(func() (bool, error) {
146+
tcs, _ := channelz.GetTopChannels(0, 0)
147+
if len(tcs) != 1 {
148+
return false, fmt.Errorf("there should only be one top channel, not %d", len(tcs))
149+
}
150+
scs := tcs[0].SubChans()
151+
if len(scs) != 1 {
152+
return false, fmt.Errorf("there should be one subchannel, not %d", len(scs))
153+
}
154+
var scid int64
155+
for scid = range scs {
156+
}
157+
sc := channelz.GetSubChannel(scid)
158+
if sc == nil {
159+
return false, fmt.Errorf("subchannel with id %v is nil", scid)
160+
}
161+
state := sc.ChannelMetrics.State.Load()
162+
if state == nil || *state != connectivity.Ready {
163+
return false, fmt.Errorf("Got subchannel state=%v; want %q", state, connectivity.Ready)
164+
}
165+
return true, nil
166+
}); err != nil {
167+
t.Fatal(err)
168+
}
169+
}
170+
134171
func (s) TestCZGetServer(t *testing.T) {
135172
e := tcpClearRREnv
136173
te := newTest(t, e)

0 commit comments

Comments
 (0)