b42230971b2fe276ea5bc8d39f25a3d2f455de83
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / OperationLimiter.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.dispatch.OnComplete;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import java.util.concurrent.Semaphore;
14 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
16 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Class for limiting operations. It extends {@link OnComplete}, so we can plug it seamlessly
22  * into akka to release permits as futures complete.
23  */
24 public class OperationLimiter extends OnComplete<Object> {
25     private static final Logger LOG = LoggerFactory.getLogger(OperationLimiter.class);
26     private final TransactionIdentifier identifier;
27     private final long acquireTimeout;
28     private final Semaphore semaphore;
29
30     OperationLimiter(final TransactionIdentifier identifier, final int maxPermits, final int acquireTimeoutSeconds) {
31         this.identifier = Preconditions.checkNotNull(identifier);
32
33         Preconditions.checkArgument(acquireTimeoutSeconds >= 0);
34         this.acquireTimeout = TimeUnit.SECONDS.toNanos(acquireTimeoutSeconds);
35
36         Preconditions.checkArgument(maxPermits >= 0);
37         this.semaphore = new Semaphore(maxPermits);
38     }
39
40     void acquire() {
41         acquire(1);
42     }
43
44     private void acquire(final int acquirePermits) {
45         try {
46             if (!semaphore.tryAcquire(acquirePermits, acquireTimeout, TimeUnit.NANOSECONDS)) {
47                 LOG.warn("Failed to acquire operation permit for transaction {}", identifier);
48             }
49         } catch (InterruptedException e) {
50             if (LOG.isDebugEnabled()) {
51                 LOG.debug("Interrupted when trying to acquire operation permit for transaction {}", identifier, e);
52             } else {
53                 LOG.warn("Interrupted when trying to acquire operation permit for transaction {}", identifier);
54             }
55         }
56     }
57
58     void release() {
59         this.semaphore.release();
60     }
61
62     @Override
63     public void onComplete(final Throwable throwable, final Object message) {
64         if (message instanceof BatchedModificationsReply) {
65             this.semaphore.release(((BatchedModificationsReply)message).getNumBatched());
66         } else {
67             this.semaphore.release();
68         }
69     }
70
71     public TransactionIdentifier getIdentifier() {
72         return identifier;
73     }
74
75     @VisibleForTesting
76     Semaphore getSemaphore() {
77         return semaphore;
78     }
79 }