Skip to content

RATIS-1526. Add ratis-shell pause&resume leader election command#603

Merged
szetszwo merged 4 commits intoapache:masterfrom
codings-dan:1526
Feb 23, 2022
Merged

RATIS-1526. Add ratis-shell pause&resume leader election command#603
szetszwo merged 4 commits intoapache:masterfrom
codings-dan:1526

Conversation

@codings-dan
Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Add a sub command to pause and resume leader election on the specific peer.

What is the link to the Apache JIRA

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

How was this patch tested?

UT

@codings-dan codings-dan changed the title Add ratis-shell pause&resume leader election command RATIS-1526. Add ratis-shell pause&resume leader election command Feb 17, 2022

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

Choose a reason for hiding this comment

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

This command and the ElectCommand are closely related. How about we have a base election command and then have the following subcommands?

  • transfer
  • pause
  • resume

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

will do

@codings-dan
Copy link
Copy Markdown
Contributor Author

@szetszwo I did code splitting related to election command, PTAL, thx!

@szetszwo
Copy link
Copy Markdown
Contributor

+++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java
@@ -72,9 +72,12 @@ public class RatisShell extends AbstractShell {
       // Add commands from <pkgName>.command.*
       if (cls.getPackage().getName().equals(pkgName + ".command")
           && !Modifier.isAbstract(cls.getModifiers())) {
+        System.out.println("Found " + cls);
         // Only instantiate a concrete class
         final Command cmd = ReflectionUtils.newInstance(cls, classArgs, objectArgs);
         commandsMap.put(cmd.getCommandName(), cmd);
+      } else {
+        System.out.println("Ignored " + cls);
       }
     }

@codings-dan , if we add the code above, the new subcommnads will be ignored.

Ignored class org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand
Found class org.apache.ratis.shell.cli.sh.command.PeerCommand
Found class org.apache.ratis.shell.cli.sh.command.ElectionCommand
Ignored class org.apache.ratis.shell.cli.sh.group.GroupListCommand
Found class org.apache.ratis.shell.cli.sh.command.SetPriorityCommand
Ignored class org.apache.ratis.shell.cli.sh.group.GroupInfoCommand
Ignored class org.apache.ratis.shell.cli.sh.snapshot.TakeSnapshotCommand
Ignored class org.apache.ratis.shell.cli.sh.election.PauseCommand
Found class org.apache.ratis.shell.cli.sh.command.SnapshotCommand
Found class org.apache.ratis.shell.cli.sh.command.GroupCommand
Ignored class org.apache.ratis.shell.cli.sh.election.TransferCommand
Ignored class org.apache.ratis.shell.cli.sh.election.ResumeCommand
Usage: ratis sh [generic options]
	 [election [resume] [transfer] [pause]]                    
	 [group [list] [info]]                                     
	 [peer -peers <PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT> [-groupid <RAFT_GROUP_ID>] -remove <PEER_HOST:PEER_PORT> -add <PEER_HOST:PEER_PORT>]
	 [setPriority -peers <PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT> [-groupid <RAFT_GROUP_ID>] -addressPriority <PEER_HOST:PEER_PORT|PRIORITY>]
	 [snapshot [create]]                                       

@codings-dan
Copy link
Copy Markdown
Contributor Author

+++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java
@@ -72,9 +72,12 @@ public class RatisShell extends AbstractShell {
       // Add commands from <pkgName>.command.*
       if (cls.getPackage().getName().equals(pkgName + ".command")
           && !Modifier.isAbstract(cls.getModifiers())) {
+        System.out.println("Found " + cls);
         // Only instantiate a concrete class
         final Command cmd = ReflectionUtils.newInstance(cls, classArgs, objectArgs);
         commandsMap.put(cmd.getCommandName(), cmd);
+      } else {
+        System.out.println("Ignored " + cls);
       }
     }

@codings-dan , if we add the code above, the new subcommnads will be ignored.

Ignored class org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand
Found class org.apache.ratis.shell.cli.sh.command.PeerCommand
Found class org.apache.ratis.shell.cli.sh.command.ElectionCommand
Ignored class org.apache.ratis.shell.cli.sh.group.GroupListCommand
Found class org.apache.ratis.shell.cli.sh.command.SetPriorityCommand
Ignored class org.apache.ratis.shell.cli.sh.group.GroupInfoCommand
Ignored class org.apache.ratis.shell.cli.sh.snapshot.TakeSnapshotCommand
Ignored class org.apache.ratis.shell.cli.sh.election.PauseCommand
Found class org.apache.ratis.shell.cli.sh.command.SnapshotCommand
Found class org.apache.ratis.shell.cli.sh.command.GroupCommand
Ignored class org.apache.ratis.shell.cli.sh.election.TransferCommand
Ignored class org.apache.ratis.shell.cli.sh.election.ResumeCommand
Usage: ratis sh [generic options]
	 [election [resume] [transfer] [pause]]                    
	 [group [list] [info]]                                     
	 [peer -peers <PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT> [-groupid <RAFT_GROUP_ID>] -remove <PEER_HOST:PEER_PORT> -add <PEER_HOST:PEER_PORT>]
	 [setPriority -peers <PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT> [-groupid <RAFT_GROUP_ID>] -addressPriority <PEER_HOST:PEER_PORT|PRIORITY>]
	 [snapshot [create]]                                       

