Java Code Examples for org.apache.ratis.client.RaftClient#Builder

The following examples show how to use org.apache.ratis.client.RaftClient#Builder . 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: Client.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
  RaftProperties raftProperties = new RaftProperties();

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())),
          getPeers());

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);


}
 
Example 2
Source File: RaftAsyncTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncConfiguration() throws IOException {
  LOG.info("Running testAsyncConfiguration");
  final RaftProperties properties = new RaftProperties();
  RaftClient.Builder clientBuilder = RaftClient.newBuilder()
      .setRaftGroup(RaftGroup.emptyGroup())
      .setProperties(properties);
  int maxOutstandingRequests = RaftClientConfigKeys.Async.OUTSTANDING_REQUESTS_MAX_DEFAULT;
  try(RaftClient client = clientBuilder.build()) {
    RaftClientTestUtil.assertAsyncRequestSemaphore(client, maxOutstandingRequests, 0);
  }

  maxOutstandingRequests = 5;
  RaftClientConfigKeys.Async.setOutstandingRequestsMax(properties, maxOutstandingRequests);
  try(RaftClient client = clientBuilder.build()) {
    RaftClientTestUtil.assertAsyncRequestSemaphore(client, maxOutstandingRequests, 0);
  }
}
 
Example 3
Source File: Client.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
  RaftProperties raftProperties = new RaftProperties();

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)),
      parsePeers(peers));

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);


}
 
Example 4
Source File: RaftAsyncTests.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncConfiguration() throws IOException {
  LOG.info("Running testAsyncConfiguration");
  final RaftProperties properties = new RaftProperties();
  RaftClient.Builder clientBuilder = RaftClient.newBuilder()
      .setRaftGroup(RaftGroup.emptyGroup())
      .setProperties(properties);
  int numThreads = RaftClientConfigKeys.Async.SCHEDULER_THREADS_DEFAULT;
  int maxOutstandingRequests = RaftClientConfigKeys.Async.MAX_OUTSTANDING_REQUESTS_DEFAULT;
  try(RaftClient client = clientBuilder.build()) {
    RaftClientTestUtil.assertScheduler(client, numThreads);
    RaftClientTestUtil.assertAsyncRequestSemaphore(client, maxOutstandingRequests, 0);
  }

  numThreads = 200;
  maxOutstandingRequests = 5;
  RaftClientConfigKeys.Async.setMaxOutstandingRequests(properties, maxOutstandingRequests);
  RaftClientConfigKeys.Async.setSchedulerThreads(properties, numThreads);
  try(RaftClient client = clientBuilder.build()) {
    RaftClientTestUtil.assertScheduler(client, numThreads);
    RaftClientTestUtil.assertAsyncRequestSemaphore(client, maxOutstandingRequests, 0);
  }
}
 
Example 5
Source File: TestCreatePipelineCommandHandler.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  ozoneContainer = Mockito.mock(OzoneContainer.class);
  stateContext = Mockito.mock(StateContext.class);
  connectionManager = Mockito.mock(SCMConnectionManager.class);
  raftClient = Mockito.mock(RaftClient.class);
  final RaftClient.Builder builder = mockRaftClientBuilder();
  Mockito.when(builder.build()).thenReturn(raftClient);
  PowerMockito.mockStatic(RaftClient.class);
  PowerMockito.when(RaftClient.newBuilder()).thenReturn(builder);
}
 
Example 6
Source File: TestCreatePipelineCommandHandler.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private RaftClient.Builder mockRaftClientBuilder() {
  final RaftClient.Builder builder = Mockito.mock(RaftClient.Builder.class);
  Mockito.when(builder.setClientId(Mockito.any(ClientId.class)))
      .thenReturn(builder);
  Mockito.when(builder.setRaftGroup(Mockito.any(RaftGroup.class)))
      .thenReturn(builder);
  Mockito.when(builder.setLeaderId(Mockito.any(RaftPeerId.class)))
      .thenReturn(builder);
  Mockito.when(builder.setProperties(Mockito.any(RaftProperties.class)))
      .thenReturn(builder);
  Mockito.when(builder.setRetryPolicy(Mockito.any(RetryPolicy.class)))
      .thenReturn(builder);
  return builder;
}
 
