You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ratis.apache.org by GitBox <gi...@apache.org> on 2021/11/11 13:24:19 UTC

[GitHub] [ratis] codings-dan opened a new pull request #532: RATIS-1429. Add ratis-shell setPriority command

codings-dan opened a new pull request #532:
URL: https://github.com/apache/ratis/pull/532


   ## What changes were proposed in this pull request?
   
   Add a setPriority sub command to set priority of the specific peer.
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/RATIS-1429)
   
   ## How was this patch tested?
   
   mI did the following test within three alluxio ratis based masters.
   
   ```
   [~/local/myproject/ratis] [setPriority *] -> %  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] [setPriority *]-> % cd ratis-assembly/target
   [~/local/myproject/ratis/ratis-assembly/target] [setPriority *] -> % tar -xzf apache-ratis-2.3.0-SNAPSHOT-ratis-shell.tar.gz
   [~/local/myproject/ratis/ratis-assembly/target] [setPriority *] -> % cd apache-ratis-2.3.0-SNAPSHOT
   [~/local/myproject/ratis/ratis-assembly/target/apache-ratis-2.3.0-SNAPSHOT] [setPriority *] -> % bin/ratis sh info -peers localhost:19200,localhost:19201,localhost:19202
   group id: 02511d47-d67c-49a3-9011-abb3109a44c1
   leader info: localhost_19200(localhost:19200)
   
   [server {
     id: "localhost_19200"
     address: "localhost:19200"
   }
   commitIndex: 8
   , server {
     id: "localhost_19202"
     address: "localhost:19202"
   }
   commitIndex: 8
   , server {
     id: "localhost_19201"
     address: "localhost:19201"
   }
   commitIndex: 8
   ]
   
   [~/local/myproject/ratis/ratis-assembly/target/apache-ratis-2.3.0-SNAPSHOT] [setPriority *] -> % bin/ratis sh setPriority -peers localhost:19200,localhost:19201,localhost:19202 -addressPriority localhost:19201,2
   
   [~/local/myproject/ratis/ratis-assembly/target/apache-ratis-2.3.0-SNAPSHOT] [setPriority *] -> % bin/ratis sh info -peers localhost:19200,localhost:19201,localhost:19202
   group id: 02511d47-d67c-49a3-9011-abb3109a44c1
   leader info: localhost_19201(localhost:19201)
   
   [server {
     id: "localhost_19200"
     address: "localhost:19200"
   }
   commitIndex: 16
   , server {
     id: "localhost_19202"
     address: "localhost:19202"
   }
   commitIndex: 15
   , server {
     id: "localhost_19201"
     address: "localhost:19201"
     priority: 2
   }
   commitIndex: 16
   ]
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] szetszwo commented on a change in pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
szetszwo commented on a change in pull request #532:
URL: https://github.com/apache/ratis/pull/532#discussion_r747932512



##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {
+      return -2;
+    }
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String peer : peersNewPriority) {
+      String[] str = peer.split(",");

Review comment:
       Use `:` (or `|`).  Comma `,` is used for separating multiple peers in a list.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] maobaolong commented on a change in pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
maobaolong commented on a change in pull request #532:
URL: https://github.com/apache/ratis/pull/532#discussion_r747526768



##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {
+      return -2;
+    }
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String peer : peersNewPriority) {
+      String[] str = peer.split(",");
+      addressPriorityMap.put(str[0], Integer.parseInt(str[1]));
+    }
+
+    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+      List<RaftPeer> peers = new ArrayList<>();
+      for (RaftPeer peer : getRaftGroup().getPeers()) {
+        if (!addressPriorityMap.containsKey(peer.getAddress())) {
+          peers.add(RaftPeer.newBuilder(peer).build());
+        } else {
+          peers.add(
+                  RaftPeer.newBuilder(peer)
+                          .setPriority(addressPriorityMap.get(peer.getAddress()))
+                          .build()
+          );
+        }
+      }
+      RaftClientReply reply = client.admin().setConfiguration(peers);
+      processReply(reply, () -> "failed to set master priorities ");
+    }
+    return 0;
+  }
+
+  @Override
+  public String getUsage() {
+    return String.format("%s"
+                    + " [-%s PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT]"

Review comment:
       -> `+ " -%s <PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT>"`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] szetszwo commented on pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
szetszwo commented on pull request #532:
URL: https://github.com/apache/ratis/pull/532#issuecomment-967796081


   @codings-dan , thanks a lot for you contribution.  Could you post a comment on https://issues.apache.org/jira/browse/RATIS-1429 so that we can set you as the assignee of the JIRA?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] codings-dan commented on pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
codings-dan commented on pull request #532:
URL: https://github.com/apache/ratis/pull/532#issuecomment-967000080


   @maobaolong I modified the code according to the comment, PTAL, thx!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] maobaolong commented on a change in pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
maobaolong commented on a change in pull request #532:
URL: https://github.com/apache/ratis/pull/532#discussion_r748139626



##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY_OPTION_NAME = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String optionValue : cl.getOptionValues(PEER_WITH_NEW_PRIORITY_OPTION_NAME)) {
+      String[] str = optionValue.split("[|]");
+      if(str.length < 2) {
+        println("The format of the parameter is wrong");

Review comment:
       return error

##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY_OPTION_NAME = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String optionValue : cl.getOptionValues(PEER_WITH_NEW_PRIORITY_OPTION_NAME)) {
+      String[] str = optionValue.split("[|]");
+      if(str.length < 2) {
+        println("The format of the parameter is wrong");
+      }
+      addressPriorityMap.put(str[0], Integer.parseInt(str[1]));
+    }
+
+    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+      List<RaftPeer> peers = new ArrayList<>();
+      for (RaftPeer peer : getRaftGroup().getPeers()) {
+        if (!addressPriorityMap.containsKey(peer.getAddress())) {
+          peers.add(RaftPeer.newBuilder(peer).build());
+        } else {
+          peers.add(
+                  RaftPeer.newBuilder(peer)
+                          .setPriority(addressPriorityMap.get(peer.getAddress()))
+                          .build()
+          );
+        }
+      }
+      RaftClientReply reply = client.admin().setConfiguration(peers);
+      processReply(reply, () -> "failed to set master priorities ");
+    }
+    return 0;
+  }
+
+  @Override
+  public String getUsage() {
+    return String.format("%s"
+                    + " -%s <PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT>"
+                    + " [-%s <RAFT_GROUP_ID>]"
+                    + " -%s <PEER_HOST:PEER_PORT,PRIORITY>",

Review comment:
       -> ` + " -%s <PEER_HOST:PEER_PORT|PRIORITY>",`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] maobaolong commented on pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
maobaolong commented on pull request #532:
URL: https://github.com/apache/ratis/pull/532#issuecomment-967249593


   @codings-dan It LGTM. @szetszwo would you please take a double check?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] codings-dan commented on pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
codings-dan commented on pull request #532:
URL: https://github.com/apache/ratis/pull/532#issuecomment-967911450


   @szetszwo I have left a comment, PTAL, thx!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] maobaolong commented on a change in pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
maobaolong commented on a change in pull request #532:
URL: https://github.com/apache/ratis/pull/532#discussion_r747530143



##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {
+      return -2;
+    }
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String peer : peersNewPriority) {
+      String[] str = peer.split(",");
+      addressPriorityMap.put(str[0], Integer.parseInt(str[1]));
+    }
+
+    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+      List<RaftPeer> peers = new ArrayList<>();
+      for (RaftPeer peer : getRaftGroup().getPeers()) {
+        if (!addressPriorityMap.containsKey(peer.getAddress())) {
+          peers.add(RaftPeer.newBuilder(peer).build());
+        } else {
+          peers.add(
+                  RaftPeer.newBuilder(peer)
+                          .setPriority(addressPriorityMap.get(peer.getAddress()))
+                          .build()
+          );
+        }
+      }
+      RaftClientReply reply = client.admin().setConfiguration(peers);
+      processReply(reply, () -> "failed to set master priorities ");
+    }
+    return 0;
+  }
+
+  @Override
+  public String getUsage() {
+    return String.format("%s"
+                    + " [-%s PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT]"
+                    + " [-%s RAFT_GROUP_ID]"
+                    + " [-%s PEER_HOST:PEER_PORT,PRIORITY]",
+            getCommandName(), PEER_OPTION_NAME, GROUPID_OPTION_NAME, PEER_WITH_NEW_PRIORITY);
+  }
+
+  @Override
+  public String getDescription() {
+    return description();
+  }
+
+  @Override
+  public Options getOptions() {
+    return super.getOptions().addOption(PEER_WITH_NEW_PRIORITY, true,

Review comment:
       -->
   ```java  
     return super.getOptions().addOption(
           Option.builder()
               .option(PEER_WITH_NEW_PRIORITY_OPTION_NAME)
               .hasArg()
               .required()
               .desc("Peers information with priority")
               .build()
   ```

##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {
+      return -2;
+    }
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String peer : peersNewPriority) {
+      String[] str = peer.split(",");
+      addressPriorityMap.put(str[0], Integer.parseInt(str[1]));
+    }
+
+    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+      List<RaftPeer> peers = new ArrayList<>();
+      for (RaftPeer peer : getRaftGroup().getPeers()) {
+        if (!addressPriorityMap.containsKey(peer.getAddress())) {
+          peers.add(RaftPeer.newBuilder(peer).build());
+        } else {
+          peers.add(
+                  RaftPeer.newBuilder(peer)
+                          .setPriority(addressPriorityMap.get(peer.getAddress()))
+                          .build()
+          );
+        }
+      }
+      RaftClientReply reply = client.admin().setConfiguration(peers);
+      processReply(reply, () -> "failed to set master priorities ");
+    }
+    return 0;
+  }
+
+  @Override
+  public String getUsage() {
+    return String.format("%s"
+                    + " [-%s PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT]"
+                    + " [-%s RAFT_GROUP_ID]"

Review comment:
       -> `+ " [-%s <RAFT_GROUP_ID>]",`

##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {
+      return -2;
+    }
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String peer : peersNewPriority) {
+      String[] str = peer.split(",");

Review comment:
       Hi @szetszwo Do you have any suggestion for the separator better than `,` ?

##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {
+      return -2;
+    }
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String peer : peersNewPriority) {

Review comment:
       -> `for (String optionValue : cl.getOptionValues(PEER_WITH_NEW_PRIORITY_OPTION_NAME)) {`

##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {

Review comment:
       Remove this checker since we defined the option is required.

##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {
+      return -2;
+    }
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String peer : peersNewPriority) {
+      String[] str = peer.split(",");
+      addressPriorityMap.put(str[0], Integer.parseInt(str[1]));
+    }
+
+    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
+      List<RaftPeer> peers = new ArrayList<>();
+      for (RaftPeer peer : getRaftGroup().getPeers()) {
+        if (!addressPriorityMap.containsKey(peer.getAddress())) {
+          peers.add(RaftPeer.newBuilder(peer).build());
+        } else {
+          peers.add(
+                  RaftPeer.newBuilder(peer)
+                          .setPriority(addressPriorityMap.get(peer.getAddress()))
+                          .build()
+          );
+        }
+      }
+      RaftClientReply reply = client.admin().setConfiguration(peers);
+      processReply(reply, () -> "failed to set master priorities ");
+    }
+    return 0;
+  }
+
+  @Override
+  public String getUsage() {
+    return String.format("%s"
+                    + " [-%s PEER0_HOST:PEER0_PORT,PEER1_HOST:PEER1_PORT,PEER2_HOST:PEER2_PORT]"
+                    + " [-%s RAFT_GROUP_ID]"
+                    + " [-%s PEER_HOST:PEER_PORT,PRIORITY]",

Review comment:
       -> `+ " -%s <PEER_HOST:PEER_PORT,PRIORITY>",`

##########
File path: ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SetPriorityCommand.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.shell.cli.sh.command;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.shell.cli.RaftUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *  Command for setting priority of the specific ratis server.
+ */
+public class SetPriorityCommand extends AbstractRatisCommand {
+  public static final String PEER_WITH_NEW_PRIORITY = "addressPriority";
+
+  /**
+   * @param context command context
+   */
+  public SetPriorityCommand(Context context) {
+    super(context);
+  }
+
+  @Override
+  public String getCommandName() {
+    return "setPriority";
+  }
+
+  @Override
+  public int run(CommandLine cl) throws IOException {
+    super.run(cl);
+    String[] peersNewPriority = cl.getOptionValues(PEER_WITH_NEW_PRIORITY);
+    if (peersNewPriority.length < 1) {
+      return -2;
+    }
+    Map<String, Integer> addressPriorityMap = new HashMap<>();
+    for (String peer : peersNewPriority) {
+      String[] str = peer.split(",");

Review comment:
       Add a check for the length of `str`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ratis] szetszwo merged pull request #532: RATIS-1429. Add ratis-shell setPriority command

Posted by GitBox <gi...@apache.org>.
szetszwo merged pull request #532:
URL: https://github.com/apache/ratis/pull/532


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org