BUG-8618: improve debug logs
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / SyncStatusTracker.java
index 8adca722a2d6a887ee37545a9b3c488f5e4c6996..e2512d5b67f911acfab9e8d94e3c5f4888381714 100644 (file)
@@ -23,21 +23,29 @@ import org.slf4j.LoggerFactory;
  * sync if it is behind by 'syncThreshold' commits.
  */
 public class SyncStatusTracker {
+    private static final class LeaderInfo {
+        final long minimumCommitIndex;
+        final String leaderId;
+
+        LeaderInfo(final String leaderId, final long minimumCommitIndex) {
+            this.leaderId = Preconditions.checkNotNull(leaderId);
+            this.minimumCommitIndex = minimumCommitIndex;
+        }
+    }
+
     private static final Logger LOG = LoggerFactory.getLogger(SyncStatusTracker.class);
 
     private static final boolean IN_SYNC = true;
     private static final boolean NOT_IN_SYNC = false;
 
-    private final String id;
+    private final long syncThreshold;
     private final ActorRef actor;
-    private final int syncThreshold;
+    private final String id;
 
-    // FIXME: what is this magic constant?
-    private long minimumExpectedIndex = -2L;
-    private String syncedLeaderId = null;
-    private boolean syncStatus = false;
+    private LeaderInfo syncTarget;
+    private boolean syncStatus;
 
-    public SyncStatusTracker(final ActorRef actor, final String id, final int syncThreshold) {
+    public SyncStatusTracker(final ActorRef actor, final String id, final long syncThreshold) {
         this.actor = Preconditions.checkNotNull(actor, "actor should not be null");
         this.id = Preconditions.checkNotNull(id, "id should not be null");
         Preconditions.checkArgument(syncThreshold >= 0, "syncThreshold should be greater than or equal to 0");
@@ -47,22 +55,21 @@ public class SyncStatusTracker {
     public void update(final String leaderId, final long leaderCommit, final long commitIndex) {
         Preconditions.checkNotNull(leaderId, "leaderId should not be null");
 
-        if (!leaderId.equals(syncedLeaderId)) {
-            minimumExpectedIndex = leaderCommit;
-            LOG.debug("Last sync leader {} does not match current leader {}, need to catch up to {}", syncedLeaderId,
+        if (syncTarget == null || !leaderId.equals(syncTarget.leaderId)) {
+            LOG.debug("{}: Last sync leader does not match current leader {}, need to catch up to {}", id,
                 leaderId, leaderCommit);
             changeSyncStatus(NOT_IN_SYNC, true);
-            syncedLeaderId = leaderId;
+            syncTarget = new LeaderInfo(leaderId, leaderCommit);
             return;
         }
 
         final long lag = leaderCommit - commitIndex;
         if (lag > syncThreshold) {
-            LOG.debug("Lagging {} entries behind leader {}", lag, leaderId);
+            LOG.debug("{}: Lagging {} entries behind leader {}", id, lag, leaderId);
             changeSyncStatus(NOT_IN_SYNC, false);
-        } else if (commitIndex >= minimumExpectedIndex) {
-            LOG.debug("Lagging {} entries behind leader and reached {} (of expected {})", lag, leaderId, commitIndex,
-                minimumExpectedIndex);
+        } else if (commitIndex >= syncTarget.minimumCommitIndex) {
+            LOG.debug("{}: Lagging {} entries behind leader and reached {} (of expected {})", id, lag, leaderId,
+                commitIndex, syncTarget.minimumCommitIndex);
             changeSyncStatus(IN_SYNC, false);
         }
     }
@@ -72,7 +79,7 @@ public class SyncStatusTracker {
             actor.tell(new FollowerInitialSyncUpStatus(newSyncStatus, id), ActorRef.noSender());
             syncStatus = newSyncStatus;
         } else {
-            LOG.trace("No change in sync status of {}, dampening message", actor);
+            LOG.trace("{}: No change in sync status of, dampening message", id);
         }
     }
 }