Example 7
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("checkstyle:ParameterNumber")
private static RaftClient newRaftClient(RpcType rpcType, RaftPeerId leader,
    RaftGroup group, RetryPolicy retryPolicy,
    GrpcTlsConfig tlsConfig, ConfigurationSource ozoneConfiguration) {
  if (LOG.isTraceEnabled()) {
    LOG.trace("newRaftClient: {}, leader={}, group={}",
        rpcType, leader, group);
  }
  final RaftProperties properties = new RaftProperties();

  RaftConfigKeys.Rpc.setType(properties, rpcType);

  // Set the ratis client headers which are matching with regex.
  createRaftClientProperties(ozoneConfiguration, properties);

  RaftClient.Builder builder =  RaftClient.newBuilder()
      .setRaftGroup(group)
      .setLeaderId(leader)
      .setProperties(properties)
      .setRetryPolicy(retryPolicy);

  // TODO: GRPC TLS only for now, netty/hadoop RPC TLS support later.
  if (tlsConfig != null && rpcType == SupportedRpcType.GRPC) {
    builder.setParameters(GrpcFactory.newRaftParameters(tlsConfig));
  }
  return builder.build();
}
 
Example 8
Source File: Client.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
  int raftSegmentPreallocatedSize = 1024 * 1024 * 1024;
  RaftProperties raftProperties = new RaftProperties();
  RaftConfigKeys.Rpc.setType(raftProperties, SupportedRpcType.GRPC);
  GrpcConfigKeys.setMessageSizeMax(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.Appender.setBufferByteLimit(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setWriteBufferSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setPreallocatedSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setSegmentSizeMax(raftProperties,
      SizeInBytes.valueOf(1 * 1024 * 1024 * 1024));

  RaftServerConfigKeys.Log.setSegmentCacheNumMax(raftProperties, 2);

  RaftClientConfigKeys.Rpc.setRequestTimeout(raftProperties,
      TimeDuration.valueOf(50000, TimeUnit.MILLISECONDS));
  RaftClientConfigKeys.Async.setOutstandingRequestsMax(raftProperties, 1000);


  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())),
          getPeers());

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);
}
 
Example 9
Source File: CounterClient.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * build the RaftClient instance which is used to communicate to
 * Counter cluster
 *
 * @return the created client of Counter cluster
 */
private static RaftClient buildClient() {
  RaftProperties raftProperties = new RaftProperties();
  RaftClient.Builder builder = RaftClient.newBuilder()
      .setProperties(raftProperties)
      .setRaftGroup(CounterCommon.RAFT_GROUP)
      .setClientRpc(
          new GrpcFactory(new Parameters())
              .newRaftClientRpc(ClientId.randomId(), raftProperties));
  return builder.build();
}
 
Example 10
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public RaftClient createClient(RaftPeerId leaderId, RaftGroup group, RetryPolicy retryPolicy) {
  RaftClient.Builder builder = RaftClient.newBuilder()
      .setRaftGroup(group)
      .setLeaderId(leaderId)
      .setProperties(properties)
      .setParameters(parameters)
      .setRetryPolicy(retryPolicy);
  return builder.build();
}
 
Example 11
Source File: Client.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
  int raftSegmentPreallocatedSize = 1024 * 1024 * 1024;
  RaftProperties raftProperties = new RaftProperties();
  RaftConfigKeys.Rpc.setType(raftProperties, SupportedRpcType.GRPC);
  GrpcConfigKeys.setMessageSizeMax(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.Appender.setBufferByteLimit(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setWriteBufferSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setPreallocatedSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setSegmentSizeMax(raftProperties,
      SizeInBytes.valueOf(1 * 1024 * 1024 * 1024));

  RaftServerConfigKeys.Log.setMaxCachedSegmentNum(raftProperties, 2);

  RaftClientConfigKeys.Rpc.setRequestTimeout(raftProperties,
      TimeDuration.valueOf(50000, TimeUnit.MILLISECONDS));
  RaftClientConfigKeys.Async.setSchedulerThreads(raftProperties, 10);
  RaftClientConfigKeys.Async.setMaxOutstandingRequests(raftProperties, 1000);


  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)),
      parsePeers(peers));

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);
}
 
Example 12
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 5 votes vote down vote up
public RaftClient createClient(RaftPeerId leaderId, RaftGroup group,
    ClientId clientId, RetryPolicy retryPolicy) {
  RaftClient.Builder builder = RaftClient.newBuilder()
      .setClientId(clientId)
      .setRaftGroup(group)
      .setLeaderId(leaderId)
      .setProperties(properties)
      .setParameters(parameters)
      .setRetryPolicy(retryPolicy);
  return builder.build();
}