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