Skip to content

Commit c860d92

Browse files
committed
channelz: implement GetServer
1 parent aa6b63d commit c860d92

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-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

+9
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ 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+
metrics, _ := db.get().GetServers(id, 1)
161+
if len(metrics) == 1 && metrics[0].ID == id {
162+
return metrics[0]
163+
}
164+
return nil
165+
}
166+
158167
// RegisterChannel registers the given channel c in channelz database with ref
159168
// as its reference name, and add it to the child list of its parent (identified
160169
// by pid). pid = 0 means no parent. It returns the unique channelz tracking id

Diff for: test/channelz_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,43 @@ func TestCZServerRegistrationAndDeletion(t *testing.T) {
102102
}
103103
}
104104

105+
func TestCZGetServer(t *testing.T) {
106+
defer leakcheck.Check(t)
107+
108+
channelz.NewChannelzStorage()
109+
e := tcpClearRREnv
110+
te := newTest(t, e)
111+
te.startServer(&testServer{security: e.security})
112+
defer te.tearDown()
113+
114+
ss, _ := channelz.GetServers(0, 0)
115+
if len(ss) != 1 {
116+
t.Fatalf("there should only be one servers, not %d", len(ss))
117+
}
118+
119+
serverID := ss[0].ID
120+
srv := channelz.GetServer(serverID)
121+
if srv == nil {
122+
t.Fatalf("server %d does not exist", serverID)
123+
}
124+
if srv.ID != serverID {
125+
t.Fatalf("server want id %d, but got %d", serverID, srv.ID)
126+
}
127+
128+
te.tearDown()
129+
130+
if err := verifyResultWithDelay(func() (bool, error) {
131+
srv := channelz.GetServer(serverID)
132+
if srv != nil {
133+
return false, fmt.Errorf("server %d does not exist", serverID)
134+
}
135+
136+
return true, nil
137+
}); err != nil {
138+
t.Fatal(err)
139+
}
140+
}
141+
105142
func TestCZTopChannelRegistrationAndDeletion(t *testing.T) {
106143
defer leakcheck.Check(t)
107144
testcases := []struct {
@@ -379,6 +416,9 @@ func TestCZServerListenSocketDeletion(t *testing.T) {
379416
if len(ss) != 1 {
380417
return false, fmt.Errorf("there should be 1 server, not %d", len(ss))
381418
}
419+
if len(ss[0].ListenSockets) != 0 {
420+
return false, fmt.Errorf("there should only be %d server listen socket, not %d", 0, len(ss[0].ListenSockets))
421+
}
382422
return true, nil
383423
}); err != nil {
384424
t.Fatal(err)

0 commit comments

Comments
 (0)