Java Code Examples for org.apache.ratis.server.impl.RaftServerImpl#getId()

The following examples show how to use org.apache.ratis.server.impl.RaftServerImpl#getId() . 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: ServerRestartTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void runTestRestartWithCorruptedLogEntry(CLUSTER cluster) throws Exception {
  // this is the only server
  final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster);
  final RaftPeerId id = leader.getId();

  // send a few messages
  final SimpleMessage[] messages = SimpleMessage.create(10);
  final SimpleMessage lastMessage = messages[messages.length - 1];
  try (final RaftClient client = cluster.createClient()) {
    for (SimpleMessage m : messages) {
      Assert.assertTrue(client.send(m).isSuccess());
    }

    // assert that the last message exists
    Assert.assertTrue(client.sendReadOnly(lastMessage).isSuccess());
  }

  final RaftLog log = leader.getState().getLog();
  final long size = TestSegmentedRaftLog.getOpenSegmentSize(log);
  leader.getProxy().close();

  // corrupt the log
  final File openLogFile = JavaUtils.attemptRepeatedly(() -> getOpenLogFile(leader),
      10, HUNDRED_MILLIS, id + "-getOpenLogFile", LOG);
  try(final RandomAccessFile raf = new RandomAccessFile(openLogFile, "rw")) {
    final long mid = size / 2;
    raf.seek(mid);
    for (long i = mid; i < size; i++) {
      raf.write(0);
    }
  }

  // after the log is corrupted and the server is restarted, the last entry should no longer exist.
  cluster.restartServer(id, false);
  testFailureCase("last-entry-not-found", () -> {
    try (final RaftClient client = cluster.createClient()) {
      client.sendReadOnly(lastMessage);
    }
  }, StateMachineException.class, IndexOutOfBoundsException.class);
}
 
Example 2
Source File: TestRaftServerWithGrpc.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
void runTestServerRestartOnException(MiniRaftClusterWithGrpc cluster) throws Exception {
  final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster);
  final RaftPeerId leaderId = leader.getId();

  final RaftProperties p = getProperties();
  GrpcConfigKeys.Server.setPort(p, leader.getServerRpc().getInetSocketAddress().getPort());

  // Create a raft server proxy with server rpc bound to a different address
  // compared to leader. This helps in locking the raft storage directory to
  // be used by next raft server proxy instance.
  final StateMachine stateMachine = cluster.getLeader().getStateMachine();
  RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId)));
  ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, p, null);
  // Close the server rpc for leader so that new raft server can be bound to it.
  cluster.getLeader().getServerRpc().close();

  // Create a raft server proxy with server rpc bound to same address as
  // the leader. This step would fail as the raft storage has been locked by
  // the raft server proxy created earlier. Raft server proxy should close
  // the rpc server on failure.
  RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId)));
  testFailureCase("start a new server with the same address",
      () -> ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, p, null).start(),
      IOException.class, OverlappingFileLockException.class);
  // Try to start a raft server rpc at the leader address.
  cluster.getServer(leaderId).getFactory().newRaftServerRpc(cluster.getServer(leaderId));
}
 
Example 3
Source File: RaftTestUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static RaftPeerId waitAndKillLeader(MiniRaftCluster cluster) throws InterruptedException {
  final RaftServerImpl leader = waitForLeader(cluster);
  Assert.assertNotNull(leader);

  LOG.info("killing leader = " + leader);
  cluster.killServer(leader.getId());
  return leader.getId();
}
 
Example 4
Source File: RaftTestUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
static RaftPeerId waitAndKillLeader(MiniRaftCluster cluster) throws InterruptedException {
  final RaftServerImpl leader = waitForLeader(cluster);
  Assert.assertNotNull(leader);

  LOG.info("killing leader = " + leader);
  cluster.killServer(leader.getId());
  return leader.getId();
}
 
