This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git
The following commit(s) were added to refs/heads/master by this push:
new c266cad48 RATIS-2113. Use consistent method names and parameter types
in RaftUtils. (#1135)
c266cad48 is described below
commit c266cad48fd6e11d1cfba924b3451ada512a71c1
Author: Tsz-Wo Nicholas Sze <[email protected]>
AuthorDate: Thu Aug 22 09:44:24 2024 -0700
RATIS-2113. Use consistent method names and parameter types in RaftUtils.
(#1135)
---
.../shell/cli/{RaftUtils.java => CliUtils.java} | 129 ++++++++++-----------
.../shell/cli/sh/command/AbstractCommand.java | 13 ---
.../shell/cli/sh/command/AbstractRatisCommand.java | 25 ++--
.../ratis/shell/cli/sh/election/PauseCommand.java | 4 +-
.../ratis/shell/cli/sh/election/ResumeCommand.java | 4 +-
.../shell/cli/sh/election/StepDownCommand.java | 4 +-
.../shell/cli/sh/election/TransferCommand.java | 4 +-
.../ratis/shell/cli/sh/group/GroupListCommand.java | 8 +-
.../shell/cli/sh/local/RaftMetaConfCommand.java | 6 +-
.../apache/ratis/shell/cli/sh/peer/AddCommand.java | 13 ++-
.../ratis/shell/cli/sh/peer/RemoveCommand.java | 4 +-
.../shell/cli/sh/peer/SetPriorityCommand.java | 4 +-
.../shell/cli/sh/snapshot/TakeSnapshotCommand.java | 4 +-
13 files changed, 101 insertions(+), 121 deletions(-)
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/CliUtils.java
similarity index 62%
rename from ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java
rename to ratis-shell/src/main/java/org/apache/ratis/shell/cli/CliUtils.java
index 9c5d90e4b..98e21be2a 100644
--- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/RaftUtils.java
+++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/CliUtils.java
@@ -37,7 +37,6 @@ import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
-import java.util.function.Consumer;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
@@ -46,43 +45,31 @@ import java.util.stream.Collectors;
import java.util.UUID;
/**
- * Helper class for raft operations.
+ * Utilities for command line interface.
*/
-public final class RaftUtils {
-
- public static final RaftGroupId DEFAULT_RAFT_GROUP_ID =
RaftGroupId.randomId();
-
- private RaftUtils() {
+public final class CliUtils {
+ private static final ExponentialBackoffRetry RETRY_POLICY =
ExponentialBackoffRetry.newBuilder()
+ .setBaseSleepTime(TimeDuration.valueOf(1000, TimeUnit.MILLISECONDS))
+ .setMaxAttempts(10)
+ .setMaxSleepTime(TimeDuration.valueOf(100_000, TimeUnit.MILLISECONDS))
+ .build();
+
+ private CliUtils() {
// prevent instantiation
}
- /**
- * Gets the raft peer id.
- *
- * @param address the address of the server
- * @return the raft peer id
- */
+ /** @return {@link RaftPeerId} from the given address. */
public static RaftPeerId getPeerId(InetSocketAddress address) {
return getPeerId(address.getHostString(), address.getPort());
}
- /**
- * Gets the raft peer id.
- *
- * @param host the hostname of the server
- * @param port the port of the server
- * @return the raft peer id
- */
+ /** @return {@link RaftPeerId} from the given host and port. */
public static RaftPeerId getPeerId(String host, int port) {
return RaftPeerId.getRaftPeerId(host + "_" + port);
}
- /**
- * Create a raft client to communicate to ratis server.
- * @param raftGroup the raft group
- * @return return a raft client
- */
- public static RaftClient createClient(RaftGroup raftGroup) {
+ /** Create a new {@link RaftClient} from the given group. */
+ public static RaftClient newRaftClient(RaftGroup group) {
RaftProperties properties = new RaftProperties();
RaftClientConfigKeys.Rpc.setRequestTimeout(properties,
TimeDuration.valueOf(15, TimeUnit.SECONDS));
@@ -92,16 +79,10 @@ public final class RaftUtils {
final Properties sys = System.getProperties();
sys.stringPropertyNames().forEach(key -> properties.set(key,
sys.getProperty(key)));
- ExponentialBackoffRetry retryPolicy = ExponentialBackoffRetry.newBuilder()
- .setBaseSleepTime(TimeDuration.valueOf(1000, TimeUnit.MILLISECONDS))
- .setMaxAttempts(10)
- .setMaxSleepTime(
- TimeDuration.valueOf(100_000, TimeUnit.MILLISECONDS))
- .build();
return RaftClient.newBuilder()
- .setRaftGroup(raftGroup)
+ .setRaftGroup(group)
.setProperties(properties)
- .setRetryPolicy(retryPolicy)
+ .setRetryPolicy(RETRY_POLICY)
.build();
}
@@ -116,7 +97,7 @@ public final class RaftUtils {
* @return the first non-null value returned by the given function applied
to the given list.
*/
private static <PARAMETER, RETURN, EXCEPTION extends Throwable> RETURN
applyFunctionReturnFirstNonNull(
- Collection<PARAMETER> list, CheckedFunction<PARAMETER, RETURN,
EXCEPTION> function) {
+ Collection<PARAMETER> list, CheckedFunction<PARAMETER, RETURN,
EXCEPTION> function, PrintStream out) {
for (PARAMETER parameter : list) {
try {
RETURN ret = function.apply(parameter);
@@ -124,13 +105,14 @@ public final class RaftUtils {
return ret;
}
} catch (Throwable e) {
- e.printStackTrace();
+ e.printStackTrace(out);
}
}
return null;
}
- public static List<RaftPeer> buildRaftPeersFromStr(String peers) {
+ /** Parse the given string as a list of {@link RaftPeer}. */
+ public static List<RaftPeer> parseRaftPeers(String peers) {
List<InetSocketAddress> addresses = new ArrayList<>();
String[] peersArray = peers.split(",");
for (String peer : peersArray) {
@@ -138,64 +120,79 @@ public final class RaftUtils {
}
return addresses.stream()
- .map(addr -> RaftPeer.newBuilder()
- .setId(RaftUtils.getPeerId(addr))
- .setAddress(addr)
- .build()
- ).collect(Collectors.toList());
+ .map(addr ->
RaftPeer.newBuilder().setId(getPeerId(addr)).setAddress(addr).build())
+ .collect(Collectors.toList());
}
- public static RaftGroupId buildRaftGroupIdFromStr(String groupId) {
- return groupId != null && groupId.isEmpty() ?
RaftGroupId.valueOf(UUID.fromString(groupId))
- : DEFAULT_RAFT_GROUP_ID;
+ /** Parse the given string as a {@link RaftGroupId}. */
+ public static RaftGroupId parseRaftGroupId(String groupId) {
+ return groupId != null && groupId.isEmpty() ?
RaftGroupId.valueOf(UUID.fromString(groupId)) : null;
}
- public static RaftGroupId retrieveRemoteGroupId(RaftGroupId
raftGroupIdFromConfig,
- List<RaftPeer> peers,
- RaftClient client,
PrintStream printStream) throws IOException {
- if (!DEFAULT_RAFT_GROUP_ID .equals(raftGroupIdFromConfig)) {
- return raftGroupIdFromConfig;
+ /**
+ * Get the group id from the given peers if the given group id is null.
+ *
+ * @param client for communicating to the peers.
+ * @param peers the peers of the group.
+ * @param groupId the given group id, if there is any.
+ * @param err for printing error messages.
+ * @return the group id from the given peers if the given group id is null;
+ * otherwise, return the given group id.
+ */
+ public static RaftGroupId getGroupId(RaftClient client, List<RaftPeer>
peers, RaftGroupId groupId,
+ PrintStream err) throws IOException {
+ if (groupId != null) {
+ return groupId;
}
- final RaftGroupId remoteGroupId;
final List<RaftGroupId> groupIds = applyFunctionReturnFirstNonNull(peers,
- p -> client.getGroupManagementApi((p.getId())).list().getGroupIds());
+ p -> client.getGroupManagementApi(p.getId()).list().getGroupIds(),
err);
if (groupIds == null) {
- printStream.println("Failed to get group ID from " + peers);
- throw new IOException("Failed to get group ID from " + peers);
+ final String message = "Failed to get group ID from " + peers;
+ err.println("Failed to get group ID from " + peers);
+ throw new IOException(message);
} else if (groupIds.size() == 1) {
- remoteGroupId = groupIds.get(0);
+ return groupIds.get(0);
} else {
String message = "Unexpected multiple group IDs " + groupIds
+ ". In such case, the target group ID must be specified.";
- printStream.println(message);
+ err.println(message);
throw new IOException(message);
}
- return remoteGroupId;
}
- public static GroupInfoReply retrieveGroupInfoByGroupId(RaftGroupId
remoteGroupId, List<RaftPeer> peers,
- RaftClient client,
PrintStream printStream)
- throws IOException {
+ /**
+ * Get the group info from the given peers.
+ *
+ * @param client for communicating to the peers.
+ * @param peers the peers of the group.
+ * @param groupId the target group
+ * @param err for printing error messages.
+ * @return the group info
+ */
+ public static GroupInfoReply getGroupInfo(RaftClient client, List<RaftPeer>
peers, RaftGroupId groupId,
+ PrintStream err) throws IOException {
GroupInfoReply groupInfoReply = applyFunctionReturnFirstNonNull(peers,
- p -> client.getGroupManagementApi((p.getId())).info(remoteGroupId));
- processReply(groupInfoReply, printStream::println,
- () -> "Failed to get group info for group id " +
remoteGroupId.getUuid() + " from " + peers);
+ p -> client.getGroupManagementApi((p.getId())).info(groupId), err);
+ checkReply(groupInfoReply, () -> "Failed to get group info for " +
groupId.getUuid()
+ + " from " + peers, err);
return groupInfoReply;
}
- public static void processReply(RaftClientReply reply, Consumer<String>
printer, Supplier<String> message)
+ /** Check if the given reply is success. */
+ public static void checkReply(RaftClientReply reply, Supplier<String>
message, PrintStream printStream)
throws IOException {
if (reply == null || !reply.isSuccess()) {
final RaftException e = Optional.ofNullable(reply)
.map(RaftClientReply::getException)
.orElseGet(() -> new RaftException("Reply: " + reply));
- printer.accept(message.get());
- throw new IOException(e.getMessage(), e);
+ printStream.println(message.get());
+ throw new IOException(message.get(), e);
}
}
+ /** Parse the given string as a {@link InetSocketAddress}. */
public static InetSocketAddress parseInetSocketAddress(String address) {
try {
final String[] hostPortPair = address.split(":");
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java
index 20a52a80f..f02761de4 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java
@@ -20,7 +20,6 @@ package org.apache.ratis.shell.cli.sh.command;
import org.apache.ratis.shell.cli.Command;
import java.io.PrintStream;
-import java.net.InetSocketAddress;
/**
* The base class for all the ratis shell {@link Command} classes.
@@ -33,18 +32,6 @@ public abstract class AbstractCommand implements Command {
printStream = context.getPrintStream();
}
- public static InetSocketAddress parseInetSocketAddress(String address) {
- try {
- final String[] hostPortPair = address.split(":");
- if (hostPortPair.length < 2) {
- throw new IllegalArgumentException("Unexpected address format
<HOST:PORT>.");
- }
- return new InetSocketAddress(hostPortPair[0],
Integer.parseInt(hostPortPair[1]));
- } catch (Exception e) {
- throw new IllegalArgumentException("Failed to parse the server address
parameter \"" + address + "\".", e);
- }
- }
-
protected PrintStream getPrintStream() {
return printStream;
}
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java
index 91bdc873b..a9d391f86 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java
@@ -24,7 +24,7 @@ import org.apache.ratis.protocol.RaftGroupId;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.protocol.GroupInfoReply;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.ratis.client.RaftClient;
@@ -47,11 +47,6 @@ import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-import static org.apache.ratis.shell.cli.RaftUtils.buildRaftGroupIdFromStr;
-import static org.apache.ratis.shell.cli.RaftUtils.buildRaftPeersFromStr;
-import static org.apache.ratis.shell.cli.RaftUtils.retrieveGroupInfoByGroupId;
-import static org.apache.ratis.shell.cli.RaftUtils.retrieveRemoteGroupId;
-
/**
* The base class for the ratis shell which need to connect to server.
*/
@@ -67,13 +62,13 @@ public abstract class AbstractRatisCommand extends
AbstractCommand {
@Override
public int run(CommandLine cl) throws IOException {
- List<RaftPeer> peers =
buildRaftPeersFromStr(cl.getOptionValue(PEER_OPTION_NAME));
- RaftGroupId raftGroupIdFromConfig =
buildRaftGroupIdFromStr(cl.getOptionValue(GROUPID_OPTION_NAME));
- raftGroup = RaftGroup.valueOf(raftGroupIdFromConfig, peers);
+ final List<RaftPeer> peers =
CliUtils.parseRaftPeers(cl.getOptionValue(PEER_OPTION_NAME));
+ final RaftGroupId groupIdSpecified =
CliUtils.parseRaftGroupId(cl.getOptionValue(GROUPID_OPTION_NAME));
+ raftGroup = RaftGroup.valueOf(groupIdSpecified != null? groupIdSpecified:
RaftGroupId.randomId(), peers);
PrintStream printStream = getPrintStream();
- try (final RaftClient client = RaftUtils.createClient(raftGroup)) {
- final RaftGroupId remoteGroupId =
retrieveRemoteGroupId(raftGroupIdFromConfig, peers, client, printStream);
- groupInfoReply = retrieveGroupInfoByGroupId(remoteGroupId, peers,
client, printStream);
+ try (final RaftClient client = CliUtils.newRaftClient(raftGroup)) {
+ final RaftGroupId remoteGroupId = CliUtils.getGroupId(client, peers,
groupIdSpecified, printStream);
+ groupInfoReply = CliUtils.getGroupInfo(client, peers, remoteGroupId,
printStream);
raftGroup = groupInfoReply.getGroup();
}
return 0;
@@ -121,7 +116,7 @@ public abstract class AbstractRatisCommand extends
AbstractCommand {
}
protected void processReply(RaftClientReply reply, Supplier<String>
messageSupplier) throws IOException {
- RaftUtils.processReply(reply, getPrintStream()::println, messageSupplier);
+ CliUtils.checkReply(reply, messageSupplier, getPrintStream());
}
protected List<RaftPeerId> getIds(String[] optionValues,
BiConsumer<RaftPeerId, InetSocketAddress> consumer) {
@@ -130,8 +125,8 @@ public abstract class AbstractRatisCommand extends
AbstractCommand {
}
final List<RaftPeerId> ids = new ArrayList<>();
for (String address : optionValues) {
- final InetSocketAddress serverAddress = parseInetSocketAddress(address);
- final RaftPeerId peerId = RaftUtils.getPeerId(serverAddress);
+ final InetSocketAddress serverAddress =
CliUtils.parseInetSocketAddress(address);
+ final RaftPeerId peerId = CliUtils.getPeerId(serverAddress);
consumer.accept(peerId, serverAddress);
ids.add(peerId);
}
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/PauseCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/PauseCommand.java
index 4ea2969ba..242e1886a 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/PauseCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/PauseCommand.java
@@ -24,7 +24,7 @@ import org.apache.ratis.client.RaftClient;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
@@ -61,7 +61,7 @@ public class PauseCommand extends AbstractRatisCommand {
printf("Peer not found: %s", strAddr);
return -1;
}
- try(final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
+ try(final RaftClient raftClient = CliUtils.newRaftClient(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));
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/ResumeCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/ResumeCommand.java
index 4b4dc225a..dbcee7bd3 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/ResumeCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/ResumeCommand.java
@@ -24,7 +24,7 @@ import org.apache.ratis.client.RaftClient;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
@@ -61,7 +61,7 @@ public class ResumeCommand extends AbstractRatisCommand {
printf("Can't find a sever with the address:%s", strAddr);
return -1;
}
- try(final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
+ try(final RaftClient raftClient = CliUtils.newRaftClient(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));
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/StepDownCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/StepDownCommand.java
index 911a2bb26..da641a07b 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/StepDownCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/StepDownCommand.java
@@ -21,7 +21,7 @@ import org.apache.commons.cli.CommandLine;
import org.apache.ratis.client.RaftClient;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
@@ -48,7 +48,7 @@ public class StepDownCommand extends AbstractRatisCommand {
public int run(CommandLine cl) throws IOException {
super.run(cl);
- try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+ try (RaftClient client = CliUtils.newRaftClient(getRaftGroup())) {
RaftPeerId leaderId =
RaftPeerId.valueOf(getLeader(getGroupInfoReply().getRoleInfoProto()).getId());
final RaftClientReply transferLeadershipReply =
client.admin().transferLeadership(null, leaderId, 60_000);
processReply(transferLeadershipReply, () -> "Failed to step down
leader");
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/TransferCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/TransferCommand.java
index c71d7f89f..24eae7353 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/TransferCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/election/TransferCommand.java
@@ -25,7 +25,7 @@ import org.apache.ratis.proto.RaftProtos.RaftPeerRole;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.exceptions.TransferLeadershipException;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
import org.apache.ratis.util.TimeDuration;
@@ -74,7 +74,7 @@ public class TransferCommand extends AbstractRatisCommand {
printf("Peer with address %s not found.", strAddr);
return -2;
}
- try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+ try (RaftClient client = CliUtils.newRaftClient(getRaftGroup())) {
// transfer leadership
if (!tryTransfer(client, newLeader, highestPriority,
timeout.orElse(timeoutDefault))) {
// legacy mode, transfer leadership by setting priority.
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/GroupListCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/GroupListCommand.java
index 5bbd1939a..5ee89c53c 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/GroupListCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/GroupListCommand.java
@@ -24,7 +24,7 @@ import org.apache.commons.cli.Options;
import org.apache.ratis.client.RaftClient;
import org.apache.ratis.protocol.GroupListReply;
import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
@@ -61,15 +61,15 @@ public class GroupListCommand extends AbstractRatisCommand {
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);
+ final InetSocketAddress serverAddress =
CliUtils.parseInetSocketAddress(address);
+ peerId = CliUtils.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())) {
+ try(final RaftClient raftClient = CliUtils.newRaftClient(getRaftGroup())) {
GroupListReply reply = raftClient.getGroupManagementApi(peerId).list();
processReply(reply, () -> String.format("Failed to get group information
of peerId %s (server %s)",
peerId, address));
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java
index e258d863b..a63b65937 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java
@@ -25,7 +25,7 @@ import
org.apache.ratis.proto.RaftProtos.RaftConfigurationProto;
import org.apache.ratis.proto.RaftProtos.RaftPeerProto;
import org.apache.ratis.proto.RaftProtos.RaftPeerRole;
import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
@@ -89,7 +89,7 @@ public class RaftMetaConfCommand extends AbstractCommand {
printf(message, idWithAddress, peersStr);
return -1;
}
- InetSocketAddress inetSocketAddress = parseInetSocketAddress(
+ InetSocketAddress inetSocketAddress = CliUtils.parseInetSocketAddress(
peerIdWithAddressArray[peerIdWithAddressArray.length - 1]);
String addressString = inetSocketAddress.getHostString() + ":" +
inetSocketAddress.getPort();
if (addresses.contains(addressString)) {
@@ -111,7 +111,7 @@ public class RaftMetaConfCommand extends AbstractCommand {
ids.add(peerId);
} else {
// If peer ID is not provided, use host address as peerId value
- peerId = RaftUtils.getPeerId(inetSocketAddress).toString();
+ peerId = CliUtils.getPeerId(inetSocketAddress).toString();
}
raftPeerProtos.add(RaftPeerProto.newBuilder()
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/AddCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/AddCommand.java
index 3c65bb12d..62c6c5793 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/AddCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/AddCommand.java
@@ -25,9 +25,10 @@ import org.apache.ratis.proto.RaftProtos.RaftPeerRole;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
+import org.apache.ratis.util.Preconditions;
import java.io.IOException;
import java.net.InetSocketAddress;
@@ -66,10 +67,10 @@ public class AddCommand extends AbstractRatisCommand {
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());
- List<InetSocketAddress> addresses =
- Arrays.stream(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","))
- .map(s ->
parseInetSocketAddress(s)).collect(Collectors.toList());
- assert ids.size() == addresses.size();
+ 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));
}
@@ -80,7 +81,7 @@ public class AddCommand extends AbstractRatisCommand {
"Both " + PEER_ID_OPTION_NAME + " and " + ADDRESS_OPTION_NAME + "
options are missing.");
}
- try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+ try (RaftClient client = CliUtils.newRaftClient(getRaftGroup())) {
final Stream<RaftPeer> remaining = getPeerStream(RaftPeerRole.FOLLOWER);
final Stream<RaftPeer> adding = ids.stream().map(raftPeerId ->
RaftPeer.newBuilder()
.setId(raftPeerId)
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/RemoveCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/RemoveCommand.java
index 591851607..e2aa786b3 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/RemoveCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/RemoveCommand.java
@@ -25,7 +25,7 @@ import org.apache.ratis.proto.RaftProtos.RaftPeerRole;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
@@ -66,7 +66,7 @@ public class RemoveCommand extends AbstractRatisCommand {
throw new IllegalArgumentException(
"Both " + PEER_ID_OPTION_NAME + " and " + ADDRESS_OPTION_NAME + "
options are missing.");
}
- try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+ try (RaftClient client = CliUtils.newRaftClient(getRaftGroup())) {
final List<RaftPeer> peers = getPeerStream(RaftPeerRole.FOLLOWER)
.filter(raftPeer ->
!ids.contains(raftPeer.getId())).collect(Collectors.toList());
final List<RaftPeer> listeners = getPeerStream(RaftPeerRole.LISTENER)
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/SetPriorityCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/SetPriorityCommand.java
index 01e81f3c3..e2d4d1a53 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/SetPriorityCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/peer/SetPriorityCommand.java
@@ -24,7 +24,7 @@ import org.apache.ratis.client.RaftClient;
import org.apache.ratis.proto.RaftProtos.RaftPeerRole;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftPeer;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
@@ -63,7 +63,7 @@ public class SetPriorityCommand extends AbstractRatisCommand {
addressPriorityMap.put(str[0], Integer.parseInt(str[1]));
}
- try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+ try (RaftClient client = CliUtils.newRaftClient(getRaftGroup())) {
final List<RaftPeer> peers =
getPeerStream(RaftPeerRole.FOLLOWER).map(peer -> {
final Integer newPriority = addressPriorityMap.get(peer.getAddress());
final int priority = newPriority != null ? newPriority :
peer.getPriority();
diff --git
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/snapshot/TakeSnapshotCommand.java
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/snapshot/TakeSnapshotCommand.java
index 10bac3497..e76f215f4 100644
---
a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/snapshot/TakeSnapshotCommand.java
+++
b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/snapshot/TakeSnapshotCommand.java
@@ -23,7 +23,7 @@ import org.apache.commons.cli.Options;
import org.apache.ratis.client.RaftClient;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftPeerId;
-import org.apache.ratis.shell.cli.RaftUtils;
+import org.apache.ratis.shell.cli.CliUtils;
import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand;
import org.apache.ratis.shell.cli.sh.command.Context;
@@ -58,7 +58,7 @@ public class TakeSnapshotCommand extends AbstractRatisCommand
{
} else {
timeout = 3000;
}
- try(final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
+ try(final RaftClient raftClient = CliUtils.newRaftClient(getRaftGroup())) {
if (cl.hasOption(PEER_ID_OPTION_NAME)) {
peerId =
RaftPeerId.getRaftPeerId(cl.getOptionValue(PEER_ID_OPTION_NAME));
} else {