in ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/AddCommand.java [62:100]
public int run(CommandLine cl) throws IOException {
super.run(cl);
final Map<RaftPeerId, InetSocketAddress> peersInfo = new HashMap<>();
List<RaftPeerId> ids;
if (cl.hasOption(ADDRESS_OPTION_NAME) && cl.hasOption(PEER_ID_OPTION_NAME)) {
ids = Arrays.stream(cl.getOptionValue(PEER_ID_OPTION_NAME).split(","))
.map(RaftPeerId::getRaftPeerId).collect(Collectors.toList());
final List<InetSocketAddress> addresses = Arrays.stream(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","))
.map(CliUtils::parseInetSocketAddress)
.collect(Collectors.toList());
Preconditions.assertSame(ids.size(), addresses.size(), "size");
for (int i = 0; i < ids.size(); i++) {
peersInfo.put(ids.get(i), addresses.get(i));
}
} else if (cl.hasOption(ADDRESS_OPTION_NAME)) {
ids = getIds(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","), peersInfo::put);
} else {
throw new IllegalArgumentException(
"Both " + PEER_ID_OPTION_NAME + " and " + ADDRESS_OPTION_NAME + " options are missing.");
}
try (RaftClient client = newRaftClient()) {
final Stream<RaftPeer> remaining = getPeerStream(RaftPeerRole.FOLLOWER);
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());
final List<RaftPeer> listeners = getPeerStream(RaftPeerRole.LISTENER)
.collect(Collectors.toList());
System.out.println("New peer list: " + peers);
System.out.println("New listener list: " + listeners);
RaftClientReply reply = client.admin().setConfiguration(peers, listeners);
processReply(reply, () -> "Failed to change raft peer");
}
return 0;
}