use of org.apache.ratis.protocol.RaftClientRequest 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.RaftClientRequest 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.RaftClientRequest in project alluxio by Alluxio.
the class RaftJournalSystem method sendMessageAsync.
/**
* Sends a message to a raft server asynchronously.
*
* @param server the raft peer id of the target server
* @param message the message to send
* @return a future to be completed with the client reply
*/
public synchronized CompletableFuture<RaftClientReply> sendMessageAsync(RaftPeerId server, Message message) {
RaftClient client = createClient();
RaftClientRequest request = RaftClientRequest.newBuilder().setClientId(mRawClientId).setServerId(server).setGroupId(RAFT_GROUP_ID).setCallId(nextCallId()).setMessage(message).setType(RaftClientRequest.staleReadRequestType(0)).setSlidingWindowEntry(null).build();
return client.getClientRpc().sendRequestAsync(request).whenComplete((reply, t) -> {
try {
client.close();
} catch (IOException e) {
throw new CompletionException(e);
}
});
}
use of org.apache.ratis.protocol.RaftClientRequest in project alluxio by Alluxio.
the class RaftJournalAppender method sendLocalRequest.
private CompletableFuture<RaftClientReply> sendLocalRequest(Message message, TimeDuration timeout) throws IOException {
LOG.trace("Sending local message {}", message);
// ClientId, ServerId, and GroupId must not be null
RaftClientRequest request = RaftClientRequest.newBuilder().setClientId(mLocalClientId).setServerId(mServer.getId()).setGroupId(RaftJournalSystem.RAFT_GROUP_ID).setCallId(RaftJournalSystem.nextCallId()).setMessage(message).setType(RaftClientRequest.writeRequestType()).setSlidingWindowEntry(null).build();
return mServer.submitClientRequestAsync(request);
}
Aggregations