Removed configuration storage in the MediaWiki class:
[lhc/web/wiklou.git] / includes / PoolCounter.php
1 <?php
2
3 /**
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.
7 *
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.
14 *
15 * The PoolCounter provides semaphore semantics for restricting the number
16 * of workers that may be concurrently performing such single task.
17 *
18 * By default PoolCounter_Stub is used, which provides no locking. You
19 * can get a useful one in the PoolCounter extension.
20 */
21 abstract class PoolCounter {
22
23 /* Return codes */
24 const LOCKED = 1; /* Lock acquired */
25 const RELEASED = 2; /* Lock released */
26 const DONE = 3; /* Another worker did the work for you */
27
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 */
33
34 /**
35 * I want to do this task and I need to do it myself.
36 *
37 * @return Locked/Error
38 */
39 abstract function acquireForMe();
40
41 /**
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.
44 *
45 * @return Locked/Done/Error
46 */
47 abstract function acquireForAnyone();
48
49 /**
50 * I have successfully finished my task.
51 * Lets another one grab the lock, and returns the workers
52 * waiting on acquireForAnyone()
53 *
54 * @return Released/NotLocked/Error
55 */
56 abstract function release();
57
58 /**
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.
65 */
66 protected $key, $workers, $maxqueue, $timeout;
67
68 /**
69 * Create a Pool counter. This should only be called from the PoolWorks.
70 */
71 public static function factory( $type, $key ) {
72 global $wgPoolCounterConf;
73 if ( !isset( $wgPoolCounterConf[$type] ) ) {
74 return new PoolCounter_Stub;
75 }
76 $conf = $wgPoolCounterConf[$type];
77 $class = $conf['class'];
78
79 return new $class( $conf, $type, $key );
80 }
81
82 protected function __construct( $conf, $type, $key ) {
83 $this->key = $key;
84 $this->workers = $conf['workers'];
85 $this->maxqueue = $conf['maxqueue'];
86 $this->timeout = $conf['timeout'];
87 }
88 }
89
90 class PoolCounter_Stub extends PoolCounter {
91 function acquireForMe() {
92 return Status::newGood( PoolCounter::LOCKED );
93 }
94
95 function acquireForAnyone() {
96 return Status::newGood( PoolCounter::LOCKED );
97 }
98
99 function release() {
100 return Status::newGood( PoolCounter::RELEASED );
101 }
102
103 public function __construct() {
104 /* No parameters needed */
105 }
106 }
107
108 /**
109 * Handy class for dealing with PoolCounters using class members instead of callbacks.
110 */
111 abstract class PoolCounterWork {
112 protected $cacheable = false; //Does this override getCachedWork() ?
113
114 /**
115 * Actually perform the work, caching it if needed.
116 */
117 abstract function doWork();
118
119 /**
120 * Retrieve the work from cache
121 * @return mixed work result or false
122 */
123 function getCachedWork() {
124 return false;
125 }
126
127 /**
128 * A work not so good (eg. expired one) but better than an error
129 * message.
130 * @return mixed work result or false
131 */
132 function fallback() {
133 return false;
134 }
135
136 /**
137 * Do something with the error, like showing it to the user.
138 */
139 function error( $status ) {
140 return false;
141 }
142
143 /**
144 * Log an error
145 */
146 function logError( $status ) {
147 wfDebugLog( 'poolcounter', $status->getWikiText() );
148 }
149
150 /**
151 * Get the result of the work (whatever it is), or false.
152 */
153 function execute( $skipcache = false ) {
154 if ( $this->cacheable && !$skipcache ) {
155 $status = $this->poolCounter->acquireForAnyone();
156 } else {
157 $status = $this->poolCounter->acquireForMe();
158 }
159
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();
164 }
165
166 switch ( $status->value ) {
167 case PoolCounter::LOCKED:
168 $result = $this->doWork();
169 $this->poolCounter->release();
170 return $result;
171
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
177 */
178 return $this->execute( true );
179 }
180 return $result;
181
182 case PoolCounter::QUEUE_FULL:
183 case PoolCounter::TIMEOUT:
184 $result = $this->fallback();
185
186 if ( $result !== false ) {
187 return $result;
188 }
189 /* no break */
190
191 /* These two cases should never be hit... */
192 case PoolCounter::ERROR:
193 default:
194 $errors = array( PoolCounter::QUEUE_FULL => 'pool-queuefull', PoolCounter::TIMEOUT => 'pool-timeout' );
195
196 $status = Status::newFatal( isset($errors[$status->value]) ? $errors[$status->value] : 'pool-errorunknown' );
197 $this->logError( $status );
198 return $this->error( $status );
199 }
200 }
201
202 function __construct( $type, $key ) {
203 $this->poolCounter = PoolCounter::factory( $type, $key );
204 }
205 }