2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.statistics;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Verify;
14 import com.google.common.collect.Iterators;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import io.netty.util.HashedWheelTimer;
19 import io.netty.util.Timeout;
20 import io.netty.util.TimerTask;
21 import java.util.Iterator;
23 import java.util.Optional;
24 import java.util.concurrent.CancellationException;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.ConcurrentMap;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.Semaphore;
29 import java.util.concurrent.TimeUnit;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
32 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
33 import org.opendaylight.openflowplugin.api.ConnectionException;
34 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
38 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
39 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
40 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
41 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
42 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
43 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
44 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsWorkMode;
50 import org.opendaylight.yangtools.yang.common.RpcError;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class StatisticsManagerImpl implements StatisticsManager, StatisticsManagerControlService {
58 private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImpl.class);
60 private static final long DEFAULT_STATS_TIMEOUT_SEC = 50L;
61 private final ConvertorExecutor converterExecutor;
63 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
64 private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
66 private final ConcurrentMap<DeviceInfo, StatisticsContext> contexts = new ConcurrentHashMap<>();
68 private static long basicTimerDelay;
69 private static long currentTimerDelay;
70 private static long maximumTimerDelay; //wait time for next statistics
72 private StatisticsWorkMode workMode = StatisticsWorkMode.COLLECTALL;
73 private final Semaphore workModeGuard = new Semaphore(1, true);
74 private boolean isStatisticsPollingOn;
75 private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
77 private final HashedWheelTimer hashedWheelTimer;
80 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
81 deviceInitPhaseHandler = handler;
84 public StatisticsManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
85 final boolean isStatisticsPollingOn,
86 final HashedWheelTimer hashedWheelTimer,
87 final ConvertorExecutor convertorExecutor,
88 final long basicTimerDelay,
89 final long maximumTimerDelay) {
90 Preconditions.checkArgument(rpcProviderRegistry != null);
91 this.converterExecutor = convertorExecutor;
92 this.controlServiceRegistration = Preconditions.checkNotNull(
93 rpcProviderRegistry.addRpcImplementation(StatisticsManagerControlService.class, this)
95 this.isStatisticsPollingOn = isStatisticsPollingOn;
96 this.basicTimerDelay = basicTimerDelay;
97 this.currentTimerDelay = basicTimerDelay;
98 this.maximumTimerDelay = maximumTimerDelay;
99 this.hashedWheelTimer = hashedWheelTimer;
103 public void onDeviceContextLevelUp(final DeviceInfo deviceInfo,
104 final LifecycleService lifecycleService) throws Exception {
106 final StatisticsContext statisticsContext =
107 new StatisticsContextImpl(
109 isStatisticsPollingOn,
115 contexts.putIfAbsent(deviceInfo, statisticsContext) == null,
116 "StatisticsCtx still not closed for Node {}", deviceInfo.getLOGValue()
119 lifecycleService.setStatContext(statisticsContext);
120 lifecycleService.registerDeviceRemovedHandler(this);
121 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo, lifecycleService);
125 void pollStatistics(final DeviceState deviceState,
126 final StatisticsContext statisticsContext,
127 final TimeCounter timeCounter,
128 final DeviceInfo deviceInfo) {
130 if (!statisticsContext.isSchedulingEnabled()) {
131 if (LOG.isDebugEnabled()) {
132 LOG.debug("Disabled statistics scheduling for device: {}", deviceInfo.getNodeId().getValue());
137 if (LOG.isDebugEnabled()) {
138 LOG.debug("POLLING ALL STATISTICS for device: {}", deviceInfo.getNodeId());
141 timeCounter.markStart();
142 final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
143 Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
145 public void onSuccess(final Boolean o) {
146 timeCounter.addTimeMark();
147 calculateTimerDelay(timeCounter);
148 scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
152 public void onFailure(@Nonnull final Throwable throwable) {
153 timeCounter.addTimeMark();
154 LOG.warn("Statistics gathering for single node {} was not successful: {}", deviceInfo.getLOGValue(),
155 throwable.getMessage());
156 if (LOG.isTraceEnabled()) {
157 LOG.trace("Gathering for node {} failure: ", deviceInfo.getLOGValue(), throwable);
159 calculateTimerDelay(timeCounter);
160 if (throwable instanceof ConnectionException) {
161 // ConnectionException is raised by StatisticsContextImpl class when the connections
162 // move to RIP state. In this particular case, there is no need to reschedule
163 // because this statistics manager should be closed soon
164 LOG.warn("Node {} is no more connected, stopping the statistics collection",
165 deviceInfo.getLOGValue(),throwable);
166 stopScheduling(deviceInfo);
168 if (!(throwable instanceof CancellationException)) {
169 LOG.warn("Unexpected error occurred during statistics collection for node {}, rescheduling " +
170 "statistics collections", deviceInfo.getLOGValue(),throwable);
172 scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
177 final long averageTime = TimeUnit.MILLISECONDS.toSeconds(timeCounter.getAverageTimeBetweenMarks());
178 final long statsTimeoutSec = averageTime > 0 ? 3 * averageTime : DEFAULT_STATS_TIMEOUT_SEC;
179 final TimerTask timerTask = timeout -> {
180 if (!deviceStatisticsCollectionFuture.isDone()) {
181 LOG.info("Statistics collection for node {} still in progress even after {} secs", deviceInfo.getLOGValue(), statsTimeoutSec);
182 deviceStatisticsCollectionFuture.cancel(true);
186 hashedWheelTimer.newTimeout(timerTask, statsTimeoutSec, TimeUnit.SECONDS);
189 private void scheduleNextPolling(final DeviceState deviceState,
190 final DeviceInfo deviceInfo,
191 final StatisticsContext statisticsContext,
192 final TimeCounter timeCounter) {
193 if (LOG.isDebugEnabled()) {
194 LOG.debug("SCHEDULING NEXT STATISTICS POLLING for device: {}", deviceInfo.getNodeId());
196 if (isStatisticsPollingOn) {
197 final Timeout pollTimeout = hashedWheelTimer.newTimeout(
198 timeout -> pollStatistics(
204 TimeUnit.MILLISECONDS);
205 statisticsContext.setPollTimeout(pollTimeout);
210 void calculateTimerDelay(final TimeCounter timeCounter) {
211 final long averageStatisticsGatheringTime = timeCounter.getAverageTimeBetweenMarks();
212 if (averageStatisticsGatheringTime > currentTimerDelay) {
213 currentTimerDelay *= 2;
214 if (currentTimerDelay > maximumTimerDelay) {
215 currentTimerDelay = maximumTimerDelay;
218 if (currentTimerDelay > basicTimerDelay) {
219 currentTimerDelay /= 2;
221 currentTimerDelay = basicTimerDelay;
227 static long getCurrentTimerDelay() {
228 return currentTimerDelay;
232 public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
233 Optional.ofNullable(contexts.get(deviceInfo)).ifPresent(OFPContext::close);
234 deviceTerminationPhaseHandler.onDeviceContextLevelDown(deviceInfo);
238 public Future<RpcResult<GetStatisticsWorkModeOutput>> getStatisticsWorkMode() {
239 final GetStatisticsWorkModeOutputBuilder smModeOutputBld = new GetStatisticsWorkModeOutputBuilder();
240 smModeOutputBld.setMode(workMode);
241 return RpcResultBuilder.success(smModeOutputBld.build()).buildFuture();
245 public Future<RpcResult<Void>> changeStatisticsWorkMode(ChangeStatisticsWorkModeInput input) {
246 final Future<RpcResult<Void>> result;
247 // acquire exclusive access
248 if (workModeGuard.tryAcquire()) {
249 final StatisticsWorkMode targetWorkMode = input.getMode();
250 if (!workMode.equals(targetWorkMode)) {
251 isStatisticsPollingOn = !(StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode));
252 // iterate through stats-ctx: propagate mode
253 for (Map.Entry<DeviceInfo, StatisticsContext> entry : contexts.entrySet()) {
254 final DeviceInfo deviceInfo = entry.getKey();
255 final StatisticsContext statisticsContext = entry.getValue();
256 final DeviceContext deviceContext = statisticsContext.gainDeviceContext();
257 switch (targetWorkMode) {
259 scheduleNextPolling(statisticsContext.gainDeviceState(), deviceInfo, statisticsContext, new TimeCounter());
260 for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
261 lifeCycleSource.setItemLifecycleListener(null);
265 final Optional<Timeout> pollTimeout = statisticsContext.getPollTimeout();
266 if (pollTimeout.isPresent()) {
267 pollTimeout.get().cancel();
269 for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
270 lifeCycleSource.setItemLifecycleListener(statisticsContext.getItemLifeCycleListener());
274 LOG.warn("Statistics work mode not supported: {}", targetWorkMode);
277 workMode = targetWorkMode;
279 workModeGuard.release();
280 result = RpcResultBuilder.<Void>success().buildFuture();
282 result = RpcResultBuilder.<Void>failed()
283 .withError(RpcError.ErrorType.APPLICATION, "mode change already in progress")
290 public void startScheduling(final DeviceInfo deviceInfo) {
291 if (!isStatisticsPollingOn) {
292 LOG.info("Statistics are shutdown for device: {}", deviceInfo.getNodeId());
296 final StatisticsContext statisticsContext = contexts.get(deviceInfo);
298 if (statisticsContext == null) {
299 LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
303 if (statisticsContext.isSchedulingEnabled()) {
304 LOG.debug("Statistics scheduling is already enabled for device: {}", deviceInfo.getNodeId());
308 LOG.info("Scheduling statistics poll for device: {}", deviceInfo.getNodeId());
310 statisticsContext.setSchedulingEnabled(true);
312 statisticsContext.gainDeviceState(),
320 public void stopScheduling(final DeviceInfo deviceInfo) {
321 if (LOG.isDebugEnabled()) {
322 LOG.debug("Stopping statistics scheduling for device: {}", deviceInfo.getNodeId());
325 final StatisticsContext statisticsContext = contexts.get(deviceInfo);
327 if (statisticsContext == null) {
328 LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
332 statisticsContext.setSchedulingEnabled(false);
336 public void close() {
337 if (controlServiceRegistration != null) {
338 controlServiceRegistration.close();
339 controlServiceRegistration = null;
342 for (final Iterator<StatisticsContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
343 iterator.hasNext();) {
344 iterator.next().close();
349 public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
350 this.deviceTerminationPhaseHandler = handler;
354 public void setIsStatisticsPollingOn(boolean isStatisticsPollingOn){
355 this.isStatisticsPollingOn = isStatisticsPollingOn;
358 public void onDeviceRemoved(DeviceInfo deviceInfo) {
359 contexts.remove(deviceInfo);
360 LOG.debug("Statistics context removed for node {}", deviceInfo.getLOGValue());
364 public void setBasicTimerDelay(final long basicTimerDelay) {
365 this.basicTimerDelay = basicTimerDelay;
369 public void setMaximumTimerDelay(final long maximumTimerDelay) {
370 this.maximumTimerDelay = maximumTimerDelay;