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-unsubscr...@ratis.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org