use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class RaftBasicTests method testRequestTimeout.
public static void testRequestTimeout(boolean async, MiniRaftCluster cluster, Logger LOG, RaftProperties properties) throws InterruptedException, IOException, ExecutionException {
LOG.info("Running testRequestTimeout");
waitForLeader(cluster);
long time = System.currentTimeMillis();
try (final RaftClient client = cluster.createClient()) {
// Get the next callId to be used by the client
long callId = RaftClientTestUtil.getCallId(client);
// Create an entry corresponding to the callId and clientId
// in each server's retry cache.
cluster.getServerAliveStream().forEach(raftServer -> RetryCacheTestUtil.getOrCreateEntry(raftServer.getRetryCache(), client.getId(), callId));
// The retry is successful when the retry cache entry for the corresponding callId and clientId expires.
if (async) {
CompletableFuture<RaftClientReply> replyFuture = client.sendAsync(new SimpleMessage("abc"));
replyFuture.get();
} else {
client.send(new SimpleMessage("abc"));
}
// Eventually the request would be accepted by the server
// when the retry cache entry is invalidated.
// The duration for which the client waits should be more than the retryCacheExpiryDuration.
TimeDuration duration = TimeDuration.valueOf(System.currentTimeMillis() - time, TimeUnit.MILLISECONDS);
TimeDuration retryCacheExpiryDuration = RaftServerConfigKeys.RetryCache.expiryTime(properties);
Assert.assertTrue(duration.compareTo(retryCacheExpiryDuration) >= 0);
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class RetryCacheTests method testRetryOnNewLeader.
/**
* Test retry while the leader changes to another peer
*/
@Test
public void testRetryOnNewLeader() throws Exception {
final MiniRaftCluster cluster = getCluster();
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId();
final RaftClient client = cluster.createClient(leaderId);
RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
final long seqNum = 111;
RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new SimpleMessage("message"));
RaftClientReply reply = rpc.sendRequest(r);
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex();
// trigger the reconfiguration, make sure the original leader is kicked out
PeerChanges change = cluster.addNewPeers(2, true);
RaftPeer[] allPeers = cluster.removePeers(2, true, asList(change.newPeers)).allPeersInNewConf;
// trigger setConfiguration
cluster.setConfiguration(allPeers);
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId newLeaderId = cluster.getLeader().getId();
Assert.assertNotEquals(leaderId, newLeaderId);
// same clientId and callId in the request
r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, seqNum, new SimpleMessage("message"));
for (int i = 0; i < 10; i++) {
try {
reply = rpc.sendRequest(r);
LOG.info("successfully sent out the retry request_" + i);
Assert.assertEquals(client.getId(), reply.getClientId());
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
} catch (Exception e) {
LOG.info("hit exception while retrying the same request: " + e);
}
Thread.sleep(100);
}
// check the new leader and make sure the retry did not get committed
Assert.assertEquals(oldLastApplied + 3, cluster.getLeader().getState().getLastAppliedIndex());
client.close();
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class RetryCacheTests method testBasicRetry.
/**
* make sure the retry cache can correct capture the retry from a client,
* and returns the result from the previous request
*/
@Test
public void testBasicRetry() throws Exception {
final MiniRaftCluster cluster = getCluster();
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId();
long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex();
final RaftClient client = cluster.createClient(leaderId);
final RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
final long seqNum = 111;
RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new SimpleMessage("message"));
RaftClientReply reply = rpc.sendRequest(r);
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
// retry with the same callId
for (int i = 0; i < 5; i++) {
reply = rpc.sendRequest(r);
Assert.assertEquals(client.getId(), reply.getClientId());
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
}
long leaderApplied = cluster.getLeader().getState().getLastAppliedIndex();
// make sure retry cache has the entry
for (RaftServerImpl server : cluster.iterateServerImpls()) {
LOG.info("check server " + server.getId());
if (server.getState().getLastAppliedIndex() < leaderApplied) {
Thread.sleep(1000);
}
Assert.assertEquals(2, RaftServerTestUtil.getRetryCacheSize(server));
Assert.assertNotNull(RaftServerTestUtil.getRetryEntry(server, client.getId(), callId));
// make sure there is only one log entry committed
Assert.assertEquals(oldLastApplied + 1, server.getState().getLastAppliedIndex());
}
client.close();
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method testRestartPeer.
/**
* Keep generating writing traffic and make sure snapshots are taken.
* We then restart the whole raft peer and check if it can correctly load
* snapshots + raft log.
*/
@Test
public void testRestartPeer() throws Exception {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
int i = 0;
try (final RaftClient client = cluster.createClient(leaderId)) {
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
RaftClientReply reply = client.send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
// wait for the snapshot to be done
final File snapshotFile = getSnapshotFile(cluster, i);
int retries = 0;
do {
Thread.sleep(1000);
} while (!snapshotFile.exists() && retries++ < 10);
Assert.assertTrue(snapshotFile + " does not exist", snapshotFile.exists());
// restart the peer and check if it can correctly load snapshot
cluster.restart(false);
try {
// 200 messages + two leader elections --> last committed = 201
assertLeaderContent(cluster);
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftClientReply in project alluxio by Alluxio.
the class RaftJournalSystem method resetPriorities.
/**
* Resets RaftPeer priorities.
*
* @throws IOException
*/
public synchronized void resetPriorities() throws IOException {
List<RaftPeer> resetPeers = new ArrayList<>();
final int NEUTRAL_PRIORITY = 1;
for (RaftPeer peer : mRaftGroup.getPeers()) {
resetPeers.add(RaftPeer.newBuilder(peer).setPriority(NEUTRAL_PRIORITY).build());
}
LOG.info("Resetting RaftPeer priorities");
try (RaftClient client = createClient()) {
RaftClientReply reply = client.admin().setConfiguration(resetPeers);
processReply(reply, "failed to reset master priorities to 1");
}
}
Aggregations