Search in sources :

Example 1 with NettyChannelBuilder

use of io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder in project pravega by pravega.

the class ControllerImplTest method testKeepAliveNoServer.

@Test
public void testKeepAliveNoServer() throws Exception {
    // Verify that keep-alive timeout less than permissible by the server results in a failure.
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", serverPort).keepAliveTime(5, TimeUnit.SECONDS);
    if (testSecure) {
        builder = builder.sslContext(GrpcSslContexts.forClient().trustManager(new File(SecurityConfigDefaults.TLS_CA_CERT_PATH)).build());
    } else {
        builder = builder.usePlaintext();
    }
    @Cleanup final ControllerImpl controller = new ControllerImpl(builder, ControllerImplConfig.builder().clientConfig(ClientConfig.builder().trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).build()).retryAttempts(1).initialBackoffMillis(1).timeoutMillis(500).build(), this.executor);
    CompletableFuture<Boolean> createStreamStatus = controller.createStream("scope1", "streamdelayed", StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build());
    AssertExtensions.assertFutureThrows("Should throw RetriesExhaustedException", createStreamStatus, throwable -> throwable instanceof RetriesExhaustedException);
}
Also used : RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) NettyChannelBuilder(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 2 with NettyChannelBuilder

use of io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder in project thingsboard by thingsboard.

the class EdgeGrpcClient method connect.

@Override
public void connect(String edgeKey, String edgeSecret, Consumer<UplinkResponseMsg> onUplinkResponse, Consumer<EdgeConfiguration> onEdgeUpdate, Consumer<DownlinkMsg> onDownlink, Consumer<Exception> onError) {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress(rpcHost, rpcPort).keepAliveTime(keepAliveTimeSec, TimeUnit.SECONDS);
    if (sslEnabled) {
        try {
            SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
            if (StringUtils.isNotEmpty(certResource)) {
                sslContextBuilder.trustManager(ResourceUtils.getInputStream(this, certResource));
            }
            builder.sslContext(sslContextBuilder.build());
        } catch (SSLException e) {
            log.error("Failed to initialize channel!", e);
            throw new RuntimeException(e);
        }
    } else {
        builder.usePlaintext();
    }
    channel = builder.build();
    EdgeRpcServiceGrpc.EdgeRpcServiceStub stub = EdgeRpcServiceGrpc.newStub(channel);
    log.info("[{}] Sending a connect request to the TB!", edgeKey);
    this.inputStream = stub.withCompression("gzip").handleMsgs(initOutputStream(edgeKey, onUplinkResponse, onEdgeUpdate, onDownlink, onError));
    this.inputStream.onNext(RequestMsg.newBuilder().setMsgType(RequestMsgType.CONNECT_RPC_MESSAGE).setConnectRequestMsg(ConnectRequestMsg.newBuilder().setEdgeRoutingKey(edgeKey).setEdgeSecret(edgeSecret).setEdgeVersion(EdgeVersion.V_3_3_3).build()).build());
}
Also used : SslContextBuilder(io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder) NettyChannelBuilder(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) EdgeRpcServiceGrpc(org.thingsboard.server.gen.edge.v1.EdgeRpcServiceGrpc) SSLException(javax.net.ssl.SSLException)

Example 3 with NettyChannelBuilder

use of io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder in project pravega by pravega.

the class ControllerImplTest method testKeepAliveWithServer.

@Test
public void testKeepAliveWithServer() throws Exception {
    // Verify that the same RPC with permissible keepalive time succeeds.
    int serverPort2 = TestUtils.getAvailableListenPort();
    NettyServerBuilder testServerBuilder = NettyServerBuilder.forPort(serverPort2).addService(testServerImpl).permitKeepAliveTime(KEEP_ALIVE_TEST_PERMIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
    if (testSecure) {
        testServerBuilder = testServerBuilder.useTransportSecurity(new File(SecurityConfigDefaults.TLS_SERVER_CERT_PATH), new File(SecurityConfigDefaults.TLS_SERVER_PRIVATE_KEY_PATH));
    }
    @Cleanup("shutdownNow") Server testServer = testServerBuilder.build().start();
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", serverPort2).keepAliveTime(KEEP_ALIVE_TEST_KEEP_ALIVE_MILLIS, TimeUnit.MILLISECONDS);
    if (testSecure) {
        builder = builder.sslContext(GrpcSslContexts.forClient().trustManager(new File(SecurityConfigDefaults.TLS_CA_CERT_PATH)).build());
    } else {
        builder = builder.usePlaintext();
    }
    @Cleanup final ControllerImpl controller1 = new ControllerImpl(builder, ControllerImplConfig.builder().clientConfig(ClientConfig.builder().trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).build()).retryAttempts(1).initialBackoffMillis(1).build(), this.executor);
    val createStreamStatus = controller1.createStream("scope1", "streamdelayed", StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build());
    assertTrue(createStreamStatus.get());
    testServer.shutdownNow();
}
Also used : lombok.val(lombok.val) NettyServerBuilder(io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder) Server(io.grpc.Server) NettyChannelBuilder(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) File(java.io.File) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 4 with NettyChannelBuilder

use of io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder in project pravega by pravega.

the class ControllerImplTest method testCredPluginException.

@Test
public void testCredPluginException() throws Exception {
    NettyChannelBuilder builder = spy(NettyChannelBuilder.forAddress("localhost", serverPort).keepAliveTime(10, TimeUnit.SECONDS));
    final NettyChannelBuilder channelBuilder;
    if (testSecure) {
        channelBuilder = builder.sslContext(GrpcSslContexts.forClient().trustManager(new File(SecurityConfigDefaults.TLS_CA_CERT_PATH)).build());
    } else {
        channelBuilder = builder.usePlaintext();
    }
    // Setup mocks.
    ClientConfig cfg = spy(ClientConfig.builder().credentials(new DefaultCredentials("pass", "user")).trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).build());
    doThrow(new IllegalStateException("Exception thrown by cred plugin")).when(cfg).getCredentials();
    ManagedChannel channel = mock(ManagedChannel.class);
    doReturn(channel).when(builder).build();
    ControllerImplConfig controllerCfg = new ControllerImplConfig(1, 1, 1, 1, 1000, cfg);
    // Verify exception scenario.
    assertThrows(IllegalStateException.class, () -> new ControllerImpl(channelBuilder, controllerCfg, this.executor));
    verify(channel, times(1)).shutdownNow();
    verify(channel, times(1)).awaitTermination(anyLong(), any(TimeUnit.class));
}
Also used : DefaultCredentials(io.pravega.shared.security.auth.DefaultCredentials) NettyChannelBuilder(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) ManagedChannel(io.grpc.ManagedChannel) TimeUnit(java.util.concurrent.TimeUnit) ClientConfig(io.pravega.client.ClientConfig) File(java.io.File) Test(org.junit.Test)

Aggregations

NettyChannelBuilder (io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder)4 File (java.io.File)3 Test (org.junit.Test)3 Cleanup (lombok.Cleanup)2 ManagedChannel (io.grpc.ManagedChannel)1 Server (io.grpc.Server)1 NettyServerBuilder (io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder)1 SslContextBuilder (io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder)1 ClientConfig (io.pravega.client.ClientConfig)1 RetriesExhaustedException (io.pravega.common.util.RetriesExhaustedException)1 DefaultCredentials (io.pravega.shared.security.auth.DefaultCredentials)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SSLException (javax.net.ssl.SSLException)1 lombok.val (lombok.val)1 EdgeRpcServiceGrpc (org.thingsboard.server.gen.edge.v1.EdgeRpcServiceGrpc)1