Merge "Change in AbstractRaftBehavior#performSnapshotWithoutCapture"
[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 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
14
15 public class FollowerLogInformationImpl implements FollowerLogInformation {
16     private static final AtomicLongFieldUpdater<FollowerLogInformationImpl> NEXT_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(FollowerLogInformationImpl.class, "nextIndex");
17     private static final AtomicLongFieldUpdater<FollowerLogInformationImpl> MATCH_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(FollowerLogInformationImpl.class, "matchIndex");
18
19     private final String id;
20
21     private final Stopwatch stopwatch = Stopwatch.createUnstarted();
22
23     private final RaftActorContext context;
24
25     private volatile long nextIndex;
26
27     private volatile long matchIndex;
28
29     public FollowerLogInformationImpl(String id, long matchIndex, RaftActorContext context) {
30         this.id = id;
31         this.nextIndex = context.getCommitIndex();
32         this.matchIndex = matchIndex;
33         this.context = context;
34     }
35
36     @Override
37     public long incrNextIndex(){
38         return NEXT_INDEX_UPDATER.incrementAndGet(this);
39     }
40
41     @Override
42     public long decrNextIndex() {
43         return NEXT_INDEX_UPDATER.decrementAndGet(this);
44     }
45
46     @Override
47     public boolean setNextIndex(long nextIndex) {
48         if(this.nextIndex != nextIndex) {
49             this.nextIndex = nextIndex;
50             return true;
51         }
52
53         return false;
54     }
55
56     @Override
57     public long incrMatchIndex(){
58         return MATCH_INDEX_UPDATER.incrementAndGet(this);
59     }
60
61     @Override
62     public boolean setMatchIndex(long matchIndex) {
63         if(this.matchIndex != matchIndex) {
64             this.matchIndex = matchIndex;
65             return true;
66         }
67
68         return false;
69     }
70
71     @Override
72     public String getId() {
73         return id;
74     }
75
76     @Override
77     public long getNextIndex() {
78         return nextIndex;
79     }
80
81     @Override
82     public long getMatchIndex() {
83         return matchIndex;
84     }
85
86     @Override
87     public boolean isFollowerActive() {
88         long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
89         return (stopwatch.isRunning()) &&
90                 (elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis());
91     }
92
93     @Override
94     public void markFollowerActive() {
95         if (stopwatch.isRunning()) {
96             stopwatch.reset();
97         }
98         stopwatch.start();
99     }
100
101     @Override
102     public void markFollowerInActive() {
103         if (stopwatch.isRunning()) {
104             stopwatch.stop();
105         }
106     }
107
108     @Override
109     public long timeSinceLastActivity() {
110         return stopwatch.elapsed(TimeUnit.MILLISECONDS);
111     }
112
113     @Override
114     public String toString() {
115         StringBuilder builder = new StringBuilder();
116         builder.append("FollowerLogInformationImpl [id=").append(id).append(", nextIndex=").append(nextIndex)
117                 .append(", matchIndex=").append(matchIndex).append(", stopwatch=")
118                 .append(stopwatch.elapsed(TimeUnit.MILLISECONDS))
119                 .append(", followerTimeoutMillis=")
120                 .append(context.getConfigParams().getElectionTimeOutInterval().toMillis()).append("]");
121         return builder.toString();
122     }
123
124
125 }