Do you mean we add these lines of code and then display this information during compilation?

@szetszwo
Copy link
Copy Markdown
Contributor

Do you mean we add these lines of code and then display this information during compilation?

@codings-dan, The code lines are for printing debug messages. Currently, the debug messages show that many of the command classes are ignored since they are not under the org.apache.ratis.shell.cli.sh.command package.

@codings-dan
Copy link
Copy Markdown
Contributor Author

codings-dan commented Feb 22, 2022

@szetszwo We don't need load classes outside the org.apache.ratis.shell.cli.sh.command package, they are all sub-commands, and their baseCommand are in the command package. BaseCommand maintains a map of the mapping relationship between sub-command name and sub-command. See org.apache.ratis.shell.cli.AbstractShell.java#run

@szetszwo
Copy link
Copy Markdown
Contributor

@codings-dan , I see. The print out is a little bit confusing. Thanks for explaining it.

I suggest that we move the setPriority command as a subcommand of the peer command and change the peer command with the following subcommands.

  • add
  • remove
  • setPriority

See if you are interested working on it.

@codings-dan
Copy link
Copy Markdown
Contributor Author

@codings-dan , I see. The print out is a little bit confusing. Thanks for explaining it.

I suggest that we move the setPriority command as a subcommand of the peer command and change the peer command with the following subcommands.

  • add
  • remove
  • setPriority

See if you are interested working on it.

Good idea, since this change has nothing to do with this jira, I will complete the code change in the future.

Copy link
Copy Markdown
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 a lot. Let's move the common code to a new AbstractParentCommand class. Could you also clean up the GroupCommnad and the SnapshotCommand? See https://issues.apache.org/jira/secure/attachment/13040333/603_review.patch

= Collections.unmodifiableList(Arrays.asList(
TransferCommand::new, PauseCommand::new, ResumeCommand::new));

private final Map<String, Command> subs;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's move the common code to a parent class:

public abstract class AbstractParentCommand extends AbstractRatisCommand{
  private final Map<String, Command> subs;

  public AbstractParentCommand(Context context, List<Function<Context, AbstractRatisCommand>> subCommandConstructors) {
    super(context);
    this.subs = Collections.unmodifiableMap(subCommandConstructors.stream()
        .map(constructor -> constructor.apply(context))
        .collect(Collectors.toMap(Command::getCommandName, Function.identity())));
  }

  @Override
  public final Map<String, Command> getSubCommands() {
    return subs;
  }

  @Override
  public final String getUsage() {
    final StringBuilder usage = new StringBuilder(getCommandName());
    for (String cmd : getSubCommands().keySet()) {
      usage.append(" [").append(cmd).append("]");
    }
    return usage.toString();
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

TransferCommand::new, PauseCommand::new, ResumeCommand::new));

private final Map<String, Command> subs;
public static final String ADDRESS_OPTION_NAME = "address";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's remove it. This is not really used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

}

@Override
public Options getOptions() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's remove it. This is not really used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines +57 to +65
final RaftPeerId peerId;
Optional<RaftPeer> peer =
getRaftGroup().getPeers().stream().filter(p -> p.getAddress().equals(strAddr)).findAny();
if (peer.isPresent()) {
peerId = peer.get().getId();
} else {
printf("Can't find a sever with the address:%s", strAddr);
return -1;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The code can be simplified as below.

    final RaftPeerId peerId = getRaftGroup().getPeers().stream()
        .filter(p -> p.getAddress().equals(strAddr)).findAny()
        .map(RaftPeer::getId)
        .orElse(null);
    if (peerId == null) {
      printf("Peer not found: %s", strAddr);
      return -1;
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines +57 to +62
final RaftPeerId peerId;
Optional<RaftPeer> peer =
getRaftGroup().getPeers().stream().filter(p -> p.getAddress().equals(strAddr)).findAny();
if (peer.isPresent()) {
peerId = peer.get().getId();
} else {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We may simplify the code as in the PauseCommnad.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@codings-dan
Copy link
Copy Markdown
Contributor Author

@codings-dan , I see. The print out is a little bit confusing. Thanks for explaining it.

I suggest that we move the setPriority command as a subcommand of the peer command and change the peer command with the following subcommands.

  • add
  • remove
  • setPriority

See if you are interested working on it.

@szetszwo I have refactored the code, PTAL, thx!

Copy link
Copy Markdown
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 069f9d8 into apache:master Feb 23, 2022
@codings-dan codings-dan deleted the 1526 branch February 24, 2022 01:44
SincereXIA pushed a commit to SincereXIA/ratis that referenced this pull request Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants