RaftServerProxy(RaftPeerId id, StateMachine.Registry stateMachineRegistry, RaftGroup group, RaftProperties properties, Parameters parameters) throws IOException { this.properties = properties; this.stateMachineRegistry = stateMachineRegistry; final RpcType rpcType = RaftConfigKeys.Rpc.type(properties); this.factory = ServerFactory.cast(rpcType.newFactory(parameters)); this.serverRpc = factory.newRaftServerRpc(this); this.id = id != null? id: RaftPeerId.valueOf(getIdStringFrom(serverRpc)); try { this.impl = CompletableFuture.completedFuture(initImpl(group)); } catch (IOException ioe) { try { serverRpc.close(); } catch (IOException closeIoe) { LOG.warn(this.id + ": Failed to close server rpc.", closeIoe); ioe.addSuppressed(closeIoe); } finally { throw ioe; } } }
@Override public void run() throws Exception { int raftSegmentPreallocatedSize = 1024 * 1024 * 1024; RaftProperties raftProperties = new RaftProperties(); RaftConfigKeys.Rpc.setType(raftProperties, SupportedRpcType.GRPC); GrpcConfigKeys.setMessageSizeMax(raftProperties, SizeInBytes.valueOf(raftSegmentPreallocatedSize)); RaftServerConfigKeys.Log.Appender.setBufferByteLimit(raftProperties, SizeInBytes.valueOf(raftSegmentPreallocatedSize)); RaftServerConfigKeys.Log.setWriteBufferSize(raftProperties, SizeInBytes.valueOf(raftSegmentPreallocatedSize)); RaftServerConfigKeys.Log.setPreallocatedSize(raftProperties, SizeInBytes.valueOf(raftSegmentPreallocatedSize)); RaftServerConfigKeys.Log.setSegmentSizeMax(raftProperties, SizeInBytes.valueOf(1 * 1024 * 1024 * 1024)); RaftServerConfigKeys.Log.setMaxCachedSegmentNum(raftProperties, 2); RaftClientConfigKeys.Rpc.setRequestTimeout(raftProperties, TimeDuration.valueOf(50000, TimeUnit.MILLISECONDS)); RaftClientConfigKeys.Async.setSchedulerThreads(raftProperties, 10); RaftClientConfigKeys.Async.setMaxOutstandingRequests(raftProperties, 1000); final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)), parsePeers(peers)); RaftClient.Builder builder = RaftClient.newBuilder().setProperties(raftProperties); builder.setRaftGroup(raftGroup); builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties)); RaftClient client = builder.build(); operation(client); }
@Override public MiniRaftClusterWithSimulatedRpc newCluster( String[] ids, RaftProperties prop) { RaftConfigKeys.Rpc.setType(prop, SimulatedRpc.INSTANCE); if (ThreadLocalRandom.current().nextBoolean()) { // turn off simulate latency half of the times. prop.setInt(SimulatedRequestReply.SIMULATE_LATENCY_KEY, 0); } final int simulateLatencyMs = ConfUtils.getInt(prop::getInt, SimulatedRequestReply.SIMULATE_LATENCY_KEY, SimulatedRequestReply.SIMULATE_LATENCY_DEFAULT, requireMin(0)); final SimulatedRequestReply<RaftServerRequest, RaftServerReply> serverRequestReply = new SimulatedRequestReply<>(simulateLatencyMs); final SimulatedClientRpc client2serverRequestReply = new SimulatedClientRpc(simulateLatencyMs); return new MiniRaftClusterWithSimulatedRpc(ids, prop, serverRequestReply, client2serverRequestReply); } };
/** @return a {@link RaftClient} object. */ public RaftClient build() { if (clientId == null) { clientId = ClientId.randomId(); } if (properties != null) { if (clientRpc == null) { final RpcType rpcType = RaftConfigKeys.Rpc.type(properties, LOG::debug); final ClientFactory factory = ClientFactory.cast(rpcType.newFactory(parameters)); clientRpc = factory.newRaftClientRpc(clientId, properties); } } return ClientImplUtils.newRaftClient(clientId, Objects.requireNonNull(group, "The 'group' field is not initialized."), leaderId, Objects.requireNonNull(clientRpc, "The 'clientRpc' field is not initialized."), properties, retryPolicy); }
/** @return a {@link RaftClient} object. */ public RaftClient build() { if (clientId == null) { clientId = ClientId.randomId(); } if (properties != null) { if (clientRpc == null) { final RpcType rpcType = RaftConfigKeys.Rpc.type(properties); final ClientFactory factory = ClientFactory.cast(rpcType.newFactory(parameters)); clientRpc = factory.newRaftClientRpc(clientId, properties); } } return ClientImplUtils.newRaftClient(clientId, Objects.requireNonNull(group, "The 'group' field is not initialized."), leaderId, Objects.requireNonNull(clientRpc, "The 'clientRpc' field is not initialized."), properties); }
@Override public MiniRaftClusterWithSimulatedRpc newCluster( String[] ids, RaftProperties prop) { RaftConfigKeys.Rpc.setType(prop, SimulatedRpc.INSTANCE); if (ThreadLocalRandom.current().nextBoolean()) { // turn off simulate latency half of the times. prop.setInt(SimulatedRequestReply.SIMULATE_LATENCY_KEY, 0); } final int simulateLatencyMs = ConfUtils.getInt(prop::getInt, SimulatedRequestReply.SIMULATE_LATENCY_KEY, SimulatedRequestReply.SIMULATE_LATENCY_DEFAULT, LOG::info, requireMin(0)); final SimulatedRequestReply<RaftServerRequest, RaftServerReply> serverRequestReply = new SimulatedRequestReply<>(simulateLatencyMs); final SimulatedClientRpc client2serverRequestReply = new SimulatedClientRpc(simulateLatencyMs); return new MiniRaftClusterWithSimulatedRpc(ids, prop, serverRequestReply, client2serverRequestReply); } };
@Override public MiniRaftClusterWithGrpc newCluster( String[] ids, RaftProperties prop) { RaftConfigKeys.Rpc.setType(prop, SupportedRpcType.GRPC); return new MiniRaftClusterWithGrpc(ids, prop); } };
@Override public MiniRaftClusterWithNetty newCluster(String[] ids, RaftProperties prop) { RaftConfigKeys.Rpc.setType(prop, SupportedRpcType.NETTY); return new MiniRaftClusterWithNetty(ids, prop); } };
public MiniRaftClusterWithHadoopRpc newCluster( String[] ids, RaftProperties prop, Configuration conf) { RaftConfigKeys.Rpc.setType(prop, SupportedRpcType.HADOOP); HadoopConfigKeys.Ipc.setAddress(conf, "0.0.0.0:0"); return new MiniRaftClusterWithHadoopRpc(ids, prop, conf); } }
@Override public MiniRaftClusterWithGRpc newCluster( String[] ids, RaftProperties prop) { RaftConfigKeys.Rpc.setType(prop, SupportedRpcType.GRPC); return new MiniRaftClusterWithGRpc(ids, prop); } };
public MiniRaftClusterWithHadoopRpc newCluster( String[] ids, RaftProperties prop, Configuration conf) { RaftConfigKeys.Rpc.setType(prop, SupportedRpcType.HADOOP); HadoopConfigKeys.Ipc.setAddress(conf, "0.0.0.0:0"); return new MiniRaftClusterWithHadoopRpc(ids, prop, conf); } }
RaftServerProxy(RaftPeerId id, StateMachine.Registry stateMachineRegistry, RaftProperties properties, Parameters parameters) { this.properties = properties; this.stateMachineRegistry = stateMachineRegistry; final RpcType rpcType = RaftConfigKeys.Rpc.type(properties, LOG::info); this.factory = ServerFactory.cast(rpcType.newFactory(parameters)); this.serverRpc = factory.newRaftServerRpc(this); this.id = id != null? id: RaftPeerId.valueOf(getIdStringFrom(serverRpc)); this.lifeCycle = new LifeCycle(this.id + "-" + getClass().getSimpleName()); }