Class MongoLockService

java.lang.Object
org.graylog2.cluster.lock.MongoLockService
All Implemented Interfaces:
LockService

@Singleton public class MongoLockService extends Object implements LockService
Lock service implementation using MongoDB to maintain locks. Unless a lock is kept alive by periodically re-requesting the same lock, it will eventually expire. Lock expiry is handled by MongoDB internally. We set lock expiry to 60 seconds, but in practice it may take up to ~2 minutes until a lock really expires.
  • Field Details

  • Constructor Details

    • MongoLockService

      @Inject public MongoLockService(NodeId nodeId, MongoConnection mongoConnection, @Named("lock_service_lock_ttl") Duration lockTTL)
  • Method Details

    • lock

      public Optional<Lock> lock(@Nonnull String resource, @Nullable String lockContext)
      Description copied from interface: LockService
      Request a lock. If a lock already exists, the lock expiry time will be extended.
      Specified by:
      lock in interface LockService
      Parameters:
      resource - Unique identifier for the resource that should be guarded by this lock.
      lockContext - an identifier that will be appended to the callers' node id. This will create the lock owner string. A context can be used for resources that should only allow a single lock to be acquired, even from the same node. If the lockContext is null, only the nodeId will be used.
      Returns:
      A Lock object, if a lock was obtained. An empty Optional, if no lock could be acquired.
    • lock

      public Optional<Lock> lock(@Nonnull String resource)
      Description copied from interface: LockService
      Request a lock. If a lock already exists, the lock expiry time will be extended.
      Specified by:
      lock in interface LockService
      Parameters:
      resource - Unique identifier for the resource that should be guarded by this lock.
      Returns:
      A Lock object, if a lock was obtained. An empty Optional, if no lock could be acquired.
    • extendLock

      public Optional<Lock> extendLock(@Nonnull Lock lock)
      Description copied from interface: LockService
      Extend the expiry time of an existing lock.
      Specified by:
      extendLock in interface LockService
      Parameters:
      lock - the lock that should be extended.
      Returns:
      A Lock object, if the lock could be extended. An empty Optional, if no lock extension could be acquired.
    • unlock

      public Optional<Lock> unlock(@Nonnull String resource, @Nullable String lockContext)
      Description copied from interface: LockService
      Unlock an existing lock.
      Specified by:
      unlock in interface LockService
      Parameters:
      resource - Unique identifier for the resource that was guarded by this lock.
      lockContext - the lockContext that was used to guard this lock.
      Returns:
      A Lock object, if the lock could be unlocked. An empty Optional, if no lock was found.
    • unlock

      public Optional<Lock> unlock(@Nonnull Lock lock)
      Description copied from interface: LockService
      Unlock an existing lock.
      Specified by:
      unlock in interface LockService
      Parameters:
      lock - The lock to unlock.
      Returns:
      A Lock object, if the lock could be unlocked. An empty Optional, if no lock was found.