Java Code Examples for org.apache.ratis.protocol.ClientId#randomId()

The following examples show how to use org.apache.ratis.protocol.ClientId#randomId() . 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: ReconControllerModule.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Provides
OzoneManagerProtocol getOzoneManagerProtocol(
    final OzoneConfiguration ozoneConfiguration) {
  OzoneManagerProtocol ozoneManagerClient = null;
  try {
    ClientId clientId = ClientId.randomId();
    UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
    OmTransport transport =
        OmTransportFactory.create(ozoneConfiguration, ugi,
            ozoneConfiguration.get(OZONE_OM_INTERNAL_SERVICE_ID));
    ozoneManagerClient = new
        OzoneManagerProtocolClientSideTranslatorPB(
        transport, clientId.toString());
  } catch (IOException ioEx) {
    LOG.error("Error in provisioning OzoneManagerProtocol ", ioEx);
  }
  return ozoneManagerClient;
}
 
Example 2
Source File: TestRatisPipelineLeader.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private boolean verifyLeaderInfo(Pipeline ratisPipeline) throws Exception {
  Optional<HddsDatanodeService> hddsDatanodeService =
      cluster.getHddsDatanodes().stream().filter(s ->
          s.getDatanodeStateMachine().getDatanodeDetails().getUuid()
              .equals(ratisPipeline.getLeaderId())).findFirst();
  Assert.assertTrue(hddsDatanodeService.isPresent());

  XceiverServerRatis serverRatis =
      (XceiverServerRatis) hddsDatanodeService.get()
          .getDatanodeStateMachine().getContainer().getWriteChannel();

  GroupInfoRequest groupInfoRequest = new GroupInfoRequest(
      ClientId.randomId(), serverRatis.getServer().getId(),
      RaftGroupId.valueOf(ratisPipeline.getId().getId()), 100);
  GroupInfoReply reply =
      serverRatis.getServer().getGroupInfo(groupInfoRequest);
  return reply.getRoleInfoProto().hasLeaderInfo() &&
      ratisPipeline.getLeaderId().toString().equals(
          reply.getRoleInfoProto().getSelf().getId().toStringUtf8());
}
 
Example 3
Source File: TestRetryCacheMetrics.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryCacheHitMissCount() {
  checkHit(0, 1.0);
  checkMiss(0, 0.0);

  ClientId clientId = ClientId.randomId();
  retryCache.getOrCreateEntry(clientId, 2);

  checkHit(0, 0.0);
  checkMiss(1, 1.0);

  retryCache.getOrCreateEntry(clientId, 2);

  checkHit(1, 0.5);
  checkMiss(1, 0.5);
}
 
Example 4
Source File: FollowerAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Configure raft group before using raft service.
 */
private void configureGroup() throws IOException {

  ClientId clientId = ClientId.randomId();

  RaftGroupId groupId = RaftGroupId
      .valueOf(UUID.fromString(pipelineId));

  RaftPeerId peerId =
      RaftPeerId.getRaftPeerId(serverId);

  RaftGroup group = RaftGroup.valueOf(groupId,
      new RaftPeer(RaftPeerId.valueOf(serverId), serverAddress),
      new RaftPeer(RaftPeerId.valueOf(FAKE_LEADER_ID),
          FAKE_LEADER_ADDDRESS));
  RaftClient client = RaftClient.newBuilder()
      .setClientId(clientId)
      .setProperties(new RaftProperties(true))
      .setRaftGroup(group)
      .build();

  RaftClientReply raftClientReply = client.groupAdd(group, peerId);

  LOG.info(
      "Group is configured in the RAFT server (one follower, one fake "
          + "leader): {}", raftClientReply);
}
 
