Release notes for Iabf4873f
[mediawiki.git] / includes / job / JobQueueGroup.php
bloba3ec8a7fea7900d4e98ac7236c0c573f4c1f0c9b
1 <?php
2 /**
3 * Job queue base code.
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.
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.
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
20 * @file
21 * @author Aaron Schulz
24 /**
25 * Class to handle enqueueing of background jobs
27 * @ingroup JobQueue
28 * @since 1.21
30 class JobQueueGroup {
31 /** @var array */
32 protected static $instances = array();
34 /** @var ProcessCacheLRU */
35 protected $cache;
37 /** @var string Wiki ID */
38 protected $wiki;
40 /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
41 protected $coalescedQueues;
43 const TYPE_DEFAULT = 1; // integer; jobs popped by default
44 const TYPE_ANY = 2; // integer; any job
46 const USE_CACHE = 1; // integer; use process or persistent cache
47 const USE_PRIORITY = 2; // integer; respect deprioritization
49 const PROC_CACHE_TTL = 15; // integer; seconds
51 const CACHE_VERSION = 1; // integer; cache version
53 /**
54 * @param string $wiki Wiki ID
56 protected function __construct( $wiki ) {
57 $this->wiki = $wiki;
58 $this->cache = new ProcessCacheLRU( 10 );
61 /**
62 * @param bool|string $wiki Wiki ID
63 * @return JobQueueGroup
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 );
71 return self::$instances[$wiki];
74 /**
75 * Destroy the singleton instances
77 * @return void
79 public static function destroySingletons() {
80 self::$instances = array();
83 /**
84 * Get the job queue object for a given queue type
86 * @param string $type
87 * @return JobQueue
89 public function get( $type ) {
90 global $wgJobTypeConf;
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'];
99 return JobQueue::factory( $conf );
103 * Insert jobs into the respective queues of with the belong.
105 * This inserts the jobs into the queue specified by $wgJobTypeConf
106 * and updates the aggregate job queue information cache as needed.
108 * @param Job|array $jobs A single Job or a list of Jobs
109 * @throws MWException
110 * @return bool
112 public function push( $jobs ) {
113 $jobs = is_array( $jobs ) ? $jobs : array( $jobs );
114 if ( !count( $jobs ) ) {
115 return true;
118 $jobsByType = array(); // (job type => list of jobs)
119 foreach ( $jobs as $job ) {
120 if ( $job instanceof Job ) {
121 $jobsByType[$job->getType()][] = $job;
122 } else {
123 throw new MWException( "Attempted to push a non-Job object into a queue." );
127 $ok = true;
128 foreach ( $jobsByType as $type => $jobs ) {
129 if ( $this->get( $type )->push( $jobs ) ) {
130 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type );
131 } else {
132 $ok = false;
136 if ( $this->cache->has( 'queues-ready', 'list' ) ) {
137 $list = $this->cache->get( 'queues-ready', 'list' );
138 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
139 $this->cache->clear( 'queues-ready' );
143 return $ok;
147 * Pop a job off one of the job queues
149 * This pops a job off a queue as specified by $wgJobTypeConf and
150 * updates the aggregate job queue information cache as needed.
152 * @param int|string $qtype JobQueueGroup::TYPE_DEFAULT or type string
153 * @param int $flags Bitfield of JobQueueGroup::USE_* constants
154 * @return Job|bool Returns false on failure
156 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0 ) {
157 if ( is_string( $qtype ) ) { // specific job type
158 if ( ( $flags & self::USE_PRIORITY ) && $this->isQueueDeprioritized( $qtype ) ) {
159 return false; // back off
161 $job = $this->get( $qtype )->pop();
162 if ( !$job ) {
163 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $qtype );
166 return $job;
167 } else { // any job in the "default" jobs types
168 if ( $flags & self::USE_CACHE ) {
169 if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
170 $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
172 $types = $this->cache->get( 'queues-ready', 'list' );
173 } else {
174 $types = $this->getQueuesWithJobs();
177 if ( $qtype == self::TYPE_DEFAULT ) {
178 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
180 shuffle( $types ); // avoid starvation
182 foreach ( $types as $type ) { // for each queue...
183 if ( ( $flags & self::USE_PRIORITY ) && $this->isQueueDeprioritized( $type ) ) {
184 continue; // back off
186 $job = $this->get( $type )->pop();
187 if ( $job ) { // found
188 return $job;
189 } else { // not found
190 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $type );
191 $this->cache->clear( 'queues-ready' );
195 return false; // no jobs found
200 * Acknowledge that a job was completed
202 * @param Job $job
203 * @return bool
205 public function ack( Job $job ) {
206 return $this->get( $job->getType() )->ack( $job );
210 * Register the "root job" of a given job into the queue for de-duplication.
211 * This should only be called right *after* all the new jobs have been inserted.
213 * @param Job $job
214 * @return bool
216 public function deduplicateRootJob( Job $job ) {
217 return $this->get( $job->getType() )->deduplicateRootJob( $job );
221 * Wait for any slaves or backup queue servers to catch up.
223 * This does nothing for certain queue classes.
225 * @return void
226 * @throws MWException
228 public function waitForBackups() {
229 global $wgJobTypeConf;
231 wfProfileIn( __METHOD__ );
232 // Try to avoid doing this more than once per queue storage medium
233 foreach ( $wgJobTypeConf as $type => $conf ) {
234 $this->get( $type )->waitForBackups();
236 wfProfileOut( __METHOD__ );
240 * Get the list of queue types
242 * @return array List of strings
244 public function getQueueTypes() {
245 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
249 * Get the list of default queue types
251 * @return array List of strings
253 public function getDefaultQueueTypes() {
254 global $wgJobTypesExcludedFromDefaultQueue;
256 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
260 * Get the list of job types that have non-empty queues
262 * @return array List of job types that have non-empty queues
264 public function getQueuesWithJobs() {
265 $types = array();
266 foreach ( $this->getCoalescedQueues() as $info ) {
267 $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() );
268 if ( is_array( $nonEmpty ) ) { // batching features supported
269 $types = array_merge( $types, $nonEmpty );
270 } else { // we have to go through the queues in the bucket one-by-one
271 foreach ( $info['types'] as $type ) {
272 if ( !$this->get( $type )->isEmpty() ) {
273 $types[] = $type;
279 return $types;
283 * Get the size of the queus for a list of job types
285 * @return array Map of (job type => size)
287 public function getQueueSizes() {
288 $sizeMap = array();
289 foreach ( $this->getCoalescedQueues() as $info ) {
290 $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() );
291 if ( is_array( $sizes ) ) { // batching features supported
292 $sizeMap = $sizeMap + $sizes;
293 } else { // we have to go through the queues in the bucket one-by-one
294 foreach ( $info['types'] as $type ) {
295 $sizeMap[$type] = $this->get( $type )->getSize();
300 return $sizeMap;
304 * @return array
306 protected function getCoalescedQueues() {
307 global $wgJobTypeConf;
309 if ( $this->coalescedQueues === null ) {
310 $this->coalescedQueues = array();
311 foreach ( $wgJobTypeConf as $type => $conf ) {
312 $queue = JobQueue::factory(
313 array( 'wiki' => $this->wiki, 'type' => 'null' ) + $conf );
314 $loc = $queue->getCoalesceLocationInternal();
315 if ( !isset( $this->coalescedQueues[$loc] ) ) {
316 $this->coalescedQueues[$loc]['queue'] = $queue;
317 $this->coalescedQueues[$loc]['types'] = array();
319 if ( $type === 'default' ) {
320 $this->coalescedQueues[$loc]['types'] = array_merge(
321 $this->coalescedQueues[$loc]['types'],
322 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
324 } else {
325 $this->coalescedQueues[$loc]['types'][] = $type;
330 return $this->coalescedQueues;
334 * Check if jobs should not be popped of a queue right now.
335 * This is only used for performance, such as to avoid spamming
336 * the queue with many sub-jobs before they actually get run.
338 * @param string $type
339 * @return bool
341 public function isQueueDeprioritized( $type ) {
342 if ( $this->cache->has( 'isDeprioritized', $type, 5 ) ) {
343 return $this->cache->get( 'isDeprioritized', $type );
345 if ( $type === 'refreshLinks2' ) {
346 // Don't keep converting refreshLinksPartition => refreshLinks jobs if the
347 // later jobs have not been done yet. This helps throttle queue spam.
348 // @TODO: this is mostly a WMF-specific hack and should be removed when
349 // refreshLinks2 jobs are drained.
350 $deprioritized = !$this->get( 'refreshLinks' )->getSize() > 10000;
351 $this->cache->set( 'isDeprioritized', $type, $deprioritized );
353 return $deprioritized;
356 return false;
360 * Execute any due periodic queue maintenance tasks for all queues.
362 * A task is "due" if the time ellapsed since the last run is greater than
363 * the defined run period. Concurrent calls to this function will cause tasks
364 * to be attempted twice, so they may need their own methods of mutual exclusion.
366 * @return int Number of tasks run
368 public function executeReadyPeriodicTasks() {
369 global $wgMemc;
371 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
372 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' );
373 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp)
375 $count = 0;
376 $tasksRun = array(); // (queue => task => UNIX timestamp)
377 foreach ( $this->getQueueTypes() as $type ) {
378 $queue = $this->get( $type );
379 foreach ( $queue->getPeriodicTasks() as $task => $definition ) {
380 if ( $definition['period'] <= 0 ) {
381 continue; // disabled
382 } elseif ( !isset( $lastRuns[$type][$task] )
383 || $lastRuns[$type][$task] < ( time() - $definition['period'] )
385 try {
386 if ( call_user_func( $definition['callback'] ) !== null ) {
387 $tasksRun[$type][$task] = time();
388 ++$count;
390 } catch ( JobQueueError $e ) {
391 MWExceptionHandler::logException( $e );
397 $wgMemc->merge( $key, function ( $cache, $key, $lastRuns ) use ( $tasksRun ) {
398 if ( is_array( $lastRuns ) ) {
399 foreach ( $tasksRun as $type => $tasks ) {
400 foreach ( $tasks as $task => $timestamp ) {
401 if ( !isset( $lastRuns[$type][$task] )
402 || $timestamp > $lastRuns[$type][$task]
404 $lastRuns[$type][$task] = $timestamp;
408 } else {
409 $lastRuns = $tasksRun;
412 return $lastRuns;
413 } );
415 return $count;
419 * @param $name string
420 * @return mixed
422 private function getCachedConfigVar( $name ) {
423 global $wgConf, $wgMemc;
425 if ( $this->wiki === wfWikiID() ) {
426 return $GLOBALS[$name]; // common case
427 } else {
428 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
429 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name );
430 $value = $wgMemc->get( $key ); // ('v' => ...) or false
431 if ( is_array( $value ) ) {
432 return $value['v'];
433 } else {
434 $value = $wgConf->getConfig( $this->wiki, $name );
435 $wgMemc->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) );
437 return $value;