MediaWiki  1.30.0
PoolCounter Class Reference

When you have many workers (threads/servers) giving service, and a cached item expensive to produce expires, you may get several workers doing the job at the same time. More...

Inheritance diagram for PoolCounter:
Collaboration diagram for PoolCounter:

Public Member Functions

 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...
 
 getKey ()
 
 release ()
 I have successfully finished my task. More...
 

Static Public Member Functions

static factory ( $type, $key)
 Create a Pool counter. More...
 

Public Attributes

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

 __construct ( $conf, $type, $key)
 
 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). More...
 
 onAcquire ()
 Update any lock tracking information when the lock is acquired. More...
 
 onRelease ()
 Update any lock tracking information when the lock is released. More...
 
 precheckAcquire ()
 Checks that the lock request is sane. More...
 

Protected Attributes

string $key
 All workers with the same key share the lock. More...
 
int $maxqueue
 If this number of workers are already working/waiting, fail instead of wait. More...
 
int $slots = 0
 Maximum number of workers working on this task type, regardless of key. More...
 
float $timeout
 Maximum time in seconds to wait for the lock. More...
 
int $workers
 Maximum number of workers working on tasks with the same key simultaneously. More...
 

Private Attributes

bool $isMightWaitKey
 Whether the key is a "might wait" key. More...
 

Static Private Attributes

static bool $acquiredMightWaitKey = 0
 Whether this process holds a "might wait" lock key. More...
 

Detailed Description

When you have many workers (threads/servers) giving service, and a cached item expensive to produce expires, you may get several workers doing the job at the same time.

Given enough requests and the item expiring fast (non-cacheable, lots of edits...) that single work can end up unfairly using most (all) of the cpu of the pool. This is also known as 'Michael Jackson effect' since this effect triggered on the english wikipedia on the day Michael Jackson died, the biographical article got hit with several edits per minutes and hundreds of read hits.

The PoolCounter provides semaphore semantics for restricting the number of workers that may be concurrently performing such single task. Only one key can be locked by any PoolCounter instance of a process, except for keys that start with "nowait:". However, only 0 timeouts (non-blocking requests) can be used with "nowait:" keys.

By default PoolCounter_Stub is used, which provides no locking. You can get a useful one in the PoolCounter extension.

Definition at line 45 of file PoolCounter.php.

Constructor & Destructor Documentation

◆ __construct()

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

Reimplemented in PoolCounterRedis.

Definition at line 87 of file PoolCounter.php.

References $key, $type, hashKeyIntoSlots(), and key.

Member Function Documentation

◆ acquireForAnyone()

PoolCounter::acquireForAnyone ( )
abstract

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 in PoolCounter_Stub, and PoolCounterRedis.

◆ acquireForMe()

PoolCounter::acquireForMe ( )
abstract

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

Returns
Status Value is one of Locked/Error

Reimplemented in PoolCounter_Stub, and PoolCounterRedis.

◆ factory()

static PoolCounter::factory (   $type,
  $key 
)
static

Create a Pool counter.

This should only be called from the PoolWorks.

Parameters
string$typeThe class of actions to limit concurrency for (task type)
string$key
Returns
PoolCounter

Definition at line 111 of file PoolCounter.php.

References $key, $type, $wgPoolCounterConf, and global.

Referenced by PoolCounterWork\__construct().

◆ getKey()

PoolCounter::getKey ( )
Returns
string

Definition at line 125 of file PoolCounter.php.

References $key.

◆ hashKeyIntoSlots()

PoolCounter::hashKeyIntoSlots (   $type,
  $key,
  $slots 
)
protected

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

This is used for a global limit on the number of instances of a given type that can acquire a lock. The hashing is deterministic so that PoolCounter::$workers is always an upper limit of how many instances with the same key can acquire a lock.

Parameters
string$typeThe class of actions to limit concurrency for (task type)
string$keyPoolCounter instance key (any string)
int$slotsThe number of slots (max allowed value is 65536)
Returns
string Slot key with the type and slot number

Definition at line 207 of file PoolCounter.php.

References $key, $slots, and $type.

Referenced by __construct().

◆ onAcquire()

