Search in sources :

Example 6 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class TestRaftStateMachineException method testHandleStateMachineException.

@Test
public void testHandleStateMachineException() throws Exception {
    setAndStart(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        client.send(new SimpleMessage("m"));
        fail("Exception expected");
    } catch (StateMachineException e) {
        e.printStackTrace();
        Assert.assertTrue(e.getCause().getMessage().contains("Fake Exception"));
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) ParameterizedBaseTest(org.apache.ratis.examples.ParameterizedBaseTest) Test(org.junit.Test)

Example 7 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage 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)

Example 8 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method testNoChangeRequest.

/**
 * When a request's new configuration is the same with the current one, make
 * sure we return success immediately and no log entry is recorded.
 */
@Test
public void testNoChangeRequest() throws Exception {
    LOG.info("Start testNoChangeRequest");
    // originally 3 peers
    final MiniRaftCluster cluster = getCluster(3);
    try {
        cluster.start();
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        final RaftClient client = cluster.createClient(leaderId);
        client.send(new SimpleMessage("m"));
        final long committedIndex = cluster.getLeader().getState().getLog().getLastCommittedIndex();
        final RaftConfiguration confBefore = cluster.getLeader().getRaftConf();
        // no real configuration change in the request
        RaftClientReply reply = client.setConfiguration(cluster.getPeers().toArray(new RaftPeer[0]));
        Assert.assertTrue(reply.isSuccess());
        Assert.assertEquals(committedIndex, cluster.getLeader().getState().getLog().getLastCommittedIndex());
        Assert.assertSame(confBefore, cluster.getLeader().getRaftConf());
        client.close();
    } finally {
        cluster.shutdown();
    }
}
Also used : MiniRaftCluster(org.apache.ratis.MiniRaftCluster) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 9 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method testBootstrapReconf.

@Test
public void testBootstrapReconf() throws Exception {
    LOG.info("Start testBootstrapReconf");
    // originally 3 peers
    final MiniRaftCluster cluster = getCluster(3);
    cluster.start();
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        final RaftClient client = cluster.createClient(leaderId);
        // submit some msgs before reconf
        for (int i = 0; i < getStagingGap() * 2; i++) {
            RaftClientReply reply = client.send(new SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        PeerChanges c1 = cluster.addNewPeers(2, true);
        LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
        final AtomicReference<Boolean> success = new AtomicReference<>();
        Thread clientThread = new Thread(() -> {
            try {
                RaftClientReply reply = client.setConfiguration(c1.allPeersInNewConf);
                success.set(reply.isSuccess());
                client.close();
            } catch (IOException ioe) {
                LOG.error("FAILED", ioe);
            }
        });
        clientThread.start();
        Thread.sleep(5000);
        LOG.info(cluster.printServers());
        assertSuccess(success);
        final RaftLog leaderLog = cluster.getLeader().getState().getLog();
        for (RaftPeer newPeer : c1.newPeers) {
            Assert.assertArrayEquals(leaderLog.getEntries(0, Long.MAX_VALUE), cluster.getServer(newPeer.getId()).getImpl().getState().getLog().getEntries(0, Long.MAX_VALUE));
        }
    } finally {
        cluster.shutdown();
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) MiniRaftCluster(org.apache.ratis.MiniRaftCluster) PeerChanges(org.apache.ratis.MiniRaftCluster.PeerChanges) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.storage.RaftLog) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 10 with SimpleMessage

use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method testReconfTwice.

@Test(timeout = 30000)
public void testReconfTwice() throws Exception {
    LOG.info("Start testReconfTwice");
    final MiniRaftCluster cluster = getCluster(3);
    cluster.start();
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        final RaftClient client = cluster.createClient(leaderId);
        // submit some msgs before reconf
        for (int i = 0; i < getStagingGap() * 2; i++) {
            RaftClientReply reply = client.send(new SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        final AtomicBoolean reconf1 = new AtomicBoolean(false);
        final AtomicBoolean reconf2 = new AtomicBoolean(false);
        final AtomicReference<RaftPeer[]> finalPeers = new AtomicReference<>(null);
        final AtomicReference<RaftPeer[]> deadPeers = new AtomicReference<>(null);
        CountDownLatch latch = new CountDownLatch(1);
        Thread clientThread = new Thread(() -> {
            try {
                PeerChanges c1 = cluster.addNewPeers(2, true);
                LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
                RaftClientReply reply = client.setConfiguration(c1.allPeersInNewConf);
                reconf1.set(reply.isSuccess());
                PeerChanges c2 = cluster.removePeers(2, true, asList(c1.newPeers));
                finalPeers.set(c2.allPeersInNewConf);
                deadPeers.set(c2.removedPeers);
                LOG.info("Start changing the configuration again: {}", asList(c2.allPeersInNewConf));
                reply = client.setConfiguration(c2.allPeersInNewConf);
                reconf2.set(reply.isSuccess());
                latch.countDown();
                client.close();
            } catch (IOException ignored) {
            }
        });
        clientThread.start();
        latch.await();
        Assert.assertTrue(reconf1.get());
        Assert.assertTrue(reconf2.get());
        waitAndCheckNewConf(cluster, finalPeers.get(), 2, null);
        // check configuration manager's internal state
        // each reconf will generate two configurations: (old, new) and (new)
        cluster.getServerAliveStream().forEach(server -> {
            ConfigurationManager confManager = (ConfigurationManager) Whitebox.getInternalState(server.getState(), "configurationManager");
            // each reconf will generate two configurations: (old, new) and (new)
            Assert.assertEquals(5, confManager.numOfConf());
        });
    } finally {
        cluster.shutdown();
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MiniRaftCluster(org.apache.ratis.MiniRaftCluster) PeerChanges(org.apache.ratis.MiniRaftCluster.PeerChanges) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)52 RaftClient (org.apache.ratis.client.RaftClient)46 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)27 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)20 Test (org.junit.Test)20 RaftServer (org.apache.ratis.server.RaftServer)19 IOException (java.io.IOException)15 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)10 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)9 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 BaseTest (org.apache.ratis.BaseTest)7 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)6 File (java.io.File)5