Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

channelz: re-add target and state #7042

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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