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.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Objects;
16 import java.util.concurrent.Semaphore;
17 import java.util.concurrent.TimeUnit;
18 import javax.annotation.Nullable;
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.openflowplugin.applications.frsync.util.SemaphoreKeeperGuavaImpl;
23 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Decorator for NodeId level syncup locking.
33 public class SyncReactorGuardDecorator implements SyncReactor {
35 private static final Logger LOG = LoggerFactory.getLogger(SyncReactorGuardDecorator.class);
36 private final SyncReactor delegate;
37 private final SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper =
38 new SemaphoreKeeperGuavaImpl<>(1, true);
40 public SyncReactorGuardDecorator(final SyncReactor delegate) {
41 this.delegate = delegate;
44 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
45 final SyncupEntry syncupEntry) {
46 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
47 final long stampBeforeGuard = System.nanoTime();
48 final Semaphore guard = semaphoreKeeper.summonGuardAndAcquire(flowcapableNodePath);
49 if (Objects.isNull(guard)) {
50 return Futures.immediateFuture(Boolean.FALSE);
52 final long stampAfterGuard = System.nanoTime();
54 if (LOG.isDebugEnabled()) {
55 LOG.debug("Syncup guard acquired and running for {} ", nodeId.getValue());
57 final ListenableFuture<Boolean> endResult = delegate.syncup(flowcapableNodePath, syncupEntry);
58 Futures.addCallback(endResult, createSyncupCallback(guard, stampBeforeGuard, stampAfterGuard, nodeId),
59 MoreExecutors.directExecutor());
63 private FutureCallback<Boolean> createSyncupCallback(final Semaphore guard,
64 final long stampBeforeGuard,
65 final long stampAfterGuard,
66 final NodeId nodeId) {
67 return new FutureCallback<Boolean>() {
69 public void onSuccess(@Nullable final Boolean result) {
70 if (LOG.isDebugEnabled()) {
71 final long stampFinished = System.nanoTime();
72 LOG.debug("Syncup finished {} took:{} rpc:{} wait:{}", nodeId.getValue(),
73 formatNanos(stampFinished - stampBeforeGuard), formatNanos(stampFinished - stampAfterGuard),
74 formatNanos(stampAfterGuard - stampBeforeGuard));
76 semaphoreKeeper.releaseGuard(guard);
79 public void onFailure(final Throwable t) {
80 final long stampFinished = System.nanoTime();
81 LOG.warn("Syncup failed {} took:{} rpc:{} wait:{}", nodeId.getValue(),
82 formatNanos(stampFinished - stampBeforeGuard), formatNanos(stampFinished - stampAfterGuard),
83 formatNanos(stampAfterGuard - stampBeforeGuard));
84 semaphoreKeeper.releaseGuard(guard);
88 private static String formatNanos(final long nanos) {
89 return "'" + TimeUnit.NANOSECONDS.toMillis(nanos) + " ms'";