Fix intermittent testOwnerChangesOnPeerAvailabilityChanges failure
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / FollowerLogInformationImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Stopwatch;
13 import java.util.concurrent.TimeUnit;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.cluster.raft.behaviors.LeaderInstallSnapshotState;
17
18 /**
19  * Implementation of the FollowerLogInformation interface.
20  *
21  * @author Moiz Raja
22  * @author Thomas Pantelis
23  */
24 public class FollowerLogInformationImpl implements FollowerLogInformation {
25     private final Stopwatch stopwatch = Stopwatch.createUnstarted();
26
27     private final RaftActorContext context;
28
29     private long nextIndex;
30
31     private long matchIndex;
32
33     private long lastReplicatedIndex = -1L;
34
35     private final Stopwatch lastReplicatedStopwatch = Stopwatch.createUnstarted();
36
37     private short payloadVersion = -1;
38
39     // Assume the HELIUM_VERSION version initially for backwards compatibility until we obtain the follower's
40     // actual version via AppendEntriesReply. Although we no longer support the Helium version, a pre-Boron
41     // follower will not have the version field in AppendEntriesReply so it will be set to 0 which is
42     // HELIUM_VERSION.
43     private short raftVersion = RaftVersions.HELIUM_VERSION;
44
45     private final PeerInfo peerInfo;
46
47     private LeaderInstallSnapshotState installSnapshotState;
48
49     private long slicedLogEntryIndex = NO_INDEX;
50
51     /**
52      * Constructs an instance.
53      *
54      * @param peerInfo the associated PeerInfo of the follower.
55      * @param matchIndex the initial match index.
56      * @param context the RaftActorContext.
57      */
58     public FollowerLogInformationImpl(PeerInfo peerInfo, long matchIndex, RaftActorContext context) {
59         this.nextIndex = context.getCommitIndex();
60         this.matchIndex = matchIndex;
61         this.context = context;
62         this.peerInfo = Preconditions.checkNotNull(peerInfo);
63     }
64
65     @Override
66     public long incrNextIndex() {
67         return nextIndex++;
68     }
69
70     @Override
71     public boolean decrNextIndex() {
72         if (nextIndex >= 0) {
73             nextIndex--;
74             return true;
75         }
76
77         return false;
78     }
79
80     @Override
81     public boolean setNextIndex(long nextIndex) {
82         if (this.nextIndex != nextIndex) {
83             this.nextIndex = nextIndex;
84             return true;
85         }
86
87         return false;
88     }
89
90     @Override
91     public long incrMatchIndex() {
92         return matchIndex++;
93     }
94
95     @Override
96     public boolean setMatchIndex(long matchIndex) {
97         // If the new match index is the index of the entry currently being sliced, then we know slicing is complete
98         // and the follower received the entry and responded so clear the slicedLogEntryIndex
99         if (isLogEntrySlicingInProgress() && slicedLogEntryIndex == matchIndex) {
100             slicedLogEntryIndex = NO_INDEX;
101         }
102
103         if (this.matchIndex != matchIndex) {
104             this.matchIndex = matchIndex;
105             return true;
106         }
107
108         return false;
109     }
110
111     @Override
112     public String getId() {
113         return peerInfo.getId();
114     }
115
116     @Override
117     public long getNextIndex() {
118         return nextIndex;
119     }
120
121     @Override
122     public long getMatchIndex() {
123         return matchIndex;
124     }
125
126     @Override
127     public boolean isFollowerActive() {
128         if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
129             return false;
130         }
131
132         long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
133         return stopwatch.isRunning()
134                 && elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis();
135     }
136
137     @Override
138     public void markFollowerActive() {
139         if (stopwatch.isRunning()) {
140             stopwatch.reset();
141         }
142         stopwatch.start();
143     }
144
145     @Override
146     public void markFollowerInActive() {
147         if (stopwatch.isRunning()) {
148             stopwatch.stop();
149         }
150     }
151
152     @Override
153     public long timeSinceLastActivity() {
154         return stopwatch.elapsed(TimeUnit.MILLISECONDS);
155     }
156
157     @Override
158     public boolean okToReplicate() {
159         if (peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
160             return false;
161         }
162
163         // Return false if we are trying to send duplicate data before the heartbeat interval
164         if (getNextIndex() == lastReplicatedIndex && lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS)
165                 < context.getConfigParams().getHeartBeatInterval().toMillis()) {
166             return false;
167         }
168
169         resetLastReplicated();
170         return true;
171     }
172
173     private void resetLastReplicated() {
174         lastReplicatedIndex = getNextIndex();
175         if (lastReplicatedStopwatch.isRunning()) {
176             lastReplicatedStopwatch.reset();
177         }
178         lastReplicatedStopwatch.start();
179     }
180
181     @Override
182     public short getPayloadVersion() {
183         return payloadVersion;
184     }
185
186     @Override
187     public void setPayloadVersion(short payloadVersion) {
188         this.payloadVersion = payloadVersion;
189     }
190
191     @Override
192     public short getRaftVersion() {
193         return raftVersion;
194     }
195
196     @Override
197     public void setRaftVersion(short raftVersion) {
198         this.raftVersion = raftVersion;
199     }
200
201     @Override
202     @Nullable
203     public LeaderInstallSnapshotState getInstallSnapshotState() {
204         return installSnapshotState;
205     }
206
207     @Override
208     public void setLeaderInstallSnapshotState(@Nonnull LeaderInstallSnapshotState state) {
209         if (this.installSnapshotState == null) {
210             this.installSnapshotState = Preconditions.checkNotNull(state);
211         }
212     }
213
214     @Override
215     public void clearLeaderInstallSnapshotState() {
216         Preconditions.checkState(installSnapshotState != null);
217         installSnapshotState.close();
218         installSnapshotState = null;
219     }
220
221     @Override
222     public void setSlicedLogEntryIndex(long index) {
223         slicedLogEntryIndex  = index;
224     }
225
226     @Override
227     public boolean isLogEntrySlicingInProgress() {
228         return slicedLogEntryIndex != NO_INDEX;
229     }
230
231     @Override
232     public String toString() {
233         return "FollowerLogInformationImpl [id=" + getId() + ", nextIndex=" + nextIndex + ", matchIndex=" + matchIndex
234                 + ", lastReplicatedIndex=" + lastReplicatedIndex + ", votingState=" + peerInfo.getVotingState()
235                 + ", stopwatch=" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ", followerTimeoutMillis="
236                 + context.getConfigParams().getElectionTimeOutInterval().toMillis() + "]";
237     }
238 }