4 * When you have many workers (threads/servers) giving service, and a
5 * cached item expensive to produce expires, you may get several workers
6 * doing the job at the same time.
8 * Given enough requests and the item expiring fast (non-cacheable,
9 * lots of edits...) that single work can end up unfairly using most (all)
10 * of the cpu of the pool. This is also known as 'Michael Jackson effect'
11 * since this effect triggered on the english wikipedia on the day Michael
12 * Jackson died, the biographical article got hit with several edits per
13 * minutes and hundreds of read hits.
15 * The PoolCounter provides semaphore semantics for restricting the number
16 * of workers that may be concurrently performing such single task.
18 * By default PoolCounter_Stub is used, which provides no locking. You
19 * can get a useful one in the PoolCounter extension.
21 abstract class PoolCounter
{
24 const LOCKED
= 1; /* Lock acquired */
25 const RELEASED
= 2; /* Lock released */
26 const DONE
= 3; /* Another worker did the work for you */
28 const ERROR
= -1; /* Indeterminate error */
29 const NOT_LOCKED
= -2; /* Called release() with no lock held */
30 const QUEUE_FULL
= -3; /* There are already maxqueue workers on this lock */
31 const TIMEOUT
= -4; /* Timeout exceeded */
32 const LOCK_HELD
= -5; /* Cannot acquire another lock while you have one lock held */
35 * I want to do this task and I need to do it myself.
37 * @return Locked/Error
39 abstract function acquireForMe();
42 * I want to do this task, but if anyone else does it
43 * instead, it's also fine for me. I will read its cached data.
45 * @return Locked/Done/Error
47 abstract function acquireForAnyone();
50 * I have successfully finished my task.
51 * Lets another one grab the lock, and returns the workers
52 * waiting on acquireForAnyone()
54 * @return Released/NotLocked/Error
56 abstract function release();
59 * $key: All workers with the same key share the lock.
60 * $workers: It wouldn't be a good idea to have more than this number of
61 * workers doing the task simultaneously.
62 * $maxqueue: If this number of workers are already working/waiting,
63 * fail instead of wait.
64 * $timeout: Maximum time in seconds to wait for the lock.
66 protected $key, $workers, $maxqueue, $timeout;
69 * Create a Pool counter. This should only be called from the PoolWorks.
71 public static function factory( $type, $key ) {
72 global $wgPoolCounterConf;
73 if ( !isset( $wgPoolCounterConf[$type] ) ) {
74 return new PoolCounter_Stub
;
76 $conf = $wgPoolCounterConf[$type];
77 $class = $conf['class'];
79 return new $class( $conf, $type, $key );
82 protected function __construct( $conf, $type, $key ) {
84 $this->workers
= $conf['workers'];
85 $this->maxqueue
= $conf['maxqueue'];
86 $this->timeout
= $conf['timeout'];
90 class PoolCounter_Stub
extends PoolCounter
{
91 function acquireForMe() {
92 return Status
::newGood( PoolCounter
::LOCKED
);
95 function acquireForAnyone() {
96 return Status
::newGood( PoolCounter
::LOCKED
);
100 return Status
::newGood( PoolCounter
::RELEASED
);
103 public function __construct() {
104 /* No parameters needed */
109 * Handy class for dealing with PoolCounters using class members instead of callbacks.
111 abstract class PoolCounterWork
{
112 protected $cacheable = false; //Does this override getCachedWork() ?
115 * Actually perform the work, caching it if needed.
117 abstract function doWork();
120 * Retrieve the work from cache
121 * @return mixed work result or false
123 function getCachedWork() {
128 * A work not so good (eg. expired one) but better than an error
130 * @return mixed work result or false
132 function fallback() {
137 * Do something with the error, like showing it to the user.
139 function error( $status ) {
146 function logError( $status ) {
147 wfDebugLog( 'poolcounter', $status->getWikiText() );
151 * Get the result of the work (whatever it is), or false.
153 function execute( $skipcache = false ) {
154 if ( $this->cacheable
&& !$skipcache ) {
155 $status = $this->poolCounter
->acquireForAnyone();
157 $status = $this->poolCounter
->acquireForMe();
160 if ( !$status->isOK() ) {
161 // Respond gracefully to complete server breakage: just log it and do the work
162 $this->logError( $status );
163 return $this->doWork();
166 switch ( $status->value
) {
167 case PoolCounter
::LOCKED
:
168 $result = $this->doWork();
169 $this->poolCounter
->release();
172 case PoolCounter
::DONE
:
173 $result = $this->getCachedWork();
174 if ( $result === false ) {
175 /* That someone else work didn't serve us.
176 * Acquire the lock for me
178 return $this->execute( true );
182 case PoolCounter
::QUEUE_FULL
:
183 case PoolCounter
::TIMEOUT
:
184 $result = $this->fallback();
186 if ( $result !== false ) {
191 /* These two cases should never be hit... */
192 case PoolCounter
::ERROR
:
194 $errors = array( PoolCounter
::QUEUE_FULL
=> 'pool-queuefull', PoolCounter
::TIMEOUT
=> 'pool-timeout' );
196 $status = Status
::newFatal( isset($errors[$status->value
]) ?
$errors[$status->value
] : 'pool-errorunknown' );
197 $this->logError( $status );
198 return $this->error( $status );
202 function __construct( $type, $key ) {
203 $this->poolCounter
= PoolCounter
::factory( $type, $key );