Search in sources :

Example 16 with RaftLog

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

the class ServerPauseResumeTest method runTestPauseResume.

void runTestPauseResume(CLUSTER cluster) throws InterruptedException, IOException {
    // wait leader be elected.
    final RaftServer.Division leader = waitForLeader(cluster);
    RaftPeerId leaderId = leader.getId();
    final List<RaftServer.Division> followers = cluster.getFollowers();
    Assert.assertTrue(followers.size() >= 1);
    final RaftServerImpl follower = (RaftServerImpl) followers.get(0);
    SimpleMessage[] batch1 = SimpleMessage.create(100, "batch1");
    Thread writeThread = RaftTestUtil.sendMessageInNewThread(cluster, leaderId, batch1);
    writeThread.join();
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    final RaftLog leaderLog = leader.getRaftLog();
    // leader should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(leaderLog, batch1));
    RaftLog followerLog = follower.getRaftLog();
    // follower should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, batch1));
    // pause follower.
    boolean isSuccess = follower.pause();
    Assert.assertTrue(isSuccess);
    Assert.assertTrue(follower.getInfo().getLifeCycleState().isPausingOrPaused());
    SimpleMessage[] batch2 = SimpleMessage.create(100, "batch2");
    Thread writeThread2 = RaftTestUtil.sendMessageInNewThread(cluster, leaderId, batch2);
    writeThread2.join();
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    // paused follower should not have any batch2 message in its raftlog.
    Assert.assertTrue(RaftTestUtil.logEntriesNotContains(followerLog, batch2));
    // resume follower.
    isSuccess = follower.resume();
    Assert.assertTrue(isSuccess);
    Assert.assertFalse(follower.getInfo().getLifeCycleState().isPausingOrPaused());
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    // follower should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, batch2));
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 17 with RaftLog

use of org.apache.ratis.server.raftlog.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 (RaftServer.Division s : iterateDivisions()) {
        b.append("  ");
        b.append(s).append("\n");
        final RaftLog log = s.getRaftLog();
        if (log instanceof MemoryRaftLog) {
            b.append("    ");
            b.append(((MemoryRaftLog) log).getEntryString());
        }
    }
    return b.toString();
}
Also used : MemoryRaftLog(org.apache.ratis.server.raftlog.memory.MemoryRaftLog) RaftServer(org.apache.ratis.server.RaftServer) MemoryRaftLog(org.apache.ratis.server.raftlog.memory.MemoryRaftLog) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 18 with RaftLog

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

the class RaftReconfigurationBaseTest method runTestBootstrapReconf.

