Package com.teracloud.streams.dl
Interface Lock
public interface Lock
This is an interface defining the available distributed lock (dl) methods.
A lock implementation class will provide concrete logic for these methods.
Use
Example usage:
LockFactory
to get a usable instance of this class.
Example usage:
LockFactory lf = DistributedLocks.getLockFactory();
Lock myLock = lf.createOrGetLock("Lock_For_Test_Store1");
// Acquire the lock
try {
myLock.acquireLock();
} catch (LockException le) {
System.out.println("Unable to acquire the lock named 'Lock_For_Test_Store1'. Error = " +
le.getErrorCode() + ", Error msg = " + le.getErrorMessage());
throw le;
}
System.out.println("Successfully acquired the lock named 'Lock_For_Test_Store1'.");
// Exclusive access to the distributed lock obtained.
// Perform your operations on a shared data store safely now.
// ....
// ....
//release the lock
try {
myLock.releaseLock();
} catch (LockException le) {}
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Acquire a distributed lock before accessing a critical shared resource.void
acquireLock
(double leaseTime, double maxWaitTimeToAcquireLock) Acquire a distributed lock with lease time and max wait time before accessing a critical shared resourcelong
getId()
Get the id for this lockvoid
Release ownership of the lock.
-
Method Details
-
getId
long getId()Get the id for this lock- Returns:
- lock id
-
acquireLock
Acquire a distributed lock before accessing a critical shared resource.IMPORTANT NOTE: Once called, this function could spin loop for a very long time (three or four minutes) until it acquires the distributed lock or it times out due to the distributed lock being not yet available. Another important factor is that once the lock is granted, it is given to you for an infinite period of time. Hence, it is your responsibility to properly release the lock at the end of your task via
releaseLock
. There is no external mechanism to forcibly release a lock. If either the behavior of waiting for four minutes to acquire the lock or the indefinite allocation of lock to yourself is undesired, then use the alternative overloaded function,acquireLock
, instead.- Throws:
LockException
- if the lock could not be acquired
-
acquireLock
Acquire a distributed lock with lease time and max wait time before accessing a critical shared resource- Parameters:
leaseTime
- time, in seconds, for which the lock will be owned by the caller. If the caller doesn't release the lock after the requested lease time due to a program crash or a programmatic error, DPS internal code will automatically release the stale lock for others to acquire.maxWaitTimeToAcquireLock
- maximum amount of time, in seconds, the caller is willing to wait to acquire the lock. If the lock acquisition is not done within the specified max wait time, this API will exit with an error instead of spin looping forever.- Throws:
LockException
- if the lock could not be acquired within the time specified.
-
releaseLock
Release ownership of the lock. It will be made available for other callers to acquire.- Throws:
LockException
- if an error occurs
-