4f11bbf7fa7875dae55fc8961a936637259e4b15
[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 if ( !count( $jobs ) ) {
115 return true;
116 }
117
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." );
124 }
125 }
126
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;
133 }
134 }
135
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' );
140 }
141 }
142
143 return $ok;
144 }
145
146 /**
147 * Pop a job off one of the job queues
148 *
149 * This pops a job off a queue as specified by $wgJobTypeConf and
150 * updates the aggregate job queue information cache as needed.
151 *
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
155 */
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
160 }
161 $job = $this->get( $qtype )->pop();
162 if ( !$job ) {
163 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $qtype );
164 }
165
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() );
171 }
172 $types = $this->cache->get( 'queues-ready', 'list' );
173 } else {
174 $types = $this->getQueuesWithJobs();
175 }
176
177 if ( $qtype == self::TYPE_DEFAULT ) {
178 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
179 }
180 shuffle( $types ); // avoid starvation
181
182 foreach ( $types as $type ) { // for each queue...
183 if ( ( $flags & self::USE_PRIORITY ) && $this->isQueueDeprioritized( $type ) ) {
184 continue; // back off
185 }
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' );
192 }
193 }
194
195 return false; // no jobs found
196 }
197 }
198
199 /**
200 * Acknowledge that a job was completed
201 *
202 * @param Job $job
203 * @return bool
204 */
205 public function ack( Job $job ) {
206 return $this->get( $job->getType() )->ack( $job );
207 }
208
209 /**
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.
212 *
213 * @param Job $job
214 * @return bool
215 */
216 public function deduplicateRootJob( Job $job ) {
217 return $this->get( $job->getType() )->deduplicateRootJob( $job );
218 }
219
220 /**
221 * Wait for any slaves or backup queue servers to catch up.
222 *
223 * This does nothing for certain queue classes.
224 *
225 * @return void
226 * @throws MWException
227 */
228 public function waitForBackups() {
229 global $wgJobTypeConf;
230
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();
235 }
236 wfProfileOut( __METHOD__ );
237 }
238
239 /**
240 * Get the list of queue types
241 *
242 * @return array List of strings
243 */
244 public function getQueueTypes() {
245 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
246 }
247
248 /**
249 * Get the list of default queue types
250 *
251 * @return array List of strings
252 */
253 public function getDefaultQueueTypes() {
254 global $wgJobTypesExcludedFromDefaultQueue;
255
256 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
257 }
258
259 /**
260 * Get the list of job types that have non-empty queues
261 *
262 * @return array List of job types that have non-empty queues
263 */
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;
274 }
275 }
276 }
277 }
278
279 return $types;
280 }
281
282 /**
283 * Get the size of the queus for a list of job types
284 *
285 * @return array Map of (job type => size)
286 */
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();
296 }
297 }
298 }
299
300 return $sizeMap;
301 }
302
303 /**
304 * @return array
305 */
306 protected function getCoalescedQueues() {
307 global $wgJobTypeConf;
308
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();
318 }
319 if ( $type === 'default' ) {
320 $this->coalescedQueues[$loc]['types'] = array_merge(
321 $this->coalescedQueues[$loc]['types'],
322 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
323 );
324 } else {
325 $this->coalescedQueues[$loc]['types'][] = $type;
326 }
327 }
328 }
329
330 return $this->coalescedQueues;
331 }
332
333 /**
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.
337 *
338 * @param string $type
339 * @return bool
340 */
341 public function isQueueDeprioritized( $type ) {
342 if ( $this->cache->has( 'isDeprioritized', $type, 5 ) ) {
343 return $this->cache->get( 'isDeprioritized', $type );
344 }
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 );
352
353 return $deprioritized;
354 }
355
356 return false;
357 }
358
359 /**
360 * Execute any due periodic queue maintenance tasks for all queues.
361 *
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.
365 *
366 * @return int Number of tasks run
367 */
368 public function executeReadyPeriodicTasks() {
369 global $wgMemc;
370
371 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
372 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' );
373 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp)
374
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'] )
384 ) {
385 try {
386 if ( call_user_func( $definition['callback'] ) !== null ) {
387 $tasksRun[$type][$task] = time();
388 ++$count;
389 }
390 } catch ( JobQueueError $e ) {
391 MWExceptionHandler::logException( $e );
392 }
393 }
394 }
395 }
396
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]
403 ) {
404 $lastRuns[$type][$task] = $timestamp;
405 }
406 }
407 }
408 } else {
409 $lastRuns = $tasksRun;
410 }
411
412 return $lastRuns;
413 } );
414
415 return $count;
416 }
417
418 /**
419 * @param $name string
420 * @return mixed
421 */
422 private function getCachedConfigVar( $name ) {
423 global $wgConf, $wgMemc;
424
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 ) );
436
437 return $value;
438 }
439 }
440 }
441 }