MediaWiki  REL1_24
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:

List of all members.

Public Member Functions

 acquireForAnyone ()
 I want to do this task, but if anyone else does it instead, it's also fine for me.
 acquireForMe ()
 I want to do this task and I need to do it myself.
 getKey ()
 release ()
 I have successfully finished my task.

Static Public Member Functions

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

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 ($key, $slots)
 Given a key (any string) and the number of lots, returns a slot number (an integer from the [0..($slots-1)] range).

Protected Attributes

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

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.

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

Definition at line 42 of file PoolCounter.php.


Constructor & Destructor Documentation

PoolCounter::__construct ( conf,
type,
key 
) [protected]
Parameters:
array$conf
string$type
string$key

Reimplemented in PoolCounterRedis.

Definition at line 71 of file PoolCounter.php.


Member Function Documentation

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.

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.

static PoolCounter::factory ( type,
key 
) [static]

Create a Pool counter.

This should only be called from the PoolWorks.

Parameters:
string$type
string$key
Returns:
PoolCounter

Definition at line 93 of file PoolCounter.php.

Returns:
string

Definition at line 107 of file PoolCounter.php.

References key.

PoolCounter::hashKeyIntoSlots ( key,
slots 
) [protected]

Given a key (any string) and the number of lots, returns a slot number (an 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$keyPoolCounter instance key (any string)
int$slotsThe number of slots (max allowed value is 65536)
Returns:
int

Definition at line 145 of file PoolCounter.php.

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

string PoolCounter::$key [protected]

All workers with the same key share the lock *.

Definition at line 54 of file PoolCounter.php.

Referenced by PoolCounterRedis\__construct().

PoolCounter::$maxqueue [protected]

Definition at line 63 of file PoolCounter.php.

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 61 of file PoolCounter.php.

float PoolCounter::$timeout [protected]

Maximum time in seconds to wait for the lock *.

Definition at line 64 of file PoolCounter.php.

int PoolCounter::$workers [protected]

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

Definition at line 55 of file PoolCounter.php.

const PoolCounter::DONE = 3

Definition at line 46 of file PoolCounter.php.

const PoolCounter::ERROR = -1

Definition at line 48 of file PoolCounter.php.

Definition at line 52 of file PoolCounter.php.

Definition at line 49 of file PoolCounter.php.

Definition at line 50 of file PoolCounter.php.

Definition at line 45 of file PoolCounter.php.

Definition at line 51 of file PoolCounter.php.


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