Merge "Bug 6366 - of-switch-config-pusher - DTCL instead of DTL"
[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
35     private final SyncReactor delegate;
36     private final SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper;
37
38     public SyncReactorGuardDecorator(SyncReactor delegate,
39             SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper) {
40         this.delegate = delegate;
41         this.semaphoreKeeper = semaphoreKeeper;
42     }
43
44     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
45                                             final SyncupEntry syncupEntry) throws InterruptedException {
46         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
47         LOG.trace("syncup guard decorator: {}", nodeId.getValue());
48
49         final long stampBeforeGuard = System.nanoTime();
50         final Semaphore guard = summonGuardAndAcquire(flowcapableNodePath);
51         if (guard == null) {
52             return Futures.immediateFuture(false);
53         }
54         final long stampAfterGuard = System.nanoTime();
55
56         try {
57             if (LOG.isDebugEnabled()) {
58                 LOG.debug("syncup start {} waiting:{} guard:{} thread:{}", nodeId.getValue(),
59                         formatNanos(stampAfterGuard - stampBeforeGuard),
60                         guard, threadName());
61             }
62
63             final ListenableFuture<Boolean> endResult =
64                     delegate.syncup(flowcapableNodePath, syncupEntry);
65
66             Futures.addCallback(endResult, createSyncupCallback(guard, stampBeforeGuard, stampAfterGuard, nodeId));
67             return endResult;
68         } catch (InterruptedException e) {
69             releaseGuardForNodeId(guard);
70             throw e;
71         }
72     }
73
74     private static FutureCallback<Boolean> createSyncupCallback(final Semaphore guard,
75                                                                 final long stampBeforeGuard,
76                                                                 final long stampAfterGuard,
77                                                                 final NodeId nodeId) {
78         return new FutureCallback<Boolean>() {
79             @Override
80             public void onSuccess(@Nullable final Boolean result) {
81                 if (LOG.isDebugEnabled()) {
82                     final long stampFinished = System.nanoTime();
83                     LOG.debug("syncup finished {} took:{} rpc:{} wait:{} guard:{} permits thread:{}", nodeId.getValue(),
84                             formatNanos(stampFinished - stampBeforeGuard), formatNanos(stampFinished - stampAfterGuard),
85                             formatNanos(stampAfterGuard - stampBeforeGuard), guard.availablePermits(), threadName());
86                 }
87                 releaseGuardForNodeId(guard);
88             }
89             @Override
90             public void onFailure(final Throwable t) {
91                 final long stampFinished = System.nanoTime();
92                 LOG.error("syncup failed {} took:{} rpc:{} wait:{} guard:{} permits thread:{}", nodeId.getValue(),
93                         formatNanos(stampFinished - stampBeforeGuard), formatNanos(stampFinished - stampAfterGuard),
94                         formatNanos(stampAfterGuard - stampBeforeGuard), guard.availablePermits(), threadName());
95                 releaseGuardForNodeId(guard);
96             }};
97     }
98
99     private static String formatNanos(long nanos) {
100         return "'" + TimeUnit.NANOSECONDS.toMillis(nanos) + " ms'";
101     }
102
103     /**
104      * Get guard and lock for node.
105      * @param flowcapableNodePath II of node for which guard should be acquired
106      * @return semaphore guard
107      */
108     private Semaphore summonGuardAndAcquire(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath) {
109         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
110         final Semaphore guard = Preconditions.checkNotNull(semaphoreKeeper.summonGuard(flowcapableNodePath),
111                 "no guard for " + nodeId.getValue());
112         try {
113             guard.acquire();
114         } catch (InterruptedException e) {
115             LOG.error("syncup summon {} failed {}", nodeId.getValue(), e);
116             return null;
117         }
118         LOG.trace("syncup summon {} guard:{} thread:{}", nodeId.getValue(), guard, threadName());
119         return guard;
120     }
121
122     /**
123      * Unlock and release guard.
124      * @param guard semaphore guard which should be unlocked
125      */
126     private static void releaseGuardForNodeId(final Semaphore guard) {
127         if (guard != null) {
128             guard.release();
129             LOG.trace("syncup release guard:{} thread:{}", guard, threadName());
130         }
131     }
132
133     private static String threadName() {
134         final Thread currentThread = Thread.currentThread();
135         return currentThread.getName();
136     }
137
138 }