Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / jobqueue / 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 */
22 use MediaWiki\MediaWikiServices;
23
24 /**
25 * Class to handle enqueueing of background jobs
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueGroup {
31 /** @var JobQueueGroup[] */
32 protected static $instances = [];
33
34 /** @var ProcessCacheLRU */
35 protected $cache;
36
37 /** @var string Wiki domain ID */
38 protected $domain;
39 /** @var string|bool Read only rationale (or false if r/w) */
40 protected $readOnlyReason;
41 /** @var bool Whether the wiki is not recognized in configuration */
42 protected $invalidDomain = false;
43
44 /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
45 protected $coalescedQueues;
46
47 const TYPE_DEFAULT = 1; // integer; jobs popped by default
48 const TYPE_ANY = 2; // integer; any job
49
50 const USE_CACHE = 1; // integer; use process or persistent cache
51
52 const PROC_CACHE_TTL = 15; // integer; seconds
53
54 const CACHE_VERSION = 1; // integer; cache version
55
56 /**
57 * @param string $domain Wiki domain ID
58 * @param string|bool $readOnlyReason Read-only reason or false
59 */
60 protected function __construct( $domain, $readOnlyReason ) {
61 $this->domain = $domain;
62 $this->readOnlyReason = $readOnlyReason;
63 $this->cache = new MapCacheLRU( 10 );
64 }
65
66 /**
67 * @param bool|string $domain Wiki domain ID
68 * @return JobQueueGroup
69 */
70 public static function singleton( $domain = false ) {
71 global $wgLocalDatabases;
72
73 if ( $domain === false ) {
74 $domain = WikiMap::getCurrentWikiDbDomain()->getId();
75 }
76
77 if ( !isset( self::$instances[$domain] ) ) {
78 self::$instances[$domain] = new self( $domain, wfConfiguredReadOnlyReason() );
79 // Make sure jobs are not getting pushed to bogus wikis. This can confuse
80 // the job runner system into spawning endless RPC requests that fail (T171371).
81 $wikiId = WikiMap::getWikiIdFromDbDomain( $domain );
82 if (
83 !WikiMap::isCurrentWikiDbDomain( $domain ) &&
84 !in_array( $wikiId, $wgLocalDatabases )
85 ) {
86 self::$instances[$domain]->invalidDomain = true;
87 }
88 }
89
90 return self::$instances[$domain];
91 }
92
93 /**
94 * Destroy the singleton instances
95 *
96 * @return void
97 */
98 public static function destroySingletons() {
99 self::$instances = [];
100 }
101
102 /**
103 * Get the job queue object for a given queue type
104 *
105 * @param string $type
106 * @return JobQueue
107 */
108 public function get( $type ) {
109 global $wgJobTypeConf;
110
111 $conf = [ 'domain' => $this->domain, 'type' => $type ];
112 if ( isset( $wgJobTypeConf[$type] ) ) {
113 $conf = $conf + $wgJobTypeConf[$type];
114 } else {
115 $conf = $conf + $wgJobTypeConf['default'];
116 }
117 if ( !isset( $conf['readOnlyReason'] ) ) {
118 $conf['readOnlyReason'] = $this->readOnlyReason;
119 }
120
121 $services = MediaWikiServices::getInstance();
122 $conf['stats'] = $services->getStatsdDataFactory();
123 $conf['wanCache'] = $services->getMainWANObjectCache();
124 $conf['stash'] = $services->getMainObjectStash();
125
126 return JobQueue::factory( $conf );
127 }
128
129 /**
130 * Insert jobs into the respective queues of which they belong
131 *
132 * This inserts the jobs into the queue specified by $wgJobTypeConf
133 * and updates the aggregate job queue information cache as needed.
134 *
135 * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
136 * @throws InvalidArgumentException
137 * @return void
138 */
139 public function push( $jobs ) {
140 global $wgJobTypesExcludedFromDefaultQueue;
141
142 if ( $this->invalidDomain ) {
143 // Do not enqueue job that cannot be run (T171371)
144 $e = new LogicException( "Domain '{$this->domain}' is not recognized." );
145 MWExceptionHandler::logException( $e );
146 return;
147 }
148
149 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
150 if ( $jobs === [] ) {
151 return;
152 }
153
154 $this->assertValidJobs( $jobs );
155
156 $jobsByType = []; // (job type => list of jobs)
157 foreach ( $jobs as $job ) {
158 $jobsByType[$job->getType()][] = $job;
159 }
160
161 foreach ( $jobsByType as $type => $jobs ) {
162 $this->get( $type )->push( $jobs );
163 }
164
165 if ( $this->cache->hasField( 'queues-ready', 'list' ) ) {
166 $list = $this->cache->getField( 'queues-ready', 'list' );
167 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
168 $this->cache->clear( 'queues-ready' );
169 }
170 }
171
172 $cache = ObjectCache::getLocalClusterInstance();
173 $cache->set(
174 $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_ANY ),
175 'true',
176 15
177 );
178 if ( array_diff( array_keys( $jobsByType ), $wgJobTypesExcludedFromDefaultQueue ) ) {
179 $cache->set(
180 $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_DEFAULT ),
181 'true',
182 15
183 );
184 }
185 }
186
187 /**
188 * Buffer jobs for insertion via push() or call it now if in CLI mode
189 *
190 * Note that pushLazyJobs() is registered as a deferred update just before
191 * DeferredUpdates::doUpdates() in MediaWiki and JobRunner classes in order
192 * to be executed as the very last deferred update (T100085, T154425).
193 *
194 * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
195 * @return void
196 * @since 1.26
197 */
198 public function lazyPush( $jobs ) {
199 if ( $this->invalidDomain ) {
200 // Do not enqueue job that cannot be run (T171371)
201 throw new LogicException( "Domain '{$this->domain}' is not recognized." );
202 }
203
204 if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
205 $this->push( $jobs );
206 return;
207 }
208
209 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
210
211 // Throw errors now instead of on push(), when other jobs may be buffered
212 $this->assertValidJobs( $jobs );
213
214 DeferredUpdates::addUpdate( new JobQueueEnqueueUpdate( $this->domain, $jobs ) );
215 }
216
217 /**
218 * Push all jobs buffered via lazyPush() into their respective queues
219 *
220 * @return void
221 * @since 1.26
222 * @deprecated Since 1.33 Not needed anymore
223 */
224 public static function pushLazyJobs() {
225 wfDeprecated( __METHOD__, '1.33' );
226 }
227
228 /**
229 * Pop a job off one of the job queues
230 *
231 * This pops a job off a queue as specified by $wgJobTypeConf and
232 * updates the aggregate job queue information cache as needed.
233 *
234 * @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string
235 * @param int $flags Bitfield of JobQueueGroup::USE_* constants
236 * @param array $blacklist List of job types to ignore
237 * @return Job|bool Returns false on failure
238 */
239 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = [] ) {
240 global $wgJobClasses;
241
242 $job = false;
243
244 if ( !WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
245 throw new JobQueueError(
246 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
247 } elseif ( is_string( $qtype ) && !isset( $wgJobClasses[$qtype] ) ) {
248 // Do not pop jobs if there is no class for the queue type
249 throw new JobQueueError( "Unrecognized job type '$qtype'." );
250 }
251
252 if ( is_string( $qtype ) ) { // specific job type
253 if ( !in_array( $qtype, $blacklist ) ) {
254 $job = $this->get( $qtype )->pop();
255 }
256 } else { // any job in the "default" jobs types
257 if ( $flags & self::USE_CACHE ) {
258 if ( !$this->cache->hasField( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
259 $this->cache->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() );
260 }
261 $types = $this->cache->getField( 'queues-ready', 'list' );
262 } else {
263 $types = $this->getQueuesWithJobs();
264 }
265
266 if ( $qtype == self::TYPE_DEFAULT ) {
267 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
268 }
269
270 $types = array_diff( $types, $blacklist ); // avoid selected types
271 shuffle( $types ); // avoid starvation
272
273 foreach ( $types as $type ) { // for each queue...
274 $job = $this->get( $type )->pop();
275 if ( $job ) { // found
276 break;
277 } else { // not found
278 $this->cache->clear( 'queues-ready' );
279 }
280 }
281 }
282
283 return $job;
284 }
285
286 /**
287 * Acknowledge that a job was completed
288 *
289 * @param Job $job
290 * @return void
291 */
292 public function ack( Job $job ) {
293 $this->get( $job->getType() )->ack( $job );
294 }
295
296 /**
297 * Register the "root job" of a given job into the queue for de-duplication.
298 * This should only be called right *after* all the new jobs have been inserted.
299 *
300 * @param Job $job
301 * @return bool
302 */
303 public function deduplicateRootJob( Job $job ) {
304 return $this->get( $job->getType() )->deduplicateRootJob( $job );
305 }
306
307 /**
308 * Wait for any replica DBs or backup queue servers to catch up.
309 *
310 * This does nothing for certain queue classes.
311 *
312 * @return void
313 */
314 public function waitForBackups() {
315 global $wgJobTypeConf;
316
317 // Try to avoid doing this more than once per queue storage medium
318 foreach ( $wgJobTypeConf as $type => $conf ) {
319 $this->get( $type )->waitForBackups();
320 }
321 }
322
323 /**
324 * Get the list of queue types
325 *
326 * @return array List of strings
327 */
328 public function getQueueTypes() {
329 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
330 }
331
332 /**
333 * Get the list of default queue types
334 *
335 * @return array List of strings
336 */
337 public function getDefaultQueueTypes() {
338 global $wgJobTypesExcludedFromDefaultQueue;
339
340 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
341 }
342
343 /**
344 * Check if there are any queues with jobs (this is cached)
345 *
346 * @param int $type JobQueueGroup::TYPE_* constant
347 * @return bool
348 * @since 1.23
349 */
350 public function queuesHaveJobs( $type = self::TYPE_ANY ) {
351 $cache = ObjectCache::getLocalClusterInstance();
352 $key = $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', $type );
353
354 $value = $cache->get( $key );
355 if ( $value === false ) {
356 $queues = $this->getQueuesWithJobs();
357 if ( $type == self::TYPE_DEFAULT ) {
358 $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
359 }
360 $value = count( $queues ) ? 'true' : 'false';
361 $cache->add( $key, $value, 15 );
362 }
363
364 return ( $value === 'true' );
365 }
366
367 /**
368 * Get the list of job types that have non-empty queues
369 *
370 * @return string[] List of job types that have non-empty queues
371 */
372 public function getQueuesWithJobs() {
373 $types = [];
374 foreach ( $this->getCoalescedQueues() as $info ) {
375 /** @var JobQueue $queue */
376 $queue = $info['queue'];
377 $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->getQueueTypes() );
378 if ( is_array( $nonEmpty ) ) { // batching features supported
379 $types = array_merge( $types, $nonEmpty );
380 } else { // we have to go through the queues in the bucket one-by-one
381 foreach ( $info['types'] as $type ) {
382 if ( !$this->get( $type )->isEmpty() ) {
383 $types[] = $type;
384 }
385 }
386 }
387 }
388
389 return $types;
390 }
391
392 /**
393 * Get the size of the queus for a list of job types
394 *
395 * @return int[] Map of (job type => size)
396 */
397 public function getQueueSizes() {
398 $sizeMap = [];
399 foreach ( $this->getCoalescedQueues() as $info ) {
400 /** @var JobQueue $queue */
401 $queue = $info['queue'];
402 $sizes = $queue->getSiblingQueueSizes( $this->getQueueTypes() );
403 if ( is_array( $sizes ) ) { // batching features supported
404 $sizeMap = $sizeMap + $sizes;
405 } else { // we have to go through the queues in the bucket one-by-one
406 foreach ( $info['types'] as $type ) {
407 $sizeMap[$type] = $this->get( $type )->getSize();
408 }
409 }
410 }
411
412 return $sizeMap;
413 }
414
415 /**
416 * @return JobQueue[]
417 */
418 protected function getCoalescedQueues() {
419 global $wgJobTypeConf;
420
421 if ( $this->coalescedQueues === null ) {
422 $this->coalescedQueues = [];
423 foreach ( $wgJobTypeConf as $type => $conf ) {
424 $queue = JobQueue::factory(
425 [ 'domain' => $this->domain, 'type' => 'null' ] + $conf );
426 $loc = $queue->getCoalesceLocationInternal();
427 if ( !isset( $this->coalescedQueues[$loc] ) ) {
428 $this->coalescedQueues[$loc]['queue'] = $queue;
429 $this->coalescedQueues[$loc]['types'] = [];
430 }
431 if ( $type === 'default' ) {
432 $this->coalescedQueues[$loc]['types'] = array_merge(
433 $this->coalescedQueues[$loc]['types'],
434 array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
435 );
436 } else {
437 $this->coalescedQueues[$loc]['types'][] = $type;
438 }
439 }
440 }
441
442 return $this->coalescedQueues;
443 }
444
445 /**
446 * @param string $name
447 * @return mixed
448 */
449 private function getCachedConfigVar( $name ) {
450 // @TODO: cleanup this whole method with a proper config system
451 if ( WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
452 return $GLOBALS[$name]; // common case
453 } else {
454 $wiki = WikiMap::getWikiIdFromDbDomain( $this->domain );
455 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
456 $value = $cache->getWithSetCallback(
457 $cache->makeGlobalKey( 'jobqueue', 'configvalue', $this->domain, $name ),
458 $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
459 function () use ( $wiki, $name ) {
460 global $wgConf;
461 // @TODO: use the full domain ID here
462 return [ 'v' => $wgConf->getConfig( $wiki, $name ) ];
463 },
464 [ 'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
465 );
466
467 return $value['v'];
468 }
469 }
470
471 /**
472 * @param array $jobs
473 * @throws InvalidArgumentException
474 */
475 private function assertValidJobs( array $jobs ) {
476 foreach ( $jobs as $job ) { // sanity checks
477 if ( !( $job instanceof IJobSpecification ) ) {
478 throw new InvalidArgumentException( "Expected IJobSpecification objects" );
479 }
480 }
481 }
482 }