MediaWiki master
MediaWiki\PoolCounter\PoolCounterRedis Class Reference

Version of PoolCounter that uses Redis. More...

Inherits MediaWiki\PoolCounter\PoolCounter.

Collaboration diagram for MediaWiki\PoolCounter\PoolCounterRedis:

Public Member Functions

 __construct (array $conf, string $type, string $key)
 acquireForAnyone ( $timeout=null)
 I want to do this task, but if anyone else does it instead, it's also fine for me.I will read its cached data.
Parameters
int | null$timeoutWait timeout, or null to use value passed to the constructor
Returns
Status Value is one of Locked/Done/Error

 acquireForMe ( $timeout=null)
 I want to do this task and I need to do it myself.
Parameters
int | null$timeoutWait timeout, or null to use value passed to the constructor
Returns
Status Value is one of Locked/Error

 release ()
 I have successfully finished my task.Lets another one grab the lock, and returns the workers waiting on acquireForAnyone()
Returns
Status Value is one of Released/NotLocked/Error

Public Member Functions inherited from MediaWiki\PoolCounter\PoolCounter
 getKey ()
 getLogger ()
 getType ()
 isFastStaleEnabled ()
 Is fast stale mode (T250248) enabled?
 setLogger (LoggerInterface $logger)
 setTracer (TracerInterface $tracer)

Static Public Member Functions

static releaseAll ()
 Try to make sure that locks get released (even with exceptions and fatals).

Protected Member Functions

 getConnection ()
 getSlotListKey ()
 getSlotRTimeSetKey ()
 getWaitSetKey ()
 getWakeupListKey ()
 initAndPopPoolSlotList (RedisConnRef $conn, $now)
 registerAcquisitionTime (RedisConnRef $conn, $slot, $now)
 waitForSlotOrNotif ( $doWakeup, $timeout=null)
Protected Member Functions inherited from MediaWiki\PoolCounter\PoolCounter
 hashKeyIntoSlots ( $type, $key, $slots)
 Given a key (any string) and the number of lots, returns a slot key (a prefix with a suffix integer from the [0..($slots-1)] range).
 onAcquire ()
 Update any lock tracking information when the lock is acquired.
 onRelease ()
 Update any lock tracking information when the lock is released.
 precheckAcquire ()
 Checks that the lock request is sensible.

Protected Attributes

RedisConnRef null $conn
string $keySha1
 SHA-1 of the key.
int $lockTTL
 TTL for locks to expire (work should finish in this time).
int null $onRelease
 AWAKE_* constant.
RedisConnectionPool $pool
HashRing $ring
array $serversByLabel
 (server label => host) map
string $session
 Unique string to identify this process.
string null $slot
 Pool slot value.
float null $slotTime
 UNIX timestamp.
Protected Attributes inherited from MediaWiki\PoolCounter\PoolCounter
SpanInterface $heldLockSpan = null
string $key
 All workers with the same key share the lock.
LoggerInterface $logger
int $maxqueue
 If this number of workers are already working/waiting, fail instead of wait.
int $slots = 0
 Maximum number of workers working on this task type, regardless of key.
int $timeout
 Maximum time in seconds to wait for the lock.
TracerInterface $tracer
string $type
int $workers
 Maximum number of workers working on tasks with the same key simultaneously.

Static Protected Attributes

static PoolCounterRedis[] $active = null
 List of active PoolCounterRedis objects in this script.

Additional Inherited Members

Public Attributes inherited from MediaWiki\PoolCounter\PoolCounter
const DONE = 3
const ERROR = -1
const LOCK_HELD = -5
const LOCKED = 1
const NOT_LOCKED = -2
const QUEUE_FULL = -3
const RELEASED = 2
const TIMEOUT = -4

Detailed Description

Version of PoolCounter that uses Redis.

There are four main redis keys used to track each pool counter key:

  • poolcounter:l-slots-* : A list of available slot IDs for a pool.
  • poolcounter:z-renewtime-* : A sorted set of (slot ID, UNIX timestamp as score) used for tracking the next time a slot should be released. This is -1 when a slot is created, and is set when released (expired), locked, and unlocked.
  • poolcounter:z-wait-* : A sorted set of (slot ID, UNIX timestamp as score) used for tracking waiting processes (and wait time).
  • poolcounter:l-wakeup-* : A list pushed to for the sake of waking up processes when a any process in the pool finishes (lasts for 1ms).

