MediaWiki  1.23.8
PoolCounterRedis Class Reference

Version of PoolCounter that uses Redis. More...

Inheritance diagram for PoolCounterRedis:
Collaboration diagram for PoolCounterRedis:

Public Member Functions

 __construct ( $conf, $type, $key)
 
 acquireForAnyone ()
 I want to do this task, but if anyone else does it instead, it's also fine for me. More...
 
 acquireForMe ()
 I want to do this task and I need to do it myself. More...
 
 release ()
 I have successfully finished my task. More...
 
- Public Member Functions inherited from PoolCounter
 getKey ()
 

Static Public Member Functions

static releaseAll ()
 Try to make sure that locks get released (even with exceptions and fatals) More...
 
- Static Public Member Functions inherited from PoolCounter
static factory ( $type, $key)
 Create a Pool counter. More...
 

Public Attributes

const AWAKE_ALL = 2
 
const AWAKE_ONE = 1
 
- Public Attributes inherited from 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
 

Protected Member Functions

 getConnection ()
 
 getSlotListKey ()
 
 getSlotRTimeSetKey ()
 
 getWaitSetKey ()
 
 getWakeupListKey ()
 
 initAndPopPoolSlotList (RedisConnRef $conn, $now)
 
 registerAcquisitionTime (RedisConnRef $conn, $slot, $now)
 
 waitForSlotOrNotif ( $doWakeup)
 

Protected Attributes

RedisConnRef $conn
 
string $keySha1
 SHA-1 of the key *. More...
 
integer $lockTTL
 TTL for locks to expire (work should finish in this time) *. More...
 
integer $onRelease
 AWAKE_* constant *. More...
 
RedisConnectionPool $pool
 
HashRing $ring
 
array $serversByLabel
 (server label => host) map * More...
 
string $session
 Unique string to identify this process *. More...
 
string $slot
 Pool slot value *. More...
 
integer $slotTime
 UNIX timestamp *. More...
 
- Protected Attributes inherited from PoolCounter
string $key
 All workers with the same key share the lock *. More...
 
 $maxqueue
 
float $timeout
 Maximum time in seconds to wait for the lock *. More...
 
integer $workers
 Maximum number of workers doing the task simultaneously *. More...
 

Static Protected Attributes

static $active = null
 

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 and redis.conf can be helpful otherwise.

Since
1.23

Definition at line 53 of file PoolCounterRedis.php.

Constructor & Destructor Documentation

◆ __construct()

PoolCounterRedis::__construct (   $conf,
  $type,
  $key 
)
Parameters
array$conf
string$type
string$key

Reimplemented from PoolCounter.

Definition at line 72 of file PoolCounterRedis.php.

References PoolCounter\$key, $type, array(), key, and RedisConnectionPool\singleton().

Member Function Documentation

◆ acquireForAnyone()

PoolCounterRedis::acquireForAnyone ( )

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.

Returns
Status Value is one of Locked/Done/Error

Reimplemented from PoolCounter.

Definition at line 119 of file PoolCounterRedis.php.

References $section, and waitForSlotOrNotif().

◆ acquireForMe()

PoolCounterRedis::acquireForMe ( )

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

Returns
Status Value is one of Locked/Error

Reimplemented from PoolCounter.

Definition at line 113 of file PoolCounterRedis.php.

References $section, and waitForSlotOrNotif().

◆ getConnection()

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

Definition at line 94 of file PoolCounterRedis.php.

References $conn, as, ArrayUtils\consistentHashSort(), key, Status\newFatal(), and Status\newGood().

Referenced by release(), and waitForSlotOrNotif().

◆ getSlotListKey()

PoolCounterRedis::getSlotListKey ( )
protected
Returns
string

Definition at line 365 of file PoolCounterRedis.php.

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

◆ getSlotRTimeSetKey()

PoolCounterRedis::getSlotRTimeSetKey ( )
protected
Returns
string

