Java Code Examples for org.apache.ratis.statemachine.SnapshotInfo#getTermIndex()

The following examples show how to use org.apache.ratis.statemachine.SnapshotInfo#getTermIndex() . 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: LogAppender.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private TermIndex getPrevious(long nextIndex) {
  if (nextIndex == RaftLog.LEAST_VALID_LOG_INDEX) {
    return null;
  }

  final long previousIndex = nextIndex - 1;
  final TermIndex previous = raftLog.getTermIndex(previousIndex);
  if (previous != null) {
    return previous;
  }

  final SnapshotInfo snapshot = server.getState().getLatestSnapshot();
  if (snapshot != null) {
    final TermIndex snapshotTermIndex = snapshot.getTermIndex();
    if (snapshotTermIndex.getIndex() == previousIndex) {
      return snapshotTermIndex;
    }
  }

  return null;
}
 
Example 2
Source File: ServerState.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
boolean isLogUpToDate(TermIndex candidateLastEntry) {
  TermIndex local = log.getLastEntryTermIndex();
  // need to take into account snapshot
  SnapshotInfo snapshot = server.getStateMachine().getLatestSnapshot();
   if (local == null && snapshot == null) {
    return true;
  } else if (candidateLastEntry == null) {
    return false;
  }
  if (local == null || (snapshot != null && snapshot.getIndex() > local.getIndex())) {
    local = snapshot.getTermIndex();
  }
  return local.compareTo(candidateLastEntry) <= 0;
}
 
Example 3
Source File: ServerState.java    From ratis with Apache License 2.0 5 votes vote down vote up
boolean isLogUpToDate(TermIndex candidateLastEntry) {
  TermIndex local = log.getLastEntryTermIndex();
  // need to take into account snapshot
  SnapshotInfo snapshot = server.getStateMachine().getLatestSnapshot();
   if (local == null && snapshot == null) {
    return true;
  } else if (candidateLastEntry == null) {
    return false;
  }
  if (local == null || (snapshot != null && snapshot.getIndex() > local.getIndex())) {
    local = snapshot.getTermIndex();
  }
  return local.compareTo(candidateLastEntry) <= 0;
}
 
Example 4
Source File: LogAppender.java    From ratis with Apache License 2.0 5 votes vote down vote up
private TermIndex getPrevious() {
  TermIndex previous = raftLog.getTermIndex(follower.getNextIndex() - 1);
  if (previous == null) {
    // if previous is null, nextIndex must be equal to the log start
    // index (otherwise we will install snapshot).
    Preconditions.assertTrue(follower.getNextIndex() == raftLog.getStartIndex(),
        "%s: follower's next index %s, local log start index %s",
        this, follower.getNextIndex(), raftLog.getStartIndex());
    SnapshotInfo snapshot = server.getState().getLatestSnapshot();
    previous = snapshot == null ? null : snapshot.getTermIndex();
  }
  return previous;
}
 
Example 5
Source File: LeaderElection.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/**
 * After a peer changes its role to candidate, it invokes this method to
 * send out requestVote rpc to all other peers.
 */
private void askForVotes() throws InterruptedException, IOException {
  final ServerState state = server.getState();
  while (shouldRun()) {
    // one round of requestVotes
    final long electionTerm;
    final RaftConfiguration conf;
    synchronized (server) {
      if (!shouldRun()) {
        break;
      }
      electionTerm = state.initElection();
      conf = state.getRaftConf();
      state.persistMetadata();
    }
    LOG.info("{}: begin an election at term {} for {}", this, electionTerm, conf);

    TermIndex lastEntry = state.getLog().getLastEntryTermIndex();
    if (lastEntry == null) {
      // lastEntry may need to be derived from snapshot
      SnapshotInfo snapshot = state.getLatestSnapshot();
      if (snapshot != null) {
        lastEntry = snapshot.getTermIndex();
      }
    }

    final ResultAndTerm r;
    final Collection<RaftPeer> others = conf.getOtherPeers(server.getId());
    if (others.isEmpty()) {
      r = new ResultAndTerm(Result.PASSED, electionTerm);
    } else {
      final Executor voteExecutor = new Executor(this, others.size());
      try {
        final int submitted = submitRequests(electionTerm, lastEntry, others, voteExecutor);
        r = waitForResults(electionTerm, submitted, conf, voteExecutor);
      } finally {
        voteExecutor.shutdown();
      }
    }

    synchronized (server) {
      if (!shouldRun(electionTerm)) {
        return; // term already passed or this should not run anymore.
      }

      switch (r.result) {
        case PASSED:
          server.changeToLeader();
          return;
        case SHUTDOWN:
          LOG.info("{} received shutdown response when requesting votes.", this);
          server.getProxy().close();
          return;
        case REJECTED:
        case DISCOVERED_A_NEW_TERM:
          final long term = Math.max(r.term, state.getCurrentTerm());
          server.changeToFollowerAndPersistMetadata(term, Result.DISCOVERED_A_NEW_TERM);
          return;
        case TIMEOUT: // should start another election
          continue;
        default: throw new IllegalArgumentException("Unable to process result " + r.result);
      }
    }
  }
}
 
Example 6
Source File: LeaderElection.java    From ratis with Apache License 2.0 4 votes vote down vote up
/**
 * After a peer changes its role to candidate, it invokes this method to
 * send out requestVote rpc to all other peers.
 */
private void askForVotes() throws InterruptedException, IOException {
  final ServerState state = server.getState();
  while (running && server.isCandidate()) {
    // one round of requestVotes
    final long electionTerm;
    synchronized (server) {
      electionTerm = state.initElection();
      server.getState().persistMetadata();
    }
    LOG.info(state.getSelfId() + ": begin an election in Term "
        + electionTerm);

    TermIndex lastEntry = state.getLog().getLastEntryTermIndex();
    if (lastEntry == null) {
      // lastEntry may need to be derived from snapshot
      SnapshotInfo snapshot = state.getLatestSnapshot();
      if (snapshot != null) {
        lastEntry = snapshot.getTermIndex();
      }
    }

    final ResultAndTerm r;
    if (others.isEmpty()) {
      r = new ResultAndTerm(Result.PASSED, electionTerm);
    } else {
      try {
        initExecutor();
        int submitted = submitRequests(electionTerm, lastEntry);
        r = waitForResults(electionTerm, submitted);
      } finally {
        if (executor != null) {
          executor.shutdown();
        }
      }
    }

    synchronized (server) {
      if (electionTerm != state.getCurrentTerm() || !running ||
          !server.isCandidate()) {
        return; // term already passed or no longer a candidate.
      }

      switch (r.result) {
        case PASSED:
          server.changeToLeader();
          return;
        case SHUTDOWN:
          LOG.info("{} received shutdown response when requesting votes.",
              server.getId());
          server.getProxy().close();
          return;
        case REJECTED:
        case DISCOVERED_A_NEW_TERM:
          final long term = r.term > server.getState().getCurrentTerm() ?
              r.term : server.getState().getCurrentTerm();
          server.changeToFollowerAndPersistMetadata(term, Result.DISCOVERED_A_NEW_TERM);
          return;
        case TIMEOUT:
          // should start another election
      }
    }
  }
}