Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,13 +32,16 @@
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;

//import static org.apache.ratis.shell.cli.sh.command.CommandUtils.parseInetSocketAddress;

/**
* The base class for the ratis shell which need to connect to server.
*/
Expand All @@ -58,7 +60,7 @@ public abstract class AbstractRatisCommand extends AbstractCommand {
* @param <E> the exception type thrown by the given function.
* @return the value returned by the given function.
*/
public static <T, K, E extends Throwable> K run(Collection<T> list, CheckedFunction<T, K, E> function) {
public static <T, K, E extends Throwable> K runFunction(Collection<T> list, CheckedFunction<T, K, E> function) {
for (T t : list) {
try {
K ret = function.apply(t);
Expand All @@ -75,21 +77,22 @@ public static <T, K, E extends Throwable> K run(Collection<T> 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<InetSocketAddress> 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<RaftPeer> peers = addresses.stream()
Expand All @@ -104,7 +107,7 @@ public int run(CommandLine cl) throws IOException {
if (raftGroupIdFromConfig != DEFAULT_RAFT_GROUP_ID) {
remoteGroupId = raftGroupIdFromConfig;
} else {
final List<RaftGroupId> groupIds = run(peers,
final List<RaftGroupId> groupIds = runFunction(peers,
p -> client.getGroupManagementApi((p.getId())).list().getGroupIds());

if (groupIds == null) {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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 <T> parameter type
* @param <K> return value type
* @param <E> the exception type thrown by the given function.
* @return the value returned by the given function.
*/
public static <T, K, E extends Throwable> K runFunction(Collection<T> list, CheckedFunction<T, K, E> 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<InetSocketAddress> 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<RaftPeer> 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<RaftGroupId> 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;
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class PauseCommand extends AbstractRatisCommand {
* @param context command context
*/
public PauseCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ResumeCommand extends AbstractRatisCommand {
* @param context command context
*/
public ResumeCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class StepDownCommand extends AbstractRatisCommand {
* @param context command context
*/
public StepDownCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class TransferCommand extends AbstractRatisCommand {
* @param context command context
*/
public TransferCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
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;

/**
* Command for querying ratis group information.
*/
public class GroupInfoCommand extends AbstractRatisCommand {
public class GroupInfoCommand extends BaseCommand {
/**
* @param context command context
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class GroupListCommand extends AbstractRatisCommand {
* @param context command context
*/
public GroupListCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class RaftMetaConfCommand extends AbstractCommand {
* @param context command context
*/
public RaftMetaConfCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class AddCommand extends AbstractRatisCommand {
* @param context command context
*/
public AddCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class RemoveCommand extends AbstractRatisCommand {
* @param context command context
*/
public RemoveCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class SetPriorityCommand extends AbstractRatisCommand {
* @param context command context
*/
public SetPriorityCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class TakeSnapshotCommand extends AbstractRatisCommand {
* @param context command context
*/
public TakeSnapshotCommand(Context context) {
super(context);
super(context.getPrintStream());
}

@Override
Expand Down