private RpcClient getOrCreateRpcClient(){ synchronized(this.rpcClients){ if(this.rpcClients.containsKey(this.leaderId)){ return this.rpcClients.get(this.leaderId); } RpcClient client = this.rpcClientFactory.createRpcClient(getLeaderEndpoint()); this.rpcClients.put(this.leaderId, client); return client; } }
private void tryCurrentLeader(RaftRequestMessage request, CompletableFuture<Boolean> future, int rpcBackoff, int retry){ logger.debug("trying request to %d as current leader", this.leaderId); getOrCreateRpcClient().send(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { if(error == null){ logger.debug("response from remote server, leader: %d, accepted: %s", response.getDestination(), String.valueOf(response.isAccepted())); this.randomLeader = false; this.leaderId = response.getDestination(); tryCurrentLeader(request, future, rpcBackoff, retry); refreshRpcClient(); tryCurrentLeader(request, future, rpcBackoff + 50, retry + 1);
private static void executeAsClient(ClusterConfiguration configuration, ExecutorService executor) throws Exception{ RaftClient client = new RaftClient(new RpcTcpClientFactory(executor), configuration, new Log4jLoggerFactory()); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while(true){ server.setEndpoint(values.get(2)); server.setId(Integer.parseInt(values.get(1))); boolean accepted = client.addServer(server).get(); System.out.println("Accepted: " + String.valueOf(accepted)); continue; for(int i = 1; i <= count; ++i){ String msg = String.format(format, i); boolean accepted = client.appendEntries(new byte[][]{ msg.getBytes() }).get(); System.out.println("Accepted: " + String.valueOf(accepted)); String text = message.substring(6); int serverId = Integer.parseInt(text.trim()); boolean accepted = client.removeServer(serverId).get(); System.out.println("Accepted: " + String.valueOf(accepted)); continue; boolean accepted = client.appendEntries(new byte[][]{ message.getBytes() }).get(); System.out.println("Accepted: " + String.valueOf(accepted));
public CompletableFuture<Boolean> removeServer(int serverId){ if(serverId < 0){ throw new IllegalArgumentException("serverId must be equal or greater than zero"); } ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); buffer.putInt(serverId); LogEntry[] logEntries = new LogEntry[1]; logEntries[0] = new LogEntry(0, buffer.array(), LogValueType.ClusterServer); RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.RemoveServerRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }
private void tryCurrentLeader(RaftRequestMessage request, CompletableFuture<Boolean> future, int rpcBackoff, int retry){ logger.debug("trying request to %d as current leader", this.leaderId); getOrCreateRpcClient().send(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { if(error == null){ logger.debug("response from remote server, leader: %d, accepted: %s", response.getDestination(), String.valueOf(response.isAccepted())); this.randomLeader = false; this.leaderId = response.getDestination(); tryCurrentLeader(request, future, rpcBackoff, retry); refreshRpcClient(); tryCurrentLeader(request, future, rpcBackoff + 50, retry + 1);
private static void executeAsClient(ClusterConfiguration configuration, ExecutorService executor) throws Exception{ RaftClient client = new RaftClient(new RpcTcpClientFactory(executor), configuration, new Log4jLoggerFactory()); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while(true){ server.setEndpoint(values.get(2)); server.setId(Integer.parseInt(values.get(1))); boolean accepted = client.addServer(server).get(); System.out.println("Accepted: " + String.valueOf(accepted)); continue; for(int i = 1; i <= count; ++i){ String msg = String.format(format, i); boolean accepted = client.appendEntries(new byte[][]{ msg.getBytes() }).get(); System.out.println("Accepted: " + String.valueOf(accepted)); String text = message.substring(6); int serverId = Integer.parseInt(text.trim()); boolean accepted = client.removeServer(serverId).get(); System.out.println("Accepted: " + String.valueOf(accepted)); continue; boolean accepted = client.appendEntries(new byte[][]{ message.getBytes() }).get(); System.out.println("Accepted: " + String.valueOf(accepted));
public CompletableFuture<Boolean> removeServer(int serverId){ if(serverId < 0){ throw new IllegalArgumentException("serverId must be equal or greater than zero"); } ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); buffer.putInt(serverId); LogEntry[] logEntries = new LogEntry[1]; logEntries[0] = new LogEntry(0, buffer.array(), LogValueType.ClusterServer); RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.RemoveServerRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }
private void tryCurrentLeader(RaftRequestMessage request, CompletableFuture<Boolean> future, int rpcBackoff, int retry){ logger.debug("trying request to %d as current leader", this.leaderId); getOrCreateRpcClient().send(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { if(error == null){ logger.debug("response from remote server, leader: %d, accepted: %s", response.getDestination(), String.valueOf(response.isAccepted())); this.randomLeader = false; this.leaderId = response.getDestination(); tryCurrentLeader(request, future, rpcBackoff, retry); refreshRpcClient(); tryCurrentLeader(request, future, rpcBackoff + 50, retry + 1);
public CompletableFuture<Boolean> removeServer(int serverId){ if(serverId < 0){ throw new IllegalArgumentException("serverId must be equal or greater than zero"); } ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); buffer.putInt(serverId); LogEntry[] logEntries = new LogEntry[1]; logEntries[0] = new LogEntry(0, buffer.array(), LogValueType.ClusterServer); RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.RemoveServerRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }
private RpcClient refreshRpcClient(){ synchronized(this.rpcClients){ RpcClient client = this.rpcClientFactory.createRpcClient(getLeaderEndpoint()); this.rpcClients.put(this.leaderId, client); return client; } }
public CompletableFuture<Boolean> appendEntries(byte[][] values){ if(values == null || values.length == 0){ throw new IllegalArgumentException("values cannot be null or empty"); } LogEntry[] logEntries = new LogEntry[values.length]; for(int i = 0; i < values.length; ++i){ logEntries[i] = new LogEntry(0, values[i]); } RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.ClientRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }
private RpcClient refreshRpcClient(){ synchronized(this.rpcClients){ RpcClient client = this.rpcClientFactory.createRpcClient(getLeaderEndpoint()); this.rpcClients.put(this.leaderId, client); return client; } }
public CompletableFuture<Boolean> appendEntries(byte[][] values){ if(values == null || values.length == 0){ throw new IllegalArgumentException("values cannot be null or empty"); } LogEntry[] logEntries = new LogEntry[values.length]; for(int i = 0; i < values.length; ++i){ logEntries[i] = new LogEntry(0, values[i]); } RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.ClientRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }
private RpcClient refreshRpcClient(){ synchronized(this.rpcClients){ RpcClient client = this.rpcClientFactory.createRpcClient(getLeaderEndpoint()); this.rpcClients.put(this.leaderId, client); return client; } }
public CompletableFuture<Boolean> appendEntries(byte[][] values){ if(values == null || values.length == 0){ throw new IllegalArgumentException("values cannot be null or empty"); } LogEntry[] logEntries = new LogEntry[values.length]; for(int i = 0; i < values.length; ++i){ logEntries[i] = new LogEntry(0, values[i]); } RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.ClientRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }
private RpcClient getOrCreateRpcClient(){ synchronized(this.rpcClients){ if(this.rpcClients.containsKey(this.leaderId)){ return this.rpcClients.get(this.leaderId); } RpcClient client = this.rpcClientFactory.createRpcClient(getLeaderEndpoint()); this.rpcClients.put(this.leaderId, client); return client; } }
public CompletableFuture<Boolean> addServer(ClusterServer server){ if(server == null){ throw new IllegalArgumentException("server cannot be null"); } LogEntry[] logEntries = new LogEntry[1]; logEntries[0] = new LogEntry(0, server.toBytes(), LogValueType.ClusterServer); RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.AddServerRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }
private RpcClient getOrCreateRpcClient(){ synchronized(this.rpcClients){ if(this.rpcClients.containsKey(this.leaderId)){ return this.rpcClients.get(this.leaderId); } RpcClient client = this.rpcClientFactory.createRpcClient(getLeaderEndpoint()); this.rpcClients.put(this.leaderId, client); return client; } }
public CompletableFuture<Boolean> addServer(ClusterServer server){ if(server == null){ throw new IllegalArgumentException("server cannot be null"); } LogEntry[] logEntries = new LogEntry[1]; logEntries[0] = new LogEntry(0, server.toBytes(), LogValueType.ClusterServer); RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.AddServerRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }
public CompletableFuture<Boolean> addServer(ClusterServer server){ if(server == null){ throw new IllegalArgumentException("server cannot be null"); } LogEntry[] logEntries = new LogEntry[1]; logEntries[0] = new LogEntry(0, server.toBytes(), LogValueType.ClusterServer); RaftRequestMessage request = new RaftRequestMessage(); request.setMessageType(RaftMessageType.AddServerRequest); request.setLogEntries(logEntries); CompletableFuture<Boolean> result = new CompletableFuture<Boolean>(); this.tryCurrentLeader(request, result, 0, 0); return result; }