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