- void pollStatistics(final DeviceState deviceState,
- final StatisticsContext statisticsContext,
- final TimeCounter timeCounter,
- final DeviceInfo deviceInfo) {
-
- if (!statisticsContext.isSchedulingEnabled()) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Disabled statistics scheduling for device: {}", deviceInfo.getNodeId().getValue());
- }
- return;
- }
-
- 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 onSuccess(final Boolean o) {
- timeCounter.addTimeMark();
- calculateTimerDelay(timeCounter);
- scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
- }
-
- @Override
- public void onFailure(@Nonnull final Throwable throwable) {
- timeCounter.addTimeMark();
- 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 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);
- }
- }
- });
-
- final long averageTime = TimeUnit.MILLISECONDS.toSeconds(timeCounter.getAverageTimeBetweenMarks());
- final long statsTimeoutSec = averageTime > 0 ? 3 * averageTime : DEFAULT_STATS_TIMEOUT_SEC;
- final TimerTask timerTask = timeout -> {
- if (!deviceStatisticsCollectionFuture.isDone()) {
- LOG.info("Statistics collection for node {} still in progress even after {} secs", deviceInfo.getLOGValue(), statsTimeoutSec);
- deviceStatisticsCollectionFuture.cancel(true);
- }
- };
-
- hashedWheelTimer.newTimeout(timerTask, statsTimeoutSec, TimeUnit.SECONDS);
- }
-
- private void scheduleNextPolling(final DeviceState deviceState,
- final DeviceInfo deviceInfo,
- final StatisticsContext statisticsContext,
- final TimeCounter timeCounter) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("SCHEDULING NEXT STATISTICS POLLING for device: {}", deviceInfo.getNodeId());
- }
- if (isStatisticsPollingOn) {
- final Timeout pollTimeout = hashedWheelTimer.newTimeout(
- timeout -> pollStatistics(
- deviceState,
- statisticsContext,
- timeCounter,
- deviceInfo),
- currentTimerDelay,
- TimeUnit.MILLISECONDS);
- statisticsContext.setPollTimeout(pollTimeout);
- }