@Override public String getRole() { return raftMachine.currentRole().toString(); }
public synchronized ConsensusOutcome handle( RaftMessages.RaftMessage incomingMessage ) throws IOException { Outcome outcome = currentRole.handler.handle( incomingMessage, state, log ); boolean newLeaderWasElected = leaderChanged( outcome, state.leader() ); state.update( outcome ); // updates to raft log happen within sendMessages( outcome ); handleTimers( outcome ); handleLogShipping( outcome ); driveMembership( outcome ); volatileLeader.set( outcome.getLeader() ); if ( newLeaderWasElected ) { notifyLeaderChanges( outcome ); } return outcome; }
@Override RoleInfo role() { return raft.isLeader() ? RoleInfo.LEADER : RoleInfo.FOLLOWER; } }
@Override public synchronized void handle( RaftMessages.ReceivedInstantClusterIdAwareMessage<?> wrappedMessage ) { try { ConsensusOutcome outcome = raftMachine.handle( wrappedMessage.message() ); if ( outcome.needsFreshSnapshot() ) { Optional<JobScheduler.JobHandle> downloadJob = downloadService.scheduleDownload( catchupAddressProvider ); if ( downloadJob.isPresent() ) { downloadJob.get().waitTermination(); } } else { notifyCommitted( outcome.getCommitIndex() ); } } catch ( Throwable e ) { log.error( "Error handling message", e ); raftMachine.panic(); localDatabase.panic( e ); } }
public void triggerElection( Clock clock ) throws IOException { handle( RaftMessages.ReceivedInstantAwareMessage.of( clock.instant(), new RaftMessages.Timeout.Election( myself ) ) ); }
public synchronized void installSnapshot( CoreSnapshot coreSnapshot ) throws IOException { long snapshotPrevIndex = coreSnapshot.prevIndex(); raftLog.skip( snapshotPrevIndex, coreSnapshot.prevTerm() ); coreState.installSnapshot( coreSnapshot ); raftMachine.installCoreState( coreSnapshot.get( CoreStateType.RAFT_CORE_STATE ) ); coreState.flush( snapshotPrevIndex ); applicationProcess.installSnapshot( coreSnapshot ); notifyAll(); }
public synchronized CoreSnapshot snapshot() throws Exception { applicationProcess.pauseApplier( OPERATION_NAME ); try { long lastApplied = applicationProcess.lastApplied(); long prevTerm = raftLog.readEntryTerm( lastApplied ); CoreSnapshot coreSnapshot = new CoreSnapshot( lastApplied, prevTerm ); coreState.augmentSnapshot( coreSnapshot ); coreSnapshot.add( CoreStateType.RAFT_CORE_STATE, raftMachine.coreState() ); return coreSnapshot; } finally { applicationProcess.resumeApplier( OPERATION_NAME ); } }
raftMachine = new RaftMachine( myself, termState, voteState, raftLog, leaderAvailabilityTimers, outbound, logProvider, raftMembershipManager, logShipping, inFlightCache, config.get( refuse_to_be_leader ),
public void prune() throws IOException { raftMachine.handle( RaftMessages.ReceivedInstantAwareMessage.of( clock.instant(), new RaftMessages.PruneRequest( applicationProcess.lastFlushed() ) ) ); } }
public boolean isLeader() { return consensusModule.raftMachine().currentRole() == Role.LEADER; }
/** * This should be called after the major recovery operations are complete. Before this is called * this instance cannot become a leader (the timers are disabled) and entries will not be cached * in the in-flight map, because the application process is not running and ready to consume them. */ public synchronized void postRecoveryActions() { leaderAvailabilityTimers.start( this::electionTimeout, clock -> handle( RaftMessages.ReceivedInstantAwareMessage.of( clock.instant(), new RaftMessages.Timeout.Heartbeat( myself ) ) ) ); inFlightCache.enable(); }
/** * Role for causal cluster * @return */ private InstanceRole getCoreRole() { try{ Role role = resolveDependency(RaftMachine.class).currentRole(); //avoid "switch" statement because it creates a dependency with Role that doesn't exist in community edition if (role == Role.LEADER) { return InstanceRole.LEADER; } else if (role == Role.FOLLOWER) { return InstanceRole.FOLLOWER; } else if (role == Role.CANDIDATE) { return InstanceRole.CANDIDATE; } else { return InstanceRole.SINGLE; } }catch(NoClassDefFoundError e){ return InstanceRole.SINGLE; } }
@Override public void assertCanWrite() { Role currentRole = raftMachine.currentRole(); if ( !currentRole.equals( Role.LEADER ) ) { throw new WriteOperationsNotAllowedException( format( NOT_LEADER_ERROR_MSG, currentRole ), Status.Cluster.NotALeader ); } } }
public Role getRole() { return getDependencyResolver().resolveDependency( RaftMachine.class ).currentRole(); }