Class StorageBasedLockProvider

  • All Implemented Interfaces:
    LockProvider

    public class StorageBasedLockProvider
    extends Object
    implements LockProvider
    Distributed lock using abstract storage

    It uses a table/collection that contains ID = lock name and a field locked_until.

    1. Attempts to insert a new lock record. As an optimization, we keep in-memory track of created lock records. If the record has been inserted, returns lock.
    2. We will try to update lock record using filter ID == name AND lock_until <= now
    3. If the update succeeded (1 updated row/document), we have the lock. If the update failed (0 updated documents) somebody else holds the lock
    4. When unlocking, lock_until is set to now.
    • Constructor Detail

      • StorageBasedLockProvider

        protected StorageBasedLockProvider​(@NotNull
                                           @NotNull StorageAccessor storageAccessor)
    • Method Detail

      • clearCache

        public void clearCache()
        Clears cache of existing lock records.
      • lock

        @NotNull
        public @NotNull Optional<SimpleLock> lock​(@NotNull
                                                  @NotNull LockConfiguration lockConfiguration)
        Specified by:
        lock in interface LockProvider
        Returns:
        If empty optional has been returned, lock could not be acquired. The lock has to be released by the callee.
      • doLock

        protected boolean doLock​(LockConfiguration lockConfiguration)
        Sets lockUntil according to LockConfiguration if current lockUntil <= now