Simplify ReplicatedLogImpl a bit 90/37490/1
authorRobert Varga <rovarga@cisco.com>
Tue, 12 Apr 2016 12:33:46 +0000 (14:33 +0200)
committerRobert Varga <rovarga@cisco.com>
Tue, 12 Apr 2016 12:33:46 +0000 (14:33 +0200)
- move fields and constructor to keep object state together
- eliminate unneeded call to isDebugEnabled() via a getter
- simplify getDataSizeForSnapshotCheck()
- keep ConfigParams in a local variable for more concise code

Change-Id: Ic93c959181148de2e653dbd3f6f6922589c37223
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/ReplicatedLogImpl.java

index 9004290..7e1cb69 100644 (file)
@@ -19,15 +19,21 @@ import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries;
 class ReplicatedLogImpl extends AbstractReplicatedLogImpl {
     private static final int DATA_SIZE_DIVIDER = 5;
 
-    private long dataSizeSinceLastSnapshot = 0L;
-    private final RaftActorContext context;
-
     private final Procedure<DeleteEntries> deleteProcedure = new Procedure<DeleteEntries>() {
         @Override
         public void apply(final DeleteEntries notUsed) {
         }
     };
 
+    private final RaftActorContext context;
+    private long dataSizeSinceLastSnapshot = 0L;
+
+    private ReplicatedLogImpl(final long snapshotIndex, final long snapshotTerm, final List<ReplicatedLogEntry> unAppliedEntries,
+            final RaftActorContext context) {
+        super(snapshotIndex, snapshotTerm, unAppliedEntries);
+        this.context = Preconditions.checkNotNull(context);
+    }
+
     static ReplicatedLog newInstance(final Snapshot snapshot, final RaftActorContext context) {
         return new ReplicatedLogImpl(snapshot.getLastAppliedIndex(), snapshot.getLastAppliedTerm(),
                 snapshot.getUnAppliedEntries(), context);
@@ -37,12 +43,6 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl {
         return new ReplicatedLogImpl(-1L, -1L, Collections.<ReplicatedLogEntry>emptyList(), context);
     }
 
-    private ReplicatedLogImpl(final long snapshotIndex, final long snapshotTerm, final List<ReplicatedLogEntry> unAppliedEntries,
-            final RaftActorContext context) {
-        super(snapshotIndex, snapshotTerm, unAppliedEntries);
-        this.context = Preconditions.checkNotNull(context);
-    }
-
     @Override
     public void removeFromAndPersist(final long logEntryIndex) {
         // FIXME: Maybe this should be done after the command is saved
@@ -59,25 +59,20 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl {
 
     @Override
     public void captureSnapshotIfReady(final ReplicatedLogEntry replicatedLogEntry) {
-        long journalSize = replicatedLogEntry.getIndex() + 1;
-        long dataThreshold = context.getTotalMemory() *
-                context.getConfigParams().getSnapshotDataThresholdPercentage() / 100;
-
-        if ((journalSize % context.getConfigParams().getSnapshotBatchCount() == 0
-                || getDataSizeForSnapshotCheck() > dataThreshold)) {
+        final ConfigParams config = context.getConfigParams();
+        final long journalSize = replicatedLogEntry.getIndex() + 1;
+        final long dataThreshold = context.getTotalMemory() * config.getSnapshotDataThresholdPercentage() / 100;
 
+        if (journalSize % config.getSnapshotBatchCount() == 0 || getDataSizeForSnapshotCheck() > dataThreshold) {
             boolean started = context.getSnapshotManager().capture(replicatedLogEntry,
                     context.getCurrentBehavior().getReplicatedToAllIndex());
-            if (started) {
-                if (!context.hasFollowers()) {
-                    dataSizeSinceLastSnapshot = 0;
-                }
+            if (started && !context.hasFollowers()) {
+                dataSizeSinceLastSnapshot = 0;
             }
         }
     }
 
     private long getDataSizeForSnapshotCheck() {
-        long dataSizeForCheck = dataSize();
         if (!context.hasFollowers()) {
             // When we do not have followers we do not maintain an in-memory log
             // due to this the journalSize will never become anything close to the
@@ -90,18 +85,17 @@ class ReplicatedLogImpl extends AbstractReplicatedLogImpl {
             // need of doing a snapshot just for the sake of freeing up memory we adjust
             // the real size of data by the DATA_SIZE_DIVIDER so that we do not snapshot as often
             // as if we were maintaining a real snapshot
-            dataSizeForCheck = dataSizeSinceLastSnapshot / DATA_SIZE_DIVIDER;
+            return dataSizeSinceLastSnapshot / DATA_SIZE_DIVIDER;
+        } else {
+            return dataSize();
         }
-        return dataSizeForCheck;
     }
 
     @Override
     public void appendAndPersist(final ReplicatedLogEntry replicatedLogEntry,
             final Procedure<ReplicatedLogEntry> callback)  {
 
-        if (context.getLogger().isDebugEnabled()) {
-            context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry);
-        }
+        context.getLogger().debug("{}: Append log entry and persist {} ", context.getId(), replicatedLogEntry);
 
         // FIXME : By adding the replicated log entry to the in-memory journal we are not truly ensuring durability of the logs
         append(replicatedLogEntry);