Definition at line 372 of file PoolCounterRedis.php.

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

◆ getWaitSetKey()

PoolCounterRedis::getWaitSetKey ( )
protected
Returns
string

Definition at line 379 of file PoolCounterRedis.php.

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

◆ getWakeupListKey()

PoolCounterRedis::getWakeupListKey ( )
protected
Returns
string

Definition at line 386 of file PoolCounterRedis.php.

Referenced by release(), and waitForSlotOrNotif().

◆ initAndPopPoolSlotList()

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

Definition at line 263 of file PoolCounterRedis.php.

References $conn, add, are, array(), as, getSlotListKey(), getSlotRTimeSetKey(), getWaitSetKey(), in, is(), key, later, list, RedisConnRef\luaEval(), network, of, on, that, then, and up.

Referenced by waitForSlotOrNotif().

◆ registerAcquisitionTime()

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

Definition at line 331 of file PoolCounterRedis.php.

References $conn, are, array(), as, getSlotListKey(), getSlotRTimeSetKey(), getWaitSetKey(), is(), RedisConnRef\luaEval(), of, on, that, then, and up.

Referenced by waitForSlotOrNotif().

◆ release()

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 PoolCounter.

Definition at line 125 of file PoolCounterRedis.php.

References $conn, $e, $res, $section, are, array(), as, e, from, getConnection(), getSlotListKey(), getSlotRTimeSetKey(), getWaitSetKey(), getWakeupListKey(), is(), list, RedisConnRef\luaEval(), needed(), Status\newFatal(), Status\newGood(), PoolCounter\NOT_LOCKED, of, on, PoolCounter\RELEASED, save, that, then, up, and will.

◆ releaseAll()

static PoolCounterRedis::releaseAll ( )
static

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

Definition at line 393 of file PoolCounterRedis.php.

References $e, and as.

◆ waitForSlotOrNotif()

Member Data Documentation

◆ $active

PoolCounterRedis::$active = null
staticprotected

Definition at line 70 of file PoolCounterRedis.php.

◆ $conn

RedisConnRef PoolCounterRedis::$conn
protected

◆ $keySha1

string PoolCounterRedis::$keySha1
protected

SHA-1 of the key *.

Definition at line 57 of file PoolCounterRedis.php.

◆ $lockTTL

integer PoolCounterRedis::$lockTTL
protected

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

Definition at line 58 of file PoolCounterRedis.php.

◆ $onRelease

integer PoolCounterRedis::$onRelease
protected

AWAKE_* constant *.

Definition at line 62 of file PoolCounterRedis.php.

◆ $pool

RedisConnectionPool PoolCounterRedis::$pool
protected

Definition at line 55 of file PoolCounterRedis.php.

◆ $ring

HashRing PoolCounterRedis::$ring
protected

Definition at line 54 of file PoolCounterRedis.php.

◆ $serversByLabel

array PoolCounterRedis::$serversByLabel
protected

(server label => host) map *

Definition at line 56 of file PoolCounterRedis.php.

◆ $session

string PoolCounterRedis::$session
protected

Unique string to identify this process *.

Definition at line 63 of file PoolCounterRedis.php.

Referenced by waitForSlotOrNotif().

◆ $slot

string PoolCounterRedis::$slot
protected

Pool slot value *.

Definition at line 61 of file PoolCounterRedis.php.

Referenced by waitForSlotOrNotif().

◆ $slotTime

integer PoolCounterRedis::$slotTime
protected

UNIX timestamp *.

Definition at line 64 of file PoolCounterRedis.php.

Referenced by waitForSlotOrNotif().

◆ AWAKE_ALL

const PoolCounterRedis::AWAKE_ALL = 2

Definition at line 67 of file PoolCounterRedis.php.

Referenced by waitForSlotOrNotif().

◆ AWAKE_ONE

const PoolCounterRedis::AWAKE_ONE = 1

Definition at line 66 of file PoolCounterRedis.php.


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