For a given pool key, all the redis keys start off non-existing and are deleted if not used for a while to prevent garbage from building up on the server. They are atomically re-initialized as needed. The "z-renewtime" key is used for detecting sessions which got slots but then disappeared. Stale entries from there have their timestamp updated and the corresponding slots freed up. The "z-wait" key is used for detecting processes registered as waiting but that disappeared. Stale entries from there are deleted and the corresponding slots are freed up. The worker count is included in all the redis key names as it does not vary within each $wgPoolCounterConf type and doing so handles configuration changes.

This class requires Redis 2.6 as it makes use Lua scripts for fast atomic operations. Also this should be on a server plenty of RAM for the working set to avoid evictions. Evictions could temporarily allow wait queues to double in size or temporarily cause pools to appear as full when they are not. Using volatile-ttl and bumping memory-samples in redis.conf can be helpful otherwise.

Since
1.23

Definition at line 47 of file PoolCounterRedis.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\PoolCounter\PoolCounterRedis::__construct ( array $conf,
string $type,
string $key )
Parameters
array$conf
string$typeThe class of actions to limit concurrency for (task type)
string$key

Reimplemented from MediaWiki\PoolCounter\PoolCounter.

Definition at line 75 of file PoolCounterRedis.php.

References MediaWiki\PoolCounter\PoolCounter\$key, and MediaWiki\PoolCounter\PoolCounter\$type.

Member Function Documentation

◆ acquireForAnyone()

MediaWiki\PoolCounter\PoolCounterRedis::acquireForAnyone ( $timeout = null)

I want to do this task, but if anyone else does it instead, it's also fine for me.I will read its cached data.

Parameters
int | null$timeoutWait timeout, or null to use value passed to the constructor
Returns
Status Value is one of Locked/Done/Error

Reimplemented from MediaWiki\PoolCounter\PoolCounter.

Definition at line 128 of file PoolCounterRedis.php.

References MediaWiki\PoolCounter\PoolCounter\$timeout, MediaWiki\PoolCounter\PoolCounter\precheckAcquire(), and waitForSlotOrNotif().

◆ acquireForMe()

MediaWiki\PoolCounter\PoolCounterRedis::acquireForMe ( $timeout = null)

I want to do this task and I need to do it myself.

Parameters
int | null$timeoutWait timeout, or null to use value passed to the constructor
Returns
Status Value is one of Locked/Error

Reimplemented from MediaWiki\PoolCounter\PoolCounter.

Definition at line 118 of file PoolCounterRedis.php.

References MediaWiki\PoolCounter\PoolCounter\$timeout, MediaWiki\PoolCounter\PoolCounter\precheckAcquire(), and waitForSlotOrNotif().

◆ getConnection()

MediaWiki\PoolCounter\PoolCounterRedis::getConnection ( )
protected
Returns
Status Uses RediConnRef as value on success

Definition at line 99 of file PoolCounterRedis.php.

References $conn.

Referenced by release(), and waitForSlotOrNotif().

◆ getSlotListKey()

MediaWiki\PoolCounter\PoolCounterRedis::getSlotListKey ( )
protected
Returns
string

Definition at line 394 of file PoolCounterRedis.php.

Referenced by initAndPopPoolSlotList(), registerAcquisitionTime(), release(), and waitForSlotOrNotif().

◆ getSlotRTimeSetKey()

MediaWiki\PoolCounter\PoolCounterRedis::getSlotRTimeSetKey ( )
protected
Returns
string

Definition at line 401 of file PoolCounterRedis.php.

Referenced by initAndPopPoolSlotList(), registerAcquisitionTime(), and release().

◆ getWaitSetKey()

MediaWiki\PoolCounter\PoolCounterRedis::getWaitSetKey ( )
protected
Returns
string

Definition at line 408 of file PoolCounterRedis.php.

Referenced by initAndPopPoolSlotList(), registerAcquisitionTime(), release(), and waitForSlotOrNotif().

◆ getWakeupListKey()

MediaWiki\PoolCounter\PoolCounterRedis::getWakeupListKey ( )
protected
Returns
string

Definition at line 415 of file PoolCounterRedis.php.

Referenced by release(), and waitForSlotOrNotif().

◆ initAndPopPoolSlotList()

