Class EntityLock
Use LockServiceClient
to build instances. Then call requestLock()
.
requestLock()
will contact master to queue the lock and start the heartbeat thread which
will check lock's status periodically and once the lock is acquired, it will send the heartbeats
to the master.
Use await(long, java.util.concurrent.TimeUnit)
or await(long, TimeUnit)
to wait for the lock to be acquired. Always
call unlock()
irrespective of whether lock was acquired or not. If the lock was
acquired, it'll be released. If it was not acquired, it is possible that master grants the lock
in future and the heartbeat thread keeps it alive forever by sending heartbeats. Calling
unlock()
will stop the heartbeat thread and cancel the lock queued on master.
There are 4 ways in which these remote locks may be released/can be lost:
- Call
unlock()
. - Lock times out on master: Can happen because of network issues, GC pauses, etc. Worker thread will call the given abortable as soon as it detects such a situation.
- Fail to contact master: If worker thread can not contact mater and thus fails to send heartbeat before the timeout expires, it assumes that lock is lost and calls the abortable.
- Worker thread is interrupted.
EntityLock lock = lockServiceClient.*Lock(...., "exampled lock", abortable);
lock.requestLock();
....
....can do other initializations here since lock is 'asynchronous'...
....
if (lock.await(timeout)) {
....logic requiring mutual exclusion
}
lock.unlock();
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprivate final Abortable
static final String
private final int
private final CountDownLatch
private final AtomicBoolean
private final org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockRequest
private static final org.slf4j.Logger
private Long
private final org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockService.BlockingInterface
private long
private final EntityLock.LockHeartbeatWorker
-
Constructor Summary
ConstructorDescriptionEntityLock
(org.apache.hadoop.conf.Configuration conf, org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockService.BlockingInterface stub, org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockRequest request, Abortable abort) Abortable.abort() is called when the lease of the lock will expire. -
Method Summary
Modifier and TypeMethodDescriptionvoid
await()
boolean
(package private) EntityLock.LockHeartbeatWorker
boolean
isLocked()
void
Sends rpc to the master to request lock.(package private) void
setTestingSleepTime
(long timeInMillis) toString()
void
unlock()
-
Field Details
-
LOG
-
HEARTBEAT_TIME_BUFFER
- See Also:
-
locked
-
latch
-
stub
private final org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockService.BlockingInterface stub -
worker
-
lockRequest
private final org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockRequest lockRequest -
abort
-
heartbeatTimeBuffer
-
testingSleepTime
-
procId
-
-
Constructor Details
-
EntityLock
EntityLock(org.apache.hadoop.conf.Configuration conf, org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockService.BlockingInterface stub, org.apache.hadoop.hbase.shaded.protobuf.generated.LockServiceProtos.LockRequest request, Abortable abort) Abortable.abort() is called when the lease of the lock will expire. It's up to the user decide if simply abort the process or handle the loss of the lock by aborting the operation that was supposed to be under lock.
-
-
Method Details
-
toString
-
setTestingSleepTime
-
getWorker
-
isLocked
-
requestLock
Sends rpc to the master to request lock. The lock request is queued with other lock requests. Callawait()
to wait on lock. Always callunlock()
after calling the below, even after error.- Throws:
IOException
-
await
- Parameters:
timeout
- in milliseconds. If set to 0, waits indefinitely.- Returns:
- true if lock was acquired; and false if waiting time elapsed before lock could be acquired.
- Throws:
InterruptedException
-
await
- Throws:
InterruptedException
-
unlock
- Throws:
IOException
-