Java Code Examples for org.apache.ratis.client.RaftClient#getClientRpc()
The following examples show how to use
org.apache.ratis.client.RaftClient#getClientRpc() .
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: RetryCacheTests.java From ratis with Apache License 2.0 | 6 votes |
void runTestBasicRetry(CLUSTER cluster) throws Exception { RaftTestUtil.waitForLeader(cluster); final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).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")); assertReply(rpc.sendRequest(r), client, callId); // retry with the same callId for (int i = 0; i < 5; i++) { assertReply(rpc.sendRequest(r), client, callId); } assertServer(cluster, client.getId(), callId, oldLastApplied); client.close(); }
Example 2
Source File: RaftStateMachineExceptionTests.java From ratis with Apache License 2.0 | 4 votes |
@Test public void testRetryOnStateMachineException() throws Exception { final RaftProperties prop = getProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, StateMachineWithException.class, StateMachine.class); final MiniRaftCluster cluster = newCluster(3); cluster.start(); RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId(); cluster.getLeaderAndSendFirstMessage(true); 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; final SimpleMessage message = new SimpleMessage("message"); final RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, message); RaftClientReply reply = rpc.sendRequest(r); Assert.assertFalse(reply.isSuccess()); Assert.assertNotNull(reply.getStateMachineException()); // 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.assertFalse(reply.isSuccess()); Assert.assertNotNull(reply.getStateMachineException()); } 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.assertNotNull( RaftServerTestUtil.getRetryEntry(server, client.getId(), callId)); final RaftLog log = server.getState().getLog(); RaftTestUtil.logEntriesContains(log, oldLastApplied + 1, log.getNextIndex(), message); } client.close(); cluster.shutdown(); }
Example 3
Source File: RaftStateMachineExceptionTests.java From ratis with Apache License 2.0 | 4 votes |
@Test public void testRetryOnExceptionDuringReplication() throws Exception { final RaftProperties prop = getProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, StateMachineWithException.class, StateMachine.class); final MiniRaftCluster cluster = newCluster(3); cluster.start(); RaftTestUtil.waitForLeader(cluster); RaftServerImpl leader = cluster.getLeader(); RaftPeerId leaderId = leader.getId(); cluster.getLeaderAndSendFirstMessage(true); // turn on the preAppend failure switch failPreAppend = true; 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 RaftTestUtil.SimpleMessage("message")); RaftClientReply reply = rpc.sendRequest(r); Objects.requireNonNull(reply.getStateMachineException()); RetryCache.CacheEntry oldEntry = RaftServerTestUtil.getRetryEntry( leader, client.getId(), callId); Assert.assertNotNull(oldEntry); Assert.assertTrue(RaftServerTestUtil.isRetryCacheEntryFailed(oldEntry)); // At this point of time the old leader would have stepped down. wait for // leader election to complete RaftTestUtil.waitForLeader(cluster); leader = cluster.getLeader(); leaderId = leader.getId(); // retry r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new RaftTestUtil.SimpleMessage("message")); reply = rpc.sendRequest(r); Objects.requireNonNull(reply.getStateMachineException()); RetryCache.CacheEntry currentEntry = RaftServerTestUtil.getRetryEntry( leader, client.getId(), callId); Assert.assertNotNull(currentEntry); Assert.assertTrue(RaftServerTestUtil.isRetryCacheEntryFailed(currentEntry)); Assert.assertNotEquals(oldEntry, currentEntry); failPreAppend = false; client.close(); }
Example 4
Source File: RetryCacheTests.java From ratis with Apache License 2.0 | 4 votes |
void runTestRetryOnNewLeader(CLUSTER cluster) throws Exception { RaftTestUtil.waitForLeader(cluster); final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).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")); assertReply(rpc.sendRequest(r), client, callId); 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); final RaftPeerId newLeaderId = JavaUtils.attempt(() -> { final RaftPeerId id = RaftTestUtil.waitForLeader(cluster).getId(); Assert.assertNotEquals(leaderId, id); return id; }, 10, TimeDuration.valueOf(100, TimeUnit.MILLISECONDS), "wait for a leader different than " + leaderId, LOG); Assert.assertNotEquals(leaderId, newLeaderId); // same clientId and callId in the request r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, seqNum, new SimpleMessage("message")); rpc.addServers(Arrays.asList(change.newPeers)); for (int i = 0; i < 10; i++) { try { assertReply(rpc.sendRequest(r), client, callId); LOG.info("successfully sent out the retry request_" + i); } catch (Exception e) { LOG.info("hit exception while retrying the same request: " + r, e); } Thread.sleep(100); } // check the new leader and make sure the retry did not get committed Assert.assertEquals(0, count(cluster.getLeader().getState().getLog(), oldLastApplied + 1)); client.close(); }