public CompletableFuture < Void > anoint ( ) { if ( role . role ( ) == RaftServer . Role . LEADER ) { return CompletableFuture . completedFuture ( null ) ; } CompletableFuture < Void > future = new CompletableFuture <> ( ) ; threadContext . execute ( ( ) -> { // Register a leader election listener to wait for the election of this node. Consumer < RaftMember > electionListener = new Consumer < RaftMember > ( ) { @ Override public void accept ( RaftMember member ) { if ( member . memberId ( ) . equals ( cluster . getMember ( ) . memberId ( ) ) ) { future . complete ( null ) ; } else { future . completeExceptionally ( new RaftException . ProtocolException ( "Failed to transfer leadership" ) ) ; } removeLeaderElectionListener ( this ) ; } } ; addLeaderElectionListener ( electionListener ) ; // If a leader already exists, request a leadership transfer from it. Otherwise, transition to the candidate // state and attempt to get elected. RaftMember member = getCluster ( ) . getMember ( ) ; RaftMember leader = getLeader ( ) ; if ( leader != null ) { protocol . transfer ( leader . memberId ( ) , TransferRequest . builder ( ) . withMember ( member . memberId ( ) ) . build ( ) ) . whenCompleteAsync ( ( response , error ) -> { if ( error != null ) { future . completeExceptionally ( error ) ; } else if ( response . status ( ) == RaftResponse . Status . ERROR ) { future . completeExceptionally ( response . error ( ) . createException ( ) ) ; } else { transition ( RaftServer . Role . CANDIDATE ) ; } } , threadContext ) ; } else { transition ( RaftServer . Role . CANDIDATE ) ; } } ) ; return future ; }
Attempts to become the leader .