Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RATIS-1436.Add ratis-shell GroupInfo command #540

Merged
merged 8 commits into from
Dec 3, 2021

Conversation

codings-dan
Copy link
Contributor

What changes were proposed in this pull request?

Add a sub command to show the group information of a ratis server

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/RATIS-1436

How was this patch tested?

[~/local/myproject/ratis] [group *] -> %  mvn clean install  assembly:single -DskipTests -Dmaven.javadoc.skip=true -Dlicense.skip=true  -Dfindbugs.skip=true -Denforcer.skip=true -Dgpg.skip=true   -Djsse.enableSNIExtension=false  -Prelease -Papache-release
[~/local/myproject/ratis] [group *]-> % cd ratis-assembly/target
[~/local/myproject/ratis/ratis-assembly/target] [group *] -> % tar -xzf apache-ratis-2.3.0-SNAPSHOT-ratis-shell.tar.gz
[~/local/myproject/ratis/ratis-assembly/target] [group *] -> % cd apache-ratis-2.3.0-SNAPSHOT
[~/local/myproject/ratis/ratis-assembly/target/apache-ratis-2.3.0-SNAPSHOT] [group *] -> % bin/ratis sh groupInfo -peers localhost:19200,localhost:19201,localhost:19202 -serverAddress localhost:19200
The server localhost:19200 is in 1 groups, and the groupIds is: [group-ABB3109A44C1]

@codings-dan
Copy link
Contributor Author

@maobaolong Can you help me review the code, thx!

@szetszwo
Copy link
Contributor

How about add a new "info" option to the existing group command?

@codings-dan
Copy link
Contributor Author

@szetszwo I think the objects targeted by these two commands are different. The group command operates on peers, and the operation in this pr is groups. I want to add sub option to delete and add groups to specific peers in this command. Therefore, can we consider changing the name of the group command to a name related to Peer. Thx!

@szetszwo
Copy link
Contributor

You are right. The existing GroupCommand should be renamed to PeerCommand. The new command in this change should be called GroupCommand. The GroupCommand should correspond to GroupManagementApi. Thanks.

Copy link
Contributor

@szetszwo szetszwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codings-dan , Thanks for the update.

This is actually a "list" command instead of "info". We should combine this with the InfoCommand (rename it go "group"). Then, use the subcommands

  • "group -info";
  • "group -list".

Later on, we may add

  • "group -add";
  • "group -remove".

These are all 4 methods in GroupManagementApi.

/**
* Command for querying the group information of a ratis server.
*/
public class GroupInfoCommand extends AbstractRatisCommand{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use GroupCommand.


@Override
public String getCommandName() {
return "groupInfo";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use "group" for the main command and "group -list" for the sub command.

@codings-dan
Copy link
Contributor Author

@szetszwo Sorry, due to a lot of things this week, this patch update is late. The code has been updated, I have tested the function, and can be used normally. Can you help review the code, thx!

Copy link
Contributor

@szetszwo szetszwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codings-dan thanks for the update. A few comments inlined; see also https://issues.apache.org/jira/secure/attachment/13036937/540_review.patch

BTW, please include the JIRA number in the title of this pull request.

Comment on lines 35 to 53
private static final Map<String, Function<Context, ? extends Command>>
SUB_COMMANDS = new HashMap<>();

static {
SUB_COMMANDS.put("info", GroupInfoCommand::new);
SUB_COMMANDS.put("list", GroupListCommand::new);
}

private Map<String, Command> mSubCommands = new HashMap<>();

/**
* @param context command context
*/
public GroupCommand(Context context) {
super(context);
SUB_COMMANDS.forEach((name, constructor) -> {
mSubCommands.put(name, constructor.apply(context));
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified as below:

  private static final List<Function<Context, AbstractRatisCommand>> SUB_COMMAND_CONSTRUCTORS
      = Collections.unmodifiableList(Arrays.asList(
          GroupInfoCommand::new, GroupListCommand::new));

  private final Map<String, Command> subs;

  /**
   * @param context command context
   */
  public GroupCommand(Context context) {
    super(context);
    this.subs = Collections.unmodifiableMap(SUB_COMMAND_CONSTRUCTORS.stream()
        .map(constructor -> constructor.apply(context))
        .collect(Collectors.toMap(Command::getCommandName, Function.identity())));
  }

getCommandName(), PEER_OPTION_NAME, GROUPID_OPTION_NAME,
REMOVE_OPTION_NAME, ADD_OPTION_NAME);
public boolean hasSubCommand() {
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the default to below. Then, we don't have to override it anymore.

return Optional.ofNullable(getSubCommands()).filter(subs -> !subs.isEmpty()).isPresent();

Comment on lines 56 to 60
String[] str = strAddr.split(":");
if(str.length < 2) {
throw new IllegalArgumentException("Failed to parse the server address parameter \"" + strAddr + "\".");
}
final InetSocketAddress serverAddress = InetSocketAddress.createUnresolved(str[0], Integer.parseInt(str[1]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a utility method so that it can be used here and also in other commands.

  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);
    }
  }

}

/**
* @return command's description
*/
public static String description() {
return "Remove or Add peers of a ratis group";
return "Manage the ratis gropu, See sub-commands' descriptions for more details.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a typo. Let's change the message to

    return "Manage ratis groups; see the sub-commands for the details.";

@codings-dan codings-dan changed the title Add ratis-shell GroupInfo command RATIS-1436.Add ratis-shell GroupInfo command Dec 3, 2021
@codings-dan
Copy link
Contributor Author

@szetszwo Thank you for reviewing the code, I updated the code according to the comment and patch you left, please see if it needs to be updated again, thx!

Copy link
Contributor

@szetszwo szetszwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a typo in GroupCommand.description(); see the comment inlined. The change look good other than that.

@codings-dan
Copy link
Contributor Author

I have fixed the typo, PTAL, thx!

Comment on lines 76 to 78
public boolean hasSubCommand() {
return Optional.ofNullable(getSubCommands()).filter(sub -> !subs.isEmpty()).isPresent();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codings-dan , We don't have to override hasSubCommand() anymore since the default will always work. Please remove this. Sorry that I did not see this last time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have delete the code, PTAL again, thx!

Copy link
Contributor

@szetszwo szetszwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo szetszwo merged commit 1f4ec88 into apache:master Dec 3, 2021
@codings-dan codings-dan deleted the groupinfo branch January 21, 2022 03:46
symious pushed a commit to symious/ratis that referenced this pull request Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants