Skip to content

Commit 9f52a86

Browse files
committed
channelz: implement GetServer
1 parent 63c13b5 commit 9f52a86

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

Diff for: channelz/service/service.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -337,5 +337,10 @@ func (s *serverImpl) GetSocket(ctx context.Context, req *channelzpb.GetSocketReq
337337
}
338338

339339
func (s *serverImpl) GetServer(ctx context.Context, req *channelzpb.GetServerRequest) (*channelzpb.GetServerResponse, error) {
340-
return nil, status.Error(codes.Unimplemented, "GetServer not implemented")
340+
var metric *channelz.ServerMetric
341+
if metric = channelz.GetServer(req.GetServerId()); metric == nil {
342+
return nil, status.Errorf(codes.NotFound, "requested server %d not found", req.GetServerId())
343+
}
344+
resp := &channelzpb.GetServerResponse{Server: serverMetricToProto(metric)}
345+
return resp, nil
341346
}

Diff for: internal/channelz/funcs.go

+22
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ func GetSocket(id int64) *SocketMetric {
155155
return db.get().GetSocket(id)
156156
}
157157

158+
// GetServer returns the ServerMetric for the server (identified by id).
159+
func GetServer(id int64) *ServerMetric {
160+
return db.get().GetServer(id)
161+
}
162+
158163
// RegisterChannel registers the given channel c in channelz database with ref
159164
// as its reference name, and add it to the child list of its parent (identified
160165
// by pid). pid = 0 means no parent. It returns the unique channelz tracking id
@@ -664,6 +669,23 @@ func (c *channelMap) GetSocket(id int64) *SocketMetric {
664669
return nil
665670
}
666671

672+
func (c *channelMap) GetServer(id int64) *ServerMetric {
673+
sm := &ServerMetric{}
674+
var svr *server
675+
var ok bool
676+
c.mu.RLock()
677+
if svr, ok = c.servers[id]; !ok {
678+
c.mu.RUnlock()
679+
return nil
680+
}
681+
sm.ListenSockets = copyMap(svr.listenSockets)
682+
c.mu.RUnlock()
683+
sm.ID = svr.id
684+
sm.RefName = svr.refName
685+
sm.ServerData = svr.s.ChannelzMetric()
686+
return sm
687+
}
688+
667689
type idGenerator struct {
668690
id int64
669691
}

Diff for: test/channelz_test.go

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

103+
func (s) TestCZGetServer(t *testing.T) {
104+
channelz.NewChannelzStorage()
105+
e := tcpClearRREnv
106+
te := newTest(t, e)
107+
te.startServer(&testServer{security: e.security})
108+
defer te.tearDown()
109+
110+
ss, _ := channelz.GetServers(0, 0)
111+
if len(ss) != 1 {
112+
t.Fatalf("there should only be one server, not %d", len(ss))
113+
}
114+
115+
serverID := ss[0].ID
116+
srv := channelz.GetServer(serverID)
117+
if srv == nil {
118+
t.Fatalf("server %d does not exist", serverID)
119+
}
120+
if srv.ID != serverID {
121+
t.Fatalf("server want id %d, but got %d", serverID, srv.ID)
122+
}
123+
124+
te.tearDown()
125+
126+
if err := verifyResultWithDelay(func() (bool, error) {
127+
srv := channelz.GetServer(serverID)
128+
if srv != nil {
129+
return false, fmt.Errorf("server %d should not exist", serverID)
130+
}
131+
132+
return true, nil
133+
}); err != nil {
134+
t.Fatal(err)
135+
}
136+
}
137+
103138
func (s) TestCZTopChannelRegistrationAndDeletion(t *testing.T) {
104139
testcases := []struct {
105140
total int
@@ -372,6 +407,9 @@ func (s) TestCZServerListenSocketDeletion(t *testing.T) {
372407
if len(ss) != 1 {
373408
return false, fmt.Errorf("there should be 1 server, not %d", len(ss))
374409
}
410+
if len(ss[0].ListenSockets) != 0 {
411+
return false, fmt.Errorf("there should only be %d server listen socket, not %d", 0, len(ss[0].ListenSockets))
412+
}
375413
return true, nil
376414
}); err != nil {
377415
t.Fatal(err)

0 commit comments

Comments
 (0)