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/RaftUtils.java index 1239fc56c4..1fc5517025 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/RaftUtils.java @@ -69,10 +69,15 @@ public static RaftClient createClient(RaftGroup raftGroup) { RaftClientConfigKeys.Rpc.setRequestTimeout(properties, TimeDuration.valueOf(15, TimeUnit.SECONDS)); + StringBuilder sb = new StringBuilder(); // Since ratis-shell support GENERIC_COMMAND_OPTIONS, here we should // merge these options to raft properties to make it work. final Properties sys = System.getProperties(); - sys.stringPropertyNames().forEach(key -> properties.set(key, sys.getProperty(key))); + sys.stringPropertyNames().forEach(key -> { + sb.append("k [" + key + "] = " + sys.getProperty(key) + ",,,,,,, "); + properties.set(key, sys.getProperty(key)); + }); + System.out.println("*****______ sssssssssssss. sys properties: " + sb.toString()); ExponentialBackoffRetry retryPolicy = ExponentialBackoffRetry.newBuilder() .setBaseSleepTime(TimeDuration.valueOf(1000, TimeUnit.MILLISECONDS)) 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 20a52a80f8..a3adbc154a 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 @@ -29,8 +29,8 @@ public abstract class AbstractCommand implements Command { private final PrintStream printStream; - protected AbstractCommand(Context context) { - printStream = context.getPrintStream(); + protected AbstractCommand(PrintStream printStream) { + this.printStream = printStream; } public static InetSocketAddress parseInetSocketAddress(String address) { 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 1888c0e0ea..d999c722e0 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 @@ -21,7 +21,6 @@ import org.apache.ratis.protocol.*; import org.apache.ratis.protocol.exceptions.RaftException; import org.apache.ratis.shell.cli.RaftUtils; -import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Options; import org.apache.ratis.client.RaftClient; import org.apache.ratis.proto.RaftProtos.RaftConfigurationProto; @@ -33,6 +32,7 @@ import org.apache.ratis.util.function.CheckedFunction; import java.io.IOException; +import java.io.PrintStream; import java.net.InetSocketAddress; import java.util.*; import java.util.function.BiConsumer; @@ -40,6 +40,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +//import static org.apache.ratis.shell.cli.sh.command.CommandUtils.parseInetSocketAddress; + /** * The base class for the ratis shell which need to connect to server. */ @@ -58,7 +60,7 @@ public abstract class AbstractRatisCommand extends AbstractCommand { * @param the exception type thrown by the given function. * @return the value returned by the given function. */ - public static K run(Collection list, CheckedFunction function) { + public static K runFunction(Collection list, CheckedFunction function) { for (T t : list) { try { K ret = function.apply(t); @@ -75,21 +77,22 @@ public static K run(Collection list, CheckedFunct private RaftGroup raftGroup; private GroupInfoReply groupInfoReply; - protected AbstractRatisCommand(Context context) { - super(context); + protected AbstractRatisCommand(PrintStream printStream) { + super(printStream); } - @Override - public int run(CommandLine cl) throws IOException { + + public int run(String peersStr, String groupId) throws IOException { List 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))) + + final RaftGroupId raftGroupIdFromConfig = (groupId != null && !groupId.trim().equals(""))? + RaftGroupId.valueOf(UUID.fromString(groupId.trim())) : DEFAULT_RAFT_GROUP_ID; List peers = addresses.stream() @@ -104,7 +107,7 @@ public int run(CommandLine cl) throws IOException { if (raftGroupIdFromConfig != DEFAULT_RAFT_GROUP_ID) { remoteGroupId = raftGroupIdFromConfig; } else { - final List groupIds = run(peers, + final List groupIds = runFunction(peers, p -> client.getGroupManagementApi((p.getId())).list().getGroupIds()); if (groupIds == null) { @@ -118,7 +121,7 @@ public int run(CommandLine cl) throws IOException { } } - groupInfoReply = run(peers, p -> client.getGroupManagementApi((p.getId())).info(remoteGroupId)); + groupInfoReply = runFunction(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(); diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/BaseCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/BaseCommand.java new file mode 100644 index 0000000000..15981983f9 --- /dev/null +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/BaseCommand.java @@ -0,0 +1,18 @@ +package org.apache.ratis.shell.cli.sh.command; + +import org.apache.commons.cli.CommandLine; + +import java.io.IOException; + +public abstract class BaseCommand + extends AbstractRatisCommand +{ + protected BaseCommand(Context context) { + super(context.getPrintStream()); + } + + public int run(CommandLine cl) throws IOException { + return super.run(cl.getOptionValue(PEER_OPTION_NAME), cl.getOptionValue(GROUPID_OPTION_NAME)); + } + +} diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/CommandUtils.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/CommandUtils.java new file mode 100644 index 0000000000..224a9ff3d6 --- /dev/null +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/CommandUtils.java @@ -0,0 +1,94 @@ +package org.apache.ratis.shell.cli.sh.command; + +import org.apache.commons.cli.CommandLine; +import org.apache.ratis.client.RaftClient; +import org.apache.ratis.protocol.*; +import org.apache.ratis.protocol.exceptions.RaftException; +import org.apache.ratis.shell.cli.RaftUtils; +import org.apache.ratis.util.ProtoUtils; +import org.apache.ratis.util.function.CheckedFunction; + +import java.io.IOException; +import java.io.PrintStream; +import java.net.InetSocketAddress; +import java.util.*; +import java.util.function.BiConsumer; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class CommandUtils { +// private PrintStream printStream; + + public static final RaftGroupId DEFAULT_RAFT_GROUP_ID = RaftGroupId.randomId(); + + /** + * Execute a given function with input parameter from the members of a list. + * + * @param list the input parameters + * @param function the function to be executed + * @param parameter type + * @param return value type + * @param the exception type thrown by the given function. + * @return the value returned by the given function. + */ + public static K runFunction(Collection list, CheckedFunction function) { + for (T t : list) { + try { + K ret = function.apply(t); + if (ret != null) { + return ret; + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + return null; + } + +// public static int run(String peersStr, String groupId) throws IOException { +// List addresses = new ArrayList<>(); +// String[] peersArray = peersStr.split(","); +// for (String peer : peersArray) { +// addresses.add(parseInetSocketAddress(peer)); +// } +// +// final RaftGroupId raftGroupIdFromConfig = (!groupId.trim().equals(""))? +// RaftGroupId.valueOf(UUID.fromString(groupId.trim())) +// : DEFAULT_RAFT_GROUP_ID; +// +// List peers = addresses.stream() +// .map(addr -> RaftPeer.newBuilder() +// .setId(RaftUtils.getPeerId(addr)) +// .setAddress(addr) +// .build() +// ).collect(Collectors.toList()); +// RaftGroup 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 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; +// } + +} 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 4ea2969bac..13e9363002 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 @@ -40,7 +40,7 @@ public class PauseCommand extends AbstractRatisCommand { * @param context command context */ public PauseCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override 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 4b4dc225a0..bcc33e5f19 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 @@ -40,7 +40,7 @@ public class ResumeCommand extends AbstractRatisCommand { * @param context command context */ public ResumeCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override 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 911a2bb26a..a424f61a87 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 @@ -36,7 +36,7 @@ public class StepDownCommand extends AbstractRatisCommand { * @param context command context */ public StepDownCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override 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 c71d7f89f6..d7d2dbd19b 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 @@ -46,7 +46,7 @@ public class TransferCommand extends AbstractRatisCommand { * @param context command context */ public TransferCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/GroupInfoCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/GroupInfoCommand.java index 0125440e90..d0c11d38c5 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/GroupInfoCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/GroupInfoCommand.java @@ -22,6 +22,7 @@ import org.apache.ratis.proto.RaftProtos; import org.apache.ratis.protocol.GroupInfoReply; import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand; +import org.apache.ratis.shell.cli.sh.command.BaseCommand; import org.apache.ratis.shell.cli.sh.command.Context; import java.io.IOException; @@ -29,7 +30,7 @@ /** * Command for querying ratis group information. */ -public class GroupInfoCommand extends AbstractRatisCommand { +public class GroupInfoCommand extends BaseCommand { /** * @param context command context */ 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 5bbd1939ad..bfe8840e4e 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 @@ -42,7 +42,7 @@ public class GroupListCommand extends AbstractRatisCommand { * @param context command context */ public GroupListCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override 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 e258d863b8..93b4ed1827 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 @@ -58,7 +58,7 @@ public class RaftMetaConfCommand extends AbstractCommand { * @param context command context */ public RaftMetaConfCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override 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 3c65bb12de..67fa136571 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 @@ -49,7 +49,7 @@ public class AddCommand extends AbstractRatisCommand { * @param context command context */ public AddCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override 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 5918516070..63bdf6d88a 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 @@ -45,7 +45,7 @@ public class RemoveCommand extends AbstractRatisCommand { * @param context command context */ public RemoveCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override 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 01e81f3c34..9ef77401f3 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 @@ -42,7 +42,7 @@ public class SetPriorityCommand extends AbstractRatisCommand { * @param context command context */ public SetPriorityCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override 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 10bac34975..f41d2bc43b 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 @@ -40,7 +40,7 @@ public class TakeSnapshotCommand extends AbstractRatisCommand { * @param context command context */ public TakeSnapshotCommand(Context context) { - super(context); + super(context.getPrintStream()); } @Override