Search in sources :

Example 81 with RaftPeerId

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

the class TransferCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    String strAddr = cl.getOptionValue(ADDRESS_OPTION_NAME);
    RaftPeerId newLeaderId = null;
    // update priorities to enable transfer
    List<RaftPeer> peersWithNewPriorities = new ArrayList<>();
    for (RaftPeer peer : getRaftGroup().getPeers()) {
        peersWithNewPriorities.add(RaftPeer.newBuilder(peer).setPriority(peer.getAddress().equals(strAddr) ? 2 : 1).build());
        if (peer.getAddress().equals(strAddr)) {
            newLeaderId = peer.getId();
        }
    }
    if (newLeaderId == null) {
        return -2;
    }
    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
        String stringPeers = "[" + peersWithNewPriorities.stream().map(RaftPeer::toString).collect(Collectors.joining(", ")) + "]";
        printf("Applying new peer state before transferring leadership: %n%s%n", stringPeers);
        RaftClientReply setConfigurationReply = client.admin().setConfiguration(peersWithNewPriorities);
        processReply(setConfigurationReply, () -> "failed to set priorities before initiating election");
        // transfer leadership
        printf("Transferring leadership to server with address <%s> %n", strAddr);
        try {
            Thread.sleep(3_000);
            RaftClientReply transferLeadershipReply = client.admin().transferLeadership(newLeaderId, 60_000);
            processReply(transferLeadershipReply, () -> "election failed");
        } catch (Throwable t) {
            printf("caught an error when executing transfer: %s%n", t.getMessage());
            return -1;
        }
        println("Transferring leadership initiated");
    }
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ArrayList(java.util.ArrayList) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Example 82 with RaftPeerId

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

the class GroupListCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    final RaftPeerId peerId;
    final String address;
    if (cl.hasOption(PEER_ID_OPTION_NAME)) {
        peerId = RaftPeerId.getRaftPeerId(cl.getOptionValue(PEER_ID_OPTION_NAME));
        address = getRaftGroup().getPeer(peerId).getAddress();
    } else if (cl.hasOption(SERVER_ADDRESS_OPTION_NAME)) {
        address = cl.getOptionValue(SERVER_ADDRESS_OPTION_NAME);
        final InetSocketAddress serverAddress = parseInetSocketAddress(address);
        peerId = RaftUtils.getPeerId(serverAddress);
    } else {
        throw new IllegalArgumentException("Both " + PEER_ID_OPTION_NAME + " and " + SERVER_ADDRESS_OPTION_NAME + " options are missing.");
    }
    try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
        GroupListReply reply = raftClient.getGroupManagementApi(peerId).list();
        processReply(reply, () -> String.format("Failed to get group information of peerId %s (server %s)", peerId, address));
        printf(String.format("The peerId %s (server %s) is in %d groups, and the groupIds is: %s", peerId, address, reply.getGroupIds().size(), reply.getGroupIds()));
    }
    return 0;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) GroupListReply(org.apache.ratis.protocol.GroupListReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 83 with RaftPeerId

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

the class ServerPauseResumeTest method runTestPauseResume.

void runTestPauseResume(CLUSTER cluster) throws InterruptedException, IOException {
    // wait leader be elected.
    final RaftServer.Division leader = waitForLeader(cluster);
    RaftPeerId leaderId = leader.getId();
    final List<RaftServer.Division> followers = cluster.getFollowers();
    Assert.assertTrue(followers.size() >= 1);
    final RaftServerImpl follower = (RaftServerImpl) followers.get(0);
    SimpleMessage[] batch1 = SimpleMessage.create(100, "batch1");
    Thread writeThread = RaftTestUtil.sendMessageInNewThread(cluster, leaderId, batch1);
    writeThread.join();
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    final RaftLog leaderLog = leader.getRaftLog();
    // leader should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(leaderLog, batch1));
    RaftLog followerLog = follower.getRaftLog();
    // follower should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, batch1));
    // pause follower.
    boolean isSuccess = follower.pause();
    Assert.assertTrue(isSuccess);
    Assert.assertTrue(follower.getInfo().getLifeCycleState().isPausingOrPaused());
    SimpleMessage[] batch2 = SimpleMessage.create(100, "batch2");
    Thread writeThread2 = RaftTestUtil.sendMessageInNewThread(cluster, leaderId, batch2);
    writeThread2.join();
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    // paused follower should not have any batch2 message in its raftlog.
    Assert.assertTrue(RaftTestUtil.logEntriesNotContains(followerLog, batch2));
    // resume follower.
    isSuccess = follower.resume();
    Assert.assertTrue(isSuccess);
    Assert.assertFalse(follower.getInfo().getLifeCycleState().isPausingOrPaused());
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
    // follower should contain all logs.
    Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, batch2));
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 84 with RaftPeerId

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

