Search in sources :

Example 11 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.

the class RaftSnapshotBaseTest method assertLeaderContent.

static void assertLeaderContent(MiniRaftCluster cluster) throws InterruptedException {
    final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster);
    Assert.assertEquals(SNAPSHOT_TRIGGER_THRESHOLD * 2, leader.getState().getLog().getLastCommittedIndex());
    final LogEntryProto[] entries = SimpleStateMachine4Testing.get(leader.getProxy()).getContent();
    for (int i = 1; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
        Assert.assertEquals(i + 1, entries[i].getIndex());
        Assert.assertArrayEquals(new SimpleMessage("m" + i).getContent().toByteArray(), entries[i].getSmLogEntry().getData().toByteArray());
    }
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage)

Example 12 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.

the class TestStateMachine method testTransactionContextIsPassedBack.

@Test
public void testTransactionContextIsPassedBack() throws Throwable {
    // tests that the TrxContext set by the StateMachine in Leader is passed back to the SM
    properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SMTransactionContext.class, StateMachine.class);
    startCluster();
    int numTrx = 100;
    final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numTrx);
    try (final RaftClient client = cluster.createClient()) {
        for (RaftTestUtil.SimpleMessage message : messages) {
            client.send(message);
        }
    }
    // TODO: there eshould be a better way to ensure all data is replicated and applied
    Thread.sleep(cluster.getMaxTimeout() + 100);
    for (RaftServerProxy raftServer : cluster.getServers()) {
        final SMTransactionContext sm = SMTransactionContext.get(raftServer);
        sm.rethrowIfException();
        assertEquals(numTrx, sm.numApplied.get());
    }
    // check leader
    RaftServerImpl raftServer = cluster.getLeader();
    // assert every transaction has obtained context in leader
    final SMTransactionContext sm = SMTransactionContext.get(raftServer.getProxy());
    List<Long> ll = sm.applied.stream().collect(Collectors.toList());
    Collections.sort(ll);
    assertEquals(ll.toString(), ll.size(), numTrx);
    for (int i = 0; i < numTrx; i++) {
        assertEquals(ll.toString(), Long.valueOf(i + 1), ll.get(i));
    }
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) RaftTestUtil(org.apache.ratis.RaftTestUtil) AtomicLong(java.util.concurrent.atomic.AtomicLong) RaftServerProxy(org.apache.ratis.server.impl.RaftServerProxy) RaftClient(org.apache.ratis.client.RaftClient) BaseTest(org.apache.ratis.BaseTest)

Example 13 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl 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)

Example 14 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.

the class RaftBasicTests method runTestBasicAppendEntries.

static void runTestBasicAppendEntries(boolean async, int numMessages, MiniRaftCluster cluster, Logger LOG) throws Exception {
    LOG.info("runTestBasicAppendEntries: async? " + async + ", numMessages=" + numMessages);
    RaftServerImpl leader = waitForLeader(cluster);
    final long term = leader.getState().getCurrentTerm();
    final RaftPeerId killed = cluster.getFollowers().get(0).getId();
    cluster.killServer(killed);
    LOG.info(cluster.printServers());
    final SimpleMessage[] messages = SimpleMessage.create(numMessages);
    try (final RaftClient client = cluster.createClient()) {
        final AtomicInteger asyncReplyCount = new AtomicInteger();
        final CompletableFuture<Void> f = new CompletableFuture<>();
        for (SimpleMessage message : messages) {
            if (async) {
                client.sendAsync(message).thenAcceptAsync(reply -> {
                    if (!reply.isSuccess()) {
                        f.completeExceptionally(new AssertionError("Failed with reply " + reply));
                    } else if (asyncReplyCount.incrementAndGet() == messages.length) {
                        f.complete(null);
                    }
                });
            } else {
                client.send(message);
            }
        }
        if (async) {
            f.join();
            Assert.assertEquals(messages.length, asyncReplyCount.get());
        }
    }
    Thread.sleep(cluster.getMaxTimeout() + 100);
    LOG.info(cluster.printAllLogs());
    cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.assertLogEntries(log, async, term, messages));
}
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) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 15 with RaftServerImpl

use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.

the class RaftTestUtil method waitForLeader.

static RaftServerImpl waitForLeader(MiniRaftCluster cluster, final String leaderId) throws InterruptedException {
    LOG.info(cluster.printServers());
    for (int i = 0; !cluster.tryEnforceLeader(leaderId) && i < 10; i++) {
        RaftServerImpl currLeader = cluster.getLeader();
        LOG.info("try enforcing leader to " + leaderId + " but " + (currLeader == null ? "no leader for this round" : "new leader is " + currLeader.getId()));
    }
    LOG.info(cluster.printServers());
    final RaftServerImpl leader = cluster.getLeader();
    Assert.assertEquals(leaderId, leader.getId().toString());
    return leader;
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl)

Aggregations

RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)20 Test (org.junit.Test)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 BaseTest (org.apache.ratis.BaseTest)7 RaftClient (org.apache.ratis.client.RaftClient)7 RaftLog (org.apache.ratis.server.storage.RaftLog)7 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)7 IOException (java.io.IOException)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 RaftTestUtil (org.apache.ratis.RaftTestUtil)5 RaftProperties (org.apache.ratis.conf.RaftProperties)5 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)5 List (java.util.List)4 Timer (java.util.Timer)4 TimerTask (java.util.TimerTask)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeUnit (java.util.concurrent.TimeUnit)4