Initial code for RaftActorServerConfigurationSupport
[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.Stopwatch;
12 import java.util.concurrent.TimeUnit;
13
14 public class FollowerLogInformationImpl implements FollowerLogInformation {
15     private final String id;
16
17     private final Stopwatch stopwatch = Stopwatch.createUnstarted();
18
19     private final RaftActorContext context;
20
21     private long nextIndex;
22
23     private long matchIndex;
24
25     private long lastReplicatedIndex = -1L;
26
27     private final Stopwatch lastReplicatedStopwatch = Stopwatch.createUnstarted();
28
29     private short payloadVersion = -1;
30
31     private FollowerState state = FollowerState.VOTING;
32
33     public FollowerLogInformationImpl(String id, long matchIndex, RaftActorContext context) {
34         this.id = id;
35         this.nextIndex = context.getCommitIndex();
36         this.matchIndex = matchIndex;
37         this.context = context;
38     }
39
40     @Override
41     public long incrNextIndex() {
42         return nextIndex++;
43     }
44
45     @Override
46     public long decrNextIndex() {
47         return nextIndex--;
48     }
49
50     @Override
51     public boolean setNextIndex(long nextIndex) {
52         if(this.nextIndex != nextIndex) {
53             this.nextIndex = nextIndex;
54             return true;
55         }
56
57         return false;
58     }
59
60     @Override
61     public long incrMatchIndex(){
62         return matchIndex++;
63     }
64
65     @Override
66     public boolean setMatchIndex(long matchIndex) {
67         if(this.matchIndex != matchIndex) {
68             this.matchIndex = matchIndex;
69             return true;
70         }
71
72         return false;
73     }
74
75     @Override
76     public String getId() {
77         return id;
78     }
79
80     @Override
81     public long getNextIndex() {
82         return nextIndex;
83     }
84
85     @Override
86     public long getMatchIndex() {
87         return matchIndex;
88     }
89
90     @Override
91     public boolean isFollowerActive() {
92         if(state == FollowerState.VOTING_NOT_INITIALIZED) {
93             return false;
94         }
95
96         long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
97         return (stopwatch.isRunning()) &&
98                 (elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis());
99     }
100
101     @Override
102     public void markFollowerActive() {
103         if (stopwatch.isRunning()) {
104             stopwatch.reset();
105         }
106         stopwatch.start();
107     }
108
109     @Override
110     public void markFollowerInActive() {
111         if (stopwatch.isRunning()) {
112             stopwatch.stop();
113         }
114     }
115
116     @Override
117     public long timeSinceLastActivity() {
118         return stopwatch.elapsed(TimeUnit.MILLISECONDS);
119     }
120
121     @Override
122     public boolean okToReplicate() {
123         if(state == FollowerState.VOTING_NOT_INITIALIZED) {
124             return false;
125         }
126
127         // Return false if we are trying to send duplicate data before the heartbeat interval
128         if(getNextIndex() == lastReplicatedIndex){
129             if(lastReplicatedStopwatch.elapsed(TimeUnit.MILLISECONDS) < context.getConfigParams()
130                     .getHeartBeatInterval().toMillis()){
131                 return false;
132             }
133         }
134
135         resetLastReplicated();
136         return true;
137     }
138
139     private void resetLastReplicated(){
140         lastReplicatedIndex = getNextIndex();
141         if(lastReplicatedStopwatch.isRunning()){
142             lastReplicatedStopwatch.reset();
143         }
144         lastReplicatedStopwatch.start();
145     }
146
147     @Override
148     public short getPayloadVersion() {
149         return payloadVersion;
150     }
151
152     @Override
153     public void setPayloadVersion(short payloadVersion) {
154         this.payloadVersion = payloadVersion;
155     }
156
157     @Override
158     public boolean canParticipateInConsensus() {
159         return state == FollowerState.VOTING;
160     }
161
162     @Override
163     public void setFollowerState(FollowerState state) {
164         this.state = state;
165     }
166
167     @Override
168     public FollowerState getFollowerState() {
169         return state;
170     }
171
172     @Override
173     public String toString() {
174         return "FollowerLogInformationImpl [id=" + id + ", nextIndex=" + nextIndex + ", matchIndex=" + matchIndex
175                 + ", lastReplicatedIndex=" + lastReplicatedIndex + ", state=" + state + ", stopwatch="
176                 + stopwatch.elapsed(TimeUnit.MILLISECONDS) + ", followerTimeoutMillis="
177                 + context.getConfigParams().getElectionTimeOutInterval().toMillis() + "]";
178     }
179 }