Merge "Fixed the log level."
[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.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;
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 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());
49
50         final long stampBeforeGuard = System.nanoTime();
51         final Semaphore guard = summonGuardAndAcquire(flowcapableNodePath);//TODO handle InteruptedException
52
53         try {
54             final long stampAfterGuard = System.nanoTime();
55             if (LOG.isDebugEnabled()) {
56                 LOG.debug("syncup start {} waiting:{} guard:{} thread:{}", nodeId.getValue(),
57                         formatNanos(stampAfterGuard - stampBeforeGuard),
58                         guard, threadName());
59             }
60
61             final ListenableFuture<Boolean> endResult =
62                     delegate.syncup(flowcapableNodePath, configTree, operationalTree, dsType);//TODO handle InteruptedException
63
64             Futures.addCallback(endResult, new FutureCallback<Boolean>() {
65                 @Override
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());
74                     }
75
76                     releaseGuardForNodeId(guard);
77                 }
78
79                 @Override
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());
88                     }
89
90                     releaseGuardForNodeId(guard);
91                 }
92             });
93             return endResult;
94         } catch(InterruptedException e) {
95             releaseGuardForNodeId(guard);
96             throw e;
97         }
98     }
99
100     private String formatNanos(long nanos) {
101         return "'" + TimeUnit.NANOSECONDS.toMillis(nanos) + " ms'";
102     }
103
104     /**
105      * Get guard and lock for node.
106      * @param flowcapableNodePath II of node for which guard should be acquired
107      * @return semaphore guard
108      */
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);
113
114         if (LOG.isDebugEnabled()) {
115             final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
116             try {
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);
120             }
121         }
122
123         guard.acquire();
124         return guard;
125     }
126
127     /**
128      * Unlock and release guard.
129      * @param guard semaphore guard which should be unlocked
130      */
131     private void releaseGuardForNodeId(final Semaphore guard) {
132         if (guard == null) {
133             return;
134         }
135         guard.release();
136     }
137
138     private static String threadName() {
139         final Thread currentThread = Thread.currentThread();
140         return currentThread.getName();
141     }
142
143 }