Search in sources :

Example 1 with SocketStats

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"));
}
Also used : Metadata(io.grpc.Metadata) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SocketStats(io.grpc.InternalChannelz.SocketStats) Test(org.junit.Test)

Example 2 with SocketStats

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)));
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ChannelOption(io.netty.channel.ChannelOption) ServerStreamTracer(io.grpc.ServerStreamTracer) ServerTransport(io.grpc.internal.ServerTransport) InetSocketAddress(java.net.InetSocketAddress) SocketStats(io.grpc.InternalChannelz.SocketStats) ServerTransportListener(io.grpc.internal.ServerTransportListener) InternalInstrumented(io.grpc.InternalInstrumented) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ServerListener(io.grpc.internal.ServerListener) Test(org.junit.Test)

Example 3 with SocketStats

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;
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) Executor(java.util.concurrent.Executor) Subchannel(io.grpc.LoadBalancer.Subchannel) Metadata(io.grpc.Metadata) InternalMetadata(io.grpc.InternalMetadata) StreamInfo(io.grpc.ClientStreamTracer.StreamInfo) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) SocketStats(io.grpc.InternalChannelz.SocketStats) Nullable(javax.annotation.Nullable)

Example 4 with SocketStats

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();
}
Also used : Server(io.grpc.channelz.v1.Server) ServerStats(io.grpc.InternalChannelz.ServerStats) SocketStats(io.grpc.InternalChannelz.SocketStats)

Example 5 with SocketStats

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();
}
Also used : SocketStats(io.grpc.InternalChannelz.SocketStats) Socket(io.grpc.channelz.v1.Socket)

Aggregations

SocketStats (io.grpc.InternalChannelz.SocketStats)11 Test (org.junit.Test)6 ServerStats (io.grpc.InternalChannelz.ServerStats)3 ServerTransportListener (io.grpc.internal.ServerTransportListener)3 ChannelOption (io.netty.channel.ChannelOption)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 ServerSocketsList (io.grpc.InternalChannelz.ServerSocketsList)2 InternalInstrumented (io.grpc.InternalInstrumented)2 Metadata (io.grpc.Metadata)2 ServerStreamTracer (io.grpc.ServerStreamTracer)2 ServerListener (io.grpc.internal.ServerListener)2 ServerTransport (io.grpc.internal.ServerTransport)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 CallOptions (io.grpc.CallOptions)1 ClientStreamTracer (io.grpc.ClientStreamTracer)1 StreamInfo (io.grpc.ClientStreamTracer.StreamInfo)1 InternalMetadata (io.grpc.InternalMetadata)1 Subchannel (io.grpc.LoadBalancer.Subchannel)1 MethodDescriptor (io.grpc.MethodDescriptor)1