From feb55e3f465176e96bace3eba6d37a5c767bbf3f Mon Sep 17 00:00:00 2001
From: Kanzhe Jiang <kanzhe.jiang@bigswitch.com>
Date: Fri, 21 Dec 2012 13:38:07 -0800
Subject: [PATCH] BSC-3006: make the count an tomicInteger.

---
 .../flowcache/FlowReconcileManager.java        | 10 ++++++----
 .../flowcache/FlowReconcileMgrTest.java        | 18 +++++++++---------
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/flowcache/FlowReconcileManager.java b/src/main/java/net/floodlightcontroller/flowcache/FlowReconcileManager.java
index 1eb9691a8..773f94505 100644
--- a/src/main/java/net/floodlightcontroller/flowcache/FlowReconcileManager.java
+++ b/src/main/java/net/floodlightcontroller/flowcache/FlowReconcileManager.java
@@ -10,6 +10,7 @@ import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import net.floodlightcontroller.core.module.FloodlightModuleContext;
 import net.floodlightcontroller.core.module.FloodlightModuleException;
@@ -64,7 +65,8 @@ public class FlowReconcileManager
     /** a minimum flow reconcile rate so that it won't stave */
     protected static int MIN_FLOW_RECONCILE_PER_SECOND = 200;
     
-    /** once per second */
+    /** start flow reconcile in 10ms after a new reconcile request is received.
+     *  The max delay is 1 second. */
     protected static int FLOW_RECONCILE_DELAY_MILLISEC = 10;
     protected Date lastReconcileTime;
     
@@ -72,7 +74,7 @@ public class FlowReconcileManager
     protected static final String EnableConfigKey = "enable";
     protected boolean flowReconcileEnabled;
     
-    volatile public int flowReconcileThreadRunCount;
+    public AtomicInteger flowReconcileThreadRunCount;
     
     @Override
     public synchronized void addFlowReconcileListener(
@@ -215,7 +217,7 @@ public class FlowReconcileManager
             flowReconcileEnabled = false;
         }
         
-        flowReconcileThreadRunCount = 0;
+        flowReconcileThreadRunCount = new AtomicInteger(0);
         lastReconcileTime = new Date(0);
         logger.debug("FlowReconcile is {}", flowReconcileEnabled);
     }
@@ -313,7 +315,7 @@ public class FlowReconcileManager
             }
             // Flush the flowCache counters.
             updateFlush();
-            flowReconcileThreadRunCount++;
+            flowReconcileThreadRunCount.incrementAndGet();
         } else {
             if (logger.isTraceEnabled()) {
                 logger.trace("No flow to be reconciled.");
diff --git a/src/test/java/net/floodlightcontroller/flowcache/FlowReconcileMgrTest.java b/src/test/java/net/floodlightcontroller/flowcache/FlowReconcileMgrTest.java
index 0dcbb357e..accac8e4c 100644
--- a/src/test/java/net/floodlightcontroller/flowcache/FlowReconcileMgrTest.java
+++ b/src/test/java/net/floodlightcontroller/flowcache/FlowReconcileMgrTest.java
@@ -117,7 +117,7 @@ public class FlowReconcileMgrTest extends FloodlightTestCase {
         flowReconcileMgr.addFlowReconcileListener(r3);
         
         int pre_flowReconcileThreadRunCount =
-                flowReconcileMgr.flowReconcileThreadRunCount;
+                flowReconcileMgr.flowReconcileThreadRunCount.get();
         Date startTime = new Date();
         OFMatchReconcile ofmRcIn = new OFMatchReconcile();
         try {
@@ -147,11 +147,11 @@ public class FlowReconcileMgrTest extends FloodlightTestCase {
         }).anyTimes();
         
         pre_flowReconcileThreadRunCount =
-            flowReconcileMgr.flowReconcileThreadRunCount;
+            flowReconcileMgr.flowReconcileThreadRunCount.get();
         startTime = new Date();
         replay(r1, r2, r3);
         flowReconcileMgr.reconcileFlow(ofmRcIn);
-        while (flowReconcileMgr.flowReconcileThreadRunCount <=
+        while (flowReconcileMgr.flowReconcileThreadRunCount.get() <=
                 pre_flowReconcileThreadRunCount) {
             Thread.sleep(10);
             Date currTime = new Date();
@@ -174,12 +174,12 @@ public class FlowReconcileMgrTest extends FloodlightTestCase {
         }).anyTimes();
         
         pre_flowReconcileThreadRunCount =
-            flowReconcileMgr.flowReconcileThreadRunCount;
+            flowReconcileMgr.flowReconcileThreadRunCount.get();
         startTime = new Date();
         
         replay(r1, r2, r3);
         flowReconcileMgr.reconcileFlow(ofmRcIn);
-        while (flowReconcileMgr.flowReconcileThreadRunCount <=
+        while (flowReconcileMgr.flowReconcileThreadRunCount.get() <=
                 pre_flowReconcileThreadRunCount) {
             Thread.sleep(10);
             Date currTime = new Date();
@@ -197,12 +197,12 @@ public class FlowReconcileMgrTest extends FloodlightTestCase {
         .andReturn(Command.STOP).times(1);
         
         pre_flowReconcileThreadRunCount =
-            flowReconcileMgr.flowReconcileThreadRunCount;
+            flowReconcileMgr.flowReconcileThreadRunCount.get();
         startTime = new Date();
         
         replay(r1, r2, r3);
         flowReconcileMgr.reconcileFlow(ofmRcIn);
-        while (flowReconcileMgr.flowReconcileThreadRunCount <=
+        while (flowReconcileMgr.flowReconcileThreadRunCount.get() <=
                 pre_flowReconcileThreadRunCount) {
             Thread.sleep(10);
             Date currTime = new Date();
@@ -227,11 +227,11 @@ public class FlowReconcileMgrTest extends FloodlightTestCase {
         .andReturn(Command.STOP).times(1);
         
         pre_flowReconcileThreadRunCount =
-            flowReconcileMgr.flowReconcileThreadRunCount;
+            flowReconcileMgr.flowReconcileThreadRunCount.get();
         startTime = new Date();
         replay(r1, r2, r3);
         flowReconcileMgr.reconcileFlow(ofmRcIn);
-        while (flowReconcileMgr.flowReconcileThreadRunCount <=
+        while (flowReconcileMgr.flowReconcileThreadRunCount.get() <=
                 pre_flowReconcileThreadRunCount) {
             Thread.sleep(10);
             Date currTime = new Date();
-- 
GitLab