MediaWiki\PoolCounter\PoolCounterRedis::initAndPopPoolSlotList ( RedisConnRef $conn,
$now )
protected
Parameters
RedisConnRef$conn
float$nowUNIX timestamp
Returns
string|bool False on failure

Definition at line 292 of file PoolCounterRedis.php.

References $conn, getSlotListKey(), getSlotRTimeSetKey(), getWaitSetKey(), and Wikimedia\ObjectCache\RedisConnRef\luaEval().

Referenced by waitForSlotOrNotif().

◆ registerAcquisitionTime()

MediaWiki\PoolCounter\PoolCounterRedis::registerAcquisitionTime ( RedisConnRef $conn,
$slot,
$now )
protected
Parameters
RedisConnRef$conn
string$slot
float$now
Returns
int|bool False on failure

Definition at line 360 of file PoolCounterRedis.php.

References $conn, $slot, getSlotListKey(), getSlotRTimeSetKey(), getWaitSetKey(), and Wikimedia\ObjectCache\RedisConnRef\luaEval().

Referenced by waitForSlotOrNotif().

◆ release()

MediaWiki\PoolCounter\PoolCounterRedis::release ( )

I have successfully finished my task.Lets another one grab the lock, and returns the workers waiting on acquireForAnyone()

Returns
Status Value is one of Released/NotLocked/Error

Reimplemented from MediaWiki\PoolCounter\PoolCounter.

Definition at line 138 of file PoolCounterRedis.php.

References $conn, getConnection(), getSlotListKey(), getSlotRTimeSetKey(), getWaitSetKey(), getWakeupListKey(), Wikimedia\ObjectCache\RedisConnRef\luaEval(), MediaWiki\PoolCounter\PoolCounter\NOT_LOCKED, MediaWiki\PoolCounter\PoolCounter\onRelease(), and MediaWiki\PoolCounter\PoolCounter\RELEASED.

◆ releaseAll()

MediaWiki\PoolCounter\PoolCounterRedis::releaseAll ( )
static

Try to make sure that locks get released (even with exceptions and fatals).

Definition at line 422 of file PoolCounterRedis.php.

◆ waitForSlotOrNotif()

Member Data Documentation

◆ $active

PoolCounterRedis [] MediaWiki\PoolCounter\PoolCounterRedis::$active = null
staticprotected

List of active PoolCounterRedis objects in this script.

Definition at line 73 of file PoolCounterRedis.php.

◆ $conn

RedisConnRef null MediaWiki\PoolCounter\PoolCounterRedis::$conn
protected

◆ $keySha1

string MediaWiki\PoolCounter\PoolCounterRedis::$keySha1
protected

SHA-1 of the key.

Definition at line 55 of file PoolCounterRedis.php.

◆ $lockTTL

int MediaWiki\PoolCounter\PoolCounterRedis::$lockTTL
protected

TTL for locks to expire (work should finish in this time).

Definition at line 57 of file PoolCounterRedis.php.

◆ $onRelease

int null MediaWiki\PoolCounter\PoolCounterRedis::$onRelease
protected

AWAKE_* constant.

Definition at line 63 of file PoolCounterRedis.php.

◆ $pool

RedisConnectionPool MediaWiki\PoolCounter\PoolCounterRedis::$pool
protected

Definition at line 51 of file PoolCounterRedis.php.

◆ $ring

HashRing MediaWiki\PoolCounter\PoolCounterRedis::$ring
protected

Definition at line 49 of file PoolCounterRedis.php.

◆ $serversByLabel

array MediaWiki\PoolCounter\PoolCounterRedis::$serversByLabel
protected

(server label => host) map

Definition at line 53 of file PoolCounterRedis.php.

◆ $session

string MediaWiki\PoolCounter\PoolCounterRedis::$session
protected

Unique string to identify this process.

Definition at line 65 of file PoolCounterRedis.php.

Referenced by waitForSlotOrNotif().

◆ $slot

string null MediaWiki\PoolCounter\PoolCounterRedis::$slot
protected

Pool slot value.

Definition at line 61 of file PoolCounterRedis.php.

Referenced by registerAcquisitionTime(), and waitForSlotOrNotif().

◆ $slotTime

float null MediaWiki\PoolCounter\PoolCounterRedis::$slotTime
protected

UNIX timestamp.

Definition at line 67 of file PoolCounterRedis.php.

Referenced by waitForSlotOrNotif().


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