io.atomix.protocols.raft.protocol.VoteResponse Java Examples

The following examples show how to use io.atomix.protocols.raft.protocol.VoteResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FollowerRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
protected VoteResponse handleVote(VoteRequest request) {
  // Reset the heartbeat timeout if we voted for another candidate.
  VoteResponse response = super.handleVote(request);
  if (response.voted()) {
    resetHeartbeatTimeout();
  }
  return response;
}
 
Example #2
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> onVote(VoteRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(VoteResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #3
Source File: LeaderRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> onVote(final VoteRequest request) {
  if (updateTermAndLeader(request.term(), null)) {
    log.debug("Received greater term");
    raft.transition(RaftServer.Role.FOLLOWER);
    return super.onVote(request);
  } else {
    logRequest(request);
    return CompletableFuture.completedFuture(logResponse(VoteResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withVoted(false)
        .build()));
  }
}
 
Example #4
Source File: CandidateRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> onVote(VoteRequest request) {
  raft.checkThread();
  logRequest(request);

  // If the request indicates a term that is greater than the current term then
  // assign that term and leader to the current context and step down as a candidate.
  if (updateTermAndLeader(request.term(), null)) {
    CompletableFuture<VoteResponse> future = super.onVote(request);
    raft.transition(RaftServer.Role.FOLLOWER);
    return future;
  }

  // If the vote request is not for this candidate then reject the vote.
  if (request.candidate() == raft.getCluster().getMember().memberId()) {
    return CompletableFuture.completedFuture(logResponse(VoteResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withVoted(true)
        .build()));
  } else {
    return CompletableFuture.completedFuture(logResponse(VoteResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withVoted(false)
        .build()));
  }
}
 
Example #5
Source File: PassiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> onVote(VoteRequest request) {
  raft.checkThread();
  logRequest(request);
  updateTermAndLeader(request.term(), null);

  return CompletableFuture.completedFuture(logResponse(VoteResponse.builder()
      .withStatus(RaftResponse.Status.ERROR)
      .withError(RaftError.Type.ILLEGAL_MEMBER_STATE, "Cannot request vote from RESERVE member")
      .build()));
}
 
Example #6
Source File: ActiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> onVote(VoteRequest request) {
  raft.checkThread();
  logRequest(request);

  // If the request indicates a term that is greater than the current term then
  // assign that term and leader to the current context.
  boolean transition = updateTermAndLeader(request.term(), null);

  CompletableFuture<VoteResponse> future = CompletableFuture.completedFuture(logResponse(handleVote(request)));
  if (transition) {
    raft.transition(RaftServer.Role.FOLLOWER);
  }
  return future;
}
 
Example #7
Source File: ActiveRole.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Handles a vote request.
 */
protected VoteResponse handleVote(VoteRequest request) {
  // If the request term is not as great as the current context term then don't
  // vote for the candidate. We want to vote for candidates that are at least
  // as up to date as us.
  if (request.term() < raft.getTerm()) {
    log.debug("Rejected {}: candidate's term is less than the current term", request);
    return VoteResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withVoted(false)
        .build();
  }
  // If a leader was already determined for this term then reject the request.
  else if (raft.getLeader() != null) {
    log.debug("Rejected {}: leader already exists", request);
    return VoteResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withVoted(false)
        .build();
  }
  // If the requesting candidate is not a known member of the cluster (to this
  // node) then don't vote for it. Only vote for candidates that we know about.
  else if (!raft.getCluster().getRemoteMemberStates().stream().map(m -> m.getMember().memberId()).collect(Collectors.toSet()).contains(request.candidate())) {
    log.debug("Rejected {}: candidate is not known to the local member", request);
    return VoteResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withVoted(false)
        .build();
  }
  // If no vote has been cast, check the log and cast a vote if necessary.
  else if (raft.getLastVotedFor() == null) {
    if (isLogUpToDate(request.lastLogIndex(), request.lastLogTerm(), request)) {
      raft.setLastVotedFor(request.candidate());
      return VoteResponse.builder()
          .withStatus(RaftResponse.Status.OK)
          .withTerm(raft.getTerm())
          .withVoted(true)
          .build();
    } else {
      return VoteResponse.builder()
          .withStatus(RaftResponse.Status.OK)
          .withTerm(raft.getTerm())
          .withVoted(false)
          .build();
    }
  }
  // If we already voted for the requesting server, respond successfully.
  else if (raft.getLastVotedFor() == request.candidate()) {
    log.debug("Accepted {}: already voted for {}", request, raft.getCluster().getMember(raft.getLastVotedFor()).memberId());
    return VoteResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withVoted(true)
        .build();
  }
  // In this case, we've already voted for someone else.
  else {
    log.debug("Rejected {}: already voted for {}", request, raft.getCluster().getMember(raft.getLastVotedFor()).memberId());
    return VoteResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withVoted(false)
        .build();
  }
}
 
Example #8
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerVoteHandler(Function<VoteRequest, CompletableFuture<VoteResponse>> handler) {
  this.voteHandler = handler;
}
 
Example #9
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> vote(MemberId memberId, VoteRequest request) {
  return getServer(memberId).thenCompose(listener -> listener.vote(encode(request))).thenApply(this::decode);
}
 
Example #10
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerVoteHandler(Function<VoteRequest, CompletableFuture<VoteResponse>> handler) {
  registerHandler("vote", handler);
}
 
Example #11
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> vote(MemberId memberId, VoteRequest request) {
  return sendAndReceive(memberId, "vote", request);
}
 
Example #12
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> vote(MemberId memberId, VoteRequest request) {
  return sendAndReceive(memberId, "vote", request);
}
 
Example #13
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerVoteHandler(Function<VoteRequest, CompletableFuture<VoteResponse>> handler) {
  clusterCommunicator.subscribe(context.voteSubject, serializer::decode, handler, serializer::encode);
}
 
Example #14
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> vote(MemberId memberId, VoteRequest request) {
  return sendAndReceive(context.voteSubject, request, memberId);
}
 
Example #15
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerVoteHandler(Function<VoteRequest,
    CompletableFuture<VoteResponse>> handler) {
  this.voteHandler = handler;
}
 
Example #16
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> vote(MemberId memberId, VoteRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.vote(encode(request))).thenApply(this::decode);
}
 
Example #17
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerVoteHandler(Function<VoteRequest,
    CompletableFuture<VoteResponse>> handler) {
  registerHandler("vote", handler);
}
 
Example #18
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> vote(MemberId memberId, VoteRequest request) {
  return sendAndReceive(memberId, "vote", request);
}
 
Example #19
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerVoteHandler(Function<VoteRequest,
    CompletableFuture<VoteResponse>> handler) {
  this.voteHandler = handler;
}
 
Example #20
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<VoteResponse> vote(MemberId memberId, VoteRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.vote(encode(request))).thenApply(this::decode);
}
 
Example #21
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerVoteHandler(Function<VoteRequest,
    CompletableFuture<VoteResponse>> handler) {
  registerHandler("vote", handler);
}
 
Example #22
Source File: RaftRole.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Handles a vote request.
 *
 * @param request The request to handle.
 * @return A completable future to be completed with the request response.
 */
CompletableFuture<VoteResponse> onVote(VoteRequest request);