SzyWilliam commented on code in PR #941: URL: https://github.com/apache/ratis/pull/941#discussion_r1363558266
########## 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: When adding new peers to the group, a more robust way would be 1. Start the peer with an *EMPTY* group configuration. 2. Call `setConfiguration` to update the membership info. By the time `setConfiguration` returns, the group leader guarantees the membership change succeed (in an atomic way) and all new peers are caught up and can serve requests. There are chances that the newly started peer will be shut down if we provide a full group configuration instead of an EMPTY group: The new peer may start an election *before* the leader receives the `setConfiguration` call (this is especially common if you turn on the fast first election timeout). Under this situation, this peer will be shutdown by the leader since in leader's perspective, the peer does not belong to the group. -- 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]