Example 5
Source File: LeaderAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private void configureGroup() throws IOException {
  ClientId clientId = ClientId.randomId();

  RaftGroupId groupId = RaftGroupId
      .valueOf(UUID.fromString(pipelineId));
  RaftPeerId peerId =
      RaftPeerId.getRaftPeerId(serverId);

  RaftGroup group = RaftGroup.valueOf(groupId,
      new RaftPeer(RaftPeerId.valueOf(serverId), serverAddress),
      new RaftPeer(RaftPeerId.valueOf(FAKE_FOLLOWER_ID1),
          FAKE_LEADER_ADDDRESS1),
      new RaftPeer(RaftPeerId.valueOf(FAKE_FOLLOWER_ID1),
          FAKE_LEADER_ADDDRESS2));
  RaftClient client = RaftClient.newBuilder()
      .setClientId(clientId)
      .setProperties(new RaftProperties(true))
      .setRaftGroup(group)
      .build();

  RaftClientReply raftClientReply = client.groupAdd(group, peerId);

  LOG.info(
      "Group is configured in the RAFT server (with two fake leader leader)"
          + ": {}",
      raftClientReply);
}
 
Example 6
Source File: TestClientProtoUtils.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
void runTestToRaftClientRequestProto(int n, SizeInBytes messageSize)
    throws Exception {
  final ClientId clientId = ClientId.randomId();
  final RaftPeerId leaderId = RaftPeerId.valueOf("s0");
  final RaftGroupId groupId = RaftGroupId.randomId();


  TimeDuration toProto = TimeDuration.ZERO;
  TimeDuration toRequest = TimeDuration.ZERO;

  for(int i = 0; i < n; i++) {
    final ByteString bytes = newByteString(messageSize.getSizeInt(), i);
    final RaftClientRequest request = new RaftClientRequest(clientId, leaderId, groupId,
        1, () -> bytes, RaftClientRequest.writeRequestType(), null);

    final Timestamp startTime = Timestamp.currentTime();
    final RaftClientRequestProto proto = ClientProtoUtils.toRaftClientRequestProto(request);
    final TimeDuration p = startTime.elapsedTime();
    final RaftClientRequest computed = ClientProtoUtils.toRaftClientRequest(proto);
    final TimeDuration r = startTime.elapsedTime().subtract(p);

    Assert.assertEquals(request.getMessage().getContent(), computed.getMessage().getContent());
    toProto = toProto.add(p);
    toRequest = toRequest.add(r);

  }

  System.out.printf("%nmessageSize=%s, n=%d%n", messageSize, n);
  print("toProto  ", toProto, n);
  print("toRequest", toRequest, n);
}
 
Example 7
Source File: TestRetryCacheMetrics.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetryCacheEntryCount() {
  checkEntryCount(0);

  ClientId clientId = ClientId.randomId();
  RetryCache.CacheKey key = new RetryCache.CacheKey(clientId, 1);
  RetryCache.CacheEntry entry = new RetryCache.CacheEntry(key);

  retryCache.refreshEntry(entry);
  checkEntryCount(1);

  retryCache.close();
  checkEntryCount(0);
}
 
Example 8
Source File: TestGrpcOutputStream.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public OutputStream newOutputStream(MiniRaftClusterWithGrpc cluster, int bufferSize) {
  final RaftProperties p = getProperties();
  GrpcConfigKeys.OutputStream.setBufferSize(p, SizeInBytes.valueOf(bufferSize));
  return new GrpcOutputStream(p, ClientId.randomId(), cluster.getGroup(), cluster.getLeader().getId(), null);
}
 
Example 9
Source File: TestRetryPolicy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private static RaftClientRequest newRaftClientRequest(RaftClientRequest.Type type) {
  return new RaftClientRequest(ClientId.randomId(), RaftPeerId.valueOf("s0"), RaftGroupId.randomId(), 1L, type);
}
 
