Search in sources :

Example 1 with RaftClientRpc

use of org.apache.ratis.client.RaftClientRpc in project incubator-ratis by apache.

the class RaftExceptionBaseTest method testNotLeaderExceptionWithReconf.

@Test
public void testNotLeaderExceptionWithReconf() throws Exception {
    Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster));
    final RaftPeerId leaderId = cluster.getLeader().getId();
    final RaftClient client = cluster.createClient(leaderId);
    // enforce leader change
    RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId);
    // also add two new peers
    // add two more peers
    MiniRaftCluster.PeerChanges change = cluster.addNewPeers(new String[] { "ss1", "ss2" }, true);
    // trigger setConfiguration
    LOG.info("Start changing the configuration: {}", Arrays.asList(change.allPeersInNewConf));
    try (final RaftClient c2 = cluster.createClient(newLeader)) {
        RaftClientReply reply = c2.setConfiguration(change.allPeersInNewConf);
        Assert.assertTrue(reply.isSuccess());
    }
    LOG.info(cluster.printServers());
    RaftClientRpc rpc = client.getClientRpc();
    RaftClientReply reply = null;
    // it is possible that the remote peer's rpc server is not ready. need retry
    for (int i = 0; reply == null && i < 10; i++) {
        try {
            reply = rpc.sendRequest(cluster.newRaftClientRequest(ClientId.randomId(), leaderId, new SimpleMessage("m1")));
        } catch (IOException ignored) {
            Thread.sleep(1000);
        }
    }
    Assert.assertNotNull(reply);
    Assert.assertFalse(reply.isSuccess());
    final NotLeaderException nle = reply.getNotLeaderException();
    Objects.requireNonNull(nle);
    Assert.assertEquals(newLeader, nle.getSuggestedLeader().getId());
    Collection<RaftPeer> peers = cluster.getPeers();
    RaftPeer[] peersFromReply = reply.getNotLeaderException().getPeers();
    Assert.assertEquals(peers.size(), peersFromReply.length);
    for (RaftPeer p : peersFromReply) {
        Assert.assertTrue(peers.contains(p));
    }
    reply = client.send(new SimpleMessage("m2"));
    Assert.assertTrue(reply.isSuccess());
    client.close();
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) IOException(java.io.IOException) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test)

Example 2 with RaftClientRpc

use of org.apache.ratis.client.RaftClientRpc 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();
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeer(org.apache.ratis.protocol.RaftPeer) IOException(java.io.IOException) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) PeerChanges(org.apache.ratis.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test)

Example 3 with RaftClientRpc

use of org.apache.ratis.client.RaftClientRpc 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();
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test)

Example 4 with RaftClientRpc

use of org.apache.ratis.client.RaftClientRpc in project incubator-ratis by apache.

the class TestRaftStateMachineException method testRetryOnStateMachineException.

@Test
public void testRetryOnStateMachineException() throws Exception {
    setAndStart(cluster);
    final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(true).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.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));
        Assert.assertEquals(oldLastApplied + 1, server.getState().getLastAppliedIndex());
    }
    client.close();
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) ParameterizedBaseTest(org.apache.ratis.examples.ParameterizedBaseTest) Test(org.junit.Test)

Example 5 with RaftClientRpc

use of org.apache.ratis.client.RaftClientRpc in project incubator-ratis by apache.

the class TestRaftStateMachineException method testRetryOnExceptionDuringReplication.

@Test
public void testRetryOnExceptionDuringReplication() throws Exception {
    setAndStart(cluster);
    final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(true).getId();
    // 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 SimpleMessage("message"));
    RaftClientReply reply = rpc.sendRequest(r);
    Objects.requireNonNull(reply.getStateMachineException());
    RetryCache.CacheEntry oldEntry = RaftServerTestUtil.getRetryEntry(cluster.getLeader(), client.getId(), callId);
    Assert.assertNotNull(oldEntry);
    Assert.assertTrue(RaftServerTestUtil.isRetryCacheEntryFailed(oldEntry));
    // retry
    reply = rpc.sendRequest(r);
    Objects.requireNonNull(reply.getStateMachineException());
    RetryCache.CacheEntry currentEntry = RaftServerTestUtil.getRetryEntry(cluster.getLeader(), client.getId(), callId);
    Assert.assertNotNull(currentEntry);
    Assert.assertTrue(RaftServerTestUtil.isRetryCacheEntryFailed(currentEntry));
    Assert.assertNotEquals(oldEntry, currentEntry);
    failPreAppend = false;
    client.close();
}
Also used : RetryCache(org.apache.ratis.server.impl.RetryCache) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) ParameterizedBaseTest(org.apache.ratis.examples.ParameterizedBaseTest) Test(org.junit.Test)

Aggregations

RaftClient (org.apache.ratis.client.RaftClient)10 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)10 Test (org.junit.Test)9 IOException (java.io.IOException)7 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)7 BaseTest (org.apache.ratis.BaseTest)4 MiniRaftCluster (org.apache.ratis.MiniRaftCluster)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 PeerChanges (org.apache.ratis.MiniRaftCluster.PeerChanges)3 ParameterizedBaseTest (org.apache.ratis.examples.ParameterizedBaseTest)2 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)2 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)2 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)2 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 RaftPeer (org.apache.ratis.protocol.RaftPeer)1 RetryCache (org.apache.ratis.server.impl.RetryCache)1 RaftLog (org.apache.ratis.server.storage.RaftLog)1