From dadbbfa2863a67ed640aac924b7b7fd18b50a429 Mon Sep 17 00:00:00 2001 From: Doug Fawley Date: Mon, 18 Mar 2024 15:31:19 -0700 Subject: [PATCH] channelz: re-add target and state (#7042) --- clientconn.go | 1 + internal/channelz/funcs.go | 9 +++++---- test/channelz_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/clientconn.go b/clientconn.go index 1565f3b11a9e..91508fb4e48e 100644 --- a/clientconn.go +++ b/clientconn.go @@ -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) diff --git a/internal/channelz/funcs.go b/internal/channelz/funcs.go index baf0fd92f39f..bdefe93cd68e 100644 --- a/internal/channelz/funcs.go +++ b/internal/channelz/funcs.go @@ -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() { @@ -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 } diff --git a/test/channelz_test.go b/test/channelz_test.go index 1709ab723035..e223c5cce1eb 100644 --- a/test/channelz_test.go +++ b/test/channelz_test.go @@ -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)