Bug 6540: Move LeaderInstallSnapshotState to FollowerLogInformation
[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 public class FollowerLogInformationImpl implements FollowerLogInformation {
19     private final Stopwatch stopwatch = Stopwatch.createUnstarted();
20
21     private final RaftActorContext context;
22
23     private long nextIndex;
24
25     private long matchIndex;
26
27     private long lastReplicatedIndex = -1L;
28
29     private final Stopwatch lastReplicatedStopwatch = Stopwatch.createUnstarted();
30
31     private short payloadVersion = -1;
32
33     // Assume the HELIUM_VERSION version initially for backwards compatibility until we obtain the follower's
34     // actual version via AppendEntriesReply. Although we no longer support the Helium version, a pre-Boron
35     // follower will not have the version field in AppendEntriesReply so it will be set to 0 which is
36     // HELIUM_VERSION.
37     private short raftVersion = RaftVersions.HELIUM_VERSION;
38
39     private final PeerInfo peerInfo;
40
41     private LeaderInstallSnapshotState installSnapshotState;
42
43     public FollowerLogInformationImpl(PeerInfo peerInfo, long matchIndex, RaftActorContext context) {
44         this.nextIndex = context.getCommitIndex();
45         this.matchIndex = matchIndex;
46         this.context = context;
47         this.peerInfo = Preconditions.checkNotNull(peerInfo);
48     }
49
50     @Override
51     public long incrNextIndex() {
52         return nextIndex++;
53     }
54
55     @Override
56     public long decrNextIndex() {
57         return nextIndex--;
58     }
59
60     @Override
61     public boolean setNextIndex(long nextIndex) {
62         if(this.nextIndex != nextIndex) {
63             this.nextIndex = nextIndex;
64             return true;
65         }
66
67         return false;
68     }
69
70     @Override
71     public long incrMatchIndex(){
72         return matchIndex++;
73     }
74
75     @Override
76     public boolean setMatchIndex(long matchIndex) {
77         if(this.matchIndex != matchIndex) {
78             this.matchIndex = matchIndex;
79             return true;
80         }
81
82         return false;
83     }
84
85     @Override
86     public String getId() {
87         return peerInfo.getId();
88     }
89
90     @Override
91     public long getNextIndex() {
92         return nextIndex;
93     }
94
95     @Override
96     public long getMatchIndex() {
97         return matchIndex;
98     }
99
100     @Override
101     public boolean isFollowerActive() {
102         if(peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
103             return false;
104         }
105
106         long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
107         return (stopwatch.isRunning()) &&
108                 (elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis());
109     }
110
111     @Override
112     public void markFollowerActive() {
113         if (stopwatch.isRunning()) {
114             stopwatch.reset();
115         }
116         stopwatch.start();
117     }
118
119     @Override
120     public void markFollowerInActive() {
121         if (stopwatch.isRunning()) {
122             stopwatch.stop();
123         }
124     }
125
126     @Override
127     public long timeSinceLastActivity() {
128         return stopwatch.elapsed(TimeUnit.MILLISECONDS);
129     }
130
131     @Override
132     public boolean okToReplicate() {
133         if(peerInfo.getVotingState() == VotingState.VOTING_NOT_INITIALIZED) {
134             return false;
135         }
136
137         // Return false if we are trying to send duplicate data before the heartbeat interval
138         if(getNextIndex() == lastReplicatedIndex){
139             if(lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS) < context.getConfigParams()
140                     .getHeartBeatInterval().toMillis()){
141                 return false;
142             }
143         }
144
145         resetLastReplicated();
146         return true;
147     }
148
149     private void resetLastReplicated(){
150         lastReplicatedIndex = getNextIndex();
151         if(lastReplicatedStopwatch.isRunning()){
152             lastReplicatedStopwatch.reset();
153         }
154         lastReplicatedStopwatch.start();
155     }
156
157     @Override
158     public short getPayloadVersion() {
159         return payloadVersion;
160     }
161
162     @Override
163     public void setPayloadVersion(short payloadVersion) {
164         this.payloadVersion = payloadVersion;
165     }
166
167     @Override
168     public short getRaftVersion() {
169         return raftVersion;
170     }
171
172     @Override
173     public void setRaftVersion(short raftVersion) {
174         this.raftVersion = raftVersion;
175     }
176
177     @Override
178     @Nullable
179     public LeaderInstallSnapshotState getInstallSnapshotState() {
180         return installSnapshotState;
181     }
182
183     @Override
184     public void setLeaderInstallSnapshotState(@Nonnull LeaderInstallSnapshotState state) {
185         this.installSnapshotState = Preconditions.checkNotNull(state);
186     }
187
188     @Override
189     public void clearLeaderInstallSnapshotState() {
190         installSnapshotState = null;
191     }
192
193     @Override
194     public String toString() {
195         return "FollowerLogInformationImpl [id=" + getId() + ", nextIndex=" + nextIndex + ", matchIndex=" + matchIndex
196                 + ", lastReplicatedIndex=" + lastReplicatedIndex + ", votingState=" + peerInfo.getVotingState()
197                 + ", stopwatch=" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ", followerTimeoutMillis="
198                 + context.getConfigParams().getElectionTimeOutInterval().toMillis() + "]";
199     }
200 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.