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