Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 1 | package org.onlab.onos.store.service; |
| 2 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 3 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 1769a1a | 2014-11-19 21:51:44 -0800 | [diff] [blame] | 4 | |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 5 | /** |
| 6 | * A lock is a tool for controlling access to a shared resource by multiple processes. |
| 7 | * Commonly, a lock provides exclusive access to a resource such as a network device |
| 8 | * or exclusive permission to a controller to perform a particular role such as serve |
| 9 | * as the master controller for a device. |
| 10 | * At any given time one and only process can acquire the lock. |
| 11 | */ |
| 12 | public interface Lock { |
| 13 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 14 | /** |
| 15 | * Returns the path this lock will be used to guard from concurrent access. |
| 16 | * @return path. |
| 17 | */ |
| 18 | String path(); |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 19 | |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 20 | /** |
| 21 | * Acquires the lock. |
| 22 | * If the lock is not available then the caller thread becomes |
| 23 | * disabled for thread scheduling purposes and lies dormant until |
| 24 | * the lock has been acquired. |
| 25 | * <p> |
| 26 | * Locks are reentrant. A thread invoking this method multiple times |
| 27 | * without an intervening unlock or lease expiration must invoke unlock() |
| 28 | * the same number of times before the lock is released (unless the lease expires). |
| 29 | * When this method is invoked for a lock that is already acquired, |
| 30 | * the lease time will be set to the maximum of the remaining lease time |
| 31 | * from the previous invocation, or leaseDurationMillis. |
| 32 | * @param leaseDurationMillis the number of milliseconds to hold the |
| 33 | * lock after granting it, before automatically releasing it if it hasn't |
| 34 | * already been released by invoking unlock(). Must be in the range |
Madan Jampani | 32fe780 | 2014-11-11 11:16:47 -0800 | [diff] [blame] | 35 | * (0, LockManager.MAX_LEASE_MILLIS] |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame] | 36 | * @throws InterruptedException if the thread is interrupted while waiting |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 37 | */ |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame] | 38 | void lock(int leaseDurationMillis) throws InterruptedException; |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
Madan Jampani | 1769a1a | 2014-11-19 21:51:44 -0800 | [diff] [blame] | 41 | * Acquires the lock asynchronously. |
| 42 | * @param leaseDurationMillis leaseDurationMillis the number of milliseconds the lock |
| 43 | * will be reserved before it becomes available for others. |
| 44 | * @return Future that can be used for blocking until lock is acquired. |
| 45 | */ |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 46 | CompletableFuture<Void> lockAsync(int leaseDurationMillis); |
Madan Jampani | 1769a1a | 2014-11-19 21:51:44 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 49 | * Acquires the lock only if it is free at the time of invocation. |
| 50 | * @param leaseDurationMillis the number of milliseconds the must be |
| 51 | * locked after it is granted, before automatically releasing it if it hasn't |
| 52 | * already been released by an invocation of unlock(). Must be in the range |
Madan Jampani | 32fe780 | 2014-11-11 11:16:47 -0800 | [diff] [blame] | 53 | * (0, LockManager.MAX_LEASE_MILLIS] |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 54 | * @return true if the lock was acquired and false otherwise |
| 55 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 56 | boolean tryLock(int leaseDurationMillis); |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Acquires the lock if it is free within the given waiting |
| 60 | * time and the current thread has not been interrupted. |
| 61 | * @param waitTimeMillis the maximum time (in milliseconds) to wait for the lock |
| 62 | * @param leaseDurationMillis the number of milliseconds to hold the |
| 63 | * lock after granting it, before automatically releasing it if it hasn't |
| 64 | * already been released by invoking unlock(Object). Must be in the range |
Madan Jampani | 32fe780 | 2014-11-11 11:16:47 -0800 | [diff] [blame] | 65 | * (0, LockManager.MAX_LEASE_MILLIS] |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 66 | * @return true if the lock was acquired and false if the waiting time |
| 67 | * elapsed before the lock was acquired |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame] | 68 | * @throws InterruptedException if the thread is interrupted while waiting |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 69 | */ |
Madan Jampani | 1769a1a | 2014-11-19 21:51:44 -0800 | [diff] [blame] | 70 | boolean tryLock(int waitTimeMillis, int leaseDurationMillis) throws InterruptedException; |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Returns true if this Lock instance currently holds the lock. |
| 74 | * @return true if this instance is the owner of the lock. |
| 75 | */ |
| 76 | boolean isLocked(); |
| 77 | |
| 78 | /** |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 79 | * Returns the epoch for this lock. |
| 80 | * If this lock is currently locked i.e. isLocked() returns true, epoch signifies the logical time |
| 81 | * when the lock was acquired. The concept of epoch lets one come up with a global ordering for all |
| 82 | * lock acquisition events |
| 83 | * @return epoch |
| 84 | */ |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame^] | 85 | long epoch(); |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 88 | * Releases the lock. |
| 89 | */ |
| 90 | void unlock(); |
| 91 | |
| 92 | /** |
Madan Jampani | 32fe780 | 2014-11-11 11:16:47 -0800 | [diff] [blame] | 93 | * Extends the expiration time for a lock that is currently owned |
| 94 | * by a specified duration. The new expiration time is computed |
| 95 | * by adding the specified duration to the current time. If this point |
| 96 | * in time is earlier than the existing expiration time then this method |
| 97 | * has no effect. |
| 98 | * @param leaseDurationMillis extension duration. |
| 99 | * @return true if successfully extended expiration, false if attempt to |
| 100 | * extend expiration fails or if the path is currently not locked by this instance. |
Madan Jampani | f73fb04 | 2014-11-11 10:49:05 -0800 | [diff] [blame] | 101 | */ |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 102 | boolean extendExpiration(int leaseDurationMillis); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 103 | } |