+ public boolean okToReplicate(final long commitIndex) {
+ if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
+ return false;
+ }
+
+ // Return false if we are trying to send duplicate data before the heartbeat interval. This check includes
+ // also our commitIndex, as followers need to be told of new commitIndex as soon as possible.
+ if (getNextIndex() == lastReplicatedIndex && !hasStaleCommitIndex(commitIndex)
+ && lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS)
+ < context.getConfigParams().getHeartBeatInterval().toMillis()) {
+ return false;
+ }
+
+ resetLastReplicated();
+ return true;
+ }
+
+ private void resetLastReplicated() {
+ lastReplicatedIndex = getNextIndex();
+ if (lastReplicatedStopwatch.isRunning()) {
+ lastReplicatedStopwatch.reset();
+ }
+ lastReplicatedStopwatch.start();
+ }
+
+ /**
+ * Returns the log entry payload data version of the follower.
+ *
+ * @return the payload data version.
+ */
+ public short getPayloadVersion() {
+ return payloadVersion;
+ }
+
+ /**
+ * Sets the payload data version of the follower.
+ *
+ * @param payloadVersion the payload data version.
+ */
+ public void setPayloadVersion(final short payloadVersion) {
+ this.payloadVersion = payloadVersion;
+ }
+
+ /**
+ * Returns the the raft version of the follower.
+ *
+ * @return the raft version of the follower.
+ */
+ public short getRaftVersion() {
+ return raftVersion;
+ }
+
+ /**
+ * Sets the raft version of the follower.
+ *
+ * @param raftVersion the raft version.
+ */
+ public void setRaftVersion(final short raftVersion) {
+ this.raftVersion = raftVersion;
+ }
+
+ /**
+ * Returns the LeaderInstallSnapshotState for the in progress install snapshot.
+ *
+ * @return the LeaderInstallSnapshotState if a snapshot install is in progress, null otherwise.
+ */
+ @Nullable
+ public LeaderInstallSnapshotState getInstallSnapshotState() {
+ return installSnapshotState;
+ }
+
+ /**
+ * Sets the LeaderInstallSnapshotState when an install snapshot is initiated.
+ *
+ * @param state the LeaderInstallSnapshotState
+ */
+ public void setLeaderInstallSnapshotState(@Nonnull final LeaderInstallSnapshotState state) {
+ if (this.installSnapshotState == null) {
+ this.installSnapshotState = Preconditions.checkNotNull(state);
+ }
+ }
+
+ /**
+ * Clears the LeaderInstallSnapshotState when an install snapshot is complete.
+ */
+ public void clearLeaderInstallSnapshotState() {
+ Preconditions.checkState(installSnapshotState != null);
+ installSnapshotState.close();
+ installSnapshotState = null;
+ }
+
+ /**
+ * Sets the index of the log entry whose payload size exceeds the maximum size for a single message and thus
+ * needs to be sliced into smaller chunks.
+ *
+ * @param index the log entry index or NO_INDEX to clear it
+ */
+ public void setSlicedLogEntryIndex(final long index) {
+ slicedLogEntryIndex = index;
+ }
+
+ /**
+ * Return whether or not log entry slicing is currently in progress.
+ *
+ * @return true if slicing is currently in progress, false otherwise
+ */
+ public boolean isLogEntrySlicingInProgress() {
+ return slicedLogEntryIndex != NO_INDEX;
+ }
+
+ public void setNeedsLeaderAddress(final boolean value) {
+ needsLeaderAddress = value;
+ }
+
+ public boolean hasStaleCommitIndex(final long commitIndex) {
+ return sentCommitIndex != commitIndex;
+ }
+
+ public void setSentCommitIndex(final long commitIndex) {
+ sentCommitIndex = commitIndex;
+ }
+
+ @Nullable
+ public String needsLeaderAddress(final String leaderId) {
+ return needsLeaderAddress ? context.getPeerAddress(leaderId) : null;
+ }