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.
76 public static function factory( $type, $key ) {
77 global $wgPoolCounterConf;
78 if ( !isset( $wgPoolCounterConf[$type] ) ) {
79 return new PoolCounter_Stub
;
81 $conf = $wgPoolCounterConf[$type];
82 $class = $conf['class'];
84 return new $class( $conf, $type, $key );
87 protected function __construct( $conf, $type, $key ) {
89 $this->workers
= $conf['workers'];
90 $this->maxqueue
= $conf['maxqueue'];
91 $this->timeout
= $conf['timeout'];
95 class PoolCounter_Stub
extends PoolCounter
{
100 function acquireForMe() {
101 return Status
::newGood( PoolCounter
::LOCKED
);
107 function acquireForAnyone() {
108 return Status
::newGood( PoolCounter
::LOCKED
);
115 return Status
::newGood( PoolCounter
::RELEASED
);
118 public function __construct() {
119 /* No parameters needed */
124 * Handy class for dealing with PoolCounters using class members instead of callbacks.
126 abstract class PoolCounterWork
{
127 protected $cacheable = false; //Does this override getCachedWork() ?
130 * Actually perform the work, caching it if needed.
132 abstract function doWork();
135 * Retrieve the work from cache
136 * @return mixed work result or false
138 function getCachedWork() {
143 * A work not so good (eg. expired one) but better than an error
145 * @return mixed work result or false
147 function fallback() {
152 * Do something with the error, like showing it to the user.
155 function error( $status ) {
162 * @param $status Status
164 function logError( $status ) {
165 wfDebugLog( 'poolcounter', $status->getWikiText() );
169 * Get the result of the work (whatever it is), or false.
170 * @param $skipcache bool
173 function execute( $skipcache = false ) {
174 if ( $this->cacheable
&& !$skipcache ) {
175 $status = $this->poolCounter
->acquireForAnyone();
177 $status = $this->poolCounter
->acquireForMe();
180 if ( !$status->isOK() ) {
181 // Respond gracefully to complete server breakage: just log it and do the work
182 $this->logError( $status );
183 return $this->doWork();
186 switch ( $status->value
) {
187 case PoolCounter
::LOCKED
:
188 $result = $this->doWork();
189 $this->poolCounter
->release();
192 case PoolCounter
::DONE
:
193 $result = $this->getCachedWork();
194 if ( $result === false ) {
195 /* That someone else work didn't serve us.
196 * Acquire the lock for me
198 return $this->execute( true );
202 case PoolCounter
::QUEUE_FULL
:
203 case PoolCounter
::TIMEOUT
:
204 $result = $this->fallback();
206 if ( $result !== false ) {
211 /* These two cases should never be hit... */
212 case PoolCounter
::ERROR
:
214 $errors = array( PoolCounter
::QUEUE_FULL
=> 'pool-queuefull', PoolCounter
::TIMEOUT
=> 'pool-timeout' );
216 $status = Status
::newFatal( isset( $errors[$status->value
] ) ?
$errors[$status->value
] : 'pool-errorunknown' );
217 $this->logError( $status );
218 return $this->error( $status );
222 function __construct( $type, $key ) {
223 $this->poolCounter
= PoolCounter
::factory( $type, $key );