Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[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 * @param $type
72 * @param $key
73 *
74 * @return PoolCounter
75 */
76 public static function factory( $type, $key ) {
77 global $wgPoolCounterConf;
78 if ( !isset( $wgPoolCounterConf[$type] ) ) {
79 return new PoolCounter_Stub;
80 }
81 $conf = $wgPoolCounterConf[$type];
82 $class = $conf['class'];
83
84 return new $class( $conf, $type, $key );
85 }
86
87 protected function __construct( $conf, $type, $key ) {
88 $this->key = $key;
89 $this->workers = $conf['workers'];
90 $this->maxqueue = $conf['maxqueue'];
91 $this->timeout = $conf['timeout'];
92 }
93 }
94
95 class PoolCounter_Stub extends PoolCounter {
96
97 /**
98 * @return Status
99 */
100 function acquireForMe() {
101 return Status::newGood( PoolCounter::LOCKED );
102 }
103
104 /**
105 * @return Status
106 */
107 function acquireForAnyone() {
108 return Status::newGood( PoolCounter::LOCKED );
109 }
110
111 /**
112 * @return Status
113 */
114 function release() {
115 return Status::newGood( PoolCounter::RELEASED );
116 }
117
118 public function __construct() {
119 /* No parameters needed */
120 }
121 }
122
123 /**
124 * Handy class for dealing with PoolCounters using class members instead of callbacks.
125 */
126 abstract class PoolCounterWork {
127 protected $cacheable = false; //Does this override getCachedWork() ?
128
129 /**
130 * Actually perform the work, caching it if needed.
131 */
132 abstract function doWork();
133
134 /**
135 * Retrieve the work from cache
136 * @return mixed work result or false
137 */
138 function getCachedWork() {
139 return false;
140 }
141
142 /**
143 * A work not so good (eg. expired one) but better than an error
144 * message.
145 * @return mixed work result or false
146 */
147 function fallback() {
148 return false;
149 }
150
151 /**
152 * Do something with the error, like showing it to the user.
153 */
154 function error( $status ) {
155 return false;
156 }
157
158 /**
159 * Log an error
160 *
161 * @param $status Status
162 */
163 function logError( $status ) {
164 wfDebugLog( 'poolcounter', $status->getWikiText() );
165 }
166
167 /**
168 * Get the result of the work (whatever it is), or false.
169 * @param $skipcache bool
170 * @return bool|mixed
171 */
172 function execute( $skipcache = false ) {
173 if ( $this->cacheable && !$skipcache ) {
174 $status = $this->poolCounter->acquireForAnyone();
175 } else {
176 $status = $this->poolCounter->acquireForMe();
177 }
178
179 if ( !$status->isOK() ) {
180 // Respond gracefully to complete server breakage: just log it and do the work
181 $this->logError( $status );
182 return $this->doWork();
183 }
184
185 switch ( $status->value ) {
186 case PoolCounter::LOCKED:
187 $result = $this->doWork();
188 $this->poolCounter->release();
189 return $result;
190
191 case PoolCounter::DONE:
192 $result = $this->getCachedWork();
193 if ( $result === false ) {
194 /* That someone else work didn't serve us.
195 * Acquire the lock for me
196 */
197 return $this->execute( true );
198 }
199 return $result;
200
201 case PoolCounter::QUEUE_FULL:
202 case PoolCounter::TIMEOUT:
203 $result = $this->fallback();
204
205 if ( $result !== false ) {
206 return $result;
207 }
208 /* no break */
209
210 /* These two cases should never be hit... */
211 case PoolCounter::ERROR:
212 default:
213 $errors = array( PoolCounter::QUEUE_FULL => 'pool-queuefull', PoolCounter::TIMEOUT => 'pool-timeout' );
214
215 $status = Status::newFatal( isset( $errors[$status->value] ) ? $errors[$status->value] : 'pool-errorunknown' );
216 $this->logError( $status );
217 return $this->error( $status );
218 }
219 }
220
221 function __construct( $type, $key ) {
222 $this->poolCounter = PoolCounter::factory( $type, $key );
223 }
224 }