/** * Does the log contains the given term and index? Used to check the * consistency between the local log of a follower and the log entries sent * by the leader. */ public boolean contains(TermIndex ti) { if (ti == null) { return false; } TermIndex local = getTermIndex(ti.getIndex()); return ti.equals(local); }
/** * Does the log contains the given term and index? Used to check the * consistency between the local log of a follower and the log entries sent * by the leader. */ public boolean contains(TermIndex ti) { if (ti == null) { return false; } TermIndex local = getTermIndex(ti.getIndex()); return ti.equals(local); }
/** * Update the last committed index. * @param majorityIndex the index that has achieved majority. * @param currentTerm the current term. * @return true if update is applied; otherwise, return false, i.e. no update required. */ public boolean updateLastCommitted(long majorityIndex, long currentTerm) { try(AutoCloseableLock writeLock = writeLock()) { if (lastCommitted.get() < majorityIndex) { // Only update last committed index for current term. See §5.4.2 in // paper for details. final TermIndex entry = getTermIndex(majorityIndex); if (entry != null && entry.getTerm() == currentTerm) { LOG.debug("{}: Updating lastCommitted to {}", selfId, majorityIndex); lastCommitted.set(majorityIndex); return true; } } } return false; }
/** * Update the last committed index. * @param majorityIndex the index that has achieved majority. * @param currentTerm the current term. * @return true if update is applied; otherwise, return false, i.e. no update required. */ public boolean updateLastCommitted(long majorityIndex, long currentTerm) { try(AutoCloseableLock writeLock = writeLock()) { final long oldCommittedIndex = getLastCommittedIndex(); if (oldCommittedIndex < majorityIndex) { // Only update last committed index for current term. See §5.4.2 in // paper for details. final TermIndex entry = getTermIndex(majorityIndex); if (entry != null && entry.getTerm() == currentTerm) { final long newCommitIndex = Math.min(majorityIndex, getLatestFlushedIndex()); if (newCommitIndex > oldCommittedIndex) { commitIndex.updateIncreasingly(newCommitIndex, traceIndexChange); } return true; } } } return false; }
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; }
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; }