Esempio n. 1
0
  @Nonnull
  @Override
  public RequestVoteResponse requestVote(@Nonnull Context ctx, @Nonnull RequestVote request) {

    LOGGER.debug("RequestVote received for term {}", request.getTerm());

    boolean voteGranted = false;

    if (request.getTerm() > log.currentTerm()) {

      log.updateCurrentTerm(request.getTerm());
      ctx.setState(FOLLOWER);
      heartbeatTask.cancel(false);

      Replica candidate = Replica.fromString(request.getCandidateId());
      voteGranted = Voting.shouldVoteFor(log, request);

      if (voteGranted) {
        log.updateVotedFor(Optional.of(candidate));
      }
    }

    return RequestVoteResponse.newBuilder()
        .setTerm(log.currentTerm())
        .setVoteGranted(voteGranted)
        .build();
  }
Esempio n. 2
0
  @Nonnull
  @Override
  public AppendEntriesResponse appendEntries(@Nonnull Context ctx, @Nonnull AppendEntries request) {

    boolean success = false;

    if (request.getTerm() > log.currentTerm()) {
      log.updateCurrentTerm(request.getTerm());
      ctx.setState(FOLLOWER);
      heartbeatTask.cancel(false);
      success = log.append(request);
    }

    return AppendEntriesResponse.newBuilder()
        .setTerm(log.currentTerm())
        .setSuccess(success)
        .build();
  }