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

The following examples show how to use io.atomix.protocols.raft.protocol.PollRequest. 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: LeaderRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<PollResponse> onPoll(final PollRequest request) {
  logRequest(request);

  // If a member sends a PollRequest to the leader, that indicates that it likely healed from
  // a network partition and may have had its status set to UNAVAILABLE by the leader. In order
  // to ensure heartbeats are immediately stored to the member, update its status if necessary.
  RaftMemberContext member = raft.getCluster().getMemberState(request.candidate());
  if (member != null) {
    member.resetFailureCount();
  }

  return CompletableFuture.completedFuture(logResponse(PollResponse.builder()
      .withStatus(RaftResponse.Status.OK)
      .withTerm(raft.getTerm())
      .withAccepted(false)
      .build()));
}
 
Example #2
Source File: ActiveRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a poll request.
 */
protected PollResponse handlePoll(PollRequest 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 PollResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withAccepted(false)
        .build();
  } else if (isLogUpToDate(request.lastLogIndex(), request.lastLogTerm(), request)) {
    return PollResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withAccepted(true)
        .build();
  } else {
    return PollResponse.builder()
        .withStatus(RaftResponse.Status.OK)
        .withTerm(raft.getTerm())
        .withAccepted(false)
        .build();
  }
}
 
Example #3
Source File: InactiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<PollResponse> onPoll(PollRequest request) {
  logRequest(request);
  return Futures.completedFuture(logResponse(PollResponse.builder()
      .withStatus(Status.ERROR)
      .withError(RaftError.Type.UNAVAILABLE)
      .build()));
}
 
Example #4
Source File: PassiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<PollResponse> onPoll(PollRequest request) {
  raft.checkThread();
  logRequest(request);

  return CompletableFuture.completedFuture(logResponse(PollResponse.builder()
      .withStatus(RaftResponse.Status.ERROR)
      .withError(RaftError.Type.ILLEGAL_MEMBER_STATE, "Cannot poll RESERVE member")
      .build()));
}
 
Example #5
Source File: ActiveRole.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<PollResponse> onPoll(PollRequest request) {
  raft.checkThread();
  logRequest(request);
  updateTermAndLeader(request.term(), null);
  return CompletableFuture.completedFuture(logResponse(handlePoll(request)));
}
 
Example #6
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPollHandler(Function<PollRequest, CompletableFuture<PollResponse>> handler) {
  this.pollHandler = handler;
}
 
Example #7
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<PollResponse> poll(MemberId memberId, PollRequest request) {
  return sendAndReceive(memberId, "poll", request);
}
 
Example #8
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<PollResponse> poll(MemberId memberId, PollRequest request) {
  return getServer(memberId).thenCompose(listener -> listener.poll(encode(request))).thenApply(this::decode);
}
 
Example #9
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPollHandler(Function<PollRequest, CompletableFuture<PollResponse>> handler) {
  registerHandler("poll", handler);
}
 
Example #10
Source File: RaftServerMessagingProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<PollResponse> poll(MemberId memberId, PollRequest request) {
  return sendAndReceive(memberId, "poll", request);
}
 
Example #11
Source File: FollowerRole.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Polls all members of the cluster to determine whether this member should transition to the CANDIDATE state.
 */
