Search in sources :

Example 1 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class PeerConfiguration method hasMajority.

boolean hasMajority(Collection<RaftPeerId> others, RaftPeerId selfId) {
    Preconditions.assertTrue(!others.contains(selfId));
    int num = 0;
    if (contains(selfId)) {
        num++;
    }
    for (RaftPeerId other : others) {
        if (contains(other)) {
            num++;
        }
        if (num > size() / 2) {
            return true;
        }
    }
    return false;
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 2 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId 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 RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId 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 RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class RaftTestUtil method sendMessageInNewThread.

static void sendMessageInNewThread(MiniRaftCluster cluster, SimpleMessage... messages) {
    RaftPeerId leaderId = cluster.getLeader().getId();
    new Thread(() -> {
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (SimpleMessage mssg : messages) {
                client.send(mssg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }).start();
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) IOException(java.io.IOException)

Example 5 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class RetryCacheTests method testRetryOnNewLeader.

/**
 * Test retry while the leader changes to another peer
 */
@Test
public void testRetryOnNewLeader() throws Exception {
    final MiniRaftCluster cluster = getCluster();
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId();
    final RaftClient client = cluster.createClient(leaderId);
    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);
    Assert.assertEquals(callId, reply.getCallId());
    Assert.assertTrue(reply.isSuccess());
    long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex();
    // trigger the reconfiguration, make sure the original leader is kicked out
    PeerChanges change = cluster.addNewPeers(2, true);
    RaftPeer[] allPeers = cluster.removePeers(2, true, asList(change.newPeers)).allPeersInNewConf;
    // trigger setConfiguration
    cluster.setConfiguration(allPeers);
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId newLeaderId = cluster.getLeader().getId();
    Assert.assertNotEquals(leaderId, newLeaderId);
    // same clientId and callId in the request
    r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, seqNum, new SimpleMessage("message"));
    for (int i = 0; i < 10; i++) {
        try {
            reply = rpc.sendRequest(r);
            LOG.info("successfully sent out the retry request_" + i);
            Assert.assertEquals(client.getId(), reply.getClientId());
            Assert.assertEquals(callId, reply.getCallId());
            Assert.assertTrue(reply.isSuccess());
        } catch (Exception e) {
            LOG.info("hit exception while retrying the same request: " + e);
        }
        Thread.sleep(100);
    }
    // check the new leader and make sure the retry did not get committed
    Assert.assertEquals(oldLastApplied + 3, cluster.getLeader().getState().getLastAppliedIndex());
    client.close();
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeer(org.apache.ratis.protocol.RaftPeer) IOException(java.io.IOException) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) PeerChanges(org.apache.ratis.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test)

Aggregations

RaftPeerId (org.apache.ratis.protocol.RaftPeerId)29 Test (org.junit.Test)16 RaftClient (org.apache.ratis.client.RaftClient)15 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)14 IOException (java.io.IOException)13 RaftProperties (org.apache.ratis.conf.RaftProperties)9 Collectors (java.util.stream.Collectors)8 RaftPeer (org.apache.ratis.protocol.RaftPeer)8 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 BaseTest (org.apache.ratis.BaseTest)7 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)6 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)6 Assert (org.junit.Assert)6 Logger (org.slf4j.Logger)6 TimeUnit (java.util.concurrent.TimeUnit)5 Level (org.apache.log4j.Level)5 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)5 LogUtils (org.apache.ratis.util.LogUtils)5 TimeDuration (org.apache.ratis.util.TimeDuration)5