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;
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;
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);
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;
}
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);
}
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>() {
@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);
}
}
@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
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);
}
controlServiceRegistration.close();
controlServiceRegistration = null;
}
+
for (final Iterator<StatisticsContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
iterator.hasNext();) {
iterator.next().close();
@Override
public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
- this.deviceTerminPhaseHandler = handler;
+ this.deviceTerminationPhaseHandler = handler;
}
@Override
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;
+ }
+}