use of io.grpc.InternalChannelz.SocketStats in project grpc-java by grpc.
the class AbstractTransportTest method socketStats.
@Test
public void socketStats() throws Exception {
server.start(serverListener);
ManagedClientTransport client = newClientTransport(server);
startTransport(client, mockClientTransportListener);
ClientStream clientStream = client.newStream(methodDescriptor, new Metadata(), callOptions, tracers);
ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase();
clientStream.start(clientStreamListener);
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
ServerStream serverStream = serverStreamCreation.stream;
SocketAddress serverAddress = clientStream.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
SocketAddress clientAddress = serverStream.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
SocketStats clientSocketStats = client.getStats().get();
assertEquals("clientLocal " + clientStream.getAttributes(), clientAddress, clientSocketStats.local);
assertEquals("clientRemote " + clientStream.getAttributes(), serverAddress, clientSocketStats.remote);
// very basic sanity check that socket options are populated
assertNotNull(clientSocketStats.socketOptions.lingerSeconds);
assertTrue(clientSocketStats.socketOptions.others.containsKey("SO_SNDBUF"));
SocketStats serverSocketStats = serverTransportListener.transport.getStats().get();
assertEquals("serverLocal " + serverStream.getAttributes(), serverAddress, serverSocketStats.local);
assertEquals("serverRemote " + serverStream.getAttributes(), clientAddress, serverSocketStats.remote);
// very basic sanity check that socket options are populated
assertNotNull(serverSocketStats.socketOptions.lingerSeconds);
assertTrue(serverSocketStats.socketOptions.others.containsKey("SO_SNDBUF"));
}
use of io.grpc.InternalChannelz.SocketStats in project grpc-java by grpc.
the class NettyServerTest method multiPortStartStopGet.
@Test
public void multiPortStartStopGet() throws Exception {
InetSocketAddress addr1 = new InetSocketAddress(0);
InetSocketAddress addr2 = new InetSocketAddress(0);
NettyServer ns = new NettyServer(Arrays.asList(addr1, addr2), new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap<ChannelOption<?>, Object>(), new HashMap<ChannelOption<?>, Object>(), new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, ProtocolNegotiators.plaintext(), Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), // ignore
1, // ignore
false, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
true, // ignore
0, Attributes.EMPTY, channelz);
final SettableFuture<Void> shutdownCompleted = SettableFuture.create();
ns.start(new ServerListener() {
@Override
public ServerTransportListener transportCreated(ServerTransport transport) {
return new NoopServerTransportListener();
}
@Override
public void serverShutdown() {
shutdownCompleted.set(null);
}
});
// SocketStats won't be available until the event loop task of adding SocketStats created by
// ns.start() complete. So submit a noop task and await until it's drained.
eventLoop.submit(new Runnable() {
@Override
public void run() {
}
}).await(5, TimeUnit.SECONDS);
assertEquals(2, ns.getListenSocketAddresses().size());
for (SocketAddress address : ns.getListenSocketAddresses()) {
assertThat(((InetSocketAddress) address).getPort()).isGreaterThan(0);
}
List<InternalInstrumented<SocketStats>> stats = ns.getListenSocketStatsList();
assertEquals(2, ns.getListenSocketStatsList().size());
for (InternalInstrumented<SocketStats> listenSocket : stats) {
assertSame(listenSocket, channelz.getSocket(id(listenSocket)));
// very basic sanity check of the contents
SocketStats socketStats = listenSocket.getStats().get();
assertThat(ns.getListenSocketAddresses()).contains(socketStats.local);
assertNull(socketStats.remote);
}
// Cleanup
ns.shutdown();
shutdownCompleted.get();
// listen socket is removed
for (InternalInstrumented<SocketStats> listenSocket : stats) {
assertNull(channelz.getSocket(id(listenSocket)));
}
}
use of io.grpc.InternalChannelz.SocketStats in project grpc-java by grpc.
the class GrpcUtil method getTransportFromPickResult.
/**
* Returns a transport out of a PickResult, or {@code null} if the result is "buffer".
*/
@Nullable
static ClientTransport getTransportFromPickResult(PickResult result, boolean isWaitForReady) {
final ClientTransport transport;
Subchannel subchannel = result.getSubchannel();
if (subchannel != null) {
transport = ((TransportProvider) subchannel.getInternalSubchannel()).obtainActiveTransport();
} else {
transport = null;
}
if (transport != null) {
final ClientStreamTracer.Factory streamTracerFactory = result.getStreamTracerFactory();
if (streamTracerFactory == null) {
return transport;
}
return new ClientTransport() {
@Override
public ClientStream newStream(MethodDescriptor<?, ?> method, Metadata headers, CallOptions callOptions, ClientStreamTracer[] tracers) {
StreamInfo info = StreamInfo.newBuilder().setCallOptions(callOptions).build();
ClientStreamTracer streamTracer = streamTracerFactory.newClientStreamTracer(info, headers);
checkState(tracers[tracers.length - 1] == NOOP_TRACER, "lb tracer already assigned");
tracers[tracers.length - 1] = streamTracer;
return transport.newStream(method, headers, callOptions, tracers);
}
@Override
public void ping(PingCallback callback, Executor executor) {
transport.ping(callback, executor);
}
@Override
public InternalLogId getLogId() {
return transport.getLogId();
}
@Override
public ListenableFuture<SocketStats> getStats() {
return transport.getStats();
}
};
}
if (!result.getStatus().isOk()) {
if (result.isDrop()) {
return new FailingClientTransport(result.getStatus(), RpcProgress.DROPPED);
}
if (!isWaitForReady) {
return new FailingClientTransport(result.getStatus(), RpcProgress.PROCESSED);
}
}
return null;
}
use of io.grpc.InternalChannelz.SocketStats in project grpc-java by grpc.
the class ChannelzProtoUtil method toServer.
static Server toServer(InternalInstrumented<ServerStats> obj) {
ServerStats stats = getFuture(obj.getStats());
Server.Builder builder = Server.newBuilder().setRef(toServerRef(obj)).setData(toServerData(stats));
for (InternalInstrumented<SocketStats> listenSocket : stats.listenSockets) {
builder.addListenSocket(toSocketRef(listenSocket));
}
return builder.build();
}
use of io.grpc.InternalChannelz.SocketStats in project grpc-java by grpc.
the class ChannelzProtoUtil method toSocket.
static Socket toSocket(InternalInstrumented<SocketStats> obj) {
SocketStats socketStats = getFuture(obj.getStats());
Socket.Builder builder = Socket.newBuilder().setRef(toSocketRef(obj)).setLocal(toAddress(socketStats.local));
if (socketStats.security != null) {
builder.setSecurity(toSecurity(socketStats.security));
}
// listen sockets do not have remote nor data
if (socketStats.remote != null) {
builder.setRemote(toAddress(socketStats.remote));
}
builder.setData(extractSocketData(socketStats));
return builder.build();
}
Aggregations