SzyWilliam commented on code in PR #941: URL: https://github.com/apache/ratis/pull/941#discussion_r1366651648
########## ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java: ########## @@ -0,0 +1,210 @@ +/* + * 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.examples.membership.server; + +import org.apache.ratis.client.RaftClient; +import org.apache.ratis.conf.Parameters; +import org.apache.ratis.conf.RaftProperties; +import org.apache.ratis.examples.counter.CounterCommand; +import org.apache.ratis.netty.NettyFactory; +import org.apache.ratis.protocol.ClientId; +import org.apache.ratis.protocol.RaftClientReply; +import org.apache.ratis.protocol.RaftGroup; +import org.apache.ratis.protocol.RaftPeer; +import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.util.Preconditions; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.apache.ratis.examples.membership.server.CServer.GROUP_ID; +import static org.apache.ratis.examples.membership.server.CServer.LOCAL_ADDR; + +/** + * An in process raft cluster. Running all servers in a single process. + */ +public class RaftCluster { + private Map<Integer, CServer> members = new HashMap<>(); + + /** + * Start cluster. + * + * @param initPorts the ports of the initial peers. + */ + public void init(Collection<Integer> initPorts) throws IOException { + RaftGroup group = initGroup(initPorts); + for (int port : initPorts) { + CServer server = new CServer(group, peerId(port), port); + server.start(); + members.put(port, server); + } + } + + /** + * Update membership to C_new. + * + * @param newPorts the ports of the C_new peers. + */ + public void update(Collection<Integer> newPorts) throws IOException { + Preconditions.assertTrue(members.size() > 0, "Cluster is empty."); + + Collection<CServer> oldPeers = members.values(); + List<CServer> newPeers = new ArrayList<>(); + List<CServer> peerToStart = new ArrayList<>(); + List<CServer> peerToStop = new ArrayList<>(); + + for (Integer port : newPorts) { + CServer server = members.get(port); + if (server == null) { + // New peer always start with an empty group. + RaftGroup group = RaftGroup.valueOf(GROUP_ID); + server = new CServer(group, peerId(port), port); + peerToStart.add(server); + } + newPeers.add(server); + } + + for (CServer peer : oldPeers) { + if (!newPeers.contains(peer)) { + peerToStop.add(peer); + } + } + + // Step 1: start new peers. + System.out.println("Update membership ...... Step 1: start new peers."); + System.out.println(peersInfo(peerToStart, "Peers_to_start")); + for (CServer server : peerToStart) { + server.start(); + } + + // Step 2: update membership. + System.out.println("Update membership ...... Step 2: update membership from C_old to C_new."); + System.out.println(peersInfo(oldPeers, "C_old")); + System.out.println(peersInfo(newPeers, "C_new")); + if (members.size() > 0) { + RaftClient client = createClient(); + try { + RaftClientReply reply = client.admin().setConfiguration(newPeers.stream() Review Comment: `RaftClient` is also `Closeable`, so we can use try-with-resources here. ########## ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/CServer.java: ########## @@ -0,0 +1,122 @@ +/* + * 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.examples.membership.server; + +import org.apache.ratis.RaftConfigKeys; +import org.apache.ratis.conf.RaftProperties; +import org.apache.ratis.examples.counter.server.CounterStateMachine; +import org.apache.ratis.netty.NettyConfigKeys; +import org.apache.ratis.protocol.RaftGroup; +import org.apache.ratis.protocol.RaftGroupId; +import org.apache.ratis.protocol.RaftPeer; +import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.rpc.SupportedRpcType; +import org.apache.ratis.server.RaftServer; +import org.apache.ratis.server.RaftServerConfigKeys; +import org.apache.ratis.server.storage.RaftStorage; +import org.apache.ratis.thirdparty.com.google.common.base.MoreObjects; +import org.apache.ratis.util.TimeDuration; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Collections; + +/** + * A simple raft server using {@link CounterStateMachine}. + */ +public class CServer implements Closeable { + public static final RaftGroupId GROUP_ID = RaftGroupId.randomId(); + public static final String LOCAL_ADDR = "0.0.0.0"; + + private final RaftServer server; + private final int port; + private final File storageDir; + + public CServer(RaftGroup group, RaftPeerId serverId, int port) throws IOException { + this.storageDir = new File("./" + serverId); + this.port = port; + + final RaftProperties properties = new RaftProperties(); + RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(storageDir)); + RaftConfigKeys.Rpc.setType(properties, SupportedRpcType.NETTY); + NettyConfigKeys.Server.setPort(properties, port); + + // create the counter state machine which holds the counter value. + final CounterStateMachine counterStateMachine = new CounterStateMachine(TimeDuration.ZERO); + + // build the Raft server. + this.server = RaftServer.newBuilder() + .setGroup(group) + .setProperties(properties) + .setServerId(serverId) + .setStateMachine(counterStateMachine) + .setOption(RaftStorage.StartupOption.FORMAT) + .build(); + } + + public void start() throws IOException { + server.start(); + } + + public RaftPeer getPeer() { + return server.getPeer(); + } + + private static final FileVisitor deleter = new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + }; + + @Override + public void close() throws IOException { + server.close(); + Files.walkFileTree(storageDir.toPath(), deleter); Review Comment: We can use simply `FileUtils.deleteFully` here. -- 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]
