use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestExceptionDependentRetry method runTestExceptionRetryAttempts.
void runTestExceptionRetryAttempts(MiniRaftClusterWithGrpc cluster) throws Exception {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final ExceptionDependentRetry policy = ExceptionDependentRetry.newBuilder().setExceptionToPolicy(TimeoutIOException.class, MultipleLinearRandomRetry.parseCommaSeparated("1ms, 5")).setDefaultPolicy(RetryPolicies.retryForeverNoSleep()).build();
// create a client with the exception dependent policy
try (final RaftClient client = cluster.createClient(policy)) {
client.async().send(new RaftTestUtil.SimpleMessage("1")).get();
}
try (final RaftClient client = cluster.createClient(policy)) {
SimpleStateMachine4Testing.get(leader).blockWriteStateMachineData();
client.async().send(new RaftTestUtil.SimpleMessage("2")).get();
Assert.fail("Test should have failed.");
} catch (ExecutionException e) {
RaftRetryFailureException rrfe = (RaftRetryFailureException) e.getCause();
Assert.assertEquals(16, rrfe.getAttemptCount());
} finally {
SimpleStateMachine4Testing.get(leader).unblockWriteStateMachineData();
cluster.shutdown();
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class ServerRestartTests method runTestRestartFollower.
void runTestRestartFollower(MiniRaftCluster cluster) throws Exception {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
// write some messages
final AtomicInteger messageCount = new AtomicInteger();
final Supplier<Message> newMessage = () -> new SimpleMessage("m" + messageCount.getAndIncrement());
writeSomething(newMessage, cluster);
// restart a follower
RaftPeerId followerId = cluster.getFollowers().get(0).getId();
LOG.info("Restart follower {}", followerId);
cluster.restartServer(followerId, false);
// write some more messages
writeSomething(newMessage, cluster);
final int truncatedMessageIndex = messageCount.get() - 1;
final long leaderLastIndex = cluster.getLeader().getRaftLog().getLastEntryTermIndex().getIndex();
// make sure the restarted follower can catchup
final RaftServer.Division followerState = cluster.getDivision(followerId);
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(followerState.getInfo().getLastAppliedIndex() >= leaderLastIndex);
return null;
}, 10, ONE_SECOND, "follower catchup", LOG);
// make sure the restarted peer's log segments is correct
final RaftServer.Division follower = cluster.restartServer(followerId, false);
final RaftLog followerLog = follower.getRaftLog();
final long followerLastIndex = followerLog.getLastEntryTermIndex().getIndex();
Assert.assertTrue(followerLastIndex >= leaderLastIndex);
final File followerOpenLogFile = getOpenLogFile(follower);
final File leaderOpenLogFile = getOpenLogFile(cluster.getDivision(leaderId));
// shutdown all servers
for (RaftServer s : cluster.getServers()) {
s.close();
}
// truncate log and
assertTruncatedLog(followerId, followerOpenLogFile, followerLastIndex, cluster);
assertTruncatedLog(leaderId, leaderOpenLogFile, leaderLastIndex, cluster);
// restart and write something.
cluster.restart(false);
writeSomething(newMessage, cluster);
// restart again and check messages.
cluster.restart(false);
try (final RaftClient client = cluster.createClient()) {
for (int i = 0; i < messageCount.get(); i++) {
if (i != truncatedMessageIndex) {
final Message m = new SimpleMessage("m" + i);
final RaftClientReply reply = client.io().sendReadOnly(m);
Assert.assertTrue(reply.isSuccess());
LOG.info("query {}: {} {}", m, reply, LogEntryProto.parseFrom(reply.getMessage().getContent()));
}
}
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestStateMachine method runTestTransactionContextIsPassedBack.
static void runTestTransactionContextIsPassedBack(MiniRaftCluster cluster) throws Throwable {
// tests that the TrxContext set by the StateMachine in Leader is passed back to the SM
int numTrx = 100;
final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numTrx);
try (final RaftClient client = cluster.createClient()) {
for (RaftTestUtil.SimpleMessage message : messages) {
client.io().send(message);
}
}
// TODO: there eshould be a better way to ensure all data is replicated and applied
Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
for (RaftServer.Division raftServer : cluster.iterateDivisions()) {
final SMTransactionContext sm = SMTransactionContext.get(raftServer);
sm.rethrowIfException();
assertEquals(numTrx, sm.numApplied.get());
}
// check leader
RaftServer.Division raftServer = cluster.getLeader();
// assert every transaction has obtained context in leader
final SMTransactionContext sm = SMTransactionContext.get(raftServer);
final List<Long> ll = new ArrayList<>(sm.applied);
Collections.sort(ll);
assertEquals(ll.toString(), ll.size(), numTrx);
for (int i = 0; i < numTrx; i++) {
assertEquals(ll.toString(), Long.valueOf(i + 1), ll.get(i));
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestStateMachine method testStateMachineRegistry.
@Test
public void testStateMachineRegistry() throws Throwable {
final Map<RaftGroupId, StateMachine> registry = new ConcurrentHashMap<>();
registry.put(RaftGroupId.randomId(), new SimpleStateMachine4Testing());
registry.put(RaftGroupId.randomId(), new SMTransactionContext());
try (MiniRaftClusterWithSimulatedRpc cluster = newCluster(0)) {
cluster.setStateMachineRegistry(registry::get);
final RaftPeerId id = RaftPeerId.valueOf("s0");
cluster.putNewServer(id, null, true);
cluster.start();
for (RaftGroupId gid : registry.keySet()) {
final RaftGroup newGroup = RaftGroup.valueOf(gid, cluster.getPeers());
LOG.info("add new group: " + newGroup);
try (final RaftClient client = cluster.createClient(newGroup)) {
for (RaftPeer p : newGroup.getPeers()) {
client.getGroupManagementApi(p.getId()).add(newGroup);
}
}
}
final RaftServer server = cluster.getServer(id);
for (Map.Entry<RaftGroupId, StateMachine> e : registry.entrySet()) {
Assert.assertSame(e.getValue(), server.getDivision(e.getKey()).getStateMachine());
}
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestCounter method testSeveralCounter.
@Test
public void testSeveralCounter() throws IOException, InterruptedException {
setAndStart(cluster);
try (final RaftClient client = cluster.createClient()) {
for (int i = 0; i < 10; i++) {
client.io().send(Message.valueOf("INCREMENT"));
}
RaftClientReply reply1 = client.io().sendReadOnly(Message.valueOf("GET"));
Assert.assertEquals("10", reply1.getMessage().getContent().toString(Charset.defaultCharset()));
for (int i = 0; i < 10; i++) {
client.io().send(Message.valueOf("INCREMENT"));
}
RaftClientReply reply2 = client.io().sendReadOnly(Message.valueOf("GET"));
Assert.assertEquals("20", reply2.getMessage().getContent().toString(Charset.defaultCharset()));
for (int i = 0; i < 10; i++) {
client.io().send(Message.valueOf("INCREMENT"));
}
RaftClientReply reply3 = client.io().sendReadOnly(Message.valueOf("GET"));
Assert.assertEquals("30", reply3.getMessage().getContent().toString(Charset.defaultCharset()));
}
}
Aggregations