Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsManagerImpl.java
index 3b01989f0d0d65618f021c81ff7451ceba7f60c8..06dc64de2688f7183a14cf35543128e70912402d 100644 (file)
@@ -21,6 +21,7 @@ import io.netty.util.TimerTask;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Optional;
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.Future;
@@ -29,6 +30,8 @@ import java.util.concurrent.TimeUnit;
 import javax.annotation.Nonnull;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.openflowplugin.api.ConnectionException;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
@@ -58,13 +61,13 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
     private final ConvertorExecutor converterExecutor;
 
     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
-    private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
+    private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
 
     private final ConcurrentMap<DeviceInfo, StatisticsContext> contexts = new ConcurrentHashMap<>();
 
-    private static final long basicTimerDelay = 3000;
-    private static long currentTimerDelay = basicTimerDelay;
-    private static final long maximumTimerDelay = 900000; //wait max 15 minutes for next statistics
+    private static long basicTimerDelay;
+    private static long currentTimerDelay;
+    private static long maximumTimerDelay; //wait time for next statistics
 
     private StatisticsWorkMode workMode = StatisticsWorkMode.COLLECTALL;
     private final Semaphore workModeGuard = new Semaphore(1, true);
@@ -81,13 +84,18 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
     public StatisticsManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
                                  final boolean isStatisticsPollingOn,
                                  final HashedWheelTimer hashedWheelTimer,
-                                 final ConvertorExecutor convertorExecutor) {
+                                 final ConvertorExecutor convertorExecutor,
+                                 final long basicTimerDelay,
+                                 final long maximumTimerDelay) {
         Preconditions.checkArgument(rpcProviderRegistry != null);
            this.converterExecutor = convertorExecutor;
         this.controlServiceRegistration = Preconditions.checkNotNull(
                 rpcProviderRegistry.addRpcImplementation(StatisticsManagerControlService.class, this)
         );
         this.isStatisticsPollingOn = isStatisticsPollingOn;
+        this.basicTimerDelay = basicTimerDelay;
+        this.currentTimerDelay = basicTimerDelay;
+        this.maximumTimerDelay = maximumTimerDelay;
         this.hashedWheelTimer = hashedWheelTimer;
     }
 
@@ -102,11 +110,14 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
                         lifecycleService,
                         converterExecutor,
                         this);
+
         Verify.verify(
                 contexts.putIfAbsent(deviceInfo, statisticsContext) == null,
                 "StatisticsCtx still not closed for Node {}", deviceInfo.getLOGValue()
         );
+
         lifecycleService.setStatContext(statisticsContext);
+        lifecycleService.registerDeviceRemovedHandler(this);
         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo, lifecycleService);
     }
 
@@ -126,6 +137,7 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
         if (LOG.isDebugEnabled()) {
             LOG.debug("POLLING ALL STATISTICS for device: {}", deviceInfo.getNodeId());
         }
+
         timeCounter.markStart();
         final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
         Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
@@ -139,14 +151,24 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
             @Override
             public void onFailure(@Nonnull final Throwable throwable) {
                 timeCounter.addTimeMark();
-                LOG.warn("Statistics gathering for single node {} was not successful: ", deviceInfo.getLOGValue(), throwable.getMessage());
+                LOG.warn("Statistics gathering for single node {} was not successful: {}", deviceInfo.getLOGValue(),
+                        throwable.getMessage());
                 if (LOG.isTraceEnabled()) {
                     LOG.trace("Gathering for node {} failure: ", deviceInfo.getLOGValue(), throwable);
                 }
                 calculateTimerDelay(timeCounter);
-                if (throwable instanceof IllegalStateException) {
+                if (throwable instanceof ConnectionException) {
+                    // ConnectionException is raised by StatisticsContextImpl class when the connections
+                    // move to RIP state. In this particular case, there is no need to reschedule
+                    // because this statistics manager should be closed soon
+                    LOG.warn("Node {} is no more connected, stopping the statistics collection",
+                            deviceInfo.getLOGValue(),throwable);
                     stopScheduling(deviceInfo);
                 } else {
+                    if (!(throwable instanceof CancellationException)) {
+                        LOG.warn("Unexpected error occurred during statistics collection for node {}, rescheduling " +
+                                "statistics collections", deviceInfo.getLOGValue(),throwable);
+                    }
                     scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
                 }
             }
@@ -208,12 +230,8 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
 
     @Override
     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
-        final StatisticsContext statisticsContext = contexts.remove(deviceInfo);
-        if (null != statisticsContext) {
-            LOG.debug("Removing device context from stack. No more statistics gathering for device: {}", deviceInfo.getLOGValue());
-            statisticsContext.close();
-        }
-        deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
+        Optional.ofNullable(contexts.get(deviceInfo)).ifPresent(OFPContext::close);
+        deviceTerminationPhaseHandler.onDeviceContextLevelDown(deviceInfo);
     }
 
     @Override
@@ -303,12 +321,14 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
         if (LOG.isDebugEnabled()) {
             LOG.debug("Stopping statistics scheduling for device: {}", deviceInfo.getNodeId());
         }
+
         final StatisticsContext statisticsContext = contexts.get(deviceInfo);
 
         if (statisticsContext == null) {
             LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
             return;
         }
+
         statisticsContext.setSchedulingEnabled(false);
     }
 
@@ -318,6 +338,7 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
             controlServiceRegistration.close();
             controlServiceRegistration = null;
         }
+
         for (final Iterator<StatisticsContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
                 iterator.hasNext();) {
             iterator.next().close();
@@ -326,7 +347,7 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
 
     @Override
     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
-        this.deviceTerminPhaseHandler = handler;
+        this.deviceTerminationPhaseHandler = handler;
     }
 
     @Override
@@ -334,4 +355,18 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
         this.isStatisticsPollingOn = isStatisticsPollingOn;
     }
 
-}
\ No newline at end of file
+    public void onDeviceRemoved(DeviceInfo deviceInfo) {
+        contexts.remove(deviceInfo);
+        LOG.debug("Statistics context removed for node {}", deviceInfo.getLOGValue());
+    }
+
+    @Override
+    public void setBasicTimerDelay(final long basicTimerDelay) {
+        this.basicTimerDelay = basicTimerDelay;
+    }
+
+    @Override
+    public void setMaximumTimerDelay(final long maximumTimerDelay) {
+        this.maximumTimerDelay = maximumTimerDelay;
+    }
+}