PoolCounter::onAcquire ( )
finalprotected

Update any lock tracking information when the lock is acquired.

Since
1.25

Definition at line 183 of file PoolCounter.php.

References $isMightWaitKey.

Referenced by PoolCounterRedis\waitForSlotOrNotif().

◆ onRelease()

PoolCounter::onRelease ( )
finalprotected

Update any lock tracking information when the lock is released.

Since
1.25

Definition at line 191 of file PoolCounter.php.

References $isMightWaitKey.

Referenced by PoolCounterRedis\release(), and PoolCounterRedis\waitForSlotOrNotif().

◆ precheckAcquire()

PoolCounter::precheckAcquire ( )
finalprotected

Checks that the lock request is sane.

Returns
Status - good for sane requests fatal for insane
Since
1.25

Definition at line 158 of file PoolCounter.php.

References StatusValue\newFatal(), and StatusValue\newGood().

Referenced by PoolCounterRedis\acquireForAnyone(), and PoolCounterRedis\acquireForMe().

◆ release()

PoolCounter::release ( )
abstract

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 in PoolCounter_Stub, and PoolCounterRedis.

Member Data Documentation

◆ $acquiredMightWaitKey

bool PoolCounter::$acquiredMightWaitKey = 0
staticprivate

Whether this process holds a "might wait" lock key.

Definition at line 80 of file PoolCounter.php.

◆ $isMightWaitKey

bool PoolCounter::$isMightWaitKey
private

Whether the key is a "might wait" key.

Definition at line 76 of file PoolCounter.php.

Referenced by onAcquire(), and onRelease().

◆ $key

string PoolCounter::$key
protected

All workers with the same key share the lock.

Definition at line 58 of file PoolCounter.php.

Referenced by PoolCounterRedis\__construct(), __construct(), factory(), getKey(), and hashKeyIntoSlots().

◆ $maxqueue

int PoolCounter::$maxqueue
protected

If this number of workers are already working/waiting, fail instead of wait.

Definition at line 69 of file PoolCounter.php.

◆ $slots

int PoolCounter::$slots = 0
protected

Maximum number of workers working on this task type, regardless of key.

0 means unlimited. Max allowed value is 65536. The way the slot limit is enforced is overzealous - this option should be used with caution.

Definition at line 67 of file PoolCounter.php.

Referenced by hashKeyIntoSlots().

◆ $timeout

float PoolCounter::$timeout
protected

Maximum time in seconds to wait for the lock.

Definition at line 71 of file PoolCounter.php.

◆ $workers

int PoolCounter::$workers
protected

Maximum number of workers working on tasks with the same key simultaneously.

Definition at line 60 of file PoolCounter.php.

◆ DONE

const PoolCounter::DONE = 3

Definition at line 49 of file PoolCounter.php.

Referenced by PoolCounterWork\execute(), and PoolCounterRedis\waitForSlotOrNotif().

◆ ERROR

const PoolCounter::ERROR = -1

Definition at line 51 of file PoolCounter.php.

Referenced by PoolCounterWork\execute().

◆ LOCK_HELD

const PoolCounter::LOCK_HELD = -5

Definition at line 55 of file PoolCounter.php.

Referenced by PoolCounterWork\execute(), and PoolCounterRedis\waitForSlotOrNotif().

◆ LOCKED

◆ NOT_LOCKED

const PoolCounter::NOT_LOCKED = -2

Definition at line 52 of file PoolCounter.php.

Referenced by PoolCounterRedis\release().

◆ QUEUE_FULL

const PoolCounter::QUEUE_FULL = -3

Definition at line 53 of file PoolCounter.php.

Referenced by PoolCounterWork\execute(), and PoolCounterRedis\waitForSlotOrNotif().

◆ RELEASED

const PoolCounter::RELEASED = 2

Definition at line 48 of file PoolCounter.php.

Referenced by PoolCounterRedis\release(), and PoolCounter_Stub\release().

◆ TIMEOUT

const PoolCounter::TIMEOUT = -4

Definition at line 54 of file PoolCounter.php.

Referenced by PoolCounterWork\execute(), and PoolCounterRedis\waitForSlotOrNotif().


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