the class ResumeCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    String strAddr = cl.getOptionValue(ADDRESS_OPTION_NAME);
    final RaftPeerId peerId = getRaftGroup().getPeers().stream().filter(p -> p.getAddress().equals(strAddr)).findAny().map(RaftPeer::getId).orElse(null);
    if (peerId == null) {
        printf("Can't find a sever with the address:%s", strAddr);
        return -1;
    }
    try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
        RaftClientReply reply = raftClient.getLeaderElectionManagementApi(peerId).resume();
        processReply(reply, () -> String.format("Failed to resume leader election on peer %s", strAddr));
        printf(String.format("Successful pause leader election on peer %s", strAddr));
    }
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) AbstractRatisCommand(org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) RaftUtils(org.apache.ratis.shell.cli.RaftUtils) Context(org.apache.ratis.shell.cli.sh.command.Context) IOException(java.io.IOException) RaftClient(org.apache.ratis.client.RaftClient) Option(org.apache.commons.cli.Option) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 85 with RaftPeerId

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

the class RaftSnapshotBaseTest method testBasicInstallSnapshot.

/**
 * Basic test for install snapshot: start a one node cluster and let it
 * generate a snapshot. Then delete the log and restart the node, and add more
 * nodes as followers.
 */
@Test
public void testBasicInstallSnapshot() throws Exception {
    final List<LogSegmentPath> logs;
    int i = 0;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
                RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // wait for the snapshot to be done
        final long nextIndex = cluster.getLeader().getRaftLog().getNextIndex();
        LOG.info("nextIndex = {}", nextIndex);
        final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
            return null;
        }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
        verifyTakeSnapshotMetric(cluster.getLeader());
        logs = LogSegmentPath.getLogSegmentPaths(cluster.getLeader().getRaftStorage());
    } finally {
        cluster.shutdown();
    }
    // delete the log segments from the leader
    for (LogSegmentPath path : logs) {
        FileUtils.delete(path.getPath());
    }
    // restart the peer
    LOG.info("Restarting the cluster");
    cluster.restart(false);
    try {
        assertLeaderContent(cluster);
        // generate some more traffic
        try (final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
            Assert.assertTrue(client.io().send(new SimpleMessage("m" + i)).isSuccess());
        }
        // add two more peers
        String[] newPeers = new String[] { "s3", "s4" };
        MiniRaftCluster.PeerChanges change = cluster.addNewPeers(newPeers, true, false);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        for (String newPeer : newPeers) {
            final RaftServer.Division s = cluster.getDivision(RaftPeerId.valueOf(newPeer));
            SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(s);
            Assert.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
        }
        // Verify installSnapshot counter on leader before restart.
        verifyInstallSnapshotMetric(cluster.getLeader());
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
        Timer timer = getTakeSnapshotTimer(cluster.getLeader());
        long count = timer.getCount();
        // restart the peer and check if it can correctly handle conf change
        cluster.restartServer(cluster.getLeader().getId(), false);
        assertLeaderContent(cluster);
        // verify that snapshot was taken when stopping the server
        Assert.assertTrue(count < timer.getCount());
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) LogSegmentPath(org.apache.ratis.server.raftlog.segmented.LogSegmentPath) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Timer(com.codahale.metrics.Timer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) 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