Skip to content

Commit dadbbfa

Browse files
authored
channelz: re-add target and state (#7042)
1 parent 55cd7a6 commit dadbbfa

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

clientconn.go

+1
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ func (csm *connectivityStateManager) updateState(state connectivity.State) {
529529
return
530530
}
531531
csm.state = state
532+
csm.channelz.ChannelMetrics.State.Store(&state)
532533
csm.pubSub.Publish(state)
533534

534535
channelz.Infof(logger, csm.channelz, "Channel Connectivity change to %v", state)

internal/channelz/funcs.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ func GetServer(id int64) *Server {
108108
}
109109

110110
// RegisterChannel registers the given channel c in the channelz database with
111-
// ref as its reference name, and adds it to the child list of its parent.
112-
// parent == nil means no parent.
111+
// target as its target and reference name, and adds it to the child list of its
112+
// parent. parent == nil means no parent.
113113
//
114114
// Returns a unique channelz identifier assigned to this channel.
115115
//
116116
// If channelz is not turned ON, the channelz database is not mutated.
117-
func RegisterChannel(parent *Channel, ref string) *Channel {
117+
func RegisterChannel(parent *Channel, target string) *Channel {
118118
id := IDGen.genID()
119119

120120
if !IsOn() {
@@ -125,12 +125,13 @@ func RegisterChannel(parent *Channel, ref string) *Channel {
125125

126126
cn := &Channel{
127127
ID: id,
128-
RefName: ref,
128+
RefName: target,
129129
nestedChans: make(map[int64]string),
130130
subChans: make(map[int64]string),
131131
Parent: parent,
132132
trace: &ChannelTrace{CreationTime: time.Now(), Events: make([]*traceEvent, 0, getMaxTraceEntry())},
133133
}
134+
cn.ChannelMetrics.Target.Store(&target)
134135
db.addChannel(id, cn, isTopChannel, cn.getParentID())
135136
return cn
136137
}

test/channelz_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,37 @@ func (s) TestCZServerRegistrationAndDeletion(t *testing.T) {
100100
}
101101
}
102102

103+
func (s) TestCZGetChannel(t *testing.T) {
104+
e := tcpClearRREnv
105+
e.balancer = ""
106+
te := newTest(t, e)
107+
te.startServer(&testServer{security: e.security})
108+
r := manual.NewBuilderWithScheme("whatever")
109+
addrs := []resolver.Address{{Addr: te.srvAddr}}
110+
r.InitialState(resolver.State{Addresses: addrs})
111+
te.resolverScheme = r.Scheme()
112+
te.clientConn(grpc.WithResolvers(r))
113+
defer te.tearDown()
114+
if err := verifyResultWithDelay(func() (bool, error) {
115+
tcs, _ := channelz.GetTopChannels(0, 0)
116+
if len(tcs) != 1 {
117+
return false, fmt.Errorf("there should only be one top channel, not %d", len(tcs))
118+
}
119+
target := tcs[0].ChannelMetrics.Target.Load()
120+
wantTarget := "whatever:///" + te.srvAddr
121+
if target == nil || *target != wantTarget {
122+
return false, fmt.Errorf("Got channelz target=%v; want %q", target, wantTarget)
123+
}
124+
state := tcs[0].ChannelMetrics.State.Load()
125+
if state == nil || *state != connectivity.Ready {
126+
return false, fmt.Errorf("Got channelz state=%v; want %q", state, connectivity.Ready)
127+
}
128+
return true, nil
129+
}); err != nil {
130+
t.Fatal(err)
131+
}
132+
}
133+
103134
func (s) TestCZGetServer(t *testing.T) {
104135
e := tcpClearRREnv
105136
te := newTest(t, e)

0 commit comments

Comments
 (0)