Search in sources :

Example 86 with RaftPeerId

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

the class LeaderElectionTests method testLateServerStart.

@Test
public void testLateServerStart() throws Exception {
    final int numServer = 3;
    LOG.info("Running testLateServerStart");
    final MiniRaftCluster cluster = newCluster(numServer);
    cluster.initServers();
    // start all except one servers
    final Iterator<RaftServer> i = cluster.getServers().iterator();
    for (int j = 1; j < numServer; j++) {
        i.next().start();
    }
    final RaftServer.Division leader = waitForLeader(cluster);
    final TimeDuration sleepTime = TimeDuration.valueOf(3, TimeUnit.SECONDS);
    LOG.info("sleep " + sleepTime);
    sleepTime.sleep();
    // start the last server
    final RaftServerProxy lastServer = (RaftServerProxy) i.next();
    lastServer.start();
    final RaftPeerId lastServerLeaderId = JavaUtils.attemptRepeatedly(() -> Optional.ofNullable(lastServer.getImpls().iterator().next().getState().getLeaderId()).orElseThrow(() -> new IllegalStateException("No leader yet")), 10, ONE_SECOND, "getLeaderId", LOG);
    LOG.info(cluster.printServers());
    Assert.assertEquals(leader.getId(), lastServerLeaderId);
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) TimeDuration(org.apache.ratis.util.TimeDuration) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 87 with RaftPeerId

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

the class LeaderElectionTests method runTestPauseResumeLeaderElection.

