Simplify code using Java 8 features
[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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.Semaphore;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Class for limiting operations.
20  */
21 public class OperationLimiter  {
22     private static final Logger LOG = LoggerFactory.getLogger(OperationLimiter.class);
23     private final TransactionIdentifier identifier;
24     private final long acquireTimeout;
25     private final Semaphore semaphore;
26     private final int maxPermits;
27
28     OperationLimiter(final TransactionIdentifier identifier, final int maxPermits, final long acquireTimeoutSeconds) {
29         this.identifier = Preconditions.checkNotNull(identifier);
30
31         Preconditions.checkArgument(acquireTimeoutSeconds >= 0);
32         this.acquireTimeout = TimeUnit.SECONDS.toNanos(acquireTimeoutSeconds);
33
34         Preconditions.checkArgument(maxPermits >= 0);
35         this.maxPermits = maxPermits;
36         this.semaphore = new Semaphore(maxPermits);
37     }
38
39     boolean acquire() {
40         return acquire(1);
41     }
42
43     boolean acquire(final int acquirePermits) {
44         try {
45             if (semaphore.tryAcquire(acquirePermits, acquireTimeout, TimeUnit.NANOSECONDS)) {
46                 return true;
47             }
48         } catch (InterruptedException e) {
49             if (LOG.isDebugEnabled()) {
50                 LOG.debug("Interrupted when trying to acquire operation permit for transaction {}", identifier, e);
51             } else {
52                 LOG.warn("Interrupted when trying to acquire operation permit for transaction {}", identifier);
53             }
54         }
55
56         return false;
57     }
58
59     void release() {
60         release(1);
61     }
62
63     void release(int permits) {
64         this.semaphore.release(permits);
65     }
66
67     @VisibleForTesting
68     TransactionIdentifier getIdentifier() {
69         return identifier;
70     }
71
72     @VisibleForTesting
73     int availablePermits() {
74         return semaphore.availablePermits();
75     }
76
77     /**
78      * Release all the permits.
79      */
80     public void releaseAll() {
81         this.semaphore.release(maxPermits - availablePermits());
82     }
83 }