2 * Copyright (c) 2016 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.applications.frsync.impl;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.concurrent.Semaphore;
16 import java.util.concurrent.TimeUnit;
17 import javax.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.openflowplugin.applications.frsync.SemaphoreKeeper;
20 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
21 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Decorator for NodeId level syncup locking.
31 public class SyncReactorGuardDecorator implements SyncReactor {
33 private static final Logger LOG = LoggerFactory.getLogger(SyncReactorGuardDecorator.class);
35 private final SyncReactor delegate;
36 private final SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper;
38 public SyncReactorGuardDecorator(SyncReactor delegate,
39 SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper) {
40 this.delegate = delegate;
41 this.semaphoreKeeper = semaphoreKeeper;
44 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
45 final FlowCapableNode configTree, final FlowCapableNode operationalTree,
46 final LogicalDatastoreType dsType) throws InterruptedException {
47 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
48 LOG.trace("syncup guard {}", nodeId.getValue());
50 final long stampBeforeGuard = System.nanoTime();
51 final Semaphore guard = summonGuardAndAcquire(flowcapableNodePath);//TODO handle InteruptedException
54 final long stampAfterGuard = System.nanoTime();
55 if (LOG.isDebugEnabled()) {
56 LOG.debug("syncup start {} waiting:{} guard:{} thread:{}", nodeId.getValue(),
57 formatNanos(stampAfterGuard - stampBeforeGuard),
61 final ListenableFuture<Boolean> endResult =
62 delegate.syncup(flowcapableNodePath, configTree, operationalTree, dsType);//TODO handle InteruptedException
64 Futures.addCallback(endResult, new FutureCallback<Boolean>() {
66 public void onSuccess(@Nullable final Boolean result) {
67 if (LOG.isDebugEnabled()) {
68 final long stampFinished = System.nanoTime();
69 LOG.debug("syncup finished {} took:{} rpc:{} wait:{} guard:{} permits thread:{}", nodeId.getValue(),
70 formatNanos(stampFinished - stampBeforeGuard),
71 formatNanos(stampFinished - stampAfterGuard),
72 formatNanos(stampAfterGuard - stampBeforeGuard),
73 guard.availablePermits(), threadName());
76 releaseGuardForNodeId(guard);
80 public void onFailure(final Throwable t) {
81 if (LOG.isDebugEnabled()) {
82 final long stampFinished = System.nanoTime();
83 LOG.warn("syncup failed {} took:{} rpc:{} wait:{} guard:{} permits thread:{}", nodeId.getValue(),
84 formatNanos(stampFinished - stampBeforeGuard),
85 formatNanos(stampFinished - stampAfterGuard),
86 formatNanos(stampAfterGuard - stampBeforeGuard),
87 guard.availablePermits(), threadName());
90 releaseGuardForNodeId(guard);
94 } catch(InterruptedException e) {
95 releaseGuardForNodeId(guard);
100 private String formatNanos(long nanos) {
101 return "'" + TimeUnit.NANOSECONDS.toMillis(nanos) + " ms'";
105 * Get guard and lock for node.
106 * @param flowcapableNodePath II of node for which guard should be acquired
107 * @return semaphore guard
109 private Semaphore summonGuardAndAcquire(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath)
110 throws InterruptedException {
111 final Semaphore guard = Preconditions.checkNotNull(semaphoreKeeper.summonGuard(flowcapableNodePath),
112 "no guard for " + flowcapableNodePath);
114 if (LOG.isDebugEnabled()) {
115 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
117 LOG.debug("syncup summon {} guard:{} thread:{}", nodeId.getValue(), guard, threadName());
118 } catch (Exception e) {
119 LOG.error("error logging guard after summon before aquiring {}", nodeId);
128 * Unlock and release guard.
129 * @param guard semaphore guard which should be unlocked
131 private void releaseGuardForNodeId(final Semaphore guard) {
138 private static String threadName() {
139 final Thread currentThread = Thread.currentThread();
140 return currentThread.getName();