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 javax.annotation.CheckForNull;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.Semaphore;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
20 import com.google.common.annotations.VisibleForTesting;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import com.google.common.base.Verify;
24 import com.google.common.collect.Iterators;
25 import com.google.common.util.concurrent.FutureCallback;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import io.netty.util.HashedWheelTimer;
29 import io.netty.util.Timeout;
30 import io.netty.util.TimerTask;
31 import java.util.Iterator;
33 import java.util.Map.Entry;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.concurrent.ConcurrentMap;
36 import java.util.concurrent.Future;
37 import java.util.concurrent.Semaphore;
38 import java.util.concurrent.TimeUnit;
39 import javax.annotation.CheckForNull;
40 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
41 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
42 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
43 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
44 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
45 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
46 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsWorkMode;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
53 import org.opendaylight.yangtools.yang.common.RpcError;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
62 public class StatisticsManagerImpl implements StatisticsManager, StatisticsManagerControlService {
64 private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImpl.class);
66 private static final long DEFAULT_STATS_TIMEOUT_SEC = 50L;
68 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
70 private HashedWheelTimer hashedWheelTimer;
72 private final ConcurrentMap<DeviceContext, StatisticsContext> contexts = new ConcurrentHashMap<>();
74 private static final long basicTimerDelay = 3000;
75 private static long currentTimerDelay = basicTimerDelay;
76 private static long maximumTimerDelay = 900000; //wait max 15 minutes for next statistics
78 private StatisticsWorkMode workMode = StatisticsWorkMode.COLLECTALL;
79 private final Semaphore workModeGuard = new Semaphore(1, true);
80 private boolean shuttingDownStatisticsPolling;
81 private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
84 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
85 deviceInitPhaseHandler = handler;
88 public StatisticsManagerImpl(@CheckForNull final RpcProviderRegistry rpcProviderRegistry, final boolean shuttingDownStatisticsPolling) {
89 Preconditions.checkArgument(rpcProviderRegistry != null);
90 controlServiceRegistration = rpcProviderRegistry.addRpcImplementation(StatisticsManagerControlService.class, this);
91 this.shuttingDownStatisticsPolling = shuttingDownStatisticsPolling;
95 public void onDeviceContextLevelUp(final DeviceContext deviceContext) throws Exception {
96 LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", deviceContext.getDeviceState().getNodeId(),
97 deviceContext.getDeviceState().getRole());
98 if (null == hashedWheelTimer) {
99 LOG.trace("This is first device that delivered timer. Starting statistics polling immediately.");
100 hashedWheelTimer = deviceContext.getTimer();
102 final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceContext, shuttingDownStatisticsPolling);
104 Verify.verify(contexts.putIfAbsent(deviceContext, statisticsContext) == null, "StatisticsCtx still not closed for Node {}",deviceContext.getDeviceState().getNodeId());
105 deviceContext.addDeviceContextClosedHandler(this);
107 if (shuttingDownStatisticsPolling) {
108 LOG.info("Statistics is shutdown for node:{}", deviceContext.getDeviceState().getNodeId());
110 LOG.info("Schedule Statistics poll for node:{}", deviceContext.getDeviceState().getNodeId());
111 if (OfpRole.BECOMEMASTER.equals(deviceContext.getDeviceState().getRole())) {
112 initialStatPollForMaster(statisticsContext, deviceContext);
113 /* we want to wait for initial statCollecting response */
116 scheduleNextPolling(deviceContext, statisticsContext, new TimeCounter());
118 deviceContext.getDeviceState().setDeviceSynchronized(true);
119 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
122 private void initialStatPollForMaster(final StatisticsContext statisticsContext, final DeviceContext deviceContext) {
123 final ListenableFuture<Boolean> weHaveDynamicData = statisticsContext.gatherDynamicData();
124 Futures.addCallback(weHaveDynamicData, new FutureCallback<Boolean>() {
126 public void onSuccess(final Boolean statisticsGathered) {
127 if (statisticsGathered) {
128 //there are some statistics on device worth gathering
129 contexts.put(deviceContext, statisticsContext);
130 final TimeCounter timeCounter = new TimeCounter();
131 deviceContext.getDeviceState().setStatisticsPollingEnabledProp(true);
132 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
133 LOG.trace("Device dynamic info collecting done. Going to announce raise to next level.");
135 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
136 } catch (final Exception e) {
137 LOG.info("failed to complete levelUp on next handler for device {}", deviceContext.getDeviceState().getNodeId());
138 deviceContext.close();
141 deviceContext.getDeviceState().setDeviceSynchronized(true);
143 final String deviceAdress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress().toString();
144 LOG.info("Statistics for device {} could not be gathered. Closing its device context.", deviceAdress);
145 deviceContext.close();
150 public void onFailure(final Throwable throwable) {
151 LOG.warn("Statistics manager was not able to collect dynamic info for device.", deviceContext.getDeviceState().getNodeId(), throwable);
152 deviceContext.close();
157 private void pollStatistics(final DeviceContext deviceContext,
158 final StatisticsContext statisticsContext,
159 final TimeCounter timeCounter) {
161 if (!deviceContext.getDeviceState().isValid()) {
162 LOG.debug("Session for device {} is not valid.", deviceContext.getDeviceState().getNodeId().getValue());
165 if (!deviceContext.getDeviceState().isStatisticsPollingEnabled()) {
166 LOG.debug("StatisticsPolling is disabled for device: {} , try later", deviceContext.getDeviceState().getNodeId());
167 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
170 if (OfpRole.BECOMESLAVE.equals(deviceContext.getDeviceState().getRole())) {
171 LOG.debug("Role is SLAVE so we don't want to poll any stat for device: {}", deviceContext.getDeviceState().getNodeId());
172 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
176 LOG.debug("POLLING ALL STATS for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
177 timeCounter.markStart();
178 final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
179 Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
181 public void onSuccess(final Boolean o) {
182 timeCounter.addTimeMark();
183 calculateTimerDelay(timeCounter);
184 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
188 public void onFailure(final Throwable throwable) {
189 timeCounter.addTimeMark();
190 LOG.info("Statistics gathering for single node was not successful: {}", throwable.getMessage());
191 LOG.debug("Statistics gathering for single node was not successful.. ", throwable);
192 calculateTimerDelay(timeCounter);
193 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
197 final long averangeTime = TimeUnit.MILLISECONDS.toSeconds(timeCounter.getAverageTimeBetweenMarks());
198 final long STATS_TIMEOUT_SEC = averangeTime > 0 ? 3 * averangeTime : DEFAULT_STATS_TIMEOUT_SEC;
199 final TimerTask timerTask = new TimerTask() {
202 public void run(final Timeout timeout) throws Exception {
203 if (!deviceStatisticsCollectionFuture.isDone()) {
204 LOG.info("Statistics collection for node {} still in progress even after {} secs", deviceContext
205 .getDeviceState().getNodeId(), STATS_TIMEOUT_SEC);
206 deviceStatisticsCollectionFuture.cancel(true);
210 deviceContext.getTimer().newTimeout(timerTask, STATS_TIMEOUT_SEC, TimeUnit.SECONDS);
213 private void scheduleNextPolling(final DeviceContext deviceContext,
214 final StatisticsContext statisticsContext,
215 final TimeCounter timeCounter) {
216 if (null != hashedWheelTimer) {
217 LOG.debug("SCHEDULING NEXT STATS POLLING for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
218 if (!shuttingDownStatisticsPolling) {
219 final Timeout pollTimeout = hashedWheelTimer.newTimeout(new TimerTask() {
221 public void run(final Timeout timeout) throws Exception {
222 pollStatistics(deviceContext, statisticsContext, timeCounter);
224 }, currentTimerDelay, TimeUnit.MILLISECONDS);
225 statisticsContext.setPollTimeout(pollTimeout);
228 LOG.debug("#!NOT SCHEDULING NEXT STATS POLLING for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
233 protected void calculateTimerDelay(final TimeCounter timeCounter) {
234 final long averageStatisticsGatheringTime = timeCounter.getAverageTimeBetweenMarks();
235 if (averageStatisticsGatheringTime > currentTimerDelay) {
236 currentTimerDelay *= 2;
237 if (currentTimerDelay > maximumTimerDelay) {
238 currentTimerDelay = maximumTimerDelay;
241 if (currentTimerDelay > basicTimerDelay) {
242 currentTimerDelay /= 2;
244 currentTimerDelay = basicTimerDelay;
250 protected static long getCurrentTimerDelay() {
251 return currentTimerDelay;
255 public void onDeviceContextClosed(final DeviceContext deviceContext) {
256 final StatisticsContext statisticsContext = contexts.remove(deviceContext);
257 if (null != statisticsContext) {
258 LOG.trace("Removing device context from stack. No more statistics gathering for node {}", deviceContext.getDeviceState().getNodeId());
259 statisticsContext.close();
264 public Future<RpcResult<GetStatisticsWorkModeOutput>> getStatisticsWorkMode() {
265 final GetStatisticsWorkModeOutputBuilder smModeOutputBld = new GetStatisticsWorkModeOutputBuilder();
266 smModeOutputBld.setMode(workMode);
267 return RpcResultBuilder.success(smModeOutputBld.build()).buildFuture();
271 public Future<RpcResult<Void>> changeStatisticsWorkMode(ChangeStatisticsWorkModeInput input) {
272 final Future<RpcResult<Void>> result;
273 // acquire exclusive access
274 if (workModeGuard.tryAcquire()) {
275 final StatisticsWorkMode targetWorkMode = input.getMode();
276 if (!workMode.equals(targetWorkMode)) {
277 shuttingDownStatisticsPolling = StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode);
278 // iterate through stats-ctx: propagate mode
279 for (final Map.Entry<DeviceContext, StatisticsContext> contextEntry : contexts.entrySet()) {
280 final DeviceContext deviceContext = contextEntry.getKey();
281 final StatisticsContext statisticsContext = contextEntry.getValue();
282 switch (targetWorkMode) {
284 scheduleNextPolling(deviceContext, statisticsContext, new TimeCounter());
285 for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
286 lifeCycleSource.setItemLifecycleListener(null);
290 final Optional<Timeout> pollTimeout = statisticsContext.getPollTimeout();
291 if (pollTimeout.isPresent()) {
292 pollTimeout.get().cancel();
294 for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
295 lifeCycleSource.setItemLifecycleListener(statisticsContext.getItemLifeCycleListener());
299 LOG.warn("statistics work mode not supported: {}", targetWorkMode);
302 workMode = targetWorkMode;
304 workModeGuard.release();
305 result = RpcResultBuilder.<Void>success().buildFuture();
307 result = RpcResultBuilder.<Void>failed()
308 .withError(RpcError.ErrorType.APPLICATION, "mode change already in progress")
315 public void close() {
316 if (controlServiceRegistration != null) {
317 controlServiceRegistration.close();
318 controlServiceRegistration = null;
320 for (final Iterator<Entry<DeviceContext, StatisticsContext>> iterator = Iterators
321 .consumingIterator(contexts.entrySet().iterator()); iterator.hasNext();) {
322 iterator.next().getValue().close();