Merge "Re-added config.version to config-module-archetype."
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / StatisticsRequestScheduler.java
index 9ebfd6fd62f7b2ab8933d86a03a788b3154d2eb5..29a27e2bb2a2cdfc85c7b49eada243bdc9f8a531 100644 (file)
@@ -18,12 +18,13 @@ import java.util.concurrent.TimeUnit;
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction.DataTransactionListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Main responsibility of the class is to check the MD-SAL data store read/write
- * transaction accumulation level and send statistics request if number of pending 
+ * transaction accumulation level and send statistics request if number of pending
  * read/write transactions are zero.
  * @author avishnoi@in.ibm.com
  *
@@ -35,21 +36,25 @@ public class StatisticsRequestScheduler implements DataTransactionListener {
     private final Timer timer = new Timer("request-monitor", true);
 
     // We need ordered retrieval, and O(1) contains operation
-    private final Map<AbstractStatsTracker,Integer> requestQueue = 
+    private final Map<AbstractStatsTracker,Integer> requestQueue =
             Collections.synchronizedMap(new LinkedHashMap<AbstractStatsTracker,Integer>());
-    
+
     private Long PendingTransactions;
-    
+
     private long lastRequestTime = System.nanoTime();
-    
+
     private static final long REQUEST_MONITOR_INTERVAL = 1000;
-    
+
     private final TimerTask task = new TimerTask() {
         @Override
         public void run() {
-            long now = System.nanoTime();
-            if(now > lastRequestTime+TimeUnit.MILLISECONDS.toNanos(REQUEST_MONITOR_INTERVAL)){
-                requestStatistics();
+            try{
+                long now = System.nanoTime();
+                if(now > lastRequestTime+TimeUnit.MILLISECONDS.toNanos(REQUEST_MONITOR_INTERVAL)){
+                    requestStatistics();
+                }
+            }catch (IllegalArgumentException | IllegalStateException | NullPointerException e){
+                srsLogger.warn("Exception occured while sending statistics request : {}",e);
             }
         }
     };
@@ -57,11 +62,24 @@ public class StatisticsRequestScheduler implements DataTransactionListener {
     public StatisticsRequestScheduler(){
         PendingTransactions = (long) 0;
     }
-    
+
     public void addRequestToSchedulerQueue(AbstractStatsTracker statsRequest){
         requestQueue.put(statsRequest, null);
     }
-    
+
+    public void removeRequestsFromSchedulerQueue(NodeRef node){
+        AbstractStatsTracker stats = null;
+        synchronized(requestQueue){
+            Iterator<Map.Entry<AbstractStatsTracker, Integer>> nodesItr = requestQueue.entrySet().iterator();
+            while(nodesItr.hasNext()){
+                stats = nodesItr.next().getKey();
+                if(stats.getNodeRef().equals(node)){
+                    nodesItr.remove();
+                }
+            }
+        }
+
+    }
     public AbstractStatsTracker getNextRequestFromSchedulerQueue(){
         //Remove first element
         AbstractStatsTracker stats = null;
@@ -79,14 +97,11 @@ public class StatisticsRequestScheduler implements DataTransactionListener {
 
     private void requestStatistics(){
         AbstractStatsTracker stats = this.getNextRequestFromSchedulerQueue();
-        if(stats != null) {
-            stats.request();
-            stats.increaseRequestCounter();
-        }
+        sendStatsRequest(stats);
     }
     @Override
     public void onStatusUpdated(DataModificationTransaction transaction, TransactionStatus status) {
-        
+
         AbstractStatsTracker stats = null;
         synchronized(PendingTransactions){
             switch(status){
@@ -106,12 +121,19 @@ public class StatisticsRequestScheduler implements DataTransactionListener {
                 break;
             }
         }
+        sendStatsRequest(stats);
+    }
+
+    private void sendStatsRequest(AbstractStatsTracker stats){
         if(stats != null){
-            stats.request();
-            stats.increaseRequestCounter();
+            try{
+                stats.request();
+                stats.increaseRequestCounter();
+            }catch(Exception e){
+                srsLogger.warn("Statistics request was not sent successfully. Reason : {}",e.getMessage());
+            }
         }
     }
-    
     public void start(){
         timer.schedule(task, 0, REQUEST_MONITOR_INTERVAL);
     }