Skip to content

Commit d7d6a2c

Browse files
authored
RATIS-2323. Extend ratis-shell add command (#1282)
1 parent 6d471e6 commit d7d6a2c

File tree

1 file changed

+64
-8
lines changed
  • ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer

1 file changed

+64
-8
lines changed

ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/AddCommand.java

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public class AddCommand extends AbstractRatisCommand {
4646

4747
public static final String ADDRESS_OPTION_NAME = "address";
4848
public static final String PEER_ID_OPTION_NAME = "peerId";
49+
public static final String CLIENT_ADDRESS_OPTION_NAME = "clientAddress";
50+
public static final String ADMIN_ADDRESS_OPTION_NAME = "adminAddress";
51+
4952
/**
5053
* @param context command context
5154
*/
@@ -62,6 +65,8 @@ public String getCommandName() {
6265
public int run(CommandLine cl) throws IOException {
6366
super.run(cl);
6467
final Map<RaftPeerId, InetSocketAddress> peersInfo = new HashMap<>();
68+
final Map<RaftPeerId, InetSocketAddress> clientAddressInfo = new HashMap<>();
69+
final Map<RaftPeerId, InetSocketAddress> adminAddressInfo = new HashMap<>();
6570
List<RaftPeerId> ids;
6671

6772
if (cl.hasOption(ADDRESS_OPTION_NAME) && cl.hasOption(PEER_ID_OPTION_NAME)) {
@@ -74,20 +79,57 @@ public int run(CommandLine cl) throws IOException {
7479
for (int i = 0; i < ids.size(); i++) {
7580
peersInfo.put(ids.get(i), addresses.get(i));
7681
}
82+
83+
if (cl.hasOption(CLIENT_ADDRESS_OPTION_NAME)) {
84+
final List<InetSocketAddress> clientAddresses =
85+
Arrays.stream(cl.getOptionValue(CLIENT_ADDRESS_OPTION_NAME).split(","))
86+
.map(CliUtils::parseInetSocketAddress)
87+
.collect(Collectors.toList());
88+
Preconditions.assertSame(ids.size(), clientAddresses.size(), "clientAddress size");
89+
for (int i = 0; i < ids.size(); i++) {
90+
clientAddressInfo.put(ids.get(i), clientAddresses.get(i));
91+
}
92+
}
93+
94+
if (cl.hasOption(ADMIN_ADDRESS_OPTION_NAME)) {
95+
final List<InetSocketAddress> adminAddresses =
96+
Arrays.stream(cl.getOptionValue(ADMIN_ADDRESS_OPTION_NAME).split(","))
97+
.map(CliUtils::parseInetSocketAddress)
98+
.collect(Collectors.toList());
99+
Preconditions.assertSame(ids.size(), adminAddresses.size(), "adminAddress size");
100+
for (int i = 0; i < ids.size(); i++) {
101+
adminAddressInfo.put(ids.get(i), adminAddresses.get(i));
102+
}
103+
}
77104
} else if (cl.hasOption(ADDRESS_OPTION_NAME)) {
78105
ids = getIds(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","), peersInfo::put);
106+
if (cl.hasOption(CLIENT_ADDRESS_OPTION_NAME) || cl.hasOption(ADMIN_ADDRESS_OPTION_NAME)) {
107+
throw new IllegalArgumentException(
108+
"When using auto-generated peer IDs, clientAddress and adminAddress are not supported.");
109+
}
79110
} else {
80111
throw new IllegalArgumentException(
81112
"Both " + PEER_ID_OPTION_NAME + " and " + ADDRESS_OPTION_NAME + " options are missing.");
82113
}
83114

84115
try (RaftClient client = newRaftClient()) {
85116
final Stream<RaftPeer> remaining = getPeerStream(RaftPeerRole.FOLLOWER);
86-
final Stream<RaftPeer> adding = ids.stream().map(raftPeerId -> RaftPeer.newBuilder()
87-
.setId(raftPeerId)
88-
.setAddress(peersInfo.get(raftPeerId))
89-
.setPriority(0)
90-
.build());
117+
final Stream<RaftPeer> adding = ids.stream().map(raftPeerId -> {
118+
RaftPeer.Builder builder = RaftPeer.newBuilder()
119+
.setId(raftPeerId)
120+
.setAddress(peersInfo.get(raftPeerId))
121+
.setPriority(0);
122+
123+
if (clientAddressInfo.containsKey(raftPeerId)) {
124+
builder.setClientAddress(clientAddressInfo.get(raftPeerId));
125+
}
126+
127+
if (adminAddressInfo.containsKey(raftPeerId)) {
128+
builder.setAdminAddress(adminAddressInfo.get(raftPeerId));
129+
}
130+
131+
return builder.build();
132+
});
91133
final List<RaftPeer> peers = Stream.concat(remaining, adding).collect(Collectors.toList());
92134
final List<RaftPeer> listeners = getPeerStream(RaftPeerRole.LISTENER)
93135
.collect(Collectors.toList());
@@ -104,9 +146,12 @@ public String getUsage() {
104146
return String.format("%s"
105147
+ " -%s <PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT>"
106148
+ " [-%s <RAFT_GROUP_ID>]"
107-
+ " <[-%s <PEER0_HOST:PEER0_PORT>]|[-%s <peerId>]>",
149+
+ " <[-%s <PEER0_HOST:PEER0_PORT>]|[-%s <peerId>]>"
150+
+ " [-%s <CLIENT_ADDRESS1,CLIENT_ADDRESS2,...>]"
151+
+ " [-%s <ADMIN_ADDRESS1,ADMIN_ADDRESS2,...>]",
108152
getCommandName(), PEER_OPTION_NAME, GROUPID_OPTION_NAME,
109-
ADDRESS_OPTION_NAME, PEER_ID_OPTION_NAME);
153+
ADDRESS_OPTION_NAME, PEER_ID_OPTION_NAME,
154+
CLIENT_ADDRESS_OPTION_NAME, ADMIN_ADDRESS_OPTION_NAME);
110155
}
111156

112157
@Override
@@ -123,8 +168,19 @@ public Options getOptions() {
123168
.desc("The address information of ratis peers")
124169
.build())
125170
.addOption(Option.builder()
126-
.option(PEER_ID_OPTION_NAME).hasArg()
171+
.option(PEER_ID_OPTION_NAME)
172+
.hasArg()
127173
.desc("The peer id of ratis peers")
174+
.build())
175+
.addOption(Option.builder()
176+
.option(CLIENT_ADDRESS_OPTION_NAME)
177+
.hasArg()
178+
.desc("The client address information of ratis peers")
179+
.build())
180+
.addOption(Option.builder()
181+
.option(ADMIN_ADDRESS_OPTION_NAME)
182+
.hasArg()
183+
.desc("The admin address information of ratis peers")
128184
.build());
129185
}
130186

0 commit comments

Comments
 (0)