Class EntityLock

java.lang.Object
org.apache.hadoop.hbase.client.locking.EntityLock

@Public public class EntityLock extends Object
Lock for HBase Entity either a Table, a Namespace, or Regions. These are remote locks which live on master, and need periodic heartbeats to keep them alive. (Once we request the lock, internally an heartbeat thread will be started on the client). If master does not receive the heartbeat in time, it'll release the lock and make it available to other users.

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.
Use example: 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();