Search in sources :

Example 1 with RaftLog

use of org.apache.ratis.server.storage.RaftLog in project incubator-ratis by apache.

the class RaftBasicTests method testOldLeaderCommit.

@Test
public void testOldLeaderCommit() throws Exception {
    LOG.info("Running testOldLeaderCommit");
    final MiniRaftCluster cluster = getCluster();
    final RaftServerImpl leader = waitForLeader(cluster);
    final RaftPeerId leaderId = leader.getId();
    final long term = leader.getState().getCurrentTerm();
    List<RaftServerImpl> followers = cluster.getFollowers();
    final RaftServerImpl followerToSendLog = followers.get(0);
    for (int i = 1; i < NUM_SERVERS - 1; i++) {
        RaftServerImpl follower = followers.get(i);
        cluster.killServer(follower.getId());
    }
    SimpleMessage[] messages = SimpleMessage.create(1);
    RaftTestUtil.sendMessageInNewThread(cluster, messages);
    Thread.sleep(cluster.getMaxTimeout() + 100);
    RaftLog followerLog = followerToSendLog.getState().getLog();
    assertTrue(logEntriesContains(followerLog, messages));
    LOG.info(String.format("killing old leader: %s", leaderId.toString()));
    cluster.killServer(leaderId);
    for (int i = 1; i < 3; i++) {
        RaftServerImpl follower = followers.get(i);
        LOG.info(String.format("restarting follower: %s", follower.getId().toString()));
        cluster.restartServer(follower.getId(), false);
    }
    Thread.sleep(cluster.getMaxTimeout() * 5);
    // confirm the server with log is elected as new leader.
    final RaftPeerId newLeaderId = waitForLeader(cluster).getId();
    Assert.assertEquals(followerToSendLog.getId(), newLeaderId);
    cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.assertLogEntries(log, false, term, messages));
    LOG.info("terminating testOldLeaderCommit test");
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) Timer(java.util.Timer) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Level(org.apache.log4j.Level) After(org.junit.After) JavaUtils(org.apache.ratis.util.JavaUtils) TimerTask(java.util.TimerTask) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) Before(org.junit.Before) LogUtils(org.apache.ratis.util.LogUtils) Logger(org.slf4j.Logger) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) Predicate(java.util.function.Predicate) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RaftLog(org.apache.ratis.server.storage.RaftLog) RaftTestUtil(org.apache.ratis.RaftTestUtil) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) TimeDuration(org.apache.ratis.util.TimeDuration) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftLog(org.apache.ratis.server.storage.RaftLog) Test(org.junit.Test)

Example 2 with RaftLog

use of org.apache.ratis.server.storage.RaftLog in project incubator-ratis by apache.

the class TestRaftStream method testSimpleWrite.

