Merge "Revert "Bug 6745 Do not ignore syncup return value""
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorGuardDecorator.java
1 /**
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
10
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.openflowplugin.applications.frsync.SemaphoreKeeper;
19 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
20 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
21 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
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;
27
28 /**
29  * Decorator for NodeId level syncup locking.
30  */
31 public class SyncReactorGuardDecorator implements SyncReactor {
32
33     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorGuardDecorator.class);
34     private final SyncReactor delegate;
35     private final SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper;
36
37     public SyncReactorGuardDecorator(final SyncReactor delegate,
38                                      final SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper) {
39         this.delegate = delegate;
40         this.semaphoreKeeper = semaphoreKeeper;
41     }
42
43     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
44                                             final SyncupEntry syncupEntry) throws InterruptedException {
45         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
46         final long stampBeforeGuard = System.nanoTime();
47         final Semaphore guard = summonGuardAndAcquire(flowcapableNodePath);
48         if (guard == null) {
49             return Futures.immediateFuture(Boolean.FALSE);
50         }
51         final long stampAfterGuard = System.nanoTime();
52
53         try {
54             if (LOG.isDebugEnabled()) {
55                 LOG.debug("Syncup guard acquired and running for {} ", nodeId.getValue());
56             }
57             final ListenableFuture<Boolean> endResult = delegate.syncup(flowcapableNodePath, syncupEntry);
58             Futures.addCallback(endResult, createSyncupCallback(guard, stampBeforeGuard, stampAfterGuard, nodeId));
59             return endResult;
60         } catch (InterruptedException e) {
61             releaseGuard(guard);
62             throw e;
63         }
64     }
65
66     private static FutureCallback<Boolean> createSyncupCallback(final Semaphore guard,
67                                                                 final long stampBeforeGuard,
68                                                                 final long stampAfterGuard,
69                                                                 final NodeId nodeId) {
70         return new FutureCallback<Boolean>() {
71             @Override
72             public void onSuccess(@Nullable final Boolean result) {
73                 if (LOG.isDebugEnabled()) {
74                     final long stampFinished = System.nanoTime();
75                     LOG.debug("Syncup finished {} took:{} rpc:{} wait:{}", nodeId.getValue(),
76                             formatNanos(stampFinished - stampBeforeGuard), formatNanos(stampFinished - stampAfterGuard),
77                             formatNanos(stampAfterGuard - stampBeforeGuard));
78                 }
79                 releaseGuard(guard);
80             }
81             @Override
82             public void onFailure(final Throwable t) {
83                 final long stampFinished = System.nanoTime();
84                 LOG.warn("Syncup failed {} took:{} rpc:{} wait:{}", nodeId.getValue(),
85                         formatNanos(stampFinished - stampBeforeGuard), formatNanos(stampFinished - stampAfterGuard),
86                         formatNanos(stampAfterGuard - stampBeforeGuard));
87                 releaseGuard(guard);
88             }};
89     }
90
91     private static String formatNanos(final long nanos) {
92         return "'" + TimeUnit.NANOSECONDS.toMillis(nanos) + " ms'";
93     }
94
95     /**
96      * Get guard and lock for node.
97      * @param flowcapableNodePath II of node for which guard should be acquired
98      * @return semaphore guard
99      */
100     private Semaphore summonGuardAndAcquire(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath) {
101         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
102         final Semaphore guard = Preconditions.checkNotNull(semaphoreKeeper.summonGuard(flowcapableNodePath),
103                 "No guard for " + nodeId.getValue());
104         try {
105             guard.acquire();
106         } catch (InterruptedException e) {
107             LOG.warn("Syncup summon {} failed {}", nodeId.getValue(), e);
108             return null;
109         }
110         if (LOG.isTraceEnabled()) {
111             LOG.trace("Syncup summon {} guard:{}", nodeId.getValue(), guard);
112         }
113         return guard;
114     }
115
116     /**
117      * Unlock and release guard.
118      * @param guard semaphore guard which should be unlocked
119      */
120     private static void releaseGuard(final Semaphore guard) {
121         if (guard != null) {
122             guard.release();
123             if (LOG.isTraceEnabled()) {
124                 LOG.trace("Syncup release guard:{} thread:{}", guard);
125             }
126         }
127     }
128 }