import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
-import io.netty.util.HashedWheelTimer;
-import io.netty.util.Timeout;
-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;
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.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import io.netty.util.HashedWheelTimer;
+import io.netty.util.Timeout;
+import io.netty.util.TimerTask;
public class StatisticsManagerImpl implements StatisticsManager, StatisticsManagerControlService {
private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImpl.class);
private static final long DEFAULT_STATS_TIMEOUT_SEC = 50L;
- private final ConvertorExecutor convertorExecutor;
+ 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);
- private boolean isStatisticsPollingEnabled;
+ private boolean isStatisticsPollingOn;
private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
private final HashedWheelTimer hashedWheelTimer;
}
public StatisticsManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
- final boolean isStatisticsPollingEnabled,
+ final boolean isStatisticsPollingOn,
final HashedWheelTimer hashedWheelTimer,
- final ConvertorExecutor convertorExecutor) {
+ final ConvertorExecutor convertorExecutor,
+ final long basicTimerDelay,
+ final long maximumTimerDelay) {
Preconditions.checkArgument(rpcProviderRegistry != null);
- this.convertorExecutor = convertorExecutor;
- this.controlServiceRegistration = Preconditions.checkNotNull(rpcProviderRegistry.addRpcImplementation(
- StatisticsManagerControlService.class, this));
- this.isStatisticsPollingEnabled = isStatisticsPollingEnabled;
+ 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;
}
@Override
- public void onDeviceContextLevelUp(final DeviceInfo deviceInfo, final LifecycleService lifecycleService) throws Exception {
+ public void onDeviceContextLevelUp(final DeviceInfo deviceInfo,
+ final LifecycleService lifecycleService) throws Exception {
+
+ final StatisticsContext statisticsContext =
+ new StatisticsContextImpl(
+ deviceInfo,
+ isStatisticsPollingOn,
+ lifecycleService,
+ converterExecutor,
+ this);
+
+ Verify.verify(
+ contexts.putIfAbsent(deviceInfo, statisticsContext) == null,
+ "StatisticsCtx still not closed for Node {}", deviceInfo.getLOGValue()
+ );
- final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceInfo, isStatisticsPollingEnabled, lifecycleService, convertorExecutor, 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);
}
final DeviceInfo deviceInfo) {
if (!statisticsContext.isSchedulingEnabled()) {
- LOG.debug("Disabled statistics scheduling for device: {}", deviceInfo.getNodeId().getValue());
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Disabled statistics scheduling for device: {}", deviceInfo.getNodeId().getValue());
+ }
return;
}
- LOG.debug("POLLING ALL STATISTICS for device: {}", deviceInfo.getNodeId());
+ 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: {}", throwable);
+ 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 CancellationException) {
- /* This often happens when something wrong with akka or DS, so closing connection will help to restart device **/
- contexts.get(deviceInfo).getLifecycleService().closeConnection();
+ 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 {
+ LOG.warn("Unexpected error occurred during statistics collection for node {}, rescheduling " +
+ "statistics collections", deviceInfo.getLOGValue(),throwable);
scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
}
}
final DeviceInfo deviceInfo,
final StatisticsContext statisticsContext,
final TimeCounter timeCounter) {
- LOG.debug("SCHEDULING NEXT STATISTICS POLLING for device: {}", deviceInfo.getNodeId());
- if (!isStatisticsPollingEnabled) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("SCHEDULING NEXT STATISTICS POLLING for device: {}", deviceInfo.getNodeId());
+ }
+ if (isStatisticsPollingOn) {
final Timeout pollTimeout = hashedWheelTimer.newTimeout(
timeout -> pollStatistics(
deviceState,
@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 (workModeGuard.tryAcquire()) {
final StatisticsWorkMode targetWorkMode = input.getMode();
if (!workMode.equals(targetWorkMode)) {
- isStatisticsPollingEnabled = StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode);
+ isStatisticsPollingOn = !(StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode));
// iterate through stats-ctx: propagate mode
for (Map.Entry<DeviceInfo, StatisticsContext> entry : contexts.entrySet()) {
final DeviceInfo deviceInfo = entry.getKey();
final StatisticsContext statisticsContext = entry.getValue();
- final DeviceContext deviceContext = statisticsContext.getLifecycleService().getDeviceContext();
+ final DeviceContext deviceContext = statisticsContext.gainDeviceContext();
switch (targetWorkMode) {
case COLLECTALL:
- scheduleNextPolling(deviceContext.getDeviceState(), deviceInfo, statisticsContext, new TimeCounter());
+ scheduleNextPolling(statisticsContext.gainDeviceState(), deviceInfo, statisticsContext, new TimeCounter());
for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
lifeCycleSource.setItemLifecycleListener(null);
}
@Override
public void startScheduling(final DeviceInfo deviceInfo) {
- if (isStatisticsPollingEnabled) {
- LOG.info("Statistics are shut down for device: {}", deviceInfo.getNodeId());
+ if (!isStatisticsPollingOn) {
+ LOG.info("Statistics are shutdown for device: {}", deviceInfo.getNodeId());
return;
}
LOG.info("Scheduling statistics poll for device: {}", deviceInfo.getNodeId());
statisticsContext.setSchedulingEnabled(true);
- final DeviceState deviceState = contexts.get(deviceInfo).getLifecycleService().getDeviceContext().getDeviceState();
- scheduleNextPolling(deviceState, deviceInfo, statisticsContext, new TimeCounter());
+ scheduleNextPolling(
+ statisticsContext.gainDeviceState(),
+ deviceInfo,
+ statisticsContext,
+ new TimeCounter()
+ );
}
@Override
public void stopScheduling(final DeviceInfo deviceInfo) {
- LOG.debug("Stopping statistics scheduling for device: {}", deviceInfo.getNodeId());
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Stopping statistics scheduling for device: {}", deviceInfo.getNodeId());
+ }
+
final StatisticsContext statisticsContext = contexts.get(deviceInfo);
if (statisticsContext == null) {
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
+ public void setIsStatisticsPollingOn(boolean isStatisticsPollingOn){
+ this.isStatisticsPollingOn = isStatisticsPollingOn;
+ }
+
+ 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 <T extends OFPContext> T gainContext(DeviceInfo deviceInfo) {
- return (T) contexts.get(deviceInfo);
+ public void setMaximumTimerDelay(final long maximumTimerDelay) {
+ this.maximumTimerDelay = maximumTimerDelay;
}
}