org.apache.ratis.grpc.client.GrpcClientProtocolService Java Examples

The following examples show how to use org.apache.ratis.grpc.client.GrpcClientProtocolService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestRatisPipelineLeader.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 120000)
public void testLeaderIdUsedOnFirstCall() throws Exception {
  List<Pipeline> pipelines = cluster.getStorageContainerManager()
      .getPipelineManager().getPipelines(HddsProtos.ReplicationType.RATIS,
          HddsProtos.ReplicationFactor.THREE);
  Assert.assertFalse(pipelines.isEmpty());
  Pipeline ratisPipeline = pipelines.iterator().next();
  Assert.assertTrue(ratisPipeline.isHealthy());
  // Verify correct leader info populated
  GenericTestUtils.waitFor(() -> {
    try {
      return verifyLeaderInfo(ratisPipeline);
    } catch (Exception e) {
      LOG.error("Failed verifying the leader info.", e);
      Assert.fail("Failed verifying the leader info.");
      return false;
    }
  }, 200, 20000);
  // Verify client connects to Leader without NotLeaderException
  XceiverClientRatis xceiverClientRatis =
      XceiverClientRatis.newXceiverClientRatis(ratisPipeline, conf);
  Logger.getLogger(GrpcClientProtocolService.class).setLevel(Level.DEBUG);
  GenericTestUtils.LogCapturer logCapturer =
      GenericTestUtils.LogCapturer.captureLogs(GrpcClientProtocolService.LOG);
  xceiverClientRatis.connect();
  ContainerProtocolCalls.createContainer(xceiverClientRatis, 1L, null);
  logCapturer.stopCapturing();
  Assert.assertFalse("Client should connect to pipeline leader on first try.",
      logCapturer.getOutput().contains(
          "org.apache.ratis.protocol.NotLeaderException"));
}
 
Example #2
Source File: GrpcService.java    From ratis with Apache License 2.0 5 votes vote down vote up
private GrpcService(RaftServer raftServer, Supplier<RaftPeerId> idSupplier, int port,
    SizeInBytes grpcMessageSizeMax, SizeInBytes appenderBufferSize,
    SizeInBytes flowControlWindow,TimeDuration requestTimeoutDuration, GrpcTlsConfig tlsConfig) {
  super(idSupplier, id -> new PeerProxyMap<>(id.toString(),
      p -> new GrpcServerProtocolClient(p, flowControlWindow.getSizeInt(),
          requestTimeoutDuration, tlsConfig)));
  if (appenderBufferSize.getSize() > grpcMessageSizeMax.getSize()) {
    throw new IllegalArgumentException("Illegal configuration: "
        + RaftServerConfigKeys.Log.Appender.BUFFER_BYTE_LIMIT_KEY + " = " + appenderBufferSize
        + " > " + GrpcConfigKeys.MESSAGE_SIZE_MAX_KEY + " = " + grpcMessageSizeMax);
  }

  NettyServerBuilder nettyServerBuilder = NettyServerBuilder.forPort(port)
      .maxInboundMessageSize(grpcMessageSizeMax.getSizeInt())
      .flowControlWindow(flowControlWindow.getSizeInt())
      .addService(new GrpcServerProtocolService(idSupplier, raftServer))
      .addService(new GrpcClientProtocolService(idSupplier, raftServer))
      .addService(new GrpcAdminProtocolService(raftServer));

  if (tlsConfig != null) {
    SslContextBuilder sslContextBuilder =
        SslContextBuilder.forServer(tlsConfig.getCertChain(),
            tlsConfig.getPrivateKey());
    if (tlsConfig.getMtlsEnabled()) {
      sslContextBuilder.clientAuth(ClientAuth.REQUIRE);
      sslContextBuilder.trustManager(tlsConfig.getCertChain());
    }
    sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, OPENSSL);
    try {
      nettyServerBuilder.sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new IllegalArgumentException("Failed to build SslContext, tlsConfig=" + tlsConfig, ex);
    }
  }
  server = nettyServerBuilder.build();
  addressSupplier = JavaUtils.memoize(() -> new InetSocketAddress(port != 0? port: server.getPort()));
}
 
Example #3
Source File: GrpcService.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:ParameterNumber") // private constructor
private GrpcService(RaftServer raftServer, Supplier<RaftPeerId> idSupplier, int port,
    SizeInBytes grpcMessageSizeMax, SizeInBytes appenderBufferSize,
    SizeInBytes flowControlWindow,TimeDuration requestTimeoutDuration, GrpcTlsConfig tlsConfig) {
  super(idSupplier, id -> new PeerProxyMap<>(id.toString(),
      p -> new GrpcServerProtocolClient(p, flowControlWindow.getSizeInt(),
          requestTimeoutDuration, tlsConfig)));
  if (appenderBufferSize.getSize() > grpcMessageSizeMax.getSize()) {
    throw new IllegalArgumentException("Illegal configuration: "
        + RaftServerConfigKeys.Log.Appender.BUFFER_BYTE_LIMIT_KEY + " = " + appenderBufferSize
        + " > " + GrpcConfigKeys.MESSAGE_SIZE_MAX_KEY + " = " + grpcMessageSizeMax);
  }

  this.clientProtocolService = new GrpcClientProtocolService(idSupplier, raftServer);

  NettyServerBuilder nettyServerBuilder = NettyServerBuilder.forPort(port)
      .withChildOption(ChannelOption.SO_REUSEADDR, true)
      .maxInboundMessageSize(grpcMessageSizeMax.getSizeInt())
      .flowControlWindow(flowControlWindow.getSizeInt())
      .addService(new GrpcServerProtocolService(idSupplier, raftServer))
      .addService(clientProtocolService)
      .addService(new GrpcAdminProtocolService(raftServer));

  if (tlsConfig != null) {
    SslContextBuilder sslContextBuilder =
        tlsConfig.isFileBasedConfig()?
            SslContextBuilder.forServer(tlsConfig.getCertChainFile(),
                tlsConfig.getPrivateKeyFile()):
            SslContextBuilder.forServer(tlsConfig.getPrivateKey(),
                tlsConfig.getCertChain());
    if (tlsConfig.getMtlsEnabled()) {
      sslContextBuilder.clientAuth(ClientAuth.REQUIRE);
      if (tlsConfig.isFileBasedConfig()) {
        sslContextBuilder.trustManager(tlsConfig.getTrustStoreFile());
      } else {
          sslContextBuilder.trustManager(tlsConfig.getTrustStore());
      }
    }
    sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, OPENSSL);
    try {
      nettyServerBuilder.sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new IllegalArgumentException("Failed to build SslContext, tlsConfig=" + tlsConfig, ex);
    }
  }
  server = nettyServerBuilder.build();
  addressSupplier = JavaUtils.memoize(() -> new InetSocketAddress(port != 0? port: server.getPort()));
}