Search in sources :

Example 1 with RaftServerImpl

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

the class MiniRaftCluster method setConfiguration.

public void setConfiguration(RaftPeer... peers) throws IOException {
    final RaftServerImpl leader = getLeader();
    final SetConfigurationRequest r = newSetConfigurationRequest(ClientId.randomId(), leader.getId(), peers);
    LOG.info("Start changing the configuration: {}", r);
    leader.setConfiguration(r);
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl)

Example 2 with RaftServerImpl

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

the class RaftBasicTests method testOldLeaderNotCommit.

@Test
public void testOldLeaderNotCommit() throws Exception {
    LOG.info("Running testOldLeaderNotCommit");
    final MiniRaftCluster cluster = getCluster();
    final RaftPeerId leaderId = waitForLeader(cluster).getId();
    List<RaftServerImpl> followers = cluster.getFollowers();
    final RaftServerImpl followerToCommit = 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);
    sendMessageInNewThread(cluster, messages);
    Thread.sleep(cluster.getMaxTimeout() + 100);
    logEntriesContains(followerToCommit.getState().getLog(), messages);
    cluster.killServer(leaderId);
    cluster.killServer(followerToCommit.getId());
    for (int i = 1; i < NUM_SERVERS - 1; i++) {
        RaftServerImpl follower = followers.get(i);
        cluster.restartServer(follower.getId(), false);
    }
    waitForLeader(cluster);
    Thread.sleep(cluster.getMaxTimeout() + 100);
    final Predicate<LogEntryProto> predicate = l -> l.getTerm() != 1;
    cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.checkLogEntries(log, messages, predicate));
}
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) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Test(org.junit.Test)

Example 3 with RaftServerImpl

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

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

the class RaftBasicTests method testWithLoad.

public static void testWithLoad(final int numClients, final int numMessages, boolean useAsync, MiniRaftCluster cluster, Logger LOG) throws Exception {
    LOG.info("Running testWithLoad: numClients=" + numClients + ", numMessages=" + numMessages + ", async=" + useAsync);
    waitForLeader(cluster);
    final List<Client4TestWithLoad> clients = Stream.iterate(0, i -> i + 1).limit(numClients).map(i -> new Client4TestWithLoad(i, numMessages, useAsync, cluster, LOG)).collect(Collectors.toList());
    final AtomicInteger lastStep = new AtomicInteger();
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        private int previousLastStep = lastStep.get();

        @Override
        public void run() {
            LOG.info(cluster.printServers());
            LOG.info(BlockRequestHandlingInjection.getInstance().toString());
            LOG.info(cluster.toString());
            clients.forEach(c -> LOG.info("  " + c));
            JavaUtils.dumpAllThreads(s -> LOG.info(s));
            final int last = lastStep.get();
            if (last != previousLastStep) {
                previousLastStep = last;
            } else {
                final RaftServerImpl leader = cluster.getLeader();
                LOG.info("NO PROGRESS at " + last + ", try to restart leader=" + leader);
                if (leader != null) {
                    try {
                        cluster.restartServer(leader.getId(), false);
                        LOG.info("Restarted leader=" + leader);
                    } catch (IOException e) {
                        LOG.error("Failed to restart leader=" + leader);
                    }
                }
            }
        }
    }, 5_000L, 10_000L);
    clients.forEach(Thread::start);
    int count = 0;
    for (; ; ) {
        if (clients.stream().filter(Client4TestWithLoad::isRunning).count() == 0) {
            break;
        }
        final int n = clients.stream().mapToInt(c -> c.step.get()).sum();
        assertTrue(n >= lastStep.get());
        if (n - lastStep.get() < 50 * numClients) {
            // Change leader at least 50 steps.
            Thread.sleep(10);
            continue;
        }
        lastStep.set(n);
        count++;
        try {
            RaftServerImpl leader = cluster.getLeader();
            if (leader != null) {
                RaftTestUtil.changeLeader(cluster, leader.getId());
            }
        } catch (IllegalStateException e) {
            LOG.error("Failed to change leader ", e);
        }
    }
    LOG.info("Leader change count=" + count);
    timer.cancel();
    for (Client4TestWithLoad c : clients) {
        if (c.exceptionInClientThread.get() != null) {
            throw new AssertionError(c.exceptionInClientThread.get());
        }
        RaftTestUtil.assertLogEntries(cluster.getServers(), c.messages);
    }
}
Also used : 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) IOException(java.io.IOException) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) Timer(java.util.Timer) TimerTask(java.util.TimerTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 5 with RaftServerImpl

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

the class RaftTestUtil method waitAndKillLeader.

static String waitAndKillLeader(MiniRaftCluster cluster, boolean expectLeader) throws InterruptedException {
    final RaftServerImpl leader = waitForLeader(cluster);
    if (!expectLeader) {
        Assert.assertNull(leader);
    } else {
        Assert.assertNotNull(leader);
        LOG.info("killing leader = " + leader);
        cluster.killServer(leader.getId());
    }
    return leader != null ? leader.getId().toString() : null;
}
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