use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class TakeSnapshotCommand method run.
@Override
public int run(CommandLine cl) throws IOException {
super.run(cl);
long timeout;
final RaftPeerId peerId;
if (cl.hasOption(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME)) {
timeout = Long.parseLong(cl.getOptionValue(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME));
} else {
timeout = 3000;
}
try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
if (cl.hasOption(PEER_ID_OPTION_NAME)) {
peerId = RaftPeerId.getRaftPeerId(cl.getOptionValue(PEER_ID_OPTION_NAME));
} else {
peerId = null;
}
RaftClientReply reply = raftClient.getSnapshotManagementApi(peerId).create(timeout);
processReply(reply, () -> String.format("Failed to take snapshot of peerId %s", peerId));
printf(String.format("Successful take snapshot on peerId %s, the latest snapshot index is %d", peerId, reply.getLogIndex()));
}
return 0;
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class AddCommand method run.
@Override
public int run(CommandLine cl) throws IOException {
super.run(cl);
final Map<RaftPeerId, InetSocketAddress> peersInfo = new HashMap<>();
final List<RaftPeerId> ids = getIds(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","), peersInfo::put);
try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
final Stream<RaftPeer> remaining = getRaftGroup().getPeers().stream();
final Stream<RaftPeer> adding = ids.stream().map(raftPeerId -> RaftPeer.newBuilder().setId(raftPeerId).setAddress(peersInfo.get(raftPeerId)).setPriority(0).build());
final List<RaftPeer> peers = Stream.concat(remaining, adding).collect(Collectors.toList());
System.out.println("New peer list: " + peers);
RaftClientReply reply = client.admin().setConfiguration(peers);
processReply(reply, () -> "Failed to change raft peer");
}
return 0;
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class TestRaftServerWithGrpc method runTestServerRestartOnException.
void runTestServerRestartOnException(MiniRaftClusterWithGrpc cluster) throws Exception {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = leader.getId();
final RaftProperties p = getProperties();
GrpcConfigKeys.Server.setPort(p, RaftServerTestUtil.getServerRpc(leader).getInetSocketAddress().getPort());
// Create a raft server proxy with server rpc bound to a different address
// compared to leader. This helps in locking the raft storage directory to
// be used by next raft server proxy instance.
final StateMachine stateMachine = cluster.getLeader().getStateMachine();
RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId)));
newRaftServer(cluster, leaderId, stateMachine, p);
// Close the server rpc for leader so that new raft server can be bound to it.
RaftServerTestUtil.getServerRpc(cluster.getLeader()).close();
// Create a raft server proxy with server rpc bound to same address as
// the leader. This step would fail as the raft storage has been locked by
// the raft server proxy created earlier. Raft server proxy should close
// the rpc server on failure.
RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId)));
testFailureCase("start a new server with the same address", () -> newRaftServer(cluster, leaderId, stateMachine, p).start(), IOException.class, OverlappingFileLockException.class);
// Try to start a raft server rpc at the leader address.
cluster.getServerFactory(leaderId).newRaftServerRpc(cluster.getServer(leaderId));
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class TestRaftServerWithGrpc method runTestUnsupportedMethods.
void runTestUnsupportedMethods(MiniRaftClusterWithGrpc cluster) throws Exception {
final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
final RaftServerRpc rpc = cluster.getServerFactory(leaderId).newRaftServerRpc(cluster.getServer(leaderId));
testFailureCase("appendEntries", () -> rpc.appendEntries(null), UnsupportedOperationException.class);
testFailureCase("installSnapshot", () -> rpc.installSnapshot(null), UnsupportedOperationException.class);
}
use of org.apache.ratis.protocol.RaftPeerId 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;
}
Aggregations