@Test
public void testSimpleWrite() throws Exception {
    final int numRequests = 500;
    LOG.info("Running testSimpleWrite, numRequests=" + numRequests);
    // default 64K is too large for a test
    GrpcConfigKeys.OutputStream.setBufferSize(prop, SizeInBytes.valueOf(4));
    cluster = MiniRaftClusterWithGRpc.FACTORY.newCluster(NUM_SERVERS, prop);
    cluster.start();
    RaftServerImpl leader = waitForLeader(cluster);
    try (RaftOutputStream out = new RaftOutputStream(prop, ClientId.randomId(), cluster.getGroup(), leader.getId())) {
        for (int i = 0; i < numRequests; i++) {
            // generate requests
            out.write(toBytes(i));
        }
    }
    // check the leader's raft log
    final RaftLog raftLog = leader.getState().getLog();
    final AtomicInteger i = new AtomicInteger();
    checkLog(raftLog, numRequests, () -> toBytes(i.getAndIncrement()));
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) RaftOutputStream(org.apache.ratis.grpc.client.RaftOutputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftLog(org.apache.ratis.server.storage.RaftLog) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 3 with RaftLog

use of org.apache.ratis.server.storage.RaftLog in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method testRevertConfigurationChange.

/**
 * Test a scenario where the follower truncates its log entries which causes
 * configuration change.
 */
@Test
public void testRevertConfigurationChange() throws Exception {
    LOG.info("Start testRevertConfigurationChange");
    RaftLog log2 = null;
    final MiniRaftCluster cluster = getCluster(5);
    try {
        cluster.start();
        RaftTestUtil.waitForLeader(cluster);
        final RaftServerImpl leader = cluster.getLeader();
        final RaftPeerId leaderId = leader.getId();
        final RaftLog log = leader.getState().getLog();
        log2 = log;
        Thread.sleep(1000);
        // we block the incoming msg for the leader and block its requests to
        // followers, so that we force the leader change and the old leader will
        // not know
        LOG.info("start blocking the leader");
        BlockRequestHandlingInjection.getInstance().blockReplier(leaderId.toString());
        cluster.setBlockRequestsFrom(leaderId.toString(), true);
        PeerChanges change = cluster.removePeers(1, false, new ArrayList<>());
        AtomicBoolean gotNotLeader = new AtomicBoolean(false);
        final Thread clientThread = new Thread(() -> {
            try (final RaftClient client = cluster.createClient(leaderId)) {
                LOG.info("client starts to change conf");
                final RaftClientRpc sender = client.getClientRpc();
                RaftClientReply reply = sender.sendRequest(cluster.newSetConfigurationRequest(client.getId(), leaderId, change.allPeersInNewConf));
                if (reply.getNotLeaderException() != null) {
                    gotNotLeader.set(true);
                }
            } catch (IOException e) {
                LOG.warn("Got unexpected exception when client1 changes conf", e);
            }
        });
        clientThread.start();
        // find CONFIGURATIONENTRY, there may be NOOP before and after it.
        final long confIndex = JavaUtils.attempt(() -> {
            final long last = log.getLastEntryTermIndex().getIndex();
            for (long i = 1; i <= last; i++) {
                if (log.get(i).getLogEntryBodyCase() == CONFIGURATIONENTRY) {
                    return i;
                }
            }
            throw new Exception("CONFIGURATIONENTRY not found: last=" + last);
        }, 10, 500, "confIndex", LOG);
        // wait till the old leader persist the new conf
        JavaUtils.attempt(() -> log.getLatestFlushedIndex() >= confIndex, 10, 500L, "FLUSH", LOG);
        final long committed = log.getLastCommittedIndex();
        Assert.assertTrue(committed < confIndex);
        // unblock the old leader
        BlockRequestHandlingInjection.getInstance().unblockReplier(leaderId.toString());
        cluster.setBlockRequestsFrom(leaderId.toString(), false);
        // the client should get NotLeaderException
        clientThread.join(5000);
        Assert.assertTrue(gotNotLeader.get());
        // the old leader should have truncated the setConf from the log
        JavaUtils.attempt(() -> log.getLastCommittedIndex() >= confIndex, 10, 500L, "COMMIT", LOG);
        Assert.assertEquals(NOOP, log.get(confIndex).getLogEntryBodyCase());
        log2 = null;
    } finally {
        RaftStorageTestUtils.printLog(log2, s -> LOG.info(s));
        cluster.shutdown();
    }
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MiniRaftCluster(org.apache.ratis.MiniRaftCluster) PeerChanges(org.apache.ratis.MiniRaftCluster.PeerChanges) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.storage.RaftLog) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 4 with RaftLog

use of org.apache.ratis.server.storage.RaftLog 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 5 with RaftLog

use of org.apache.ratis.server.storage.RaftLog in project incubator-ratis by apache.

the class MiniRaftCluster method printAllLogs.

public String printAllLogs() {
    StringBuilder b = new StringBuilder("\n#servers = " + servers.size() + "\n");
    for (RaftServerImpl s : iterateServerImpls()) {
        b.append("  ");
        b.append(s).append("\n");
        final RaftLog log = s.getState().getLog();
        if (log instanceof MemoryRaftLog) {
            b.append("    ");
            b.append(((MemoryRaftLog) log).getEntryString());
        }
    }
    return b.toString();
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) MemoryRaftLog(org.apache.ratis.server.storage.MemoryRaftLog) MemoryRaftLog(org.apache.ratis.server.storage.MemoryRaftLog) RaftLog(org.apache.ratis.server.storage.RaftLog)

Aggregations

RaftLog (org.apache.ratis.server.storage.RaftLog)6 Test (org.junit.Test)5 BaseTest (org.apache.ratis.BaseTest)4 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)4 IOException (java.io.IOException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 RaftClient (org.apache.ratis.client.RaftClient)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 MiniRaftCluster (org.apache.ratis.MiniRaftCluster)2 PeerChanges (org.apache.ratis.MiniRaftCluster.PeerChanges)2 RaftOutputStream (org.apache.ratis.grpc.client.RaftOutputStream)2 List (java.util.List)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1