szetszwo commented on code in PR #901: URL: https://github.com/apache/ratis/pull/901#discussion_r1298868774
########## 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) { Review Comment: Let's also move the `Context` and `PrintStream` related code from `AbstractRatisCommand` to `AbstractCommand`. ########## ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.local; + +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.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.util.ArrayList; +import java.util.List; + +/** + * 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 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"; + + public RaftMetaConfCommand() { + } + + @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); + if (peersStr == null || path == null || peersStr.isEmpty() || path.isEmpty()) { + System.out.println("peers or path can't be empty."); + return -1; + } + List<RaftProtos.RaftPeerProto> raftPeerProtos = new ArrayList<>(); + 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(address) + .setStartupRole(RaftProtos.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))) { Review Comment: Use the `Files` api instead. ########## ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.local; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.ratis.proto.RaftProtos; Review Comment: Let's import the inner classes. Then, the code is easier to read. ```java 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; ``` ########## 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<String, Command> subs; public AbstractParentCommand(Context context, List<Function<Context, AbstractRatisCommand>> subCommandConstructors) { Review Comment: Let's change the `Function` to `Function<Context, Command>`. Then, the new `LocalCommand` can also use it. ########## ratis-shell/src/main/java/org/apache/ratis/shell/cli/sh/local/RaftMetaConfCommand.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.local; + +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.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.util.ArrayList; +import java.util.List; + +/** + * 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 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"; + + public RaftMetaConfCommand() { + } + + @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); + if (peersStr == null || path == null || peersStr.isEmpty() || path.isEmpty()) { + System.out.println("peers or path can't be empty."); Review Comment: We should use the `PrintStream` from `Context` instead of `System.out`. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
