2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.raft;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Stopwatch;
14 import java.util.concurrent.TimeUnit;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.cluster.raft.behaviors.LeaderInstallSnapshotState;
20 * The state of the followers log as known by the Leader.
23 * @author Thomas Pantelis
25 public final class FollowerLogInformation {
26 public static final long NO_INDEX = -1;
28 private final Stopwatch stopwatch = Stopwatch.createUnstarted();
30 private final RaftActorContext context;
32 private long nextIndex;
34 private long matchIndex;
36 private long lastReplicatedIndex = -1L;
38 private long sentCommitIndex = -1L;
40 private final Stopwatch lastReplicatedStopwatch = Stopwatch.createUnstarted();
42 private short payloadVersion = -1;
44 // Assume the HELIUM_VERSION version initially for backwards compatibility until we obtain the follower's
45 // actual version via AppendEntriesReply. Although we no longer support the Helium version, a pre-Boron
46 // follower will not have the version field in AppendEntriesReply so it will be set to 0 which is
48 private short raftVersion = RaftVersions.HELIUM_VERSION;
50 private final PeerInfo peerInfo;
52 private LeaderInstallSnapshotState installSnapshotState;
54 private long slicedLogEntryIndex = NO_INDEX;
56 private boolean needsLeaderAddress;
59 * Constructs an instance.
61 * @param peerInfo the associated PeerInfo of the follower.
62 * @param matchIndex the initial match index.
63 * @param context the RaftActorContext.
66 FollowerLogInformation(final PeerInfo peerInfo, final long matchIndex, final RaftActorContext context) {
67 this.nextIndex = context.getCommitIndex();
68 this.matchIndex = matchIndex;
69 this.context = context;
70 this.peerInfo = Preconditions.checkNotNull(peerInfo);
74 * Constructs an instance with no matching index.
76 * @param peerInfo the associated PeerInfo of the follower.
77 * @param context the RaftActorContext.
79 public FollowerLogInformation(final PeerInfo peerInfo, final RaftActorContext context) {
80 this(peerInfo, NO_INDEX, context);
84 * Increments the value of the follower's next index.
86 * @return the new value of nextIndex.
89 long incrNextIndex() {
94 * Decrements the value of the follower's next index, taking into account its reported last log index.
96 * @param followerLastIndex follower's last reported index.
97 * @return true if the next index was decremented, i.e. it was previously >= 0, false otherwise.
99 public boolean decrNextIndex(final long followerLastIndex) {
104 if (followerLastIndex >= 0 && nextIndex > followerLastIndex) {
105 // If the follower's last log index is lower than nextIndex, jump directly to it, so we converge
106 // on a common index more quickly.
107 nextIndex = followerLastIndex;
115 * Sets the index of the follower's next log entry.
117 * @param nextIndex the new index.
118 * @return true if the new index differed from the current index and the current index was updated, false
121 @SuppressWarnings("checkstyle:hiddenField")
122 public boolean setNextIndex(final long nextIndex) {
123 if (this.nextIndex != nextIndex) {
124 this.nextIndex = nextIndex;
132 * Increments the value of the follower's match index.
134 * @return the new value of matchIndex.
136 public long incrMatchIndex() {
141 * Sets the index of the follower's highest log entry.
143 * @param matchIndex the new index.
144 * @return true if the new index differed from the current index and the current index was updated, false
147 @SuppressWarnings("checkstyle:hiddenField")
148 public boolean setMatchIndex(final long matchIndex) {
149 // If the new match index is the index of the entry currently being sliced, then we know slicing is complete
150 // and the follower received the entry and responded so clear the slicedLogEntryIndex
151 if (isLogEntrySlicingInProgress() && slicedLogEntryIndex == matchIndex) {
152 slicedLogEntryIndex = NO_INDEX;
155 if (this.matchIndex != matchIndex) {
156 this.matchIndex = matchIndex;
164 * Returns the identifier of the follower.
166 * @return the identifier of the follower.
168 public String getId() {
169 return peerInfo.getId();
173 * Returns the index of the next log entry to send to the follower.
175 * @return index of the follower's next log entry.
177 public long getNextIndex() {
182 * Returns the index of highest log entry known to be replicated on the follower.
184 * @return the index of highest log entry.
186 public long getMatchIndex() {
191 * Checks if the follower is active by comparing the time of the last activity with the election time out. The
192 * follower is active if some activity has occurred for the follower within the election time out interval.
194 * @return true if follower is active, false otherwise.
196 public boolean isFollowerActive() {
197 if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
201 long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
202 return stopwatch.isRunning()
203 && elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis();
207 * Marks the follower as active. This should be called when some activity has occurred for the follower.
209 public void markFollowerActive() {
210 if (stopwatch.isRunning()) {
217 * Marks the follower as inactive. This should only be called from unit tests.
220 public void markFollowerInActive() {
221 if (stopwatch.isRunning()) {
227 * Returns the time since the last activity occurred for the follower.
229 * @return time in nanoseconds since the last activity from the follower.
231 public long nanosSinceLastActivity() {
232 return stopwatch.elapsed(TimeUnit.NANOSECONDS);
236 * This method checks if the next replicate message can be sent to the follower. This is an optimization to avoid
237 * sending duplicate message too frequently if the last replicate message was sent and no reply has been received
238 * yet within the current heart beat interval
240 * @param commitIndex current commitIndex
241 * @return true if it is OK to replicate, false otherwise
243 public boolean okToReplicate(final long commitIndex) {
244 if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
248 // Return false if we are trying to send duplicate data before the heartbeat interval. This check includes
249 // also our commitIndex, as followers need to be told of new commitIndex as soon as possible.
250 if (getNextIndex() == lastReplicatedIndex && !hasStaleCommitIndex(commitIndex)
251 && lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS)
252 < context.getConfigParams().getHeartBeatInterval().toMillis()) {
256 resetLastReplicated();
260 private void resetLastReplicated() {
261 lastReplicatedIndex = getNextIndex();
262 if (lastReplicatedStopwatch.isRunning()) {
263 lastReplicatedStopwatch.reset();
265 lastReplicatedStopwatch.start();
269 * Returns the log entry payload data version of the follower.
271 * @return the payload data version.
273 public short getPayloadVersion() {
274 return payloadVersion;
278 * Sets the payload data version of the follower.
280 * @param payloadVersion the payload data version.
282 public void setPayloadVersion(final short payloadVersion) {
283 this.payloadVersion = payloadVersion;
287 * Returns the the raft version of the follower.
289 * @return the raft version of the follower.
291 public short getRaftVersion() {
296 * Sets the raft version of the follower.
298 * @param raftVersion the raft version.
300 public void setRaftVersion(final short raftVersion) {
301 this.raftVersion = raftVersion;
305 * Returns the LeaderInstallSnapshotState for the in progress install snapshot.
307 * @return the LeaderInstallSnapshotState if a snapshot install is in progress, null otherwise.
310 public LeaderInstallSnapshotState getInstallSnapshotState() {
311 return installSnapshotState;
315 * Sets the LeaderInstallSnapshotState when an install snapshot is initiated.
317 * @param state the LeaderInstallSnapshotState
319 public void setLeaderInstallSnapshotState(@Nonnull final LeaderInstallSnapshotState state) {
320 if (this.installSnapshotState == null) {
321 this.installSnapshotState = Preconditions.checkNotNull(state);
326 * Clears the LeaderInstallSnapshotState when an install snapshot is complete.
328 public void clearLeaderInstallSnapshotState() {
329 Preconditions.checkState(installSnapshotState != null);
330 installSnapshotState.close();
331 installSnapshotState = null;
335 * Sets the index of the log entry whose payload size exceeds the maximum size for a single message and thus
336 * needs to be sliced into smaller chunks.
338 * @param index the log entry index or NO_INDEX to clear it
340 public void setSlicedLogEntryIndex(final long index) {
341 slicedLogEntryIndex = index;
345 * Return whether or not log entry slicing is currently in progress.
347 * @return true if slicing is currently in progress, false otherwise
349 public boolean isLogEntrySlicingInProgress() {
350 return slicedLogEntryIndex != NO_INDEX;
353 public void setNeedsLeaderAddress(final boolean value) {
354 needsLeaderAddress = value;
357 public boolean hasStaleCommitIndex(final long commitIndex) {
358 return sentCommitIndex != commitIndex;
361 public void setSentCommitIndex(final long commitIndex) {
362 sentCommitIndex = commitIndex;
366 public String needsLeaderAddress(final String leaderId) {
367 return needsLeaderAddress ? context.getPeerAddress(leaderId) : null;
371 public String toString() {
372 return "FollowerLogInformation [id=" + getId() + ", nextIndex=" + nextIndex + ", matchIndex=" + matchIndex
373 + ", lastReplicatedIndex=" + lastReplicatedIndex + ", commitIndex=" + sentCommitIndex
374 + ", votingState=" + peerInfo.getVotingState()
375 + ", stopwatch=" + stopwatch.elapsed(TimeUnit.MILLISECONDS)
376 + ", followerTimeoutMillis=" + context.getConfigParams().getElectionTimeOutInterval().toMillis() + "]";