private void sendPollRequests() {
  // Set a new timer within which other nodes must respond in order for this node to transition to candidate.
  heartbeatTimer = raft.getThreadContext().schedule(raft.getElectionTimeout(), () -> {
    log.debug("Failed to poll a majority of the cluster in {}", raft.getElectionTimeout());
    resetHeartbeatTimeout();
  });

  // Create a quorum that will track the number of nodes that have responded to the poll request.
  final AtomicBoolean complete = new AtomicBoolean();
  final Set<DefaultRaftMember> votingMembers = raft.getCluster().getActiveMemberStates()
      .stream()
      .map(RaftMemberContext::getMember)
      .collect(Collectors.toSet());

  // If there are no other members in the cluster, immediately transition to leader.
  if (votingMembers.isEmpty()) {
    raft.transition(RaftServer.Role.CANDIDATE);
    return;
  }

  final Quorum quorum = new Quorum(raft.getCluster().getQuorum(), (elected) -> {
    // If a majority of the cluster indicated they would vote for us then transition to candidate.
    complete.set(true);
    if (raft.getLeader() == null && elected) {
      raft.transition(RaftServer.Role.CANDIDATE);
    } else {
      resetHeartbeatTimeout();
    }
  });

  // First, load the last log entry to get its term. We load the entry
  // by its index since the index is required by the protocol.
  final Indexed<RaftLogEntry> lastEntry = raft.getLogWriter().getLastEntry();

  final long lastTerm;
  if (lastEntry != null) {
    lastTerm = lastEntry.entry().term();
  } else {
    lastTerm = 0;
  }

  log.debug("Polling members {}", votingMembers);

  // Once we got the last log term, iterate through each current member
  // of the cluster and vote each member for a vote.
  for (DefaultRaftMember member : votingMembers) {
    log.debug("Polling {} for next term {}", member, raft.getTerm() + 1);
    PollRequest request = PollRequest.builder()
        .withTerm(raft.getTerm())
        .withCandidate(raft.getCluster().getMember().memberId())
        .withLastLogIndex(lastEntry != null ? lastEntry.index() : 0)
        .withLastLogTerm(lastTerm)
        .build();
    raft.getProtocol().poll(member.memberId(), request).whenCompleteAsync((response, error) -> {
      raft.checkThread();
      if (isRunning() && !complete.get()) {
        if (error != null) {
          log.warn("{}", error.getMessage());
          quorum.fail();
        } else {
          if (response.term() > raft.getTerm()) {
            raft.setTerm(response.term());
          }

          if (!response.accepted()) {
            log.debug("Received rejected poll from {}", member);
            quorum.fail();
          } else if (response.term() != raft.getTerm()) {
            log.debug("Received accepted poll for a different term from {}", member);
            quorum.fail();
          } else {
            log.debug("Received accepted poll from {}", member);
            quorum.succeed();
          }
        }
      }
    }, raft.getThreadContext());
  }
}
 
Example #12
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPollHandler(Function<PollRequest, CompletableFuture<PollResponse>> handler) {
  clusterCommunicator.subscribe(context.pollSubject, serializer::decode, handler, serializer::encode);
}
 
Example #13
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<PollResponse> poll(MemberId memberId, PollRequest request) {
  return sendAndReceive(context.pollSubject, request, memberId);
}
 
Example #14
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPollHandler(Function<PollRequest,
    CompletableFuture<PollResponse>> handler) {
  this.pollHandler = handler;
}
 
Example #15
Source File: LocalRaftServerProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<PollResponse> poll(MemberId memberId, PollRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.poll(encode(request))).thenApply(this::decode);
}
 
Example #16
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPollHandler(Function<PollRequest,
    CompletableFuture<PollResponse>> handler) {
  registerHandler("poll", handler);
}
 
Example #17
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<PollResponse> poll(MemberId memberId, PollRequest request) {
  return sendAndReceive(memberId, "poll", request);
}
 
Example #18
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPollHandler(Function<PollRequest,
    CompletableFuture<PollResponse>> handler) {
  this.pollHandler = handler;
}
 
Example #19
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<PollResponse> poll(MemberId memberId, PollRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.poll(encode(request))).thenApply(this::decode);
}
 
Example #20
Source File: RaftServerMessagingProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public void registerPollHandler(Function<PollRequest,
    CompletableFuture<PollResponse>> handler) {
  registerHandler("poll", handler);
}
 
Example #21
Source File: RaftRole.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Handles a poll request.
 *
 * @param request The request to handle.
 * @return A completable future to be completed with the request response.
 */
CompletableFuture<PollResponse> onPoll(PollRequest request);