/** @return a {@link RaftServer} object. */ public RaftServer build() throws IOException { return ServerImplUtils.newRaftServer( serverId, group, Objects.requireNonNull(stateMachineRegistry , "Neither 'stateMachine' nor 'setStateMachineRegistry' is initialized."), Objects.requireNonNull(properties, "The 'properties' field is not initialized."), parameters); }
/** Create a new {@link TermIndex} instance. */ static TermIndex newTermIndex(long term, long index) { return ServerImplUtils.newTermIndex(term, index); }
/** @return a {@link RaftServer} object. */ public RaftServer build() throws IOException { return ServerImplUtils.newRaftServer( serverId, group, Objects.requireNonNull(stateMachine, "The 'stateMachine' is not initialized."), Objects.requireNonNull(properties, "The 'properties' field is not initialized."), parameters); }
/** Create a new {@link TermIndex} instance. */ static TermIndex newTermIndex(long term, long index) { return ServerImplUtils.newTermIndex(term, index); }
/** For the case that all {@link RaftServerImpl} objects share the same {@link StateMachine}. */ public static RaftServerProxy newRaftServer( RaftPeerId id, RaftGroup group, StateMachine.Registry stateMachineRegistry, RaftProperties properties, Parameters parameters) throws IOException { RaftServerProxy.LOG.debug("newRaftServer: {}, {}", id, group); final RaftServerProxy proxy = newRaftServer(id, stateMachineRegistry, properties, parameters); proxy.initGroups(group); return proxy; }
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine stateMachine, RaftGroup group, RaftProperties properties) throws IOException { GrpcConfigKeys.Server.setPort(properties, getPort(id, group)); return ServerImplUtils.newRaftServer(id, group, stateMachine, properties, null); }
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry , RaftGroup group, RaftProperties properties) throws IOException { NettyConfigKeys.Server.setPort(properties, getPort(id, group)); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, null); }
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine stateMachine, RaftGroup group, RaftProperties properties) throws IOException { serverRequestReply.addPeer(id); client2serverRequestReply.addPeer(id); return ServerImplUtils.newRaftServer(id, group, stateMachine, properties, parameters); }
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry, RaftGroup group, RaftProperties properties) throws IOException { GrpcConfigKeys.Server.setPort(properties, getPort(id, group)); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, null); }
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry , RaftGroup group, RaftProperties properties) throws IOException { serverRequestReply.addPeer(id); client2serverRequestReply.addPeer(id); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, parameters); }
public static RaftServerProxy newRaftServer( RaftPeerId id, RaftGroup group, StateMachine.Registry stateMachineRegistry, RaftProperties properties, Parameters parameters) throws IOException { final RaftServerProxy proxy; try { // attempt multiple times to avoid temporary bind exception proxy = JavaUtils.attempt( () -> new RaftServerProxy(id, stateMachineRegistry, group, properties, parameters), 5, 500L, "new RaftServerProxy", RaftServerProxy.LOG); } catch (InterruptedException e) { throw IOUtils.toInterruptedIOException( "Interrupted when creating RaftServer " + id + ", " + group, e); } catch (IOException e) { throw new IOException("Failed to create RaftServer " + id + ", " + group, e); } // add peers into rpc service if (!group.getPeers().isEmpty()) { proxy.getServerRpc().addPeers(group.getPeers()); } return proxy; }
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine stateMachine, RaftGroup group, RaftProperties properties) throws IOException { final Configuration hconf = new Configuration(hadoopConf); final String address = "0.0.0.0:" + getPort(id, group); HadoopConfigKeys.Ipc.setAddress(hconf, address); return ServerImplUtils.newRaftServer(id, group, stateMachine, properties, HadoopFactory.newRaftParameters(hconf)); }
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry , RaftGroup group, RaftProperties properties) throws IOException { final Configuration hconf = new Configuration(hadoopConf); final String address = "0.0.0.0:" + getPort(id, group); HadoopConfigKeys.Ipc.setAddress(hconf, address); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, HadoopFactory.newRaftParameters(hconf)); }
@Test public void testServerRestartOnException() throws Exception { RaftProperties properties = new RaftProperties(); final MiniRaftClusterWithGRpc cluster = MiniRaftClusterWithGRpc.FACTORY.newCluster(1, properties); cluster.start(); RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId(); GrpcConfigKeys.Server.setPort(properties, cluster.getLeader().getServerRpc().getInetSocketAddress().getPort()); // Create a raft server proxy with server rpc bound to a different address // compared to leader. This helps in locking the raft storage directory to // be used by next raft server proxy instance. final StateMachine stateMachine = cluster.getLeader().getStateMachine(); ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), stateMachine, properties, null); // Close the server rpc for leader so that new raft server can be bound to it. cluster.getLeader().getServerRpc().close(); // Create a raft server proxy with server rpc bound to same address as // the leader. This step would fail as the raft storage has been locked by // the raft server proxy created earlier. Raft server proxy should close // the rpc server on failure. testFailureCase("start a new server with the same address", () -> ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), stateMachine, properties, null), IOException.class, IOException.class, OverlappingFileLockException.class); // Try to start a raft server rpc at the leader address. cluster.getServer(leaderId).getFactory().newRaftServerRpc(cluster.getServer(leaderId)); } }
@Test public void testServerRestartOnException() throws Exception { RaftProperties properties = new RaftProperties(); final MiniRaftClusterWithGrpc cluster = MiniRaftClusterWithGrpc.FACTORY.newCluster(1, properties); cluster.start(); RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId(); GrpcConfigKeys.Server.setPort(properties, cluster.getLeader().getServerRpc().getInetSocketAddress().getPort()); // Create a raft server proxy with server rpc bound to a different address // compared to leader. This helps in locking the raft storage directory to // be used by next raft server proxy instance. final StateMachine stateMachine = cluster.getLeader().getStateMachine(); ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, properties, null); // Close the server rpc for leader so that new raft server can be bound to it. cluster.getLeader().getServerRpc().close(); // Create a raft server proxy with server rpc bound to same address as // the leader. This step would fail as the raft storage has been locked by // the raft server proxy created earlier. Raft server proxy should close // the rpc server on failure. testFailureCase("start a new server with the same address", () -> ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, properties, null).start(), IOException.class, OverlappingFileLockException.class); // Try to start a raft server rpc at the leader address. cluster.getServer(leaderId).getFactory().newRaftServerRpc(cluster.getServer(leaderId)); } }