From 672d5f915f4c25690738246710577897cf8cbe62 Mon Sep 17 00:00:00 2001 From: idellzheng Date: Thu, 10 Aug 2023 11:45:35 +0800 Subject: [PATCH 01/11] Add ratis-shell cmd to generate a new raft-meta.conf. --- .../cli/sh/command/GenerationCommand.java | 58 ++++++++ .../sh/generation/RaftMetaConfCommand.java | 136 ++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GenerationCommand.java create mode 100644 ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GenerationCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GenerationCommand.java new file mode 100644 index 0000000000..b96dd1f963 --- /dev/null +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GenerationCommand.java @@ -0,0 +1,58 @@ +/* + * 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.ratis.shell.cli.sh.generation.RaftMetaConfCommand; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.function.Function; + +/** + * Command for generate ratis meta file + */ +public class GenerationCommand extends AbstractParentCommand { + + private static final List> SUB_COMMAND_CONSTRUCTORS + = Collections.unmodifiableList(Arrays.asList(RaftMetaConfCommand::new)); + + /** + * @param context command context + */ + public GenerationCommand(Context context) { + super(context, SUB_COMMAND_CONSTRUCTORS); + } + + @Override + public String getCommandName() { + return "generation"; + } + + @Override + public String getDescription() { + return description(); + } + + /** + * @return command's description + */ + public static String description() { + return "Generate ratis meta data file; see the sub-commands for the details."; + } +} diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java new file mode 100644 index 0000000000..8daad7eee0 --- /dev/null +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java @@ -0,0 +1,136 @@ +/* + * 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.generation; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.ratis.proto.RaftProtos; +import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand; +import org.apache.ratis.shell.cli.sh.command.Context; +import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +public class RaftMetaConfCommand extends AbstractRatisCommand { + public static final String PATH_OPTION_NAME = "path"; + public static final String LOG_ENTRY_INDEX_OPTION_NAME = "index"; + + private static final String RAFT_META_CONF = "raft-meta.conf"; + private static final String NEW_RAFT_META_CONF = "new-raft-meta.conf"; + + /** + * @param context command context + */ + public RaftMetaConfCommand(Context context) { + super(context); + } + + @Override + public String getCommandName() { + return "raftMetaConf"; + } + + @Override + public int run(CommandLine cl) throws IOException { + String peersStr = cl.getOptionValue(PEER_OPTION_NAME); + String path = cl.getOptionValue(PATH_OPTION_NAME); + List raftPeerProtos = new ArrayList<>(); + for (String peer : peersStr.split(",")) { + String[] peerInfos = peer.split(":"); + String peerId = peerInfos[0] + "_" + peerInfos[1]; + raftPeerProtos.add(RaftProtos.RaftPeerProto.newBuilder() + .setId(ByteString.copyFrom(peerId.getBytes(StandardCharsets.UTF_8))).setAddress(peer) + .setStartupRole(RaftProtos.RaftPeerRole.FOLLOWER).build()); + } + long index; + if (cl.hasOption(LOG_ENTRY_INDEX_OPTION_NAME)) { + index = Long.parseLong(cl.getOptionValue(LOG_ENTRY_INDEX_OPTION_NAME)); + } else { + try (InputStream in = new FileInputStream(new File(path, RAFT_META_CONF))) { + index = RaftProtos.LogEntryProto.newBuilder().mergeFrom(in).build().getIndex() + 1; + System.out.println("Index in the original file is: " + (index - 1)); + } + } + try (OutputStream out = new FileOutputStream(new File(path, NEW_RAFT_META_CONF))) { + RaftProtos.LogEntryProto generateLogEntryProto = RaftProtos.LogEntryProto.newBuilder() + .setConfigurationEntry(RaftProtos.RaftConfigurationProto.newBuilder() + .addAllPeers(raftPeerProtos).build()) + .setIndex(index).build(); + System.out.println("Generate new LogEntryProto info is:\n"+ generateLogEntryProto); + generateLogEntryProto.writeTo(out); + } + return 0; + } + + @Override + public String getUsage() { + return String.format("%s" + + " -%s " + + " -%s " + + " [-%s ]", + getCommandName(), PEER_OPTION_NAME, PATH_OPTION_NAME, LOG_ENTRY_INDEX_OPTION_NAME); + } + + @Override + public String getDescription() { + return description(); + } + + @Override + public Options getOptions() { + return new Options() + .addOption( + Option.builder() + .option(PEER_OPTION_NAME) + .hasArg() + .required() + .desc("Peer addresses seperated by comma") + .build()) + .addOption( + Option.builder() + .option(PATH_OPTION_NAME) + .hasArg() + .required() + .desc("The parent path of raft-meta.conf") + .build()) + .addOption( + Option.builder() + .option(LOG_ENTRY_INDEX_OPTION_NAME) + .hasArg() + .desc("The log entry index in raft-meta.conf(If index is specified directly, " + + "the original file will be ignored)") + .build()); + } + + /** + * @return command's description + */ + public static String description() { + return "Generate a new raft-meta.conf file."; + } +} + From 9b13aaee44f9280236df7e767198e967b05e268c Mon Sep 17 00:00:00 2001 From: idellzheng Date: Tue, 15 Aug 2023 20:48:58 +0800 Subject: [PATCH 02/11] Remove index option. --- .../sh/generation/RaftMetaConfCommand.java | 29 +++++-------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java index 8daad7eee0..9158c67fb5 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java @@ -37,7 +37,6 @@ public class RaftMetaConfCommand extends AbstractRatisCommand { public static final String PATH_OPTION_NAME = "path"; - public static final String LOG_ENTRY_INDEX_OPTION_NAME = "index"; private static final String RAFT_META_CONF = "raft-meta.conf"; private static final String NEW_RAFT_META_CONF = "new-raft-meta.conf"; @@ -66,20 +65,14 @@ public int run(CommandLine cl) throws IOException { .setId(ByteString.copyFrom(peerId.getBytes(StandardCharsets.UTF_8))).setAddress(peer) .setStartupRole(RaftProtos.RaftPeerRole.FOLLOWER).build()); } - long index; - if (cl.hasOption(LOG_ENTRY_INDEX_OPTION_NAME)) { - index = Long.parseLong(cl.getOptionValue(LOG_ENTRY_INDEX_OPTION_NAME)); - } else { - try (InputStream in = new FileInputStream(new File(path, RAFT_META_CONF))) { - index = RaftProtos.LogEntryProto.newBuilder().mergeFrom(in).build().getIndex() + 1; - System.out.println("Index in the original file is: " + (index - 1)); - } - } - try (OutputStream out = new FileOutputStream(new File(path, NEW_RAFT_META_CONF))) { + try (InputStream in = new FileInputStream(new File(path, RAFT_META_CONF)); + OutputStream out = new FileOutputStream(new File(path, NEW_RAFT_META_CONF))) { + long index = RaftProtos.LogEntryProto.newBuilder().mergeFrom(in).build().getIndex(); + System.out.println("Index in the original file is: " + index); RaftProtos.LogEntryProto generateLogEntryProto = RaftProtos.LogEntryProto.newBuilder() .setConfigurationEntry(RaftProtos.RaftConfigurationProto.newBuilder() .addAllPeers(raftPeerProtos).build()) - .setIndex(index).build(); + .setIndex(index + 1).build(); System.out.println("Generate new LogEntryProto info is:\n"+ generateLogEntryProto); generateLogEntryProto.writeTo(out); } @@ -90,9 +83,8 @@ public int run(CommandLine cl) throws IOException { public String getUsage() { return String.format("%s" + " -%s " - + " -%s " - + " [-%s ]", - getCommandName(), PEER_OPTION_NAME, PATH_OPTION_NAME, LOG_ENTRY_INDEX_OPTION_NAME); + + " -%s ", + getCommandName(), PEER_OPTION_NAME, PATH_OPTION_NAME); } @Override @@ -116,13 +108,6 @@ public Options getOptions() { .hasArg() .required() .desc("The parent path of raft-meta.conf") - .build()) - .addOption( - Option.builder() - .option(LOG_ENTRY_INDEX_OPTION_NAME) - .hasArg() - .desc("The log entry index in raft-meta.conf(If index is specified directly, " - + "the original file will be ignored)") .build()); } From 8a5556d36a3b4a665ca823e49cf7389b66bad2cb Mon Sep 17 00:00:00 2001 From: idellzheng Date: Tue, 15 Aug 2023 20:52:48 +0800 Subject: [PATCH 03/11] Style fix. --- .../ratis/shell/cli/sh/generation/RaftMetaConfCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java index 9158c67fb5..d75dcec4f8 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java @@ -73,7 +73,7 @@ public int run(CommandLine cl) throws IOException { .setConfigurationEntry(RaftProtos.RaftConfigurationProto.newBuilder() .addAllPeers(raftPeerProtos).build()) .setIndex(index + 1).build(); - System.out.println("Generate new LogEntryProto info is:\n"+ generateLogEntryProto); + System.out.println("Generate new LogEntryProto info is:\n" + generateLogEntryProto); generateLogEntryProto.writeTo(out); } return 0; From 24b7d1f259664b0902e66d2be2d85108a6a6eff8 Mon Sep 17 00:00:00 2001 From: idellzheng Date: Wed, 16 Aug 2023 10:35:54 +0800 Subject: [PATCH 04/11] Use GroupCommand as the parent of RaftMetaConfCommand. --- .../cli/sh/command/GenerationCommand.java | 58 ------------------- .../shell/cli/sh/command/GroupCommand.java | 3 +- .../RaftMetaConfCommand.java | 5 +- 3 files changed, 6 insertions(+), 60 deletions(-) delete mode 100644 ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GenerationCommand.java rename ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/{generation => group}/RaftMetaConfCommand.java (96%) diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GenerationCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GenerationCommand.java deleted file mode 100644 index b96dd1f963..0000000000 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GenerationCommand.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.ratis.shell.cli.sh.generation.RaftMetaConfCommand; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.function.Function; - -/** - * Command for generate ratis meta file - */ -public class GenerationCommand extends AbstractParentCommand { - - private static final List> SUB_COMMAND_CONSTRUCTORS - = Collections.unmodifiableList(Arrays.asList(RaftMetaConfCommand::new)); - - /** - * @param context command context - */ - public GenerationCommand(Context context) { - super(context, SUB_COMMAND_CONSTRUCTORS); - } - - @Override - public String getCommandName() { - return "generation"; - } - - @Override - public String getDescription() { - return description(); - } - - /** - * @return command's description - */ - public static String description() { - return "Generate ratis meta data file; see the sub-commands for the details."; - } -} diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java index e1d7ec058f..c9b5c83c17 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java @@ -17,6 +17,7 @@ */ package org.apache.ratis.shell.cli.sh.command; +import org.apache.ratis.shell.cli.sh.group.RaftMetaConfCommand; import org.apache.ratis.shell.cli.sh.group.GroupInfoCommand; import org.apache.ratis.shell.cli.sh.group.GroupListCommand; @@ -32,7 +33,7 @@ public class GroupCommand extends AbstractParentCommand { private static final List> SUB_COMMAND_CONSTRUCTORS = Collections.unmodifiableList(Arrays.asList( - GroupInfoCommand::new, GroupListCommand::new)); + GroupInfoCommand::new, GroupListCommand::new, RaftMetaConfCommand::new)); /** * @param context command context */ diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java similarity index 96% rename from ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java rename to ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java index d75dcec4f8..19ea3ff21e 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/generation/RaftMetaConfCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.ratis.shell.cli.sh.generation; +package org.apache.ratis.shell.cli.sh.group; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; @@ -35,6 +35,9 @@ import java.util.ArrayList; import java.util.List; +/** + * Command for generate a new raft-meta.conf file, which is used to move a raft node to a new node. + */ public class RaftMetaConfCommand extends AbstractRatisCommand { public static final String PATH_OPTION_NAME = "path"; From 74ca5ae6b345c8e9f8ca2de6ff24aa02d568ba28 Mon Sep 17 00:00:00 2001 From: idellzheng Date: Wed, 16 Aug 2023 10:52:24 +0800 Subject: [PATCH 05/11] Modified by review comments. --- .../shell/cli/sh/group/RaftMetaConfCommand.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java index 19ea3ff21e..e2964cb4ba 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java @@ -21,6 +21,7 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.ratis.proto.RaftProtos; +import org.apache.ratis.shell.cli.RaftUtils; import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand; import org.apache.ratis.shell.cli.sh.command.Context; import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; @@ -60,12 +61,15 @@ public String getCommandName() { public int run(CommandLine cl) throws IOException { String peersStr = cl.getOptionValue(PEER_OPTION_NAME); String path = cl.getOptionValue(PATH_OPTION_NAME); + if (peersStr == null || path == null || peersStr.isEmpty() || path.isEmpty()) { + System.out.println("peers or path can't be empty."); + return -1; + } List raftPeerProtos = new ArrayList<>(); - for (String peer : peersStr.split(",")) { - String[] peerInfos = peer.split(":"); - String peerId = peerInfos[0] + "_" + peerInfos[1]; + for (String address : peersStr.split(",")) { + String peerId = RaftUtils.getPeerId(parseInetSocketAddress(address)).toString(); raftPeerProtos.add(RaftProtos.RaftPeerProto.newBuilder() - .setId(ByteString.copyFrom(peerId.getBytes(StandardCharsets.UTF_8))).setAddress(peer) + .setId(ByteString.copyFrom(peerId.getBytes(StandardCharsets.UTF_8))).setAddress(address) .setStartupRole(RaftProtos.RaftPeerRole.FOLLOWER).build()); } try (InputStream in = new FileInputStream(new File(path, RAFT_META_CONF)); From f3c9be256375655951aca7027f08fd0afd1beced Mon Sep 17 00:00:00 2001 From: idellzheng Date: Wed, 16 Aug 2023 11:25:33 +0800 Subject: [PATCH 06/11] Update cli.md --- ratis-docs/src/site/markdown/cli.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ratis-docs/src/site/markdown/cli.md b/ratis-docs/src/site/markdown/cli.md index 75be20c12b..79475d2a28 100644 --- a/ratis-docs/src/site/markdown/cli.md +++ b/ratis-docs/src/site/markdown/cli.md @@ -77,7 +77,7 @@ The following command can be invoked in order to get the basic usage: $ ratis sh Usage: ratis sh [generic options] [election [transfer] [stepDown] [pause] [resume]] - [group [info] [list]] + [group [info] [list] [raftMetaConf]] [peer [add] [remove] [setPriority]] [snapshot [create]] ``` @@ -125,7 +125,7 @@ $ ratis sh election resume -peers [-groupid ] <[-serverAddress ]|[-peerId ]> ``` +### group raftMetaConf +Generate a new raft-meta.conf file, which is used to move a raft node to a new node +``` +$ ratis sh group raftMetaConf -peers -path +``` + ## peer The `peer` command manages ratis cluster peers. It has the following subcommands: From 1d56bccc2ca42f38275d7f93378f1ee8681b3fce Mon Sep 17 00:00:00 2001 From: idellzheng Date: Fri, 18 Aug 2023 10:27:31 +0800 Subject: [PATCH 07/11] Use LocalCommand as the parent of RaftMetaConfCommand. --- ratis-docs/src/site/markdown/cli.md | 22 ++++--- .../shell/cli/sh/command/GroupCommand.java | 3 +- .../shell/cli/sh/command/LocalCommand.java | 59 +++++++++++++++++++ .../{group => local}/RaftMetaConfCommand.java | 7 ++- 4 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java rename ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/{group => local}/RaftMetaConfCommand.java (93%) diff --git a/ratis-docs/src/site/markdown/cli.md b/ratis-docs/src/site/markdown/cli.md index 79475d2a28..60958fc7ed 100644 --- a/ratis-docs/src/site/markdown/cli.md +++ b/ratis-docs/src/site/markdown/cli.md @@ -77,9 +77,10 @@ The following command can be invoked in order to get the basic usage: $ ratis sh Usage: ratis sh [generic options] [election [transfer] [stepDown] [pause] [resume]] - [group [info] [list] [raftMetaConf]] + [group [info] [list]] [peer [add] [remove] [setPriority]] [snapshot [create]] + [local [raftMetaConf]] ``` ## generic options @@ -125,7 +126,7 @@ $ ratis sh election resume -peers [-groupid ] <[-serverAddress ]|[-peerId ]> ``` -### group raftMetaConf -Generate a new raft-meta.conf file, which is used to move a raft node to a new node -``` -$ ratis sh group raftMetaConf -peers -path -``` - ## peer The `peer` command manages ratis cluster peers. It has the following subcommands: @@ -178,3 +173,14 @@ Trigger the specified server take snapshot. ``` $ ratis sh snapshot create -peers -peerId [-groupid ] ``` + +## local +The `local` command is used to process local operation, which no need to connect to ratis server. +It has the following subcommands: +`raftMetaConf` + +### local raftMetaConf +Generate a new raft-meta.conf file based on original raft-meta.conf and new peers, which is used to move a raft node to a new node. +``` +$ ratis sh local raftMetaConf -peers -path +``` diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java index c9b5c83c17..e1d7ec058f 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java @@ -17,7 +17,6 @@ */ package org.apache.ratis.shell.cli.sh.command; -import org.apache.ratis.shell.cli.sh.group.RaftMetaConfCommand; import org.apache.ratis.shell.cli.sh.group.GroupInfoCommand; import org.apache.ratis.shell.cli.sh.group.GroupListCommand; @@ -33,7 +32,7 @@ public class GroupCommand extends AbstractParentCommand { private static final List> SUB_COMMAND_CONSTRUCTORS = Collections.unmodifiableList(Arrays.asList( - GroupInfoCommand::new, GroupListCommand::new, RaftMetaConfCommand::new)); + GroupInfoCommand::new, GroupListCommand::new)); /** * @param context command context */ diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java new file mode 100644 index 0000000000..4743688349 --- /dev/null +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java @@ -0,0 +1,59 @@ +/* + * 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.ratis.shell.cli.sh.local.RaftMetaConfCommand; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.function.Function; + +/** + * Command for local operation, which no need to connect to ratis server + */ +public class LocalCommand extends AbstractParentCommand { + + private static final List> SUB_COMMAND_CONSTRUCTORS + = Collections.unmodifiableList(Arrays.asList(RaftMetaConfCommand::new)); + + /** + * @param context command context + */ + public LocalCommand(Context context) { + super(context, SUB_COMMAND_CONSTRUCTORS); + } + + @Override + public String getCommandName() { + return "local"; + } + + @Override + public String getDescription() { + return description(); + } + + /** + * @return command's description + */ + public static String description() { + return "Command for local operation, which no need to connect to ratis server; " + + "see the sub-commands for the details."; + } +} diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java similarity index 93% rename from ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java rename to ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java index e2964cb4ba..cd4433b742 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/group/RaftMetaConfCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.ratis.shell.cli.sh.group; +package org.apache.ratis.shell.cli.sh.local; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; @@ -37,7 +37,8 @@ import java.util.List; /** - * Command for generate a new raft-meta.conf file, which is used to move a raft node to a new node. + * Command for generate a new raft-meta.conf file based on original raft-meta.conf and new peers, + * which is used to move a raft node to a new node. */ public class RaftMetaConfCommand extends AbstractRatisCommand { public static final String PATH_OPTION_NAME = "path"; @@ -122,7 +123,7 @@ public Options getOptions() { * @return command's description */ public static String description() { - return "Generate a new raft-meta.conf file."; + return "Generate a new raft-meta.conf file based on original raft-meta.conf and new peers."; } } From 723723441bd84bc9c161568892277e9f8855d334 Mon Sep 17 00:00:00 2001 From: idellzheng Date: Fri, 18 Aug 2023 13:24:02 +0800 Subject: [PATCH 08/11] ReConstruct some abstract command. --- .../apache/ratis/shell/cli/sh/RatisShell.java | 7 +++- .../shell/cli/sh/command/AbstractCommand.java | 40 +++++++++++++++++++ .../cli/sh/command/AbstractParentCommand.java | 13 +++++- .../cli/sh/command/AbstractRatisCommand.java | 14 +------ .../shell/cli/sh/command/LocalCommand.java | 11 ++--- .../cli/sh/local/RaftMetaConfCommand.java | 10 ++--- 6 files changed, 66 insertions(+), 29 deletions(-) create mode 100644 ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java index 54f6132105..437a559b5a 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java @@ -77,7 +77,12 @@ private Map loadCommands(String pkgName, Class[] classArgs, if (cls.getPackage().getName().equals(pkgName + ".command") && !Modifier.isAbstract(cls.getModifiers())) { // Only instantiate a concrete class - final Command cmd = ReflectionUtils.newInstance(cls, classArgs, objectArgs); + final Command cmd; + if (cls.getSimpleName().equals("LocalCommand")) { + cmd = ReflectionUtils.newInstance(cls); + } else { + cmd = ReflectionUtils.newInstance(cls, classArgs, objectArgs); + } commandsMap.put(cmd.getCommandName(), cmd); } } diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java new file mode 100644 index 0000000000..0874917c10 --- /dev/null +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java @@ -0,0 +1,40 @@ +/* + * 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.ratis.shell.cli.Command; + +import java.net.InetSocketAddress; + +/** + * The base class for all the ratis shell {@link Command} classes. + */ +public abstract class AbstractCommand implements Command { + + public static InetSocketAddress parseInetSocketAddress(String address) { + try { + final String[] hostPortPair = address.split(":"); + if (hostPortPair.length < 2) { + throw new IllegalArgumentException("Unexpected address format ."); + } + return new InetSocketAddress(hostPortPair[0], Integer.parseInt(hostPortPair[1])); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to parse the server address parameter \"" + address + "\".", e); + } + } +} diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractParentCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractParentCommand.java index 01428dfedf..358711b19c 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractParentCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractParentCommand.java @@ -24,13 +24,13 @@ import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; -public abstract class AbstractParentCommand extends AbstractRatisCommand{ +public abstract class AbstractParentCommand implements Command { private final Map subs; public AbstractParentCommand(Context context, List> subCommandConstructors) { - super(context); this.subs = Collections.unmodifiableMap(subCommandConstructors.stream() .map(constructor -> constructor.apply(context)) .collect(Collectors.toMap(Command::getCommandName, Function.identity(), @@ -39,6 +39,15 @@ public AbstractParentCommand(Context context, List> subCommands) { + this.subs = Collections.unmodifiableMap(subCommands.stream() + .map(subCommand -> subCommand.get()) + .collect(Collectors.toMap(Command::getCommandName, Function.identity(), + (a, b) -> { + throw new IllegalStateException("Found duplicated commands: " + a + " and " + b); + }, LinkedHashMap::new))); + } + @Override public final Map getSubCommands() { return subs; diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java index 74fcbae3dd..bde8a72b73 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java @@ -45,23 +45,11 @@ /** * The base class for all the ratis shell {@link Command} classes. */ -public abstract class AbstractRatisCommand implements Command { +public abstract class AbstractRatisCommand extends AbstractCommand { public static final String PEER_OPTION_NAME = "peers"; public static final String GROUPID_OPTION_NAME = "groupid"; public static final RaftGroupId DEFAULT_RAFT_GROUP_ID = RaftGroupId.randomId(); - public static InetSocketAddress parseInetSocketAddress(String address) { - try { - final String[] hostPortPair = address.split(":"); - if (hostPortPair.length < 2) { - throw new IllegalArgumentException("Unexpected address format ."); - } - return new InetSocketAddress(hostPortPair[0], Integer.parseInt(hostPortPair[1])); - } catch (Exception e) { - throw new IllegalArgumentException("Failed to parse the server address parameter \"" + address + "\".", e); - } - } - /** * Execute a given function with input parameter from the members of a list. * diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java index 4743688349..38a1d6d2f0 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java @@ -22,21 +22,18 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.function.Function; +import java.util.function.Supplier; /** * Command for local operation, which no need to connect to ratis server */ public class LocalCommand extends AbstractParentCommand { - private static final List> SUB_COMMAND_CONSTRUCTORS + private static final List> SUB_COMMAND_CONSTRUCTORS = Collections.unmodifiableList(Arrays.asList(RaftMetaConfCommand::new)); - /** - * @param context command context - */ - public LocalCommand(Context context) { - super(context, SUB_COMMAND_CONSTRUCTORS); + public LocalCommand() { + super(SUB_COMMAND_CONSTRUCTORS); } @Override diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java index cd4433b742..4e1d08ae5c 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java @@ -22,6 +22,7 @@ import org.apache.commons.cli.Options; import org.apache.ratis.proto.RaftProtos; import org.apache.ratis.shell.cli.RaftUtils; +import org.apache.ratis.shell.cli.sh.command.AbstractCommand; import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand; import org.apache.ratis.shell.cli.sh.command.Context; import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; @@ -40,17 +41,14 @@ * Command for generate a new raft-meta.conf file based on original raft-meta.conf and new peers, * which is used to move a raft node to a new node. */ -public class RaftMetaConfCommand extends AbstractRatisCommand { +public class RaftMetaConfCommand extends AbstractCommand { + public static final String PEER_OPTION_NAME = "peers"; public static final String PATH_OPTION_NAME = "path"; private static final String RAFT_META_CONF = "raft-meta.conf"; private static final String NEW_RAFT_META_CONF = "new-raft-meta.conf"; - /** - * @param context command context - */ - public RaftMetaConfCommand(Context context) { - super(context); + public RaftMetaConfCommand() { } @Override From cb6398a7ea729f26c894c94067aa3024d97e8dc5 Mon Sep 17 00:00:00 2001 From: idellzheng Date: Sun, 20 Aug 2023 20:47:59 +0800 Subject: [PATCH 09/11] Modified by review comments. --- .../apache/ratis/shell/cli/sh/RatisShell.java | 7 +--- .../shell/cli/sh/command/AbstractCommand.java | 19 +++++++++ .../cli/sh/command/AbstractParentCommand.java | 12 +----- .../cli/sh/command/AbstractRatisCommand.java | 15 +------ .../shell/cli/sh/command/ElectionCommand.java | 3 +- .../shell/cli/sh/command/GroupCommand.java | 3 +- .../shell/cli/sh/command/LocalCommand.java | 12 ++++-- .../shell/cli/sh/command/PeerCommand.java | 3 +- .../shell/cli/sh/command/SnapshotCommand.java | 3 +- .../cli/sh/local/RaftMetaConfCommand.java | 41 +++++++++++-------- 10 files changed, 61 insertions(+), 57 deletions(-) diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java index 437a559b5a..54f6132105 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/RatisShell.java @@ -77,12 +77,7 @@ private Map loadCommands(String pkgName, Class[] classArgs, if (cls.getPackage().getName().equals(pkgName + ".command") && !Modifier.isAbstract(cls.getModifiers())) { // Only instantiate a concrete class - final Command cmd; - if (cls.getSimpleName().equals("LocalCommand")) { - cmd = ReflectionUtils.newInstance(cls); - } else { - cmd = ReflectionUtils.newInstance(cls, classArgs, objectArgs); - } + final Command cmd = ReflectionUtils.newInstance(cls, classArgs, objectArgs); commandsMap.put(cmd.getCommandName(), cmd); } } diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java index 0874917c10..20a52a80f8 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractCommand.java @@ -19,6 +19,7 @@ import org.apache.ratis.shell.cli.Command; +import java.io.PrintStream; import java.net.InetSocketAddress; /** @@ -26,6 +27,12 @@ */ public abstract class AbstractCommand implements Command { + private final PrintStream printStream; + + protected AbstractCommand(Context context) { + printStream = context.getPrintStream(); + } + public static InetSocketAddress parseInetSocketAddress(String address) { try { final String[] hostPortPair = address.split(":"); @@ -37,4 +44,16 @@ public static InetSocketAddress parseInetSocketAddress(String address) { throw new IllegalArgumentException("Failed to parse the server address parameter \"" + address + "\".", e); } } + + protected PrintStream getPrintStream() { + return printStream; + } + + protected void printf(String format, Object... args) { + printStream.printf(format, args); + } + + protected void println(Object message) { + printStream.println(message); + } } diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractParentCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractParentCommand.java index 358711b19c..ec8401f4f7 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractParentCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractParentCommand.java @@ -24,13 +24,12 @@ import java.util.List; import java.util.Map; import java.util.function.Function; -import java.util.function.Supplier; import java.util.stream.Collectors; public abstract class AbstractParentCommand implements Command { private final Map subs; - public AbstractParentCommand(Context context, List> subCommandConstructors) { + public AbstractParentCommand(Context context, List> subCommandConstructors) { this.subs = Collections.unmodifiableMap(subCommandConstructors.stream() .map(constructor -> constructor.apply(context)) .collect(Collectors.toMap(Command::getCommandName, Function.identity(), @@ -39,15 +38,6 @@ public AbstractParentCommand(Context context, List> subCommands) { - this.subs = Collections.unmodifiableMap(subCommands.stream() - .map(subCommand -> subCommand.get()) - .collect(Collectors.toMap(Command::getCommandName, Function.identity(), - (a, b) -> { - throw new IllegalStateException("Found duplicated commands: " + a + " and " + b); - }, LinkedHashMap::new))); - } - @Override public final Map getSubCommands() { return subs; diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java index bde8a72b73..dde7f42db0 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java @@ -74,12 +74,11 @@ public static K run(Collection list, CheckedFunct return null; } - private final PrintStream printStream; private RaftGroup raftGroup; private GroupInfoReply groupInfoReply; protected AbstractRatisCommand(Context context) { - printStream = context.getPrintStream(); + super(context); } @Override @@ -142,18 +141,6 @@ public Options getOptions() { .addOption(GROUPID_OPTION_NAME, true, "Raft group id"); } - protected PrintStream getPrintStream() { - return printStream; - } - - protected void printf(String format, Object... args) { - printStream.printf(format, args); - } - - protected void println(Object message) { - printStream.println(message); - } - protected RaftGroup getRaftGroup() { return raftGroup; } diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/ElectionCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/ElectionCommand.java index 43d68467f0..054f8c6bb3 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/ElectionCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/ElectionCommand.java @@ -17,6 +17,7 @@ */ package org.apache.ratis.shell.cli.sh.command; +import org.apache.ratis.shell.cli.Command; import org.apache.ratis.shell.cli.sh.election.PauseCommand; import org.apache.ratis.shell.cli.sh.election.ResumeCommand; import org.apache.ratis.shell.cli.sh.election.StepDownCommand; @@ -28,7 +29,7 @@ import java.util.function.Function; public class ElectionCommand extends AbstractParentCommand { - private static final List> SUB_COMMAND_CONSTRUCTORS + private static final List> SUB_COMMAND_CONSTRUCTORS = Collections.unmodifiableList(Arrays.asList( TransferCommand::new, StepDownCommand::new, PauseCommand::new, ResumeCommand::new)); diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java index e1d7ec058f..69953a9824 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/GroupCommand.java @@ -17,6 +17,7 @@ */ package org.apache.ratis.shell.cli.sh.command; +import org.apache.ratis.shell.cli.Command; import org.apache.ratis.shell.cli.sh.group.GroupInfoCommand; import org.apache.ratis.shell.cli.sh.group.GroupListCommand; @@ -30,7 +31,7 @@ */ public class GroupCommand extends AbstractParentCommand { - private static final List> SUB_COMMAND_CONSTRUCTORS + private static final List> SUB_COMMAND_CONSTRUCTORS = Collections.unmodifiableList(Arrays.asList( GroupInfoCommand::new, GroupListCommand::new)); /** diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java index 38a1d6d2f0..4a22b27631 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/LocalCommand.java @@ -17,23 +17,27 @@ */ package org.apache.ratis.shell.cli.sh.command; +import org.apache.ratis.shell.cli.Command; import org.apache.ratis.shell.cli.sh.local.RaftMetaConfCommand; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.function.Supplier; +import java.util.function.Function; /** * Command for local operation, which no need to connect to ratis server */ public class LocalCommand extends AbstractParentCommand { - private static final List> SUB_COMMAND_CONSTRUCTORS + private static final List> SUB_COMMAND_CONSTRUCTORS = Collections.unmodifiableList(Arrays.asList(RaftMetaConfCommand::new)); - public LocalCommand() { - super(SUB_COMMAND_CONSTRUCTORS); + /** + * @param context command context + */ + public LocalCommand(Context context) { + super(context, SUB_COMMAND_CONSTRUCTORS); } @Override diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/PeerCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/PeerCommand.java index 6cb2796547..2394a568fe 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/PeerCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/PeerCommand.java @@ -17,6 +17,7 @@ */ package org.apache.ratis.shell.cli.sh.command; +import org.apache.ratis.shell.cli.Command; import org.apache.ratis.shell.cli.sh.peer.AddCommand; import org.apache.ratis.shell.cli.sh.peer.RemoveCommand; import org.apache.ratis.shell.cli.sh.peer.SetPriorityCommand; @@ -31,7 +32,7 @@ */ public class PeerCommand extends AbstractParentCommand{ - private static final List> SUB_COMMAND_CONSTRUCTORS + private static final List> SUB_COMMAND_CONSTRUCTORS = Collections.unmodifiableList(Arrays.asList(AddCommand::new, RemoveCommand::new, SetPriorityCommand::new)); diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SnapshotCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SnapshotCommand.java index 4dd5842591..34f8786ad6 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SnapshotCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/SnapshotCommand.java @@ -17,6 +17,7 @@ */ package org.apache.ratis.shell.cli.sh.command; +import org.apache.ratis.shell.cli.Command; import org.apache.ratis.shell.cli.sh.snapshot.TakeSnapshotCommand; import java.util.Arrays; @@ -28,7 +29,7 @@ * Command for the ratis snapshot */ public class SnapshotCommand extends AbstractParentCommand { - private static final List> SUB_COMMAND_CONSTRUCTORS + private static final List> SUB_COMMAND_CONSTRUCTORS = Collections.unmodifiableList(Arrays.asList(TakeSnapshotCommand::new)); /** diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java index 4e1d08ae5c..0fccd56b07 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java @@ -20,20 +20,21 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.apache.ratis.proto.RaftProtos; +import org.apache.ratis.proto.RaftProtos.LogEntryProto; +import org.apache.ratis.proto.RaftProtos.RaftConfigurationProto; +import org.apache.ratis.proto.RaftProtos.RaftPeerProto; +import org.apache.ratis.proto.RaftProtos.RaftPeerRole; import org.apache.ratis.shell.cli.RaftUtils; import org.apache.ratis.shell.cli.sh.command.AbstractCommand; -import org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand; import org.apache.ratis.shell.cli.sh.command.Context; import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -48,7 +49,11 @@ public class RaftMetaConfCommand extends AbstractCommand { private static final String RAFT_META_CONF = "raft-meta.conf"; private static final String NEW_RAFT_META_CONF = "new-raft-meta.conf"; - public RaftMetaConfCommand() { + /** + * @param context command context + */ + public RaftMetaConfCommand(Context context) { + super(context); } @Override @@ -61,25 +66,25 @@ public int run(CommandLine cl) throws IOException { String peersStr = cl.getOptionValue(PEER_OPTION_NAME); String path = cl.getOptionValue(PATH_OPTION_NAME); if (peersStr == null || path == null || peersStr.isEmpty() || path.isEmpty()) { - System.out.println("peers or path can't be empty."); + printf("peers or path can't be empty."); return -1; } - List raftPeerProtos = new ArrayList<>(); + List raftPeerProtos = new ArrayList<>(); for (String address : peersStr.split(",")) { String peerId = RaftUtils.getPeerId(parseInetSocketAddress(address)).toString(); - raftPeerProtos.add(RaftProtos.RaftPeerProto.newBuilder() + raftPeerProtos.add(RaftPeerProto.newBuilder() .setId(ByteString.copyFrom(peerId.getBytes(StandardCharsets.UTF_8))).setAddress(address) - .setStartupRole(RaftProtos.RaftPeerRole.FOLLOWER).build()); + .setStartupRole(RaftPeerRole.FOLLOWER).build()); } - try (InputStream in = new FileInputStream(new File(path, RAFT_META_CONF)); - OutputStream out = new FileOutputStream(new File(path, NEW_RAFT_META_CONF))) { - long index = RaftProtos.LogEntryProto.newBuilder().mergeFrom(in).build().getIndex(); - System.out.println("Index in the original file is: " + index); - RaftProtos.LogEntryProto generateLogEntryProto = RaftProtos.LogEntryProto.newBuilder() - .setConfigurationEntry(RaftProtos.RaftConfigurationProto.newBuilder() + try (InputStream in = Files.newInputStream(Paths.get(path, RAFT_META_CONF)); + OutputStream out = Files.newOutputStream(Paths.get(path, NEW_RAFT_META_CONF))) { + long index = LogEntryProto.newBuilder().mergeFrom(in).build().getIndex(); + printf("Index in the original file is: " + index + "\n"); + LogEntryProto generateLogEntryProto = LogEntryProto.newBuilder() + .setConfigurationEntry(RaftConfigurationProto.newBuilder() .addAllPeers(raftPeerProtos).build()) .setIndex(index + 1).build(); - System.out.println("Generate new LogEntryProto info is:\n" + generateLogEntryProto); + printf("Generate new LogEntryProto info is:\n" + generateLogEntryProto); generateLogEntryProto.writeTo(out); } return 0; @@ -89,7 +94,7 @@ public int run(CommandLine cl) throws IOException { public String getUsage() { return String.format("%s" + " -%s " - + " -%s ", + + " -%s ", getCommandName(), PEER_OPTION_NAME, PATH_OPTION_NAME); } From fece8fb9a9af0f426dd9682895254e5de3efb714 Mon Sep 17 00:00:00 2001 From: idellzheng Date: Sun, 20 Aug 2023 22:16:44 +0800 Subject: [PATCH 10/11] Use println instead of printf. --- .../apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java index 0fccd56b07..231c643ac3 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java @@ -79,7 +79,7 @@ public int run(CommandLine cl) throws IOException { try (InputStream in = Files.newInputStream(Paths.get(path, RAFT_META_CONF)); OutputStream out = Files.newOutputStream(Paths.get(path, NEW_RAFT_META_CONF))) { long index = LogEntryProto.newBuilder().mergeFrom(in).build().getIndex(); - printf("Index in the original file is: " + index + "\n"); + println("Index in the original file is: " + index); LogEntryProto generateLogEntryProto = LogEntryProto.newBuilder() .setConfigurationEntry(RaftConfigurationProto.newBuilder() .addAllPeers(raftPeerProtos).build()) From f645ea82443f3dbdcb40ba84b09af1373730ffbd Mon Sep 17 00:00:00 2001 From: idellzheng Date: Mon, 21 Aug 2023 10:19:43 +0800 Subject: [PATCH 11/11] Checkstyle error fix(delete some unuse import). --- .../ratis/shell/cli/sh/command/AbstractRatisCommand.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java index dde7f42db0..1888c0e0ea 100644 --- a/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java +++ b/ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/command/AbstractRatisCommand.java @@ -20,7 +20,6 @@ import org.apache.commons.cli.Option; import org.apache.ratis.protocol.*; import org.apache.ratis.protocol.exceptions.RaftException; -import org.apache.ratis.shell.cli.Command; import org.apache.ratis.shell.cli.RaftUtils; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Options; @@ -34,7 +33,6 @@ import org.apache.ratis.util.function.CheckedFunction; import java.io.IOException; -import java.io.PrintStream; import java.net.InetSocketAddress; import java.util.*; import java.util.function.BiConsumer; @@ -43,7 +41,7 @@ import java.util.stream.Stream; /** - * The base class for all the ratis shell {@link Command} classes. + * The base class for the ratis shell which need to connect to server. */ public abstract class AbstractRatisCommand extends AbstractCommand { public static final String PEER_OPTION_NAME = "peers";