MediaWiki master
JobQueueFederated.php
Go to the documentation of this file.
1<?php
48 protected $partitionRing;
50 protected $partitionQueues = [];
51
54
71 protected function __construct( array $params ) {
72 parent::__construct( $params );
73 $section = $params['sectionsByWiki'][$this->domain] ?? 'default';
74 if ( !isset( $params['partitionsBySection'][$section] ) ) {
75 throw new InvalidArgumentException( "No configuration for section '$section'." );
76 }
77 $this->maxPartitionsTry = $params['maxPartitionsTry'] ?? 2;
78 // Get the full partition map
79 $partitionMap = $params['partitionsBySection'][$section];
80 arsort( $partitionMap, SORT_NUMERIC );
81 // Get the config to pass to merge into each partition queue config
82 $baseConfig = $params;
83 foreach ( [ 'class', 'sectionsByWiki', 'maxPartitionsTry',
84 'partitionsBySection', 'configByPartition', ] as $o
85 ) {
86 unset( $baseConfig[$o] ); // partition queue doesn't care about this
87 }
88 // Get the partition queue objects
89 foreach ( $partitionMap as $partition => $w ) {
90 if ( !isset( $params['configByPartition'][$partition] ) ) {
91 throw new InvalidArgumentException( "No configuration for partition '$partition'." );
92 }
93 $this->partitionQueues[$partition] = JobQueue::factory(
94 $baseConfig + $params['configByPartition'][$partition] );
95 }
96 // Ring of all partitions
97 $this->partitionRing = new HashRing( $partitionMap );
98 }
99
100 protected function supportedOrders() {
101 // No FIFO due to partitioning, though "rough timestamp order" is supported
102 return [ 'undefined', 'random', 'timestamp' ];
103 }
104
105 protected function optimalOrder() {
106 return 'undefined'; // defer to the partitions
107 }
108
109 protected function supportsDelayedJobs() {
110 foreach ( $this->partitionQueues as $queue ) {
111 if ( !$queue->supportsDelayedJobs() ) {
112 return false;
113 }
114 }
115
116 return true;
117 }
118
119 protected function doIsEmpty() {
120 $empty = true;
121 $failed = 0;
122 foreach ( $this->partitionQueues as $queue ) {
123 try {
124 $empty = $empty && $queue->doIsEmpty();
125 } catch ( JobQueueError $e ) {
126 ++$failed;
127 $this->logException( $e );
128 }
129 }
130 $this->throwErrorIfAllPartitionsDown( $failed );
131
132 return $empty;
133 }
134
135 protected function doGetSize() {
136 return $this->getCrossPartitionSum( 'size', 'doGetSize' );
137 }
138
139 protected function doGetAcquiredCount() {
140 return $this->getCrossPartitionSum( 'acquiredcount', 'doGetAcquiredCount' );
141 }
142
143 protected function doGetDelayedCount() {
144 return $this->getCrossPartitionSum( 'delayedcount', 'doGetDelayedCount' );
145 }
146
147 protected function doGetAbandonedCount() {
148 return $this->getCrossPartitionSum( 'abandonedcount', 'doGetAbandonedCount' );
149 }
150
156 protected function getCrossPartitionSum( $type, $method ) {
157 $count = 0;
158 $failed = 0;
159 foreach ( $this->partitionQueues as $queue ) {
160 try {
161 $count += $queue->$method();
162 } catch ( JobQueueError $e ) {
163 ++$failed;
164 $this->logException( $e );
165 }
166 }
167 $this->throwErrorIfAllPartitionsDown( $failed );
168
169 return $count;
170 }
171
172 protected function doBatchPush( array $jobs, $flags ) {
173 // Local ring variable that may be changed to point to a new ring on failure
175 // Try to insert the jobs and update $partitionsTry on any failures.
176 // Retry to insert any remaining jobs again, ignoring the bad partitions.
177 $jobsLeft = $jobs;
178 for ( $i = $this->maxPartitionsTry; $i > 0 && count( $jobsLeft ); --$i ) {
179 try {
181 } catch ( UnexpectedValueException $e ) {
182 break; // all servers down; nothing to insert to
183 }
184 $jobsLeft = $this->tryJobInsertions( $jobsLeft, $partitionRing, $flags );
185 }
186 if ( count( $jobsLeft ) ) {
187 throw new JobQueueError(
188 "Could not insert job(s), {$this->maxPartitionsTry} partitions tried." );
189 }
190 }
191
199 protected function tryJobInsertions( array $jobs, HashRing &$partitionRing, $flags ) {
200 $jobsLeft = [];
201
202 // Because jobs are spread across partitions, per-job de-duplication needs
203 // to use a consistent hash to avoid allowing duplicate jobs per partition.
204 // When inserting a batch of de-duplicated jobs, QOS_ATOMIC is disregarded.
205 $uJobsByPartition = []; // (partition name => job list)
207 foreach ( $jobs as $key => $job ) {
208 if ( $job->ignoreDuplicates() ) {
209 $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
210 $uJobsByPartition[$partitionRing->getLiveLocation( $sha1 )][] = $job;
211 unset( $jobs[$key] );
212 }
213 }
214 // Get the batches of jobs that are not de-duplicated
215 if ( $flags & self::QOS_ATOMIC ) {
216 $nuJobBatches = [ $jobs ]; // all or nothing
217 } else {
218 // Split the jobs into batches and spread them out over servers if there
219 // are many jobs. This helps keep the partitions even. Otherwise, send all
220 // the jobs to a single partition queue to avoids the extra connections.
221 $nuJobBatches = array_chunk( $jobs, 300 );
222 }
223
224 // Insert the de-duplicated jobs into the queues...
225 foreach ( $uJobsByPartition as $partition => $jobBatch ) {
227 $queue = $this->partitionQueues[$partition];
228 try {
229 $ok = true;
230 $queue->doBatchPush( $jobBatch, $flags | self::QOS_ATOMIC );
231 } catch ( JobQueueError $e ) {
232 $ok = false;
233 $this->logException( $e );
234 }
235 if ( !$ok ) {
236 if ( !$partitionRing->ejectFromLiveRing( $partition, 5 ) ) {
237 throw new JobQueueError( "Could not insert job(s), no partitions available." );
238 }
239 $jobsLeft = array_merge( $jobsLeft, $jobBatch ); // not inserted
240 }
241 }
242
243 // Insert the jobs that are not de-duplicated into the queues...
244 foreach ( $nuJobBatches as $jobBatch ) {
245 $partition = ArrayUtils::pickRandom( $partitionRing->getLiveLocationWeights() );
246 $queue = $this->partitionQueues[$partition];
247 try {
248 $ok = true;
249 $queue->doBatchPush( $jobBatch, $flags | self::QOS_ATOMIC );
250 } catch ( JobQueueError $e ) {
251 $ok = false;
252 $this->logException( $e );
253 }
254 if ( !$ok ) {
255 if ( !$partitionRing->ejectFromLiveRing( $partition, 5 ) ) {
256 throw new JobQueueError( "Could not insert job(s), no partitions available." );
257 }
258 $jobsLeft = array_merge( $jobsLeft, $jobBatch ); // not inserted
259 }
260 }
261
262 return $jobsLeft;
263 }
264
265 protected function doPop() {
266 $partitionsTry = $this->partitionRing->getLiveLocationWeights(); // (partition => weight)
267
268 $failed = 0;
269 while ( count( $partitionsTry ) ) {
270 $partition = ArrayUtils::pickRandom( $partitionsTry );
271 if ( $partition === false ) {
272 break; // all partitions at 0 weight
273 }
274
276 $queue = $this->partitionQueues[$partition];
277 try {
278 $job = $queue->pop();
279 } catch ( JobQueueError $e ) {
280 ++$failed;
281 $this->logException( $e );
282 $job = false;
283 }
284 if ( $job ) {
285 $job->setMetadata( 'QueuePartition', $partition );
286
287 return $job;
288 } else {
289 unset( $partitionsTry[$partition] );
290 }
291 }
292 $this->throwErrorIfAllPartitionsDown( $failed );
293
294 return false;
295 }
296
297 protected function doAck( RunnableJob $job ) {
298 $partition = $job->getMetadata( 'QueuePartition' );
299 if ( $partition === null ) {
300 throw new UnexpectedValueException( "The given job has no defined partition name." );
301 }
302
303 $this->partitionQueues[$partition]->ack( $job );
304 }
305
307 $signature = $job->getRootJobParams()['rootJobSignature'];
308 $partition = $this->partitionRing->getLiveLocation( $signature );
309 try {
310 return $this->partitionQueues[$partition]->doIsRootJobOldDuplicate( $job );
311 } catch ( JobQueueError $e ) {
312 if ( $this->partitionRing->ejectFromLiveRing( $partition, 5 ) ) {
313 $partition = $this->partitionRing->getLiveLocation( $signature );
314 return $this->partitionQueues[$partition]->doIsRootJobOldDuplicate( $job );
315 }
316 }
317
318 return false;
319 }
320
322 $signature = $job->getRootJobParams()['rootJobSignature'];
323 $partition = $this->partitionRing->getLiveLocation( $signature );
324 try {
325 return $this->partitionQueues[$partition]->doDeduplicateRootJob( $job );
326 } catch ( JobQueueError $e ) {
327 if ( $this->partitionRing->ejectFromLiveRing( $partition, 5 ) ) {
328 $partition = $this->partitionRing->getLiveLocation( $signature );
329 return $this->partitionQueues[$partition]->doDeduplicateRootJob( $job );
330 }
331 }
332
333 return false;
334 }
335
336 protected function doDelete() {
337 $failed = 0;
339 foreach ( $this->partitionQueues as $queue ) {
340 try {
341 $queue->doDelete();
342 } catch ( JobQueueError $e ) {
343 ++$failed;
344 $this->logException( $e );
345 }
346 }
347 $this->throwErrorIfAllPartitionsDown( $failed );
348 return true;
349 }
350
351 protected function doWaitForBackups() {
352 $failed = 0;
354 foreach ( $this->partitionQueues as $queue ) {
355 try {
356 $queue->waitForBackups();
357 } catch ( JobQueueError $e ) {
358 ++$failed;
359 $this->logException( $e );
360 }
361 }
362 $this->throwErrorIfAllPartitionsDown( $failed );
363 }
364
365 protected function doFlushCaches() {
367 foreach ( $this->partitionQueues as $queue ) {
368 $queue->doFlushCaches();
369 }
370 }
371
372 public function getAllQueuedJobs() {
373 $iterator = new AppendIterator();
374
376 foreach ( $this->partitionQueues as $queue ) {
377 $iterator->append( $queue->getAllQueuedJobs() );
378 }
379
380 return $iterator;
381 }
382
383 public function getAllDelayedJobs() {
384 $iterator = new AppendIterator();
385
387 foreach ( $this->partitionQueues as $queue ) {
388 $iterator->append( $queue->getAllDelayedJobs() );
389 }
390
391 return $iterator;
392 }
393
394 public function getAllAcquiredJobs() {
395 $iterator = new AppendIterator();
396
398 foreach ( $this->partitionQueues as $queue ) {
399 $iterator->append( $queue->getAllAcquiredJobs() );
400 }
401
402 return $iterator;
403 }
404
405 public function getAllAbandonedJobs() {
406 $iterator = new AppendIterator();
407
409 foreach ( $this->partitionQueues as $queue ) {
410 $iterator->append( $queue->getAllAbandonedJobs() );
411 }
412
413 return $iterator;
414 }
415
416 public function getCoalesceLocationInternal() {
417 return "JobQueueFederated:wiki:{$this->domain}" .
418 sha1( serialize( array_keys( $this->partitionQueues ) ) );
419 }
420
421 protected function doGetSiblingQueuesWithJobs( array $types ) {
422 $result = [];
423
424 $failed = 0;
426 foreach ( $this->partitionQueues as $queue ) {
427 try {
428 $nonEmpty = $queue->doGetSiblingQueuesWithJobs( $types );
429 if ( is_array( $nonEmpty ) ) {
430 $result = array_unique( array_merge( $result, $nonEmpty ) );
431 } else {
432 return null; // not supported on all partitions; bail
433 }
434 if ( count( $result ) == count( $types ) ) {
435 break; // short-circuit
436 }
437 } catch ( JobQueueError $e ) {
438 ++$failed;
439 $this->logException( $e );
440 }
441 }
442 $this->throwErrorIfAllPartitionsDown( $failed );
443
444 return array_values( $result );
445 }
446
447 protected function doGetSiblingQueueSizes( array $types ) {
448 $result = [];
449 $failed = 0;
451 foreach ( $this->partitionQueues as $queue ) {
452 try {
453 $sizes = $queue->doGetSiblingQueueSizes( $types );
454 if ( is_array( $sizes ) ) {
455 foreach ( $sizes as $type => $size ) {
456 $result[$type] = ( $result[$type] ?? 0 ) + $size;
457 }
458 } else {
459 return null; // not supported on all partitions; bail
460 }
461 } catch ( JobQueueError $e ) {
462 ++$failed;
463 $this->logException( $e );
464 }
465 }
466 $this->throwErrorIfAllPartitionsDown( $failed );
467
468 return $result;
469 }
470
471 protected function logException( Exception $e ) {
472 wfDebugLog( 'JobQueue', $e->getMessage() . "\n" . $e->getTraceAsString() );
473 }
474
482 protected function throwErrorIfAllPartitionsDown( $down ) {
483 if ( $down >= count( $this->partitionQueues ) ) {
484 throw new JobQueueError( 'No queue partitions available.' );
485 }
486 }
487}
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
array $params
The job parameters.
Convenience class for weighted consistent hash rings.
Definition HashRing.php:44
getLiveLocationWeights()
Get the map of "live" locations to weight (does not include zero weight items)
Definition HashRing.php:268
getLiveLocation( $item)
Get the location of an item on the "live" ring.
Definition HashRing.php:246
ejectFromLiveRing( $location, $ttl)
Remove a location from the "live" hash ring.
Definition HashRing.php:226
Enqueue and run background jobs via a federated queue, for wiki farms.
getAllDelayedJobs()
Get an iterator to traverse over all delayed jobs in this queue.
doGetSiblingQueuesWithJobs(array $types)
tryJobInsertions(array $jobs, HashRing &$partitionRing, $flags)
doGetSiblingQueueSizes(array $types)
getAllAcquiredJobs()
Get an iterator to traverse over all claimed jobs in this queue.
doAck(RunnableJob $job)
doIsRootJobOldDuplicate(IJobSpecification $job)
optimalOrder()
Get the default queue order to use if configuration does not specify one.
doDeduplicateRootJob(IJobSpecification $job)
int $maxPartitionsTry
Maximum number of partitions to try.
doBatchPush(array $jobs, $flags)
supportsDelayedJobs()
Find out if delayed jobs are supported for configuration validation.
getCoalesceLocationInternal()
Do not use this function outside of JobQueue/JobQueueGroup.
JobQueue[] $partitionQueues
(partition name => JobQueue) reverse sorted by weight
throwErrorIfAllPartitionsDown( $down)
Throw an error if no partitions available.
getCrossPartitionSum( $type, $method)
getAllAbandonedJobs()
Get an iterator to traverse over all abandoned jobs in this queue.
logException(Exception $e)
supportedOrders()
Get the allowed queue orders for configuration validation.
__construct(array $params)
getAllQueuedJobs()
Get an iterator to traverse over all available jobs in this queue.
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:43
string $type
Job type.
Definition JobQueue.php:47
static factory(array $params)
Get a job queue object of the specified type.
Definition JobQueue.php:150
string $domain
DB domain ID.
Definition JobQueue.php:45
Describe and execute a background job.
Definition Job.php:40
Interface for serializable objects that describe a job queue task.
Job that has a run() method and metadata accessors for JobQueue::pop() and JobQueue::ack().
if(count( $args)< 1) $job