void runTestPauseResumeLeaderElection(CLUSTER cluster) throws IOException, InterruptedException {
    final RaftClientReply pauseLeaderReply;
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = leader.getId();
    final List<RaftServer.Division> followers = cluster.getFollowers();
    Assert.assertTrue(followers.size() >= 1);
    final RaftServerImpl f1 = (RaftServerImpl) followers.get(0);
    try (final RaftClient client = cluster.createClient()) {
        pauseLeaderReply = client.getLeaderElectionManagementApi(f1.getId()).pause();
        Assert.assertTrue(pauseLeaderReply.isSuccess());
        client.io().send(new RaftTestUtil.SimpleMessage("message"));
        RaftServer.Division newLeader = followers.get(0);
        List<RaftPeer> peers = cluster.getPeers();
        List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, newLeader.getPeer());
        RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
        Assert.assertTrue(reply.isSuccess());
        JavaUtils.attempt(() -> Assert.assertEquals(leaderId, leader.getId()), 20, HUNDRED_MILLIS, "check leader id", LOG);
        final RaftClientReply resumeLeaderReply = client.getLeaderElectionManagementApi(f1.getId()).resume();
        Assert.assertTrue(resumeLeaderReply.isSuccess());
        JavaUtils.attempt(() -> Assert.assertEquals(f1.getId(), cluster.getLeader().getId()), 20, HUNDRED_MILLIS, "check new leader", LOG);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Example 88 with RaftPeerId

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

the class PreAppendLeaderStepDownTest method runTestLeaderStepDownAsync.

void runTestLeaderStepDownAsync(CLUSTER cluster) throws IOException, InterruptedException {
    RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    RaftPeerId leaderId = leader.getId();
    RaftServerImpl l = (RaftServerImpl) leader;
    try (RaftClient client = cluster.createClient(leader.getId())) {
        JavaUtils.attempt(() -> Assert.assertEquals(leaderId, leader.getId()), 20, ONE_SECOND, "check leader id", LOG);
        RaftClientReply reply = client.admin().transferLeadership(null, 3000);
        Assert.assertTrue(reply.isSuccess());
        Assert.assertEquals(2, ((RaftServerImpl) leader).getRole().getCurrentRole().getNumber());
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftServer(org.apache.ratis.server.RaftServer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 89 with RaftPeerId

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

the class GroupManagementBaseTest method testGroupWithPriority.

@Test
public void testGroupWithPriority() throws Exception {
    final MiniRaftCluster cluster = getCluster(0);
    LOG.info("Start testMultiGroup" + cluster.printServers());
    // Start server with null group
    final List<RaftPeerId> ids = Arrays.stream(MiniRaftCluster.generateIds(3, 0)).map(RaftPeerId::valueOf).collect(Collectors.toList());
    ids.forEach(id -> cluster.putNewServer(id, null, true));
    LOG.info("putNewServer: " + cluster.printServers());
    cluster.start();
    // Make sure that there are no leaders.
    TimeUnit.SECONDS.sleep(1);
    LOG.info("start: " + cluster.printServers());
    Assert.assertNull(cluster.getLeader());
    // Add groups
    List<RaftPeer> peers = cluster.getPeers();
    Random r = new Random(1);
    final int suggestedLeaderIndex = r.nextInt(peers.size());
    List<RaftPeer> peersWithPriority = getPeersWithPriority(peers, peers.get(suggestedLeaderIndex));
    final RaftGroup newGroup = RaftGroup.valueOf(RaftGroupId.randomId(), peersWithPriority);
    LOG.info("add new group: " + newGroup);
    try (final RaftClient client = cluster.createClient(newGroup)) {
        // Before request, client try leader with the highest priority
        Assert.assertTrue(client.getLeaderId() == peersWithPriority.get(suggestedLeaderIndex).getId());
        for (RaftPeer p : newGroup.getPeers()) {
            client.getGroupManagementApi(p.getId()).add(newGroup);
        }
    }
    JavaUtils.attempt(() -> {
        final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
        Assert.assertTrue(leader.getId() == peers.get(suggestedLeaderIndex).getId());
    }, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
    String suggestedLeader = peers.get(suggestedLeaderIndex).getId().toString();
    // isolate leader, then follower will trigger leader election.
    // Because leader was isolated, so leader can not vote, and candidate wait timeout,
    // then if candidate get majority, candidate can pass vote
    BlockRequestHandlingInjection.getInstance().blockRequestor(suggestedLeader);
    BlockRequestHandlingInjection.getInstance().blockReplier(suggestedLeader);
    cluster.setBlockRequestsFrom(suggestedLeader, true);
    JavaUtils.attempt(() -> {
        final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
        Assert.assertTrue(leader.getId() != peers.get(suggestedLeaderIndex).getId());
    }, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
    // when suggested leader rejoin cluster, it will catch up log first.
    try (final RaftClient client = cluster.createClient(newGroup)) {
        for (int i = 0; i < 10; i++) {
            RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
    }
    BlockRequestHandlingInjection.getInstance().unblockRequestor(suggestedLeader);
    BlockRequestHandlingInjection.getInstance().unblockReplier(suggestedLeader);
    cluster.setBlockRequestsFrom(suggestedLeader, false);
    // suggested leader with highest priority rejoin cluster, then current leader will yield
    // leadership to suggested leader when suggested leader catch up the log.
    JavaUtils.attempt(() -> {
        final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
        Assert.assertTrue(leader.getId() == peers.get(suggestedLeaderIndex).getId());
    }, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
    // change the suggest leader
    final int newSuggestedLeaderIndex = (suggestedLeaderIndex + 1) % peersWithPriority.size();
    List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, peers.get(newSuggestedLeaderIndex));
    try (final RaftClient client = cluster.createClient(newGroup)) {
        RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
        Assert.assertTrue(reply.isSuccess());
    }
    JavaUtils.attempt(() -> {
        final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
        Assert.assertTrue(leader.getId() == peers.get(newSuggestedLeaderIndex).getId());
    }, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
    cluster.killServer(peers.get(newSuggestedLeaderIndex).getId());
    JavaUtils.attempt(() -> {
        final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
        Assert.assertTrue(leader.getId() != peers.get(newSuggestedLeaderIndex).getId());
    }, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
    cluster.shutdown();
}
Also used : RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeer(org.apache.ratis.protocol.RaftPeer) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 90 with RaftPeerId

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

the class GroupManagementBaseTest method testGroupAlreadyExists.

@Test
public void testGroupAlreadyExists() throws Exception {
    final MiniRaftCluster cluster = getCluster(1);
    cluster.start();
    final RaftPeer peer = cluster.getPeers().get(0);
    final RaftPeerId peerId = peer.getId();
    final RaftGroup group = RaftGroup.valueOf(cluster.getGroupId(), peer);
    try (final RaftClient client = cluster.createClient()) {
        Assert.assertEquals(group, cluster.getDivision(peerId).getGroup());
        try {
            client.getGroupManagementApi(peer.getId()).add(group);
        } catch (IOException ex) {
            // HadoopRPC throws RemoteException, which makes it hard to check if
            // the exception is instance of AlreadyExistsException
            Assert.assertTrue(ex.toString().contains(AlreadyExistsException.class.getCanonicalName()));
        }
        Assert.assertEquals(group, cluster.getDivision(peerId).getGroup());
        cluster.shutdown();
    }
}
Also used : AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroup(org.apache.ratis.protocol.RaftGroup) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

RaftPeerId (org.apache.ratis.protocol.RaftPeerId)101 RaftClient (org.apache.ratis.client.RaftClient)58 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)45 IOException (java.io.IOException)36 Test (org.junit.Test)32 RaftPeer (org.apache.ratis.protocol.RaftPeer)31 RaftServer (org.apache.ratis.server.RaftServer)31 BaseTest (org.apache.ratis.BaseTest)21 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)20 CompletableFuture (java.util.concurrent.CompletableFuture)18 RaftProperties (org.apache.ratis.conf.RaftProperties)18 File (java.io.File)17 ArrayList (java.util.ArrayList)15 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)15 RaftGroup (org.apache.ratis.protocol.RaftGroup)14 TimeDuration (org.apache.ratis.util.TimeDuration)14 List (java.util.List)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Collectors (java.util.stream.Collectors)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)11