Don't display multiple language links to the same language
[mediawiki.git] / includes / PoolCounter.php
blob452dbc547f73a2e8a166ee6af1e424e0e4e3ffc6
1 <?php
2 /**
3 * Provides of semaphore semantics for restricting the number
4 * of workers that may be concurrently performing the same task.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
24 /**
25 * When you have many workers (threads/servers) giving service, and a
26 * cached item expensive to produce expires, you may get several workers
27 * doing the job at the same time.
29 * Given enough requests and the item expiring fast (non-cacheable,
30 * lots of edits...) that single work can end up unfairly using most (all)
31 * of the cpu of the pool. This is also known as 'Michael Jackson effect'
32 * since this effect triggered on the english wikipedia on the day Michael
33 * Jackson died, the biographical article got hit with several edits per
34 * minutes and hundreds of read hits.
36 * The PoolCounter provides semaphore semantics for restricting the number
37 * of workers that may be concurrently performing such single task.
39 * By default PoolCounter_Stub is used, which provides no locking. You
40 * can get a useful one in the PoolCounter extension.
42 abstract class PoolCounter {
44 /* Return codes */
45 const LOCKED = 1; /* Lock acquired */
46 const RELEASED = 2; /* Lock released */
47 const DONE = 3; /* Another worker did the work for you */
49 const ERROR = -1; /* Indeterminate error */
50 const NOT_LOCKED = -2; /* Called release() with no lock held */
51 const QUEUE_FULL = -3; /* There are already maxqueue workers on this lock */
52 const TIMEOUT = -4; /* Timeout exceeded */
53 const LOCK_HELD = -5; /* Cannot acquire another lock while you have one lock held */
55 /**
56 * I want to do this task and I need to do it myself.
58 * @return Locked/Error
60 abstract function acquireForMe();
62 /**
63 * I want to do this task, but if anyone else does it
64 * instead, it's also fine for me. I will read its cached data.
66 * @return Locked/Done/Error
68 abstract function acquireForAnyone();
70 /**
71 * I have successfully finished my task.
72 * Lets another one grab the lock, and returns the workers
73 * waiting on acquireForAnyone()
75 * @return Released/NotLocked/Error
77 abstract function release();
79 /**
80 * $key: All workers with the same key share the lock.
81 * $workers: It wouldn't be a good idea to have more than this number of
82 * workers doing the task simultaneously.
83 * $maxqueue: If this number of workers are already working/waiting,
84 * fail instead of wait.
85 * $timeout: Maximum time in seconds to wait for the lock.
87 protected $key, $workers, $maxqueue, $timeout;
89 /**
90 * Create a Pool counter. This should only be called from the PoolWorks.
92 * @param $type
93 * @param $key
95 * @return PoolCounter
97 public static function factory( $type, $key ) {
98 global $wgPoolCounterConf;
99 if ( !isset( $wgPoolCounterConf[$type] ) ) {
100 return new PoolCounter_Stub;
102 $conf = $wgPoolCounterConf[$type];
103 $class = $conf['class'];
105 return new $class( $conf, $type, $key );
108 protected function __construct( $conf, $type, $key ) {
109 $this->key = $key;
110 $this->workers = $conf['workers'];
111 $this->maxqueue = $conf['maxqueue'];
112 $this->timeout = $conf['timeout'];
116 class PoolCounter_Stub extends PoolCounter {
119 * @return Status
121 function acquireForMe() {
122 return Status::newGood( PoolCounter::LOCKED );
126 * @return Status
128 function acquireForAnyone() {
129 return Status::newGood( PoolCounter::LOCKED );
133 * @return Status
135 function release() {
136 return Status::newGood( PoolCounter::RELEASED );
139 public function __construct() {
140 /* No parameters needed */
145 * Handy class for dealing with PoolCounters using class members instead of callbacks.
147 abstract class PoolCounterWork {
148 protected $cacheable = false; //Does this override getCachedWork() ?
151 * Actually perform the work, caching it if needed.
153 abstract function doWork();
156 * Retrieve the work from cache
157 * @return mixed work result or false
159 function getCachedWork() {
160 return false;
164 * A work not so good (eg. expired one) but better than an error
165 * message.
166 * @return mixed work result or false
168 function fallback() {
169 return false;
173 * Do something with the error, like showing it to the user.
174 * @return bool
176 function error( $status ) {
177 return false;
181 * Log an error
183 * @param $status Status
185 function logError( $status ) {
186 wfDebugLog( 'poolcounter', $status->getWikiText() );
190 * Get the result of the work (whatever it is), or false.
191 * @param $skipcache bool
192 * @return bool|mixed
194 function execute( $skipcache = false ) {
195 if ( $this->cacheable && !$skipcache ) {
196 $status = $this->poolCounter->acquireForAnyone();
197 } else {
198 $status = $this->poolCounter->acquireForMe();
201 if ( !$status->isOK() ) {
202 // Respond gracefully to complete server breakage: just log it and do the work
203 $this->logError( $status );
204 return $this->doWork();
207 switch ( $status->value ) {
208 case PoolCounter::LOCKED:
209 $result = $this->doWork();
210 $this->poolCounter->release();
211 return $result;
213 case PoolCounter::DONE:
214 $result = $this->getCachedWork();
215 if ( $result === false ) {
216 /* That someone else work didn't serve us.
217 * Acquire the lock for me
219 return $this->execute( true );
221 return $result;
223 case PoolCounter::QUEUE_FULL:
224 case PoolCounter::TIMEOUT:
225 $result = $this->fallback();
227 if ( $result !== false ) {
228 return $result;
230 /* no break */
232 /* These two cases should never be hit... */
233 case PoolCounter::ERROR:
234 default:
235 $errors = array( PoolCounter::QUEUE_FULL => 'pool-queuefull', PoolCounter::TIMEOUT => 'pool-timeout' );
237 $status = Status::newFatal( isset( $errors[$status->value] ) ? $errors[$status->value] : 'pool-errorunknown' );
238 $this->logError( $status );
239 return $this->error( $status );
243 function __construct( $type, $key ) {
244 $this->poolCounter = PoolCounter::factory( $type, $key );