Measure follower activity in nanoseconds
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / SyncStatusTracker.java
1 /*
2  * Copyright (c) 2015 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.behaviors;
10
11 import akka.actor.ActorRef;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * The SyncStatusTracker tracks if a Follower is in sync with any given Leader or not
19  * When an update is received from the Leader and the update happens to be the first update
20  * from that Leader then the SyncStatusTracker will not mark the Follower as not in-sync till the
21  * Followers commitIndex matches the commitIndex that the Leader sent in it's very first update.
22  * Subsequently when an update is received the tracker will consider the Follower to be out of
23  * sync if it is behind by 'syncThreshold' commits.
24  */
25 public class SyncStatusTracker {
26     private static final class LeaderInfo {
27         final long minimumCommitIndex;
28         final String leaderId;
29
30         LeaderInfo(final String leaderId, final long minimumCommitIndex) {
31             this.leaderId = Preconditions.checkNotNull(leaderId);
32             this.minimumCommitIndex = minimumCommitIndex;
33         }
34     }
35
36     private static final Logger LOG = LoggerFactory.getLogger(SyncStatusTracker.class);
37
38     private static final boolean IN_SYNC = true;
39     private static final boolean NOT_IN_SYNC = false;
40
41     private final long syncThreshold;
42     private final ActorRef actor;
43     private final String id;
44
45     private LeaderInfo syncTarget;
46     private boolean syncStatus;
47
48     public SyncStatusTracker(final ActorRef actor, final String id, final long syncThreshold) {
49         this.actor = Preconditions.checkNotNull(actor, "actor should not be null");
50         this.id = Preconditions.checkNotNull(id, "id should not be null");
51         Preconditions.checkArgument(syncThreshold >= 0, "syncThreshold should be greater than or equal to 0");
52         this.syncThreshold = syncThreshold;
53     }
54
55     public void update(final String leaderId, final long leaderCommit, final long commitIndex) {
56         Preconditions.checkNotNull(leaderId, "leaderId should not be null");
57
58         if (syncTarget == null || !leaderId.equals(syncTarget.leaderId)) {
59             LOG.debug("{}: Last sync leader does not match current leader {}, need to catch up to {}", id,
60                 leaderId, leaderCommit);
61             changeSyncStatus(NOT_IN_SYNC, true);
62             syncTarget = new LeaderInfo(leaderId, leaderCommit);
63             return;
64         }
65
66         final long lag = leaderCommit - commitIndex;
67         if (lag > syncThreshold) {
68             LOG.debug("{}: Lagging {} entries behind leader {}", id, lag, leaderId);
69             changeSyncStatus(NOT_IN_SYNC, false);
70         } else if (commitIndex >= syncTarget.minimumCommitIndex) {
71             LOG.debug("{}: Lagging {} entries behind leader and reached {} (of expected {})", id, lag, leaderId,
72                 commitIndex, syncTarget.minimumCommitIndex);
73             changeSyncStatus(IN_SYNC, false);
74         }
75     }
76
77     private void changeSyncStatus(final boolean newSyncStatus, final boolean forceStatusChange) {
78         if (forceStatusChange || newSyncStatus != syncStatus) {
79             actor.tell(new FollowerInitialSyncUpStatus(newSyncStatus, id), ActorRef.noSender());
80             syncStatus = newSyncStatus;
81         } else {
82             LOG.trace("{}: No change in sync status of, dampening message", id);
83         }
84     }
85 }