private void restartElectionTimer(){ // don't start the election timer while this server is still catching up the logs if(this.catchingUp){ return; } if(this.scheduledElection != null){ this.scheduledElection.cancel(false); } RaftParameters parameters = this.context.getRaftParameters(); int electionTimeout = parameters.getElectionTimeoutLowerBound() + this.random.nextInt(parameters.getElectionTimeoutUpperBound() - parameters.getElectionTimeoutLowerBound() + 1); this.scheduledElection = this.context.getScheduledExecutor().schedule(this.electionTimeoutTask, electionTimeout, TimeUnit.MILLISECONDS); }
private void restartElectionTimer(){ // don't start the election timer while this server is still catching up the logs if(this.catchingUp){ return; } if(this.scheduledElection != null){ this.scheduledElection.cancel(false); } RaftParameters parameters = this.context.getRaftParameters(); int electionTimeout = parameters.getElectionTimeoutLowerBound() + this.random.nextInt(parameters.getElectionTimeoutUpperBound() - parameters.getElectionTimeoutLowerBound() + 1); this.scheduledElection = this.context.getScheduledExecutor().schedule(this.electionTimeoutTask, electionTimeout, TimeUnit.MILLISECONDS); }
private void restartElectionTimer(){ // don't start the election timer while this server is still catching up the logs if(this.catchingUp){ return; } if(this.scheduledElection != null){ this.scheduledElection.cancel(false); } RaftParameters parameters = this.context.getRaftParameters(); int electionTimeout = parameters.getElectionTimeoutLowerBound() + this.random.nextInt(parameters.getElectionTimeoutUpperBound() - parameters.getElectionTimeoutLowerBound() + 1); this.scheduledElection = this.context.getScheduledExecutor().schedule(this.electionTimeoutTask, electionTimeout, TimeUnit.MILLISECONDS); }
private void enableHeartbeatForPeer(PeerServer peer){ peer.enableHeartbeat(true); peer.resumeHeartbeatingSpeed(); peer.setHeartbeatTask(this.context.getScheduledExecutor().schedule(peer.getHeartbeartHandler(), peer.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS)); }
private void enableHeartbeatForPeer(PeerServer peer){ peer.enableHeartbeat(true); peer.resumeHeartbeatingSpeed(); peer.setHeartbeatTask(this.context.getScheduledExecutor().schedule(peer.getHeartbeartHandler(), peer.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS)); }
private void enableHeartbeatForPeer(PeerServer peer){ peer.enableHeartbeat(true); peer.resumeHeartbeatingSpeed(); peer.setHeartbeatTask(this.context.getScheduledExecutor().schedule(peer.getHeartbeartHandler(), peer.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS)); }
@Override public Void call() throws Exception { self.logger.debug("retrying the request %s", request.getMessageType().toString()); server.SendRequest(request).whenCompleteAsync((RaftResponseMessage furtherResponse, Throwable furtherError) -> { self.handleExtendedResponse(furtherResponse, furtherError); }, self.context.getScheduledExecutor()); return null; }}, server.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS); }
@Override public Void call() throws Exception { self.logger.debug("retrying the request %s", request.getMessageType().toString()); server.SendRequest(request).whenCompleteAsync((RaftResponseMessage furtherResponse, Throwable furtherError) -> { self.handleExtendedResponse(furtherResponse, furtherError); }, self.context.getScheduledExecutor()); return null; }}, server.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS); }
@Override public Void call() throws Exception { self.logger.debug("retrying the request %s", request.getMessageType().toString()); server.SendRequest(request).whenCompleteAsync((RaftResponseMessage furtherResponse, Throwable furtherError) -> { self.handleExtendedResponse(furtherResponse, furtherError); }, self.context.getScheduledExecutor()); return null; }}, server.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS); }
private boolean requestAppendEntries(PeerServer peer){ if(peer.makeBusy()){ peer.SendRequest(this.createAppendEntriesRequest(peer)) .whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { try{ handlePeerResponse(response, error); }catch(Throwable err){ this.logger.error("Uncaught exception %s", err.toString()); } }, this.context.getScheduledExecutor()); return true; } this.logger.debug("Server %d is busy, skip the request", peer.getId()); return false; }
private boolean requestAppendEntries(PeerServer peer){ if(peer.makeBusy()){ peer.SendRequest(this.createAppendEntriesRequest(peer)) .whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { try{ handlePeerResponse(response, error); }catch(Throwable err){ this.logger.error("Uncaught exception %s", err.toString()); } }, this.context.getScheduledExecutor()); return true; } this.logger.debug("Server %d is busy, skip the request", peer.getId()); return false; }
public PeerServer(ClusterServer server, RaftContext context, final Consumer<PeerServer> heartbeatConsumer){ this.clusterConfig = server; this.rpcClient = context.getRpcClientFactory().createRpcClient(server.getEndpoint()); this.busyFlag = new AtomicInteger(0); this.pendingCommitFlag = new AtomicInteger(0); this.heartbeatInterval = this.currentHeartbeatInterval = context.getRaftParameters().getHeartbeatInterval(); this.maxHeartbeatInterval = context.getRaftParameters().getMaxHeartbeatInterval(); this.rpcBackoffInterval = context.getRaftParameters().getRpcFailureBackoff(); this.heartbeatTask = null; this.snapshotSyncContext = null; this.nextLogIndex = 1; this.matchedIndex = 0; this.heartbeatEnabled = false; this.executor = context.getScheduledExecutor(); PeerServer self = this; this.heartbeatTimeoutHandler = new Callable<Void>(){ @Override public Void call() throws Exception { heartbeatConsumer.accept(self); return null; }}; }
private boolean requestAppendEntries(PeerServer peer){ if(peer.makeBusy()){ peer.SendRequest(this.createAppendEntriesRequest(peer)) .whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { try{ handlePeerResponse(response, error); }catch(Throwable err){ this.logger.error("Uncaught exception %s", err.toString()); } }, this.context.getScheduledExecutor()); return true; } this.logger.debug("Server %d is busy, skip the request", peer.getId()); return false; }
public PeerServer(ClusterServer server, RaftContext context, final Consumer<PeerServer> heartbeatConsumer){ this.clusterConfig = server; this.rpcClient = context.getRpcClientFactory().createRpcClient(server.getEndpoint()); this.busyFlag = new AtomicInteger(0); this.pendingCommitFlag = new AtomicInteger(0); this.heartbeatInterval = this.currentHeartbeatInterval = context.getRaftParameters().getHeartbeatInterval(); this.maxHeartbeatInterval = context.getRaftParameters().getMaxHeartbeatInterval(); this.rpcBackoffInterval = context.getRaftParameters().getRpcFailureBackoff(); this.heartbeatTask = null; this.snapshotSyncContext = null; this.nextLogIndex = 1; this.matchedIndex = 0; this.heartbeatEnabled = false; this.executor = context.getScheduledExecutor(); PeerServer self = this; this.heartbeatTimeoutHandler = new Callable<Void>(){ @Override public Void call() throws Exception { heartbeatConsumer.accept(self); return null; }}; }
private synchronized void handleHeartbeatTimeout(PeerServer peer){ this.logger.debug("Heartbeat timeout for %d", peer.getId()); if(this.role == ServerRole.Leader){ this.requestAppendEntries(peer); synchronized(peer){ if(peer.isHeartbeatEnabled()){ // Schedule another heartbeat if heartbeat is still enabled peer.setHeartbeatTask(this.context.getScheduledExecutor().schedule(peer.getHeartbeartHandler(), peer.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS)); }else{ this.logger.debug("heartbeat is disabled for peer %d", peer.getId()); } } }else{ this.logger.info("Receive a heartbeat event for %d while no longer as a leader", peer.getId()); } }
private synchronized void handleHeartbeatTimeout(PeerServer peer){ this.logger.debug("Heartbeat timeout for %d", peer.getId()); if(this.role == ServerRole.Leader){ this.requestAppendEntries(peer); synchronized(peer){ if(peer.isHeartbeatEnabled()){ // Schedule another heartbeat if heartbeat is still enabled peer.setHeartbeatTask(this.context.getScheduledExecutor().schedule(peer.getHeartbeartHandler(), peer.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS)); }else{ this.logger.debug("heartbeat is disabled for peer %d", peer.getId()); } } }else{ this.logger.info("Receive a heartbeat event for %d while no longer as a leader", peer.getId()); } }
private synchronized void handleHeartbeatTimeout(PeerServer peer){ this.logger.debug("Heartbeat timeout for %d", peer.getId()); if(this.role == ServerRole.Leader){ this.requestAppendEntries(peer); synchronized(peer){ if(peer.isHeartbeatEnabled()){ // Schedule another heartbeat if heartbeat is still enabled peer.setHeartbeatTask(this.context.getScheduledExecutor().schedule(peer.getHeartbeartHandler(), peer.getCurrentHeartbeatInterval(), TimeUnit.MILLISECONDS)); }else{ this.logger.debug("heartbeat is disabled for peer %d", peer.getId()); } } }else{ this.logger.info("Receive a heartbeat event for %d while no longer as a leader", peer.getId()); } }
private void inviteServerToJoinCluster(){ RaftRequestMessage request = new RaftRequestMessage(); request.setCommitIndex(this.quickCommitIndex); request.setDestination(this.serverToJoin.getId()); request.setSource(this.id); request.setTerm(this.state.getTerm()); request.setMessageType(RaftMessageType.JoinClusterRequest); request.setLastLogIndex(this.logStore.getFirstAvailableIndex() - 1); request.setLogEntries(new LogEntry[] { new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration) }); this.serverToJoin.SendRequest(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { this.handleExtendedResponse(response, error); }, this.context.getScheduledExecutor()); }
private void inviteServerToJoinCluster(){ RaftRequestMessage request = new RaftRequestMessage(); request.setCommitIndex(this.quickCommitIndex); request.setDestination(this.serverToJoin.getId()); request.setSource(this.id); request.setTerm(this.state.getTerm()); request.setMessageType(RaftMessageType.JoinClusterRequest); request.setLastLogIndex(this.logStore.getFirstAvailableIndex() - 1); request.setLogEntries(new LogEntry[] { new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration) }); this.serverToJoin.SendRequest(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { this.handleExtendedResponse(response, error); }, this.context.getScheduledExecutor()); }
private void inviteServerToJoinCluster(){ RaftRequestMessage request = new RaftRequestMessage(); request.setCommitIndex(this.quickCommitIndex); request.setDestination(this.serverToJoin.getId()); request.setSource(this.id); request.setTerm(this.state.getTerm()); request.setMessageType(RaftMessageType.JoinClusterRequest); request.setLastLogIndex(this.logStore.getFirstAvailableIndex() - 1); request.setLogEntries(new LogEntry[] { new LogEntry(this.state.getTerm(), this.config.toBytes(), LogValueType.Configuration) }); this.serverToJoin.SendRequest(request).whenCompleteAsync((RaftResponseMessage response, Throwable error) -> { this.handleExtendedResponse(response, error); }, this.context.getScheduledExecutor()); }