Skip to content

Commit

Permalink
Merge pull request #19 from maobaolong/config
Browse files Browse the repository at this point in the history
Support dynamic get groupId from server-side
  • Loading branch information
maobaolong authored Sep 27, 2021
2 parents 9103a61 + a06ceca commit a86906b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import opendataio.ratisshell.conf.PropertyKey;
import opendataio.ratisshell.conf.RatisShellConfiguration;
import org.apache.commons.cli.CommandLine;
import org.apache.ratis.client.RaftClient;
import org.apache.ratis.proto.RaftProtos;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftGroup;
Expand All @@ -27,9 +28,9 @@ public abstract class AbstractRatisCommand implements Command {
public static final String SERVICE_ID_OPTION_NAME = "serviceid";
public static final String PEER_OPTION_NAME = "peers";
public static final String GROUPID_OPTION_NAME = "groupid";
public static final RaftGroupId DEFAULT_ALLUXIO_RAFT_GROUP_ID
public static final RaftGroupId DEFAULT_RAFT_GROUP_ID
= RaftGroupId.valueOf(
UUID.fromString("02511d47-d67c-49a3-9011-abb3109a44c1"));
UUID.fromString("1-1-1-1-1"));
protected final PrintStream mPrintStream;
protected RaftGroup mRaftGroup;
protected List<RaftPeer> peers;
Expand Down Expand Up @@ -60,16 +61,21 @@ public int run(CommandLine cl) throws IOException {
addresses.add(addr);
}

RaftGroupId raftGroupId = DEFAULT_ALLUXIO_RAFT_GROUP_ID;
RaftGroupId raftGroupId = DEFAULT_RAFT_GROUP_ID;
if (cl.hasOption(GROUPID_OPTION_NAME)) {
raftGroupId = RaftGroupId.valueOf(
UUID.fromString(cl.getOptionValue(GROUPID_OPTION_NAME)));
} else {
if (cl.hasOption(SERVICE_ID_OPTION_NAME)) {
RaftGroupId.valueOf(
UUID.fromString(conf.get(
PropertyKey.Template.RATIS_SHELL_GROUP_ID.format(
cl.getOptionValue(SERVICE_ID_OPTION_NAME)))));
PropertyKey groupIdKey =
PropertyKey.Template.RATIS_SHELL_GROUP_ID.format(
cl.getOptionValue(SERVICE_ID_OPTION_NAME));
try {
raftGroupId =
RaftGroupId.valueOf(UUID.fromString(conf.get(groupIdKey)));
} catch (IllegalArgumentException e) {
// do nothing
}
}
}

Expand All @@ -80,6 +86,20 @@ public int run(CommandLine cl) throws IOException {
.build()
).collect(Collectors.toList());
mRaftGroup = RaftGroup.valueOf(raftGroupId, peers);
if (raftGroupId == DEFAULT_RAFT_GROUP_ID) {
try (RaftClient client = RaftUtils.createClient(mRaftGroup)) {
List<RaftGroupId> groupIds =
client.getGroupManagementApi((peers.get(0).getId())).list()
.getGroupIds();
if (groupIds.size() == 1) {
mRaftGroup = RaftGroup.valueOf(groupIds.get(0), peers);
} else {
mPrintStream.println(
"there are more than one group, you should specific one." + groupIds);
return -1;
}
}
}
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public String getCommandName() {
@Override
public int run(CommandLine cl) throws IOException {
super.run(cl);
mPrintStream.println("group id: " + mRaftGroup.getGroupId().getUuid());
try (RaftClient client = RaftUtils.createClient(mRaftGroup)) {
GroupInfoReply reply =
client.getGroupManagementApi(peers.get(0).getId()).info(mRaftGroup.getGroupId());
processReply(reply,
"failed to get info");
mPrintStream.println(reply.getCommitInfos());
mPrintStream.println("leader id: " + getLeaderId(reply.getRoleInfoProto()));
mPrintStream.println(reply.getCommitInfos());
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ private String lookupRecursively(String base, Set<String> seen)
while (matcher.find()) {
String match = matcher.group(2).trim();
if (!seen.add(match)) {
throw new RuntimeException("KEY_CIRCULAR_DEPENDENCY");
throw new RuntimeException("KEY_CIRCULAR_DEPENDENCY " + match);
}
if (!PropertyKey.isValid(match)) {
throw new RuntimeException("INVALID_CONFIGURATION_KEY");
throw new RuntimeException("INVALID_CONFIGURATION_KEY " + match);
}
String value = lookupRecursively(mProperties.get(PropertyKey.fromString(match)), seen);
seen.remove(match);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/opendataio/ratisshell/conf/PropertyKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public static PropertyKey fromString(String input) {
}
}

throw new IllegalArgumentException("Invalid configuration key");
throw new IllegalArgumentException("Invalid configuration key " + input);
}

/**
Expand Down

0 comments on commit a86906b

Please sign in to comment.