Example 5
Source File: ServerRestartTests.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
void runTestRestartCommitIndex(MiniRaftCluster cluster) throws Exception {
  final SimpleMessage[] messages = SimpleMessage.create(100);
  final List<CompletableFuture<Void>> futures = new ArrayList<>(messages.length);
  for(int i = 0; i < messages.length; i++) {
    final CompletableFuture<Void> f = new CompletableFuture<>();
    futures.add(f);

    final SimpleMessage m = messages[i];
    new Thread(() -> {
      try (final RaftClient client = cluster.createClient()) {
        Assert.assertTrue(client.send(m).isSuccess());
      } catch (IOException e) {
        throw new IllegalStateException("Failed to send " + m, e);
      }
      f.complete(null);
    }).start();
  }
  JavaUtils.allOf(futures).get();

  final List<RaftPeerId> ids = new ArrayList<>();
  final RaftServerImpl leader = cluster.getLeader();
  final RaftLog leaderLog = leader.getState().getLog();
  final RaftPeerId leaderId = leader.getId();
  ids.add(leaderId);

  RaftTestUtil.getStateMachineLogEntries(leaderLog);

  // check that the last metadata entry is written to the log
  JavaUtils.attempt(() -> assertLastLogEntry(leader), 20, HUNDRED_MILLIS, "leader last metadata entry", LOG);

  final long lastIndex = leaderLog.getLastEntryTermIndex().getIndex();
  LOG.info("{}: leader lastIndex={}", leaderId, lastIndex);
  final LogEntryProto lastEntry = leaderLog.get(lastIndex);
  LOG.info("{}: leader lastEntry entry[{}] = {}", leaderId, lastIndex, ServerProtoUtils.toLogEntryString(lastEntry));
  final long loggedCommitIndex = lastEntry.getMetadataEntry().getCommitIndex();
  final LogEntryProto lastCommittedEntry = leaderLog.get(loggedCommitIndex);
  LOG.info("{}: leader lastCommittedEntry = entry[{}] = {}",
      leaderId, loggedCommitIndex, ServerProtoUtils.toLogEntryString(lastCommittedEntry));

  final SimpleStateMachine4Testing leaderStateMachine = SimpleStateMachine4Testing.get(leader);
  final TermIndex lastAppliedTermIndex = leaderStateMachine.getLastAppliedTermIndex();
  LOG.info("{}: leader lastAppliedTermIndex = {}", leaderId, lastAppliedTermIndex);

  // check follower logs
  for(RaftServerImpl s : cluster.iterateServerImpls()) {
    if (!s.getId().equals(leaderId)) {
      ids.add(s.getId());
      RaftTestUtil.assertSameLog(leaderLog, s.getState().getLog());
    }
  }

  // take snapshot and truncate last (metadata) entry
  leaderStateMachine.takeSnapshot();
  leaderLog.truncate(lastIndex);

  // kill all servers
  ids.forEach(cluster::killServer);

  // Restart and kill servers one by one so that they won't talk to each other.
  for(RaftPeerId id : ids) {
    cluster.restartServer(id, false);
    final RaftServerImpl server = cluster.getRaftServerImpl(id);
    final RaftLog raftLog = server.getState().getLog();
    JavaUtils.attemptRepeatedly(() -> {
      Assert.assertTrue(raftLog.getLastCommittedIndex() >= loggedCommitIndex);
      return null;
    }, 10, HUNDRED_MILLIS, id + "(commitIndex >= loggedCommitIndex)", LOG);
    JavaUtils.attemptRepeatedly(() -> {
      Assert.assertTrue(server.getState().getLastAppliedIndex() >= loggedCommitIndex);
      return null;
    }, 10, HUNDRED_MILLIS, id + "(lastAppliedIndex >= loggedCommitIndex)", LOG);
    LOG.info("{}: commitIndex={}, lastAppliedIndex={}",
        id, raftLog.getLastCommittedIndex(), server.getState().getLastAppliedIndex());
    cluster.killServer(id);
  }
}
 
Example 6
Source File: InstallSnapshotNotificationTests.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private void testRestartFollower(CLUSTER cluster) throws Exception {
  leaderSnapshotInfoRef.set(null);
  int i = 0;
  final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster);
  final RaftPeerId leaderId = leader.getId();

  try (final RaftClient client = cluster.createClient(leaderId)) {
    for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
      final RaftClientReply reply = client.send(new RaftTestUtil.SimpleMessage("m" + i));
      Assert.assertTrue(reply.isSuccess());
    }
  }

  // wait for the snapshot to be done
  final long oldLeaderNextIndex = leader.getState().getLog().getNextIndex();
  {
    LOG.info("{}: oldLeaderNextIndex = {}", leaderId, oldLeaderNextIndex);
    final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster,
        oldLeaderNextIndex - SNAPSHOT_TRIGGER_THRESHOLD, oldLeaderNextIndex);
    JavaUtils.attemptRepeatedly(() -> {
      Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
      return null;
    }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
  }

  final RaftPeerId followerId = cluster.getFollowers().get(0).getId();
  cluster.killServer(followerId);

  // generate some more traffic
  try (final RaftClient client = cluster.createClient(leader.getId())) {
    Assert.assertTrue(client.send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
  }

  FIVE_SECONDS.sleep();
  cluster.restartServer(followerId, false);
  final RaftServerImpl follower = cluster.getRaftServerImpl(followerId);
  JavaUtils.attempt(() -> {
    final long newLeaderNextIndex = leader.getState().getLog().getNextIndex();
    LOG.info("{}: newLeaderNextIndex = {}", leaderId, newLeaderNextIndex);
    Assert.assertTrue(newLeaderNextIndex > oldLeaderNextIndex);
    Assert.assertEquals(newLeaderNextIndex, follower.getState().getLog().getNextIndex());
  }, 10, ONE_SECOND, "followerNextIndex", LOG);

}
 
Example 7
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 8
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);
}