Merge "Fixed discard-changes for mdsal netconf, mapping code cleanup."
[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 void setNextIndex(long nextIndex) {
48         this.nextIndex = nextIndex;
49     }
50
51     @Override
52     public long incrMatchIndex(){
53         return MATCH_INDEX_UPDATER.incrementAndGet(this);
54     }
55
56     @Override
57     public void setMatchIndex(long matchIndex) {
58         this.matchIndex = matchIndex;
59     }
60
61     @Override
62     public String getId() {
63         return id;
64     }
65
66     @Override
67     public long getNextIndex() {
68         return nextIndex;
69     }
70
71     @Override
72     public long getMatchIndex() {
73         return matchIndex;
74     }
75
76     @Override
77     public boolean isFollowerActive() {
78         long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
79         return (stopwatch.isRunning()) &&
80                 (elapsed <= context.getConfigParams().getElectionTimeOutInterval().toMillis());
81     }
82
83     @Override
84     public void markFollowerActive() {
85         if (stopwatch.isRunning()) {
86             stopwatch.reset();
87         }
88         stopwatch.start();
89     }
90
91     @Override
92     public void markFollowerInActive() {
93         if (stopwatch.isRunning()) {
94             stopwatch.stop();
95         }
96     }
97
98     @Override
99     public long timeSinceLastActivity() {
100         return stopwatch.elapsed(TimeUnit.MILLISECONDS);
101     }
102
103     @Override
104     public String toString() {
105         StringBuilder builder = new StringBuilder();
106         builder.append("FollowerLogInformationImpl [id=").append(id).append(", nextIndex=").append(nextIndex)
107                 .append(", matchIndex=").append(matchIndex).append(", stopwatch=")
108                 .append(stopwatch.elapsed(TimeUnit.MILLISECONDS))
109                 .append(", followerTimeoutMillis=")
110                 .append(context.getConfigParams().getElectionTimeOutInterval().toMillis()).append("]");
111         return builder.toString();
112     }
113
114
115 }