Example 10
Source File: TestRaftStream.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteAndFlush() throws Exception {
  LOG.info("Running testWriteAndFlush");

  GrpcConfigKeys.OutputStream.setBufferSize(prop, SizeInBytes.valueOf(ByteValue.BUFFERSIZE));
  cluster = MiniRaftClusterWithGrpc.FACTORY.newCluster(NUM_SERVERS, prop);
  cluster.start();

  RaftServerImpl leader = waitForLeader(cluster);
  GrpcOutputStream out = new GrpcOutputStream(prop, ClientId.randomId(),
      cluster.getGroup(), leader.getId(), null);

  int[] lengths = new int[]{1, 500, 1023, 1024, 1025, 2048, 3000, 3072};
  ByteValue[] values = new ByteValue[lengths.length];
  for (int i = 0; i < values.length; i++) {
    values[i] = new ByteValue(lengths[i], (byte) 9);
  }

  List<byte[]> expectedTxs = new ArrayList<>();
  for (ByteValue v : values) {
    byte[] data = v.genData();
    expectedTxs.addAll(v.getTransactions());
    out.write(data);
    out.flush();

    // make sure after the flush the data has been committed
    Assert.assertEquals(expectedTxs.size(),
        leader.getState().getLastAppliedIndex());
  }
  out.close();

  try {
    out.write(0);
    fail("The OutputStream has been closed");
  } catch (IOException ignored) {
  }

  LOG.info("Start to check leader's log");
  final AtomicInteger index = new AtomicInteger(0);
  checkLog(leader.getState().getLog(), expectedTxs.size(),
      () -> expectedTxs.get(index.getAndIncrement()));
}
 
Example 11
Source File: TestRaftStream.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteWithOffset() throws Exception {
  LOG.info("Running testWriteWithOffset");
  GrpcConfigKeys.OutputStream.setBufferSize(prop, SizeInBytes.valueOf(ByteValue.BUFFERSIZE));

  cluster = MiniRaftClusterWithGrpc.FACTORY.newCluster(NUM_SERVERS, prop);
  cluster.start();
  RaftServerImpl leader = waitForLeader(cluster);

  GrpcOutputStream out = new GrpcOutputStream(prop, ClientId.randomId(),
      cluster.getGroup(), leader.getId(), null);

  byte[] b1 = new byte[ByteValue.BUFFERSIZE / 2];
  Arrays.fill(b1, (byte) 1);
  byte[] b2 = new byte[ByteValue.BUFFERSIZE];
  Arrays.fill(b2, (byte) 2);
  byte[] b3 = new byte[ByteValue.BUFFERSIZE * 2 + ByteValue.BUFFERSIZE / 2];
  Arrays.fill(b3, (byte) 3);
  byte[] b4 = new byte[ByteValue.BUFFERSIZE * 4];
  Arrays.fill(b3, (byte) 4);

  byte[] expected = new byte[ByteValue.BUFFERSIZE * 8];
  byte[][] data = new byte[][]{b1, b2, b3, b4};
  final Random random = new Random();
  int totalSize = 0;
  for (byte[] b : data) {
    System.arraycopy(b, 0, expected, totalSize, b.length);
    totalSize += b.length;

    int written = 0;
    while (written < b.length) {
      int toWrite = random.nextInt(b.length - written) + 1;
      LOG.info("write {} bytes", toWrite);
      out.write(b, written, toWrite);
      written += toWrite;
    }
  }
  out.close();

  final RaftLog log = leader.getState().getLog();
  // 0.5 + 1 + 2.5 + 4 = 8
  Assert.assertEquals(8, leader.getState().getLastAppliedIndex());
  Assert.assertEquals(8, log.getLastCommittedIndex());
  TermIndex[] entries = log.getEntries(1, 9);
  byte[] actual = new byte[ByteValue.BUFFERSIZE * 8];
  totalSize = 0;
  for (TermIndex e : entries) {
    byte[] eValue = log.get(e.getIndex()).getStateMachineLogEntry().getLogData().toByteArray();
    Assert.assertEquals(ByteValue.BUFFERSIZE, eValue.length);
    System.arraycopy(eValue, 0, actual, totalSize, eValue.length);
    totalSize += eValue.length;
  }
  Assert.assertArrayEquals(expected, actual);
}