Search in sources :

Example 1 with RaftClientReply

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);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) TimeDuration(org.apache.ratis.util.TimeDuration) RaftClient(org.apache.ratis.client.RaftClient)

Example 2 with RaftClientReply

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();
}
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 RaftClientReply

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();
}
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 RaftClientReply

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();
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 5 with RaftClientReply

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");
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ArrayList(java.util.ArrayList) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)18 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)11 RaftClient (org.apache.ratis.client.RaftClient)9 IOException (java.io.IOException)8 InetSocketAddress (java.net.InetSocketAddress)5 ArrayList (java.util.ArrayList)5 RaftPeer (org.apache.ratis.protocol.RaftPeer)5 Test (org.junit.Test)5 File (java.io.File)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)4 ServerConfiguration (alluxio.conf.ServerConfiguration)3 JournalQueryRequest (alluxio.grpc.JournalQueryRequest)3 Map (java.util.Map)3 CompletionException (java.util.concurrent.CompletionException)3 Collectors (java.util.stream.Collectors)3 Message (org.apache.ratis.protocol.Message)3 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)3 AbortedException (alluxio.exception.status.AbortedException)2 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)2