Interface FencedLockManager

  • All Superinterfaces:
    Lifecycle
    All Known Implementing Classes:
    DBFencedLockManager

    public interface FencedLockManager
    extends Lifecycle
    This library provides a Distributed Locking Manager based of the Fenced Locking concept described here

    The FencedLockManager is responsible for obtaining and managing distributed FencedLock's, which are named exclusive locks.
    Only one FencedLockManager instance can acquire a FencedLock at a time.
    The implementation has been on supporting intra-service (i.e. across different deployed instances of the same service) Lock support through database based implementations (MongoFencedLockManager and PostgresqlFencedLockManager).
    In a service oriented architecture it's common for all deployed instances of a given service (e.g. a Sales service) to share the same underlying database(s). As long as the different deployed (Sales) services instances can share the same underlying database, then you use the FencedLockManager concept for handling distributed locks across all deployed (Sales service) instances in the cluster.
    If you need cross-service lock support, e.g. across instances of different services (such as across Sales, Billing and Shipping services), then you need to use a dedicated distributed locking service such as Zookeeper.

    To coordinate this properly it's important that each getLockManagerInstanceId() is unique across the cluster.
    Per default, it uses the local machine hostname as getLockManagerInstanceId() value.
    • Method Detail

      • tryAcquireLock

        Optional<FencedLock> tryAcquireLock​(LockName lockName)
        Try to acquire the lock using this Lock Manager instance
        Parameters:
        lockName - the name of the lock
        Returns:
        An Optional with the FencedLock IF it was acquired by the FencedLockManager on this JVM Node, otherwise it return an empty Optional
      • tryAcquireLock

        Optional<FencedLock> tryAcquireLock​(LockName lockName,
                                            Duration timeout)
        Try to acquire the lock on this JVM Node
        Parameters:
        lockName - the name of the lock
        timeout - timeout for the lock acquiring
        Returns:
        An Optional with the FencedLock IF it was acquired by the FencedLockManager on this JVM Node, otherwise it return an empty Optional
      • acquireLock

        FencedLock acquireLock​(LockName lockName)
        Acquire the lock on this JVM Node. If the lock already is acquired by another JVM Node, then this method will wait until the lock can be acquired by this JVM node
        Parameters:
        lockName - the name of the lock
        Returns:
        The FencedLock when can be acquired by this JVM Node
      • isLockAcquired

        boolean isLockAcquired​(LockName lockName)
        Is the lock acquired
        Parameters:
        lockName - the name of the lock
        Returns:
        true if the lock is acquired, otherwise false
      • isLockedByThisLockManagerInstance

        boolean isLockedByThisLockManagerInstance​(LockName lockName)
        Is the lock already acquired by this JVM node
        Parameters:
        lockName - the lock name
        Returns:
        true of the lock is acquired by this JVM node
      • isLockAcquiredByAnotherLockManagerInstance

        boolean isLockAcquiredByAnotherLockManagerInstance​(LockName lockName)
        Is the lock already acquired by another JVM node
        Parameters:
        lockName - the name of the lock
        Returns:
        true of the lock is acquired by this JVM node
      • getLockManagerInstanceId

        String getLockManagerInstanceId()
        Get the instance id that distinguishes different FencedLockManager instances from each other
        For local JVM testing you can assign a unique instance id to allow multiple FencedLockManager's to compete for Locks.
        In production the hostname should be returned
        Returns:
        the instance id