Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 141 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
JobQueueGroup | |
0.00% |
0 / 141 |
|
0.00% |
0 / 15 |
3306 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
get | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
push | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
132 | |||
lazyPush | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
pop | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
132 | |||
ack | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
deduplicateRootJob | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
waitForBackups | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getQueueTypes | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getDefaultQueueTypes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
queuesHaveJobs | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
getQueuesWithJobs | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
getQueueSizes | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
getCoalescedQueues | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
assertValidJobs | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | use MediaWiki\Deferred\DeferredUpdates; |
22 | use MediaWiki\Deferred\JobQueueEnqueueUpdate; |
23 | use MediaWiki\MediaWikiServices; |
24 | use Wikimedia\Rdbms\ReadOnlyMode; |
25 | use Wikimedia\UUID\GlobalIdGenerator; |
26 | |
27 | /** |
28 | * Handle enqueueing of background jobs. |
29 | * |
30 | * @warning This service class supports queuing jobs to foreign wikis via JobQueueGroupFactory, |
31 | * but other operations may be called for the local wiki only. Exceptions may be thrown if you |
32 | * attempt to inspect, pop, or execute a foreign wiki's job queue. |
33 | * |
34 | * @since 1.21 |
35 | * @ingroup JobQueue |
36 | */ |
37 | class JobQueueGroup { |
38 | /** @var MapCacheLRU */ |
39 | protected $cache; |
40 | |
41 | /** @var string Wiki domain ID */ |
42 | protected $domain; |
43 | /** @var ReadOnlyMode Read only mode */ |
44 | protected $readOnlyMode; |
45 | /** @var array|null */ |
46 | private $localJobClasses; |
47 | /** @var array */ |
48 | private $jobTypeConfiguration; |
49 | /** @var array */ |
50 | private $jobTypesExcludedFromDefaultQueue; |
51 | /** @var IBufferingStatsdDataFactory */ |
52 | private $statsdDataFactory; |
53 | /** @var WANObjectCache */ |
54 | private $wanCache; |
55 | /** @var GlobalIdGenerator */ |
56 | private $globalIdGenerator; |
57 | |
58 | /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */ |
59 | protected $coalescedQueues; |
60 | |
61 | public const TYPE_DEFAULT = 1; // integer; jobs popped by default |
62 | private const TYPE_ANY = 2; // integer; any job |
63 | |
64 | public const USE_CACHE = 1; // integer; use process or persistent cache |
65 | |
66 | private const PROC_CACHE_TTL = 15; // integer; seconds |
67 | |
68 | /** |
69 | * @internal Use MediaWikiServices::getJobQueueGroupFactory |
70 | * |
71 | * @param string $domain Wiki domain ID |
72 | * @param ReadOnlyMode $readOnlyMode Read-only mode |
73 | * @param array|null $localJobClasses |
74 | * @param array $jobTypeConfiguration |
75 | * @param array $jobTypesExcludedFromDefaultQueue |
76 | * @param IBufferingStatsdDataFactory $statsdDataFactory |
77 | * @param WANObjectCache $wanCache |
78 | * @param GlobalIdGenerator $globalIdGenerator |
79 | * |
80 | */ |
81 | public function __construct( |
82 | $domain, |
83 | ReadOnlyMode $readOnlyMode, |
84 | ?array $localJobClasses, |
85 | array $jobTypeConfiguration, |
86 | array $jobTypesExcludedFromDefaultQueue, |
87 | IBufferingStatsdDataFactory $statsdDataFactory, |
88 | WANObjectCache $wanCache, |
89 | GlobalIdGenerator $globalIdGenerator |
90 | ) { |
91 | $this->domain = $domain; |
92 | $this->readOnlyMode = $readOnlyMode; |
93 | $this->cache = new MapCacheLRU( 10 ); |
94 | $this->localJobClasses = $localJobClasses; |
95 | $this->jobTypeConfiguration = $jobTypeConfiguration; |
96 | $this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue; |
97 | $this->statsdDataFactory = $statsdDataFactory; |
98 | $this->wanCache = $wanCache; |
99 | $this->globalIdGenerator = $globalIdGenerator; |
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 | $conf = [ 'domain' => $this->domain, 'type' => $type ]; |
110 | $conf += $this->jobTypeConfiguration[$type] ?? $this->jobTypeConfiguration['default']; |
111 | if ( !isset( $conf['readOnlyReason'] ) ) { |
112 | $conf['readOnlyReason'] = $this->readOnlyMode->getConfiguredReason(); |
113 | } |
114 | |
115 | $conf['stats'] = $this->statsdDataFactory; |
116 | $conf['wanCache'] = $this->wanCache; |
117 | $conf['idGenerator'] = $this->globalIdGenerator; |
118 | |
119 | return JobQueue::factory( $conf ); |
120 | } |
121 | |
122 | /** |
123 | * Insert jobs into the respective queues of which they belong |
124 | * |
125 | * This inserts the jobs into the queue specified by $wgJobTypeConf |
126 | * and updates the aggregate job queue information cache as needed. |
127 | * |
128 | * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs |
129 | * @return void |
130 | */ |
131 | public function push( $jobs ) { |
132 | $jobs = is_array( $jobs ) ? $jobs : [ $jobs ]; |
133 | if ( $jobs === [] ) { |
134 | return; |
135 | } |
136 | |
137 | $this->assertValidJobs( $jobs ); |
138 | |
139 | $jobsByType = []; // (job type => list of jobs) |
140 | foreach ( $jobs as $job ) { |
141 | $type = $job->getType(); |
142 | if ( isset( $this->jobTypeConfiguration[$type] ) ) { |
143 | $jobsByType[$type][] = $job; |
144 | } else { |
145 | if ( |
146 | isset( $this->jobTypeConfiguration['default']['typeAgnostic'] ) && |
147 | $this->jobTypeConfiguration['default']['typeAgnostic'] |
148 | ) { |
149 | $jobsByType['default'][] = $job; |
150 | } else { |
151 | $jobsByType[$type][] = $job; |
152 | } |
153 | } |
154 | } |
155 | |
156 | foreach ( $jobsByType as $type => $jobs ) { |
157 | $this->get( $type )->push( $jobs ); |
158 | } |
159 | |
160 | if ( $this->cache->hasField( 'queues-ready', 'list' ) ) { |
161 | $list = $this->cache->getField( 'queues-ready', 'list' ); |
162 | if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) { |
163 | $this->cache->clear( 'queues-ready' ); |
164 | } |
165 | } |
166 | |
167 | $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance(); |
168 | $cache->set( |
169 | $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_ANY ), |
170 | 'true', |
171 | 15 |
172 | ); |
173 | if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) { |
174 | $cache->set( |
175 | $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_DEFAULT ), |
176 | 'true', |
177 | 15 |
178 | ); |
179 | } |
180 | } |
181 | |
182 | /** |
183 | * Buffer jobs for insertion via push() or call it now if in CLI mode |
184 | * |
185 | * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs |
186 | * @return void |
187 | * @since 1.26 |
188 | */ |
189 | public function lazyPush( $jobs ) { |
190 | if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) { |
191 | $this->push( $jobs ); |
192 | return; |
193 | } |
194 | |
195 | $jobs = is_array( $jobs ) ? $jobs : [ $jobs ]; |
196 | |
197 | // Throw errors now instead of on push(), when other jobs may be buffered |
198 | $this->assertValidJobs( $jobs ); |
199 | |
200 | DeferredUpdates::addUpdate( new JobQueueEnqueueUpdate( $this->domain, $jobs ) ); |
201 | } |
202 | |
203 | /** |
204 | * Pop one job off a job queue |
205 | * |
206 | * @warning May not be called on foreign wikis! |
207 | * |
208 | * This pops a job off a queue as specified by $wgJobTypeConf and |
209 | * updates the aggregate job queue information cache as needed. |
210 | * |
211 | * @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string |
212 | * @param int $flags Bitfield of JobQueueGroup::USE_* constants |
213 | * @param array $ignored List of job types to ignore |
214 | * @return RunnableJob|false Returns false on failure |
215 | * @throws JobQueueError |
216 | */ |
217 | public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) { |
218 | $job = false; |
219 | |
220 | if ( !$this->localJobClasses ) { |
221 | throw new JobQueueError( |
222 | "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." ); |
223 | } |
224 | if ( is_string( $qtype ) && !isset( $this->localJobClasses[$qtype] ) ) { |
225 | // Do not pop jobs if there is no class for the queue type |
226 | throw new JobQueueError( "Unrecognized job type '$qtype'." ); |
227 | } |
228 | |
229 | if ( is_string( $qtype ) ) { // specific job type |
230 | if ( !in_array( $qtype, $ignored ) ) { |
231 | $job = $this->get( $qtype )->pop(); |
232 | } |
233 | } else { // any job in the "default" jobs types |
234 | if ( $flags & self::USE_CACHE ) { |
235 | if ( !$this->cache->hasField( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) { |
236 | $this->cache->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() ); |
237 | } |
238 | $types = $this->cache->getField( 'queues-ready', 'list' ); |
239 | } else { |
240 | $types = $this->getQueuesWithJobs(); |
241 | } |
242 | |
243 | if ( $qtype == self::TYPE_DEFAULT ) { |
244 | $types = array_intersect( $types, $this->getDefaultQueueTypes() ); |
245 | } |
246 | |
247 | $types = array_diff( $types, $ignored ); // avoid selected types |
248 | shuffle( $types ); // avoid starvation |
249 | |
250 | foreach ( $types as $type ) { // for each queue... |
251 | $job = $this->get( $type )->pop(); |
252 | if ( $job ) { // found |
253 | break; |
254 | } else { // not found |
255 | $this->cache->clear( 'queues-ready' ); |
256 | } |
257 | } |
258 | } |
259 | |
260 | return $job; |
261 | } |
262 | |
263 | /** |
264 | * Acknowledge that a job was completed |
265 | * |
266 | * @param RunnableJob $job |
267 | * @return void |
268 | */ |
269 | public function ack( RunnableJob $job ) { |
270 | $this->get( $job->getType() )->ack( $job ); |
271 | } |
272 | |
273 | /** |
274 | * Register the "root job" of a given job into the queue for de-duplication. |
275 | * This should only be called right *after* all the new jobs have been inserted. |
276 | * |
277 | * @deprecated since 1.40 |
278 | * @param RunnableJob $job |
279 | * @return bool |
280 | */ |
281 | public function deduplicateRootJob( RunnableJob $job ) { |
282 | wfDeprecated( __METHOD__, '1.40' ); |
283 | return true; |
284 | } |
285 | |
286 | /** |
287 | * Wait for any replica DBs or backup queue servers to catch up. |
288 | * |
289 | * This does nothing for certain queue classes. |
290 | * |
291 | * @deprecated since 1.41, use JobQueue::waitForBackups() instead. |
292 | * |
293 | * @return void |
294 | */ |
295 | public function waitForBackups() { |
296 | wfDeprecated( __METHOD__, '1.41' ); |
297 | // Try to avoid doing this more than once per queue storage medium |
298 | foreach ( $this->jobTypeConfiguration as $type => $conf ) { |
299 | $this->get( $type )->waitForBackups(); |
300 | } |
301 | } |
302 | |
303 | /** |
304 | * Get the list of queue types |
305 | * |
306 | * @warning May not be called on foreign wikis! |
307 | * |
308 | * @return string[] |
309 | */ |
310 | public function getQueueTypes() { |
311 | if ( !$this->localJobClasses ) { |
312 | throw new JobQueueError( 'Cannot inspect job queue from foreign wiki' ); |
313 | } |
314 | return array_keys( $this->localJobClasses ); |
315 | } |
316 | |
317 | /** |
318 | * Get the list of default queue types |
319 | * |
320 | * @warning May not be called on foreign wikis! |
321 | * |
322 | * @return string[] |
323 | */ |
324 | public function getDefaultQueueTypes() { |
325 | return array_diff( $this->getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue ); |
326 | } |
327 | |
328 | /** |
329 | * Check if there are any queues with jobs (this is cached) |
330 | * |
331 | * @warning May not be called on foreign wikis! |
332 | * |
333 | * @since 1.23 |
334 | * @param int $type JobQueueGroup::TYPE_* constant |
335 | * @return bool |
336 | */ |
337 | public function queuesHaveJobs( $type = self::TYPE_ANY ) { |
338 | $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance(); |
339 | $key = $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', $type ); |
340 | |
341 | $value = $cache->get( $key ); |
342 | if ( $value === false ) { |
343 | $queues = $this->getQueuesWithJobs(); |
344 | if ( $type == self::TYPE_DEFAULT ) { |
345 | $queues = array_intersect( $queues, $this->getDefaultQueueTypes() ); |
346 | } |
347 | $value = count( $queues ) ? 'true' : 'false'; |
348 | $cache->add( $key, $value, 15 ); |
349 | } |
350 | |
351 | return ( $value === 'true' ); |
352 | } |
353 | |
354 | /** |
355 | * Get the list of job types that have non-empty queues |
356 | * |
357 | * @warning May not be called on foreign wikis! |
358 | * |
359 | * @return string[] List of job types that have non-empty queues |
360 | */ |
361 | public function getQueuesWithJobs() { |
362 | $types = []; |
363 | foreach ( $this->getCoalescedQueues() as $info ) { |
364 | /** @var JobQueue $queue */ |
365 | $queue = $info['queue']; |
366 | $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->getQueueTypes() ); |
367 | if ( is_array( $nonEmpty ) ) { // batching features supported |
368 | $types = array_merge( $types, $nonEmpty ); |
369 | } else { // we have to go through the queues in the bucket one-by-one |
370 | foreach ( $info['types'] as $type ) { |
371 | if ( !$this->get( $type )->isEmpty() ) { |
372 | $types[] = $type; |
373 | } |
374 | } |
375 | } |
376 | } |
377 | |
378 | return $types; |
379 | } |
380 | |
381 | /** |
382 | * Get the size of the queues for a list of job types |
383 | * |
384 | * @warning May not be called on foreign wikis! |
385 | * |
386 | * @return int[] Map of (job type => size) |
387 | */ |
388 | public function getQueueSizes() { |
389 | $sizeMap = []; |
390 | foreach ( $this->getCoalescedQueues() as $info ) { |
391 | /** @var JobQueue $queue */ |
392 | $queue = $info['queue']; |
393 | $sizes = $queue->getSiblingQueueSizes( $this->getQueueTypes() ); |
394 | if ( is_array( $sizes ) ) { // batching features supported |
395 | $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 | * @phan-return array<string,array{queue:JobQueue,types:array<string,class-string>}> |
409 | */ |
410 | protected function getCoalescedQueues() { |
411 | if ( $this->coalescedQueues === null ) { |
412 | $this->coalescedQueues = []; |
413 | foreach ( $this->jobTypeConfiguration as $type => $conf ) { |
414 | $conf['domain'] = $this->domain; |
415 | $conf['type'] = 'null'; |
416 | $conf['stats'] = $this->statsdDataFactory; |
417 | $conf['wanCache'] = $this->wanCache; |
418 | $conf['idGenerator'] = $this->globalIdGenerator; |
419 | |
420 | $queue = JobQueue::factory( $conf ); |
421 | $loc = $queue->getCoalesceLocationInternal(); |
422 | if ( !isset( $this->coalescedQueues[$loc] ) ) { |
423 | $this->coalescedQueues[$loc]['queue'] = $queue; |
424 | $this->coalescedQueues[$loc]['types'] = []; |
425 | } |
426 | if ( $type === 'default' ) { |
427 | $this->coalescedQueues[$loc]['types'] = array_merge( |
428 | $this->coalescedQueues[$loc]['types'], |
429 | array_diff( $this->getQueueTypes(), array_keys( $this->jobTypeConfiguration ) ) |
430 | ); |
431 | } else { |
432 | $this->coalescedQueues[$loc]['types'][] = $type; |
433 | } |
434 | } |
435 | } |
436 | |
437 | return $this->coalescedQueues; |
438 | } |
439 | |
440 | /** |
441 | * @param array $jobs |
442 | * @throws InvalidArgumentException |
443 | */ |
444 | private function assertValidJobs( array $jobs ) { |
445 | foreach ( $jobs as $job ) { |
446 | if ( !( $job instanceof IJobSpecification ) ) { |
447 | $type = get_debug_type( $job ); |
448 | throw new InvalidArgumentException( "Expected IJobSpecification objects, got " . $type ); |
449 | } |
450 | } |
451 | } |
452 | } |