void runTestBootstrapReconf(int numNewPeer, boolean startNewPeer, CLUSTER cluster) throws Exception {
    LOG.info("Originally {} peer(s), add {} more, startNewPeer={}", cluster.getNumServers(), numNewPeer, startNewPeer);
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        // submit some msgs before reconf
        for (int i = 0; i < STAGING_CATCHUP_GAP * 2; i++) {
            RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
        final PeerChanges c1 = cluster.addNewPeers(numNewPeer, startNewPeer);
        LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
        final AtomicReference<Boolean> success = new AtomicReference<>();
        Thread clientThread = new Thread(() -> {
            try {
                RaftClientReply reply = client.admin().setConfiguration(c1.allPeersInNewConf);
                success.set(reply.isSuccess());
            } catch (IOException ioe) {
                LOG.error("FAILED", ioe);
            }
        });
        clientThread.start();
        if (!startNewPeer) {
            // Make sure that set configuration is run inside the thread
            RaftTestUtil.waitFor(() -> clientThread.isAlive(), 300, 5000);
            ONE_SECOND.sleep();
            LOG.info("start new peer(s): {}", c1.newPeers);
            for (RaftPeer p : c1.newPeers) {
                cluster.restartServer(p.getId(), true);
            }
        }
        RaftTestUtil.waitFor(() -> success.get() != null && success.get(), 300, 15000);
        LOG.info(cluster.printServers());
        final RaftLog leaderLog = cluster.getLeader().getRaftLog();
        for (RaftPeer newPeer : c1.newPeers) {
            final RaftServer.Division d = cluster.getDivision(newPeer.getId());
            RaftTestUtil.waitFor(() -> leaderLog.getEntries(0, Long.MAX_VALUE).length == d.getRaftLog().getEntries(0, Long.MAX_VALUE).length, 300, 15000);
            Assert.assertArrayEquals(leaderLog.getEntries(0, Long.MAX_VALUE), d.getRaftLog().getEntries(0, Long.MAX_VALUE));
        }
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) PeerChanges(org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 19 with RaftLog

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

the class RaftReconfigurationBaseTest method runTestNoChangeRequest.

void runTestNoChangeRequest(CLUSTER cluster) throws Exception {
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    try (final RaftClient client = cluster.createClient(leader.getId())) {
        client.io().send(new SimpleMessage("m"));
        final RaftLog leaderLog = leader.getRaftLog();
        final long committedIndex = leaderLog.getLastCommittedIndex();
        final RaftConfiguration confBefore = cluster.getLeader().getRaftConf();
        // no real configuration change in the request
        final RaftClientReply reply = client.admin().setConfiguration(cluster.getPeers().toArray(RaftPeer.emptyArray()));
        Assert.assertTrue(reply.isSuccess());
        final long newCommittedIndex = leaderLog.getLastCommittedIndex();
        for (long i = committedIndex + 1; i <= newCommittedIndex; i++) {
            final LogEntryProto e = leaderLog.get(i);
            Assert.assertTrue(e.hasMetadataEntry());
        }
        Assert.assertSame(confBefore, cluster.getLeader().getRaftConf());
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftConfiguration(org.apache.ratis.server.RaftConfiguration) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 20 with RaftLog

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

the class RaftTestUtil method countEntries.

static EnumMap<LogEntryBodyCase, AtomicLong> countEntries(RaftLog raftLog) throws Exception {
    final EnumMap<LogEntryBodyCase, AtomicLong> counts = new EnumMap<>(LogEntryBodyCase.class);
    for (long i = 0; i < raftLog.getNextIndex(); i++) {
        final LogEntryProto e = raftLog.get(i);
        counts.computeIfAbsent(e.getLogEntryBodyCase(), c -> new AtomicLong()).incrementAndGet();
    }
    return counts;
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) BooleanSupplier(java.util.function.BooleanSupplier) ProtoUtils(org.apache.ratis.util.ProtoUtils) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) JavaUtils(org.apache.ratis.util.JavaUtils) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) EnumMap(java.util.EnumMap) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Predicate(java.util.function.Predicate) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) Objects(java.util.Objects) List(java.util.List) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase) ClientId(org.apache.ratis.protocol.ClientId) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Preconditions(org.apache.ratis.thirdparty.com.google.common.base.Preconditions) Optional(java.util.Optional) DelayLocalExecutionInjection(org.apache.ratis.server.impl.DelayLocalExecutionInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftLog(org.apache.ratis.server.raftlog.RaftLog) CollectionUtils(org.apache.ratis.util.CollectionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Message(org.apache.ratis.protocol.Message) StreamSupport(java.util.stream.StreamSupport) IntSupplier(java.util.function.IntSupplier) AssumptionViolatedException(org.junit.AssumptionViolatedException) Logger(org.slf4j.Logger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) IOException(java.io.IOException) Field(java.lang.reflect.Field) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) RaftServer(org.apache.ratis.server.RaftServer) RaftLogBase(org.apache.ratis.server.raftlog.RaftLogBase) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) AtomicLong(java.util.concurrent.atomic.AtomicLong) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) EnumMap(java.util.EnumMap) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase)

Aggregations

RaftLog (org.apache.ratis.server.raftlog.RaftLog)23 RaftClient (org.apache.ratis.client.RaftClient)12 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)12 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)12 RaftServer (org.apache.ratis.server.RaftServer)11 IOException (java.io.IOException)9 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)9 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 TimeDuration (org.apache.ratis.util.TimeDuration)6 ArrayList (java.util.ArrayList)5 TimeUnit (java.util.concurrent.TimeUnit)5 JavaUtils (org.apache.ratis.util.JavaUtils)5 Assert (org.junit.Assert)5 File (java.io.File)4 List (java.util.List)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 TermIndex (org.apache.ratis.server.protocol.TermIndex)4 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)4 Arrays (java.util.Arrays)3