Search in sources :

Example 76 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply 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 77 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply 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 78 with RaftClientReply

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

the class RaftReconfigurationBaseTest method testLeaderNotReadyException.

/**
 * Delay the commit of the leader placeholder log entry and see if the client
 * can correctly receive and handle the LeaderNotReadyException.
 */
@Test
public void testLeaderNotReadyException() throws Exception {
    LOG.info("Start testLeaderNotReadyException");
    final MiniRaftCluster cluster = newCluster(1).initServers();
    try {
        // delay 1s for each logSync call
        cluster.getServers().forEach(peer -> leaderPlaceHolderDelay.setDelayMs(peer.getId().toString(), 2000));
        cluster.start();
        AtomicBoolean caughtNotReady = new AtomicBoolean(false);
        AtomicBoolean success = new AtomicBoolean(false);
        final RaftPeerId leaderId = cluster.getPeers().iterator().next().getId();
        new Thread(() -> {
            try (final RaftClient client = cluster.createClient(leaderId)) {
                final RaftClientRpc sender = client.getClientRpc();
                final RaftClientRequest request = cluster.newRaftClientRequest(client.getId(), leaderId, new SimpleMessage("test"));
                while (!success.get()) {
                    try {
                        final RaftClientReply reply = sender.sendRequest(request);
                        success.set(reply.isSuccess());
                        if (reply.getException() != null && reply.getException() instanceof LeaderNotReadyException) {
                            caughtNotReady.set(true);
                        }
                    } catch (IOException e) {
                        LOG.info("Hit other IOException", e);
                    }
                    if (!success.get()) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException ignored) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            } catch (Exception ignored) {
                LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
            }
        }).start();
        RaftTestUtil.waitForLeader(cluster);
        for (int i = 0; !success.get() && i < 5; i++) {
            Thread.sleep(1000);
        }
        Assert.assertTrue(success.get());
        Assert.assertTrue(caughtNotReady.get());
    } finally {
        leaderPlaceHolderDelay.clear();
        cluster.shutdown();
    }
}
Also used : SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) IOException(java.io.IOException) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) TimeoutException(java.util.concurrent.TimeoutException) ReconfigurationInProgressException(org.apache.ratis.protocol.exceptions.ReconfigurationInProgressException) ReconfigurationTimeoutException(org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 79 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply 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 80 with RaftClientReply

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

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)96 RaftClient (org.apache.ratis.client.RaftClient)71 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)43 RaftServer (org.apache.ratis.server.RaftServer)40 IOException (java.io.IOException)32 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)25 CompletableFuture (java.util.concurrent.CompletableFuture)22 RaftPeer (org.apache.ratis.protocol.RaftPeer)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 TimeDuration (org.apache.ratis.util.TimeDuration)18 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)14 RaftTestUtil (org.apache.ratis.RaftTestUtil)13 RaftProperties (org.apache.ratis.conf.RaftProperties)13 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 List (java.util.List)12 RetryPolicy (org.apache.ratis.retry.RetryPolicy)12 CompletionException (java.util.concurrent.CompletionException)11 ExecutionException (java.util.concurrent.ExecutionException)11