services: add support for channelz.GetServer()

This commit is contained in:
Sergii Tkachenko 2021-02-01 18:09:32 -05:00 committed by Sergii Tkachenko
parent 4c5559d54f
commit 461b10a907
3 changed files with 76 additions and 0 deletions

View File

@ -179,6 +179,12 @@ public final class InternalChannelz {
return new ServerList(serverList, !iterator.hasNext()); return new ServerList(serverList, !iterator.hasNext());
} }
/** Returns a server. */
@Nullable
public InternalInstrumented<ServerStats> getServer(long id) {
return servers.get(id);
}
/** Returns socket refs for a server. */ /** Returns socket refs for a server. */
@Nullable @Nullable
public ServerSocketsList getServerSockets(long serverId, long fromId, int maxPageSize) { public ServerSocketsList getServerSockets(long serverId, long fromId, int maxPageSize) {

View File

@ -22,6 +22,7 @@ import io.grpc.InternalChannelz;
import io.grpc.InternalChannelz.ChannelStats; import io.grpc.InternalChannelz.ChannelStats;
import io.grpc.InternalChannelz.ServerList; import io.grpc.InternalChannelz.ServerList;
import io.grpc.InternalChannelz.ServerSocketsList; import io.grpc.InternalChannelz.ServerSocketsList;
import io.grpc.InternalChannelz.ServerStats;
import io.grpc.InternalChannelz.SocketStats; import io.grpc.InternalChannelz.SocketStats;
import io.grpc.InternalInstrumented; import io.grpc.InternalInstrumented;
import io.grpc.Status; import io.grpc.Status;
@ -29,6 +30,8 @@ import io.grpc.StatusRuntimeException;
import io.grpc.channelz.v1.ChannelzGrpc; import io.grpc.channelz.v1.ChannelzGrpc;
import io.grpc.channelz.v1.GetChannelRequest; import io.grpc.channelz.v1.GetChannelRequest;
import io.grpc.channelz.v1.GetChannelResponse; import io.grpc.channelz.v1.GetChannelResponse;
import io.grpc.channelz.v1.GetServerRequest;
import io.grpc.channelz.v1.GetServerResponse;
import io.grpc.channelz.v1.GetServerSocketsRequest; import io.grpc.channelz.v1.GetServerSocketsRequest;
import io.grpc.channelz.v1.GetServerSocketsResponse; import io.grpc.channelz.v1.GetServerSocketsResponse;
import io.grpc.channelz.v1.GetServersRequest; import io.grpc.channelz.v1.GetServersRequest;
@ -126,6 +129,33 @@ public final class ChannelzService extends ChannelzGrpc.ChannelzImplBase {
responseObserver.onCompleted(); responseObserver.onCompleted();
} }
/** Returns a server. */
@Override
public void getServer(
GetServerRequest request, StreamObserver<GetServerResponse> responseObserver) {
InternalInstrumented<ServerStats> s = channelz.getServer(request.getServerId());
if (s == null) {
responseObserver.onError(
Status.NOT_FOUND.withDescription("Can't find server " + request.getServerId())
.asRuntimeException());
return;
}
GetServerResponse resp;
try {
resp = GetServerResponse
.newBuilder()
.setServer(ChannelzProtoUtil.toServer(s))
.build();
} catch (StatusRuntimeException e) {
responseObserver.onError(e);
return;
}
responseObserver.onNext(resp);
responseObserver.onCompleted();
}
/** Returns a subchannel. */ /** Returns a subchannel. */
@Override @Override
public void getSubchannel( public void getSubchannel(

View File

@ -25,6 +25,8 @@ import io.grpc.InternalChannelz;
import io.grpc.Status; import io.grpc.Status;
import io.grpc.channelz.v1.GetChannelRequest; import io.grpc.channelz.v1.GetChannelRequest;
import io.grpc.channelz.v1.GetChannelResponse; import io.grpc.channelz.v1.GetChannelResponse;
import io.grpc.channelz.v1.GetServerRequest;
import io.grpc.channelz.v1.GetServerResponse;
import io.grpc.channelz.v1.GetServersRequest; import io.grpc.channelz.v1.GetServersRequest;
import io.grpc.channelz.v1.GetServersResponse; import io.grpc.channelz.v1.GetServersResponse;
import io.grpc.channelz.v1.GetSocketRequest; import io.grpc.channelz.v1.GetSocketRequest;
@ -127,6 +129,24 @@ public class ChannelzServiceTest {
getServersHelper(0)); getServersHelper(0));
} }
@Test
public void getServer() throws ExecutionException, InterruptedException {
TestServer server = new TestServer();
assertServerNotFound(server.getLogId().getId());
channelz.addServer(server);
assertEquals(
GetServerResponse
.newBuilder()
.setServer(ChannelzProtoUtil.toServer(server))
.build(),
getServerHelper(server.getLogId().getId()));
channelz.removeServer(server);
assertServerNotFound(server.getLogId().getId());
}
@Test @Test
public void getSocket() throws Exception { public void getSocket() throws Exception {
TestSocket socket = new TestSocket(); TestSocket socket = new TestSocket();
@ -212,6 +232,26 @@ public class ChannelzServiceTest {
return responseCaptor.getValue(); return responseCaptor.getValue();
} }
private void assertServerNotFound(long id) {
@SuppressWarnings("unchecked")
StreamObserver<GetServerResponse> observer = mock(StreamObserver.class);
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
service.getServer(GetServerRequest.newBuilder().setServerId(id).build(), observer);
verify(observer).onError(exceptionCaptor.capture());
Status s = Status.fromThrowable(exceptionCaptor.getValue());
assertWithMessage(s.toString()).that(s.getCode()).isEqualTo(Status.Code.NOT_FOUND);
}
private GetServerResponse getServerHelper(long id) {
@SuppressWarnings("unchecked")
StreamObserver<GetServerResponse> observer = mock(StreamObserver.class);
ArgumentCaptor<GetServerResponse> response = ArgumentCaptor.forClass(GetServerResponse.class);
service.getServer(GetServerRequest.newBuilder().setServerId(id).build(), observer);
verify(observer).onNext(response.capture());
verify(observer).onCompleted();
return response.getValue();
}
private void assertSocketNotFound(long id) { private void assertSocketNotFound(long id) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
StreamObserver<GetSocketResponse> observer = mock(StreamObserver.class); StreamObserver<GetSocketResponse> observer = mock(StreamObserver.class);