use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestRaftWithGrpc method runTestUpdateViaHeartbeat.
void runTestUpdateViaHeartbeat(MiniRaftClusterWithGrpc cluster) throws Exception {
waitForLeader(cluster);
try (final RaftClient client = cluster.createClient()) {
// block append requests
cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::blockWriteStateMachineData);
CompletableFuture<RaftClientReply> replyFuture = client.async().send(new RaftTestUtil.SimpleMessage("abc"));
TimeDuration.valueOf(5, TimeUnit.SECONDS).sleep();
// replyFuture should not be completed until append request is unblocked.
Assert.assertFalse(replyFuture.isDone());
// unblock append request.
cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::unblockWriteStateMachineData);
final RaftLog leaderLog = cluster.getLeader().getRaftLog();
// The entries have been appended in the followers
// although the append entry timed out at the leader
cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).forEach(raftServer -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
final long leaderNextIndex = leaderLog.getNextIndex();
final LogEntryHeader[] leaderEntries = leaderLog.getEntries(0, Long.MAX_VALUE);
final RaftLog followerLog = raftServer.getRaftLog();
Assert.assertEquals(leaderNextIndex, followerLog.getNextIndex());
final LogEntryHeader[] serverEntries = followerLog.getEntries(0, Long.MAX_VALUE);
Assert.assertArrayEquals(serverEntries, leaderEntries);
}, 10, HUNDRED_MILLIS, "assertRaftLog-" + raftServer.getId(), LOG)));
// Wait for heartbeats from leader to be received by followers
Thread.sleep(500);
RaftServerTestUtil.getLogAppenders(cluster.getLeader()).forEach(logAppender -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
final long leaderNextIndex = leaderLog.getNextIndex();
// FollowerInfo in the leader state should have updated next and match index.
final long followerMatchIndex = logAppender.getFollower().getMatchIndex();
Assert.assertTrue(followerMatchIndex >= leaderNextIndex - 1);
Assert.assertEquals(followerMatchIndex + 1, logAppender.getFollower().getNextIndex());
}, 10, HUNDRED_MILLIS, "assertRaftLog-" + logAppender.getFollower(), LOG)));
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class AbstractRatisCommand method run.
@Override
public int run(CommandLine cl) throws IOException {
List<InetSocketAddress> addresses = new ArrayList<>();
String peersStr = cl.getOptionValue(PEER_OPTION_NAME);
String[] peersArray = peersStr.split(",");
for (String peer : peersArray) {
addresses.add(parseInetSocketAddress(peer));
}
final RaftGroupId raftGroupIdFromConfig = cl.hasOption(GROUPID_OPTION_NAME) ? RaftGroupId.valueOf(UUID.fromString(cl.getOptionValue(GROUPID_OPTION_NAME))) : DEFAULT_RAFT_GROUP_ID;
List<RaftPeer> peers = addresses.stream().map(addr -> RaftPeer.newBuilder().setId(RaftUtils.getPeerId(addr)).setAddress(addr).build()).collect(Collectors.toList());
raftGroup = RaftGroup.valueOf(raftGroupIdFromConfig, peers);
try (final RaftClient client = RaftUtils.createClient(raftGroup)) {
final RaftGroupId remoteGroupId;
if (raftGroupIdFromConfig != DEFAULT_RAFT_GROUP_ID) {
remoteGroupId = raftGroupIdFromConfig;
} else {
final List<RaftGroupId> groupIds = run(peers, p -> client.getGroupManagementApi((p.getId())).list().getGroupIds());
if (groupIds == null) {
println("Failed to get group ID from " + peers);
return -1;
} else if (groupIds.size() == 1) {
remoteGroupId = groupIds.get(0);
} else {
println("There are more than one groups, you should specific one. " + groupIds);
return -2;
}
}
groupInfoReply = run(peers, p -> client.getGroupManagementApi((p.getId())).info(remoteGroupId));
processReply(groupInfoReply, () -> "Failed to get group info for group id " + remoteGroupId.getUuid() + " from " + peers);
raftGroup = groupInfoReply.getGroup();
}
return 0;
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class PauseCommand 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("Peer not found: %s", strAddr);
return -1;
}
try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
RaftClientReply reply = raftClient.getLeaderElectionManagementApi(peerId).pause();
processReply(reply, () -> String.format("Failed to pause leader election on peer %s", strAddr));
printf(String.format("Successful pause leader election on peer %s", strAddr));
}
return 0;
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class StepDownCommand method run.
@Override
public int run(CommandLine cl) throws IOException {
super.run(cl);
try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
final RaftClientReply transferLeadershipReply = client.admin().transferLeadership(null, 60_000);
processReply(transferLeadershipReply, () -> "Failed to step down leader");
} catch (Throwable t) {
printf("caught an error when executing step down leader: %s%n", t.getMessage());
return -1;
}
println("Step down leader successfully");
return 0;
}
use of org.apache.ratis.client.RaftClient 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;
}
Aggregations