Merge "(bug #56685) make sure commafy can deal with strings."
[lhc/web/wiklou.git] / includes / job / JobQueueGroup.php
1 <?php
2 /**
3 * Job queue base code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle enqueueing of background jobs
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueGroup {
31 /** @var array */
32 protected static $instances = array();
33
34 /** @var ProcessCacheLRU */
35 protected $cache;
36
37 /** @var string Wiki ID */
38 protected $wiki;
39
40 /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
41 protected $coalescedQueues;
42
43 const TYPE_DEFAULT = 1; // integer; jobs popped by default
44 const TYPE_ANY = 2; // integer; any job
45
46 const USE_CACHE = 1; // integer; use process or persistent cache
47 const USE_PRIORITY = 2; // integer; respect deprioritization
48
49 const PROC_CACHE_TTL = 15; // integer; seconds
50
51 const CACHE_VERSION = 1; // integer; cache version
52
53 /**
54 * @param string $wiki Wiki ID
55 */
56 protected function __construct( $wiki ) {
57 $this->wiki = $wiki;
58 $this->cache = new ProcessCacheLRU( 10 );
59 }
60
61 /**
62 * @param bool|string $wiki Wiki ID
63 * @return JobQueueGroup
64 */
65 public static function singleton( $wiki = false ) {
66 $wiki = ( $wiki === false ) ? wfWikiID() : $wiki;
67 if ( !isset( self::$instances[$wiki] ) ) {
68 self::$instances[$wiki] = new self( $wiki );
69 }
70
71 return self::$instances[$wiki];
72 }
73
74 /**
75 * Destroy the singleton instances
76 *
77 * @return void
78 */
79 public static function destroySingletons() {
80 self::$instances = array();
81 }
82
83 /**
84 * Get the job queue object for a given queue type
85 *
86 * @param string $type
87 * @return JobQueue
88 */
89 public function get( $type ) {
90 global $wgJobTypeConf;
91
92 $conf = array( 'wiki' => $this->wiki, 'type' => $type );
93 if ( isset( $wgJobTypeConf[$type] ) ) {
94 $conf = $conf + $wgJobTypeConf[$type];
95 } else {
96 $conf = $conf + $wgJobTypeConf['default'];
97 }
98
99 return JobQueue::factory( $conf );
100 }
101
102 /**
103 * Insert jobs into the respective queues of with the belong.
104 *
105 * This inserts the jobs into the queue specified by $wgJobTypeConf
106 * and updates the aggregate job queue information cache as needed.
107 *
108 * @param Job|array $jobs A single Job or a list of Jobs
109 * @throws MWException
110 * @return bool
111 */
112 public function push( $jobs ) {
113 $jobs = is_array( $jobs ) ? $jobs : array( $jobs );
114
115 $jobsByType = array(); // (job type => list of jobs)
116 foreach ( $jobs as $job ) {
117 if ( $job instanceof Job ) {
118 $jobsByType[$job->getType()][] = $job;
119 } else {
120 throw new MWException( "Attempted to push a non-Job object into a queue." );
121 }
122 }
123
124 $ok = true;
125 foreach ( $jobsByType as $type => $jobs ) {
126 if ( $this->get( $type )->push( $jobs ) ) {
127 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type );
128 } else {
129 $ok = false;
130 }
131 }
132
133 if ( $this->cache->has( 'queues-ready', 'list' ) ) {
134 $list = $this->cache->get( 'queues-ready', 'list' );
135 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
136 $this->cache->clear( 'queues-ready' );
137 }
138 }
139
140 return $ok;
141 }
142
143 /**
144 * Pop a job off one of the job queues
145 *
146 * This pops a job off a queue as specified by $wgJobTypeConf and
147 * updates the aggregate job queue information cache as needed.
148 *
149 * @param int|string $qtype JobQueueGroup::TYPE_DEFAULT or type string
150 * @param int $flags Bitfield of JobQueueGroup::USE_* constants
151 * @return Job|bool Returns false on failure
152 */
153 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0 ) {
154 if ( is_string( $qtype ) ) { // specific job type
155 if ( ( $flags & self::USE_PRIORITY ) && $this->isQueueDeprioritized( $qtype ) ) {
156 return false; // back off
157 }
158 $job = $this->get( $qtype )->pop();
159 if ( !$job ) {
160 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $qtype );
161 }
162
163 return $job;
164 } else { // any job in the "default" jobs types
165 if ( $flags & self::USE_CACHE ) {
166 if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
167 $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
168 }
169 $types = $this->cache->get( 'queues-ready', 'list' );
170 } else {
171 $types = $this->getQueuesWithJobs();
172 }
173
174 if ( $qtype == self::TYPE_DEFAULT ) {
175 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
176 }
177 shuffle( $types ); // avoid starvation
178
179 foreach ( $types as $type ) { // for each queue...
180 if ( ( $flags & self::USE_PRIORITY ) && $this->isQueueDeprioritized( $type ) ) {
181 continue; // back off
182 }
183 $job = $this->get( $type )->pop();
184 if ( $job ) { // found
185 return $job;
186 } else { // not found
187 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $type );
188 $this->cache->clear( 'queues-ready' );
189 }
190 }
191
192 return false; // no jobs found
193 }
194 }
195
196 /**
197 * Acknowledge that a job was completed
198 *
199 * @param Job $job
200 * @return bool
201 */
202 public function ack( Job $job ) {
203 return $this->get( $job->getType() )->ack( $job );
204 }
205
206 /**
207 * Register the "root job" of a given job into the queue for de-duplication.
208 * This should only be called right *after* all the new jobs have been inserted.
209 *
210 * @param Job $job
211 * @return bool
212 */
213 public function deduplicateRootJob( Job $job ) {
214 return $this->get( $job->getType() )->deduplicateRootJob( $job );
215 }
216
217 /**
218 * Wait for any slaves or backup queue servers to catch up.
219 *
220 * This does nothing for certain queue classes.
221 *
222 * @return void
223 * @throws MWException
224 */
225 public function waitForBackups() {
226 global $wgJobTypeConf;
227
228 wfProfileIn( __METHOD__ );
229 // Try to avoid doing this more than once per queue storage medium
230 foreach ( $wgJobTypeConf as $type => $conf ) {
231 $this->get( $type )->waitForBackups();
232 }
233 wfProfileOut( __METHOD__ );
234 }
235
236 /**
237 * Get the list of queue types
238 *
239 * @return array List of strings
240 */
241 public function getQueueTypes() {
242 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
243 }
244
245 /**
246 * Get the list of default queue types
247 *
248 * @return array List of strings
249 */
250 public function getDefaultQueueTypes() {
251 global $wgJobTypesExcludedFromDefaultQueue;
252
253 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
254 }
255
256 /**
257 * Get the list of job types that have non-empty queues
258 *
259 * @return array List of job types that have non-empty queues
260 */
261 public function getQueuesWithJobs() {
262 $types = array();
263 foreach ( $this->getCoalescedQueues() as $info ) {
264 $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() );
265 if ( is_array( $nonEmpty ) ) { // batching features supported
266 $types = array_merge( $types, $nonEmpty );
267 } else { // we have to go through the queues in the bucket one-by-one
268 foreach ( $info['types'] as $type ) {
269 if ( !$this->get( $type )->isEmpty() ) {
270 $types[] = $type;
271 }
272 }
273 }
274 }
275
276 return $types;
277 }
278
279 /**
280 * Get the size of the queus for a list of job types
281 *
282 * @return array Map of (job type => size)
283 */
284 public function getQueueSizes() {
285 $sizeMap = array();
286 foreach ( $this->getCoalescedQueues() as $info ) {
287 $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() );
288 if ( is_array( $sizes ) ) { // batching features supported
289 $sizeMap = $sizeMap + $sizes;
290 } else { // we have to go through the queues in the bucket one-by-one
291 foreach ( $info['types'] as $type ) {
292 $sizeMap[$type] = $this->get( $type )->getSize();
293 }
294 }
295 }
296
297 return $sizeMap;
298 }
299
300 /**
301 * @return array
302 */
303 protected function getCoalescedQueues() {
304 global $wgJobTypeConf;
305
306 if ( $this->coalescedQueues === null ) {
307 $this->coalescedQueues = array();
308 foreach ( $wgJobTypeConf as $type => $conf ) {
309 $queue = JobQueue::factory(
310 array( 'wiki' => $this->wiki, 'type' => 'null' ) + $conf );
311 $loc = $queue->getCoalesceLocationInternal();
312 if ( !isset( $this->coalescedQueues[$loc] ) ) {
313 $this->coalescedQueues[$loc]['queue'] = $queue;
314 $this->coalescedQueues[$loc]['types'] = array();
315 }
316 if ( $type === 'default' ) {
317 $this->coalescedQueues[$loc]['types'] = array_merge(
318 $this->coalescedQueues[$loc]['types'],
319 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
320 );
321 } else {
322 $this->coalescedQueues[$loc]['types'][] = $type;
323 }
324 }
325 }
326
327 return $this->coalescedQueues;
328 }
329
330 /**
331 * Check if jobs should not be popped of a queue right now.
332 * This is only used for performance, such as to avoid spamming
333 * the queue with many sub-jobs before they actually get run.
334 *
335 * @param string $type
336 * @return bool
337 */
338 public function isQueueDeprioritized( $type ) {
339 if ( $this->cache->has( 'isDeprioritized', $type, 5 ) ) {
340 return $this->cache->get( 'isDeprioritized', $type );
341 }
342 if ( $type === 'refreshLinks2' ) {
343 // Don't keep converting refreshLinks2 => refreshLinks jobs if the
344 // later jobs have not been done yet. This helps throttle queue spam.
345 $deprioritized = !$this->get( 'refreshLinks' )->isEmpty();
346 $this->cache->set( 'isDeprioritized', $type, $deprioritized );
347
348 return $deprioritized;
349 }
350
351 return false;
352 }
353
354 /**
355 * Execute any due periodic queue maintenance tasks for all queues.
356 *
357 * A task is "due" if the time ellapsed since the last run is greater than
358 * the defined run period. Concurrent calls to this function will cause tasks
359 * to be attempted twice, so they may need their own methods of mutual exclusion.
360 *
361 * @return int Number of tasks run
362 */
363 public function executeReadyPeriodicTasks() {
364 global $wgMemc;
365
366 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
367 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' );
368 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp)
369
370 $count = 0;
371 $tasksRun = array(); // (queue => task => UNIX timestamp)
372 foreach ( $this->getQueueTypes() as $type ) {
373 $queue = $this->get( $type );
374 foreach ( $queue->getPeriodicTasks() as $task => $definition ) {
375 if ( $definition['period'] <= 0 ) {
376 continue; // disabled
377 } elseif ( !isset( $lastRuns[$type][$task] )
378 || $lastRuns[$type][$task] < ( time() - $definition['period'] )
379 ) {
380 try {
381 if ( call_user_func( $definition['callback'] ) !== null ) {
382 $tasksRun[$type][$task] = time();
383 ++$count;
384 }
385 } catch ( JobQueueError $e ) {
386 MWExceptionHandler::logException( $e );
387 }
388 }
389 }
390 }
391
392 $wgMemc->merge( $key, function ( $cache, $key, $lastRuns ) use ( $tasksRun ) {
393 if ( is_array( $lastRuns ) ) {
394 foreach ( $tasksRun as $type => $tasks ) {
395 foreach ( $tasks as $task => $timestamp ) {
396 if ( !isset( $lastRuns[$type][$task] )
397 || $timestamp > $lastRuns[$type][$task]
398 ) {
399 $lastRuns[$type][$task] = $timestamp;
400 }
401 }
402 }
403 } else {
404 $lastRuns = $tasksRun;
405 }
406
407 return $lastRuns;
408 } );
409
410 return $count;
411 }
412
413 /**
414 * @param $name string
415 * @return mixed
416 */
417 private function getCachedConfigVar( $name ) {
418 global $wgConf, $wgMemc;
419
420 if ( $this->wiki === wfWikiID() ) {
421 return $GLOBALS[$name]; // common case
422 } else {
423 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
424 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name );
425 $value = $wgMemc->get( $key ); // ('v' => ...) or false
426 if ( is_array( $value ) ) {
427 return $value['v'];
428 } else {
429 $value = $wgConf->getConfig( $this->wiki, $name );
430 $wgMemc->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) );
431
432 return $value;
433 }
434 }
435 }
436 }