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