MediaWiki master
Wikimedia\LockManager\LockManager Class Reference

Resource locking handling. More...

Inherits Wikimedia\LockManager\ILockManager.

Inherited by Wikimedia\LockManager\DatabaseLockManager, Wikimedia\LockManager\FSLockManager, Wikimedia\LockManager\NullLockManager, and Wikimedia\LockManager\QuorumLockManager.

Collaboration diagram for Wikimedia\LockManager\LockManager:

Public Member Functions

 __construct (array $config)
 Construct a new instance from configuration.
 lock (array $paths, $type=self::LOCK_EX, $timeout=0)
 Lock the resources at the given abstract paths.
 lockByType (array $pathsByType, $timeout=0)
 Lock the resources at the given abstract paths.
 lockKey (string $key, int $timeout=0)
 Provide a mutex of the key.
Parameters
string$keyKey to lock (relative to a domain, usually represented by the wiki ID, which callers don't need to specify)
int$timeoutTime to wait in seconds
Returns
bool true if the lock is acquired, false otherwise

 scopedLock (string $key, int $timeout=0)
 Lock a key and unlock it automatically when the object is destructed.
Parameters
string$key
int$timeout
Returns
ScopedCallback|null returns null when we failed to acquire the lock

 unlock (array $paths, $type=self::LOCK_EX)
 Unlock the resources at the given abstract paths.
 unlockByType (array $pathsByType)
 Unlock the resources at the given abstract paths.
 unlockKey (string $key)
 
Parameters
string$keyKey to unlock (relative to a domain, usually represented by the wiki ID, which callers don't need to specify)
Returns
bool true if the lock is released, false otherwise

Public Attributes

const LOCK_EX = 3
const LOCK_SH = 1
 Lock types; stronger locks have higher values.
const LOCK_UW = 2

Protected Member Functions

 doLockByType (array $pathsByType)
 Acquire a set of locks without blocking.
 doUnlockByType (array $pathsByType)
 Release a set of locks.
 normalizePathsByType (array $pathsByType)
 Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any duplicated paths for each lock type.
 sha1Base36Absolute ( $path)
 Get the base 36 SHA-1 of a string, padded to 31 digits.

Protected Attributes

string $domain
 domain (usually wiki ID)
array $locksHeld = []
 Map of (resource path => lock type => count).
int $lockTTL
 maximum time locks can be held
array $lockTypeMap
 Mapping of lock types to the type actually used.
LoggerInterface $logger
string $session
 Random 32-char hex number.
const MAX_LOCK_TTL = 2 * 3600
 Max expected lock expiry in any context.
const MIN_LOCK_TTL = 5
 Minimum lock TTL.

Detailed Description

Resource locking handling.

Locks on resource keys can either be shared or exclusive.

Implementations must keep track of what is locked by this process in-memory and support nested locking calls (using reference counting). At least LOCK_UW and LOCK_EX must be implemented. LOCK_SH can be a no-op. Locks should either be non-blocking or have low wait timeouts.

Subclasses should avoid throwing exceptions at all costs.

Stability: stable
to extend
Since
1.19

Definition at line 37 of file LockManager.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\LockManager\LockManager::__construct ( array $config)

Construct a new instance from configuration.

Stability: stable
to call
Parameters
array$configParameters include:
  • domain : [optional] Domain (usually wiki ID) that all resources are relative to.
  • lockTTL : [optional] Customize the maximum duration (in seconds) that a lock may be held for. This TTL is stored by LockManager subclasses and serves as a recovery mechanism. If a process crashes before it can unlock, locks are eventually released. Default for web: 5min, or twice the length of max_execution_time (if higher). Default for CLI: 1 hour.

Reimplemented in Wikimedia\LockManager\DatabaseLockManager, Wikimedia\LockManager\FSLockManager, Wikimedia\LockManager\MemcLockManager, and Wikimedia\LockManager\RedisLockManager.

Definition at line 83 of file LockManager.php.

Member Function Documentation

◆ doLockByType()

Wikimedia\LockManager\LockManager::doLockByType ( array $pathsByType)
abstractprotected

Acquire a set of locks without blocking.

If some locks could not be acquired, then the other locks should be released. This must set the "lockmanager-fail-conflict" error if there are any lock conflicts.

Parameters
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
Returns
StatusValue
See also
LockManager::lockByType()

Reimplemented in Wikimedia\LockManager\DatabaseLockManager, Wikimedia\LockManager\FSLockManager, Wikimedia\LockManager\NullLockManager, and Wikimedia\LockManager\QuorumLockManager.

Referenced by lockByType().

◆ doUnlockByType()

Wikimedia\LockManager\LockManager::doUnlockByType ( array $pathsByType)
abstractprotected

Release a set of locks.

Parameters
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
Returns
StatusValue
See also
LockManager::unlockByType()

Reimplemented in Wikimedia\LockManager\DatabaseLockManager, Wikimedia\LockManager\FSLockManager, Wikimedia\LockManager\NullLockManager, and Wikimedia\LockManager\QuorumLockManager.

Referenced by unlockByType().

◆ lock()

Wikimedia\LockManager\LockManager::lock ( array $paths,
$type = self::LOCK_EX,
$timeout = 0 )
final

Lock the resources at the given abstract paths.

Parameters
array$pathsList of resource names
int$typeLockManager::LOCK_* constant
int$timeoutTimeout in seconds (0 means non-blocking) (since 1.21)
Returns
StatusValue

Definition at line 114 of file LockManager.php.

References lockByType().

Referenced by Wikimedia\LockManager\RedisLockManager\getLocksOnServer().

◆ lockByType()

Wikimedia\LockManager\LockManager::lockByType ( array $pathsByType,
$timeout = 0 )

Lock the resources at the given abstract paths.

Parameters
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
int$timeoutTimeout in seconds (0 means non-blocking) (since 1.21)
Returns
StatusValue
Since
1.22

Reimplemented in Wikimedia\LockManager\DatabaseLockManager.

Definition at line 126 of file LockManager.php.

References doLockByType(), and normalizePathsByType().

Referenced by Wikimedia\LockManager\ScopedLock\factory(), lock(), and lockKey().

◆ lockKey()

Wikimedia\LockManager\LockManager::lockKey ( string $key,
int $timeout = 0 )
final

Provide a mutex of the key.

Parameters
string$keyKey to lock (relative to a domain, usually represented by the wiki ID, which callers don't need to specify)
int$timeoutTime to wait in seconds
Returns
bool true if the lock is acquired, false otherwise

Implements Wikimedia\LockManager\ILockManager.

Definition at line 232 of file LockManager.php.

References LOCK_EX, lockByType(), and return.

Referenced by Wikimedia\LockManager\RedisLockManager\getLocksOnServer(), and scopedLock().

◆ normalizePathsByType()

Wikimedia\LockManager\LockManager::normalizePathsByType ( array $pathsByType)
finalprotected

Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any duplicated paths for each lock type.

Parameters
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
Returns
array
Since
1.22

Definition at line 194 of file LockManager.php.

References $path.

Referenced by Wikimedia\LockManager\DatabaseLockManager\lockByType(), lockByType(), and unlockByType().

◆ scopedLock()

Wikimedia\LockManager\LockManager::scopedLock ( string $key,
int $timeout = 0 )
final

Lock a key and unlock it automatically when the object is destructed.

Parameters
string$key
int$timeout
Returns
ScopedCallback|null returns null when we failed to acquire the lock

Implements Wikimedia\LockManager\ILockManager.

Definition at line 246 of file LockManager.php.

References if, and lockKey().

◆ sha1Base36Absolute()

Wikimedia\LockManager\LockManager::sha1Base36Absolute ( $path)
finalprotected

Get the base 36 SHA-1 of a string, padded to 31 digits.

Before hashing, the path will be prefixed with the domain ID. This should be used internally for lock key or file names.

Parameters
string$path
Returns
string

Definition at line 182 of file LockManager.php.

References $path.

Referenced by Wikimedia\LockManager\DatabaseLockManager\doUnlockByType(), Wikimedia\LockManager\DatabaseLockManager\lockByType(), Wikimedia\LockManager\MemcLockManager\recordKeyForPath(), and Wikimedia\LockManager\RedisLockManager\recordKeyForPath().

◆ unlock()

Wikimedia\LockManager\LockManager::unlock ( array $paths,
$type = self::LOCK_EX )
final

Unlock the resources at the given abstract paths.

Parameters
array$pathsList of paths
int$typeLockManager::LOCK_* constant
Returns
StatusValue

Definition at line 156 of file LockManager.php.

References unlockByType().

◆ unlockByType()

Wikimedia\LockManager\LockManager::unlockByType ( array $pathsByType)
final

Unlock the resources at the given abstract paths.

Parameters
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
Returns
StatusValue
Since
1.22

Definition at line 167 of file LockManager.php.

References doUnlockByType(), and normalizePathsByType().

Referenced by Wikimedia\LockManager\MemcLockManager\__destruct(), Wikimedia\LockManager\RedisLockManager\__destruct(), unlock(), and unlockKey().

◆ unlockKey()

Wikimedia\LockManager\LockManager::unlockKey ( string $key)
final

Parameters
string$keyKey to unlock (relative to a domain, usually represented by the wiki ID, which callers don't need to specify)
Returns
bool true if the lock is released, false otherwise

Implements Wikimedia\LockManager\ILockManager.

Definition at line 239 of file LockManager.php.

References LOCK_EX, return, and unlockByType().

Member Data Documentation

◆ $domain

string Wikimedia\LockManager\LockManager::$domain
protected

domain (usually wiki ID)

Definition at line 52 of file LockManager.php.

◆ $locksHeld

array Wikimedia\LockManager\LockManager::$locksHeld = []
protected

Map of (resource path => lock type => count).

Definition at line 49 of file LockManager.php.

Referenced by Wikimedia\LockManager\MemcLockManager\freeLocksOnServer(), and Wikimedia\LockManager\MemcLockManager\getLocksOnServer().

◆ $lockTTL

int Wikimedia\LockManager\LockManager::$lockTTL
protected

maximum time locks can be held

Definition at line 54 of file LockManager.php.

Referenced by Wikimedia\LockManager\MemcLockManager\getLocksOnServer().

◆ $lockTypeMap

array Wikimedia\LockManager\LockManager::$lockTypeMap
protected
Initial value:
= [
self::LOCK_SH => self::LOCK_SH,
self::LOCK_UW => self::LOCK_EX,
self::LOCK_EX => self::LOCK_EX
]

Mapping of lock types to the type actually used.

Definition at line 42 of file LockManager.php.

◆ $logger

LoggerInterface Wikimedia\LockManager\LockManager::$logger
protected

Definition at line 39 of file LockManager.php.

◆ $session

string Wikimedia\LockManager\LockManager::$session
protected

Random 32-char hex number.

Definition at line 57 of file LockManager.php.

Referenced by Wikimedia\LockManager\MemcLockManager\getLocksOnServer().

◆ LOCK_EX

const Wikimedia\LockManager\LockManager::LOCK_EX = 3

◆ LOCK_SH

const Wikimedia\LockManager\LockManager::LOCK_SH = 1

◆ LOCK_UW

const Wikimedia\LockManager\LockManager::LOCK_UW = 2

Definition at line 61 of file LockManager.php.

◆ MAX_LOCK_TTL

const Wikimedia\LockManager\LockManager::MAX_LOCK_TTL = 2 * 3600
protected

Max expected lock expiry in any context.

Definition at line 65 of file LockManager.php.

◆ MIN_LOCK_TTL

const Wikimedia\LockManager\LockManager::MIN_LOCK_TTL = 5
protected

Minimum lock TTL.

The configured lockTTL is ignored if it is less than this value.

Definition at line 68 of file LockManager.php.


The documentation for this class was generated from the following file: