MediaWiki master
Wikimedia\LockManager\RedisLockManager Class Reference

Manage locks using redis servers. More...

Inherits Wikimedia\LockManager\QuorumLockManager.

Collaboration diagram for Wikimedia\LockManager\RedisLockManager:

Public Member Functions

 __construct (array $config)
 Construct a new instance from configuration.
 __destruct ()
 Make sure remaining locks get cleared.
Public Member Functions inherited from Wikimedia\LockManager\LockManager
 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

Protected Member Functions

 freeLocksOnServer ( $lockSrv, array $pathsByType)
 Get a connection to a lock server and release locks on $paths.Subclasses must effectively implement this or releaseAllLocks().
Parameters
string$lockServer
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
Returns
StatusValue

 getLocksOnServer ( $lockSrv, array $pathsByType)
 Get a connection to a lock server and acquire locks.
Parameters
string$lockServer
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
Returns
StatusValue

 isServerUp ( $lockSrv)
 Check if a lock server is up.This should process cache results to reduce RTT.
Parameters
string$lockServer
Returns
bool

 recordKeyForPath ( $path, $type)
 releaseAllLocks ()
 Release all locks that this session is holding.Subclasses must effectively implement this or freeLocksOnServer().
Returns
StatusValue

Protected Member Functions inherited from Wikimedia\LockManager\QuorumLockManager
 doLockByType (array $pathsByType)
 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()

 doLockingRequest (string $path, string $type)
 Attempt to acquire locks with the peers.
 doUnlockByType (array $pathsByType)
 doUnlockingRequest (string $path, string $type)
 Attempt to release locks with the peers.
Protected Member Functions inherited from Wikimedia\LockManager\LockManager
 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

array $lockServers = []
 Map server names to hostname/IP and port numbers.
array $lockTypeMap
 Mapping of lock types to the type actually used.
RedisConnectionPool $redisPool
Protected Attributes inherited from Wikimedia\LockManager\QuorumLockManager
array $downServers = []
 list of down server names
array $lockServers = []
 Map server names to data used by subclasses.
int $minVotes = 2
 Minimum number of success to consider lock/unlock successful.
Protected Attributes inherited from Wikimedia\LockManager\LockManager
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.

Additional Inherited Members

Public Attributes inherited from Wikimedia\LockManager\LockManager
const LOCK_EX = 3
const LOCK_SH = 1
 Lock types; stronger locks have higher values.
const LOCK_UW = 2

Detailed Description

Manage locks using redis servers.

This is meant for multi-wiki systems that may share files. All locks are non-blocking, which avoids deadlocks.

All lock requests for a resource, identified by a hash string, will map to several servers, each running redis. A majority of peers must agree for a lock to be acquired.

This class requires Redis 2.6 as it makes use of Lua scripts for fast atomic operations.

Since
1.22

Definition at line 28 of file RedisLockManager.php.

Constructor & Destructor Documentation

◆ __construct()

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

Construct a new instance from configuration.

Parameters
array$configParameters include:
  • lockServers : Associative array of server names to "<IP>:<port>" strings.
  • redisConfig : Configuration for RedisConnectionPool::singleton() (optional).
  • minVotes : Minimum number of votes to consider lock valid. The higher, the stronger (and slower). Default: 2
Exceptions
Exception

Reimplemented from Wikimedia\LockManager\LockManager.

Definition at line 52 of file RedisLockManager.php.

◆ __destruct()

Wikimedia\LockManager\RedisLockManager::__destruct ( )

Make sure remaining locks get cleared.

Definition at line 252 of file RedisLockManager.php.

References $path, and Wikimedia\LockManager\LockManager\unlockByType().

Member Function Documentation

◆ freeLocksOnServer()

Wikimedia\LockManager\RedisLockManager::freeLocksOnServer ( $lockSrv,
array $pathsByType )
protected

Get a connection to a lock server and release locks on $paths.Subclasses must effectively implement this or releaseAllLocks().

Parameters
string$lockServer
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
Returns
StatusValue

Reimplemented from Wikimedia\LockManager\QuorumLockManager.

Definition at line 156 of file RedisLockManager.php.

References $path, Wikimedia\LockManager\LockManager\LOCK_SH, and recordKeyForPath().

◆ getLocksOnServer()

Wikimedia\LockManager\RedisLockManager::getLocksOnServer ( $lockSrv,
array $pathsByType )
protected

Get a connection to a lock server and acquire locks.

Parameters
string$lockServer
array$pathsByTypeMap of LockManager::LOCK_* constants to lists of paths
Returns
StatusValue

Reimplemented from Wikimedia\LockManager\QuorumLockManager.

Definition at line 62 of file RedisLockManager.php.

References $path, Wikimedia\LockManager\LockManager\lock(), Wikimedia\LockManager\LockManager\LOCK_SH, Wikimedia\LockManager\LockManager\lockKey(), and recordKeyForPath().

◆ isServerUp()

Wikimedia\LockManager\RedisLockManager::isServerUp ( $lockSrv)
protected

Check if a lock server is up.This should process cache results to reduce RTT.

Parameters
string$lockServer
Returns
bool

Reimplemented from Wikimedia\LockManager\QuorumLockManager.

Definition at line 233 of file RedisLockManager.php.

◆ recordKeyForPath()

Wikimedia\LockManager\RedisLockManager::recordKeyForPath ( $path,
$type )
protected
Parameters
string$path
string$typeOne of (EX,SH)
Returns
string

Definition at line 244 of file RedisLockManager.php.

References $path, and Wikimedia\LockManager\LockManager\sha1Base36Absolute().

Referenced by freeLocksOnServer(), and getLocksOnServer().

◆ releaseAllLocks()

Wikimedia\LockManager\RedisLockManager::releaseAllLocks ( )
protected

Release all locks that this session is holding.Subclasses must effectively implement this or freeLocksOnServer().

Returns
StatusValue

Reimplemented from Wikimedia\LockManager\QuorumLockManager.

Definition at line 228 of file RedisLockManager.php.

Member Data Documentation

◆ $lockServers

array Wikimedia\LockManager\RedisLockManager::$lockServers = []
protected

Map server names to hostname/IP and port numbers.

Definition at line 40 of file RedisLockManager.php.

◆ $lockTypeMap

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

Mapping of lock types to the type actually used.

Definition at line 30 of file RedisLockManager.php.

◆ $redisPool

RedisConnectionPool Wikimedia\LockManager\RedisLockManager::$redisPool
protected

Definition at line 37 of file RedisLockManager.php.


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