wojiaodoubao commented on code in PR #941: URL: https://github.com/apache/ratis/pull/941#discussion_r1365091466
########## ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java: ########## @@ -0,0 +1,223 @@ +/* + * 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) { + RaftGroup group = newPeerGroup(port); Review Comment: The 'empty conf' does have a chance stuck the cluster into 'infinity election'. I described it in https://issues.apache.org/jira/browse/RATIS-1912. But the possibility of 'infinity election' is much much less than 'newly started peer be shut down'. IMHO 'empty conf' is still the best solution. Let's use the 'empty conf' solution in the example. -- 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]
