Skip to content

Commit

Permalink
channelz: re-add target and state (#7042)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley authored Mar 18, 2024
1 parent 55cd7a6 commit dadbbfa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ func (csm *connectivityStateManager) updateState(state connectivity.State) {
return
}
csm.state = state
csm.channelz.ChannelMetrics.State.Store(&state)
csm.pubSub.Publish(state)

channelz.Infof(logger, csm.channelz, "Channel Connectivity change to %v", state)
Expand Down
9 changes: 5 additions & 4 deletions internal/channelz/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ func GetServer(id int64) *Server {
}

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

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

cn := &Channel{
ID: id,
RefName: ref,
RefName: target,
nestedChans: make(map[int64]string),
subChans: make(map[int64]string),
Parent: parent,
trace: &ChannelTrace{CreationTime: time.Now(), Events: make([]*traceEvent, 0, getMaxTraceEntry())},
}
cn.ChannelMetrics.Target.Store(&target)
db.addChannel(id, cn, isTopChannel, cn.getParentID())
return cn
}
Expand Down
31 changes: 31 additions & 0 deletions test/channelz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ func (s) TestCZServerRegistrationAndDeletion(t *testing.T) {
}
}

func (s) TestCZGetChannel(t *testing.T) {
e := tcpClearRREnv
e.balancer = ""
te := newTest(t, e)
te.startServer(&testServer{security: e.security})
r := manual.NewBuilderWithScheme("whatever")
addrs := []resolver.Address{{Addr: te.srvAddr}}
r.InitialState(resolver.State{Addresses: addrs})
te.resolverScheme = r.Scheme()
te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
if err := verifyResultWithDelay(func() (bool, error) {
tcs, _ := channelz.GetTopChannels(0, 0)
if len(tcs) != 1 {
return false, fmt.Errorf("there should only be one top channel, not %d", len(tcs))
}
target := tcs[0].ChannelMetrics.Target.Load()
wantTarget := "whatever:///" + te.srvAddr
if target == nil || *target != wantTarget {
return false, fmt.Errorf("Got channelz target=%v; want %q", target, wantTarget)
}
state := tcs[0].ChannelMetrics.State.Load()
if state == nil || *state != connectivity.Ready {
return false, fmt.Errorf("Got channelz state=%v; want %q", state, connectivity.Ready)
}
return true, nil
}); err != nil {
t.Fatal(err)
}
}

func (s) TestCZGetServer(t *testing.T) {
e := tcpClearRREnv
te := newTest(t, e)
Expand Down

0 comments on commit dadbbfa

Please sign in to comment.