74 if ( !isset(
$params[
'partitionsBySection'][$section] ) ) {
75 throw new InvalidArgumentException(
"No configuration for section '$section'." );
77 $this->maxPartitionsTry =
$params[
'maxPartitionsTry'] ?? 2;
79 $partitionMap =
$params[
'partitionsBySection'][$section];
80 arsort( $partitionMap, SORT_NUMERIC );
83 foreach ( [
'class',
'sectionsByWiki',
'maxPartitionsTry',
84 'partitionsBySection',
'configByPartition', ] as $o
86 unset( $baseConfig[$o] );
89 foreach ( $partitionMap as $partition => $w ) {
90 if ( !isset(
$params[
'configByPartition'][$partition] ) ) {
91 throw new InvalidArgumentException(
"No configuration for partition '$partition'." );
94 $baseConfig +
$params[
'configByPartition'][$partition] );
97 $this->partitionRing =
new HashRing( $partitionMap );
102 return [
'undefined',
'random',
'timestamp' ];
110 foreach ( $this->partitionQueues as $queue ) {
111 if ( !$queue->supportsDelayedJobs() ) {
122 foreach ( $this->partitionQueues as $queue ) {
124 $empty = $empty && $queue->doIsEmpty();
159 foreach ( $this->partitionQueues as $queue ) {
161 $count += $queue->$method();
178 for ( $i = $this->maxPartitionsTry; $i > 0 && count( $jobsLeft ); --$i ) {
181 }
catch ( UnexpectedValueException $e ) {
186 if ( count( $jobsLeft ) ) {
188 "Could not insert job(s), {$this->maxPartitionsTry} partitions tried." );
205 $uJobsByPartition = [];
207 foreach ( $jobs as $key =>
$job ) {
208 if (
$job->ignoreDuplicates() ) {
209 $sha1 = sha1( serialize(
$job->getDeduplicationInfo() ) );
211 unset( $jobs[$key] );
215 if ( $flags & self::QOS_ATOMIC ) {
216 $nuJobBatches = [ $jobs ];
221 $nuJobBatches = array_chunk( $jobs, 300 );
225 foreach ( $uJobsByPartition as $partition => $jobBatch ) {
227 $queue = $this->partitionQueues[$partition];
230 $queue->doBatchPush( $jobBatch, $flags | self::QOS_ATOMIC );
237 throw new JobQueueError(
"Could not insert job(s), no partitions available." );
239 $jobsLeft = array_merge( $jobsLeft, $jobBatch );
244 foreach ( $nuJobBatches as $jobBatch ) {
246 $queue = $this->partitionQueues[$partition];
249 $queue->doBatchPush( $jobBatch, $flags | self::QOS_ATOMIC );
256 throw new JobQueueError(
"Could not insert job(s), no partitions available." );
258 $jobsLeft = array_merge( $jobsLeft, $jobBatch );
266 $partitionsTry = $this->partitionRing->getLiveLocationWeights();
269 while ( count( $partitionsTry ) ) {
270 $partition = ArrayUtils::pickRandom( $partitionsTry );
271 if ( $partition ===
false ) {
276 $queue = $this->partitionQueues[$partition];
278 $job = $queue->pop();
285 $job->setMetadata(
'QueuePartition', $partition );
289 unset( $partitionsTry[$partition] );
298 $partition =
$job->getMetadata(
'QueuePartition' );
299 if ( $partition ===
null ) {
300 throw new UnexpectedValueException(
"The given job has no defined partition name." );
303 $this->partitionQueues[$partition]->ack(
$job );
307 $signature =
$job->getRootJobParams()[
'rootJobSignature'];
308 $partition = $this->partitionRing->getLiveLocation( $signature );
310 return $this->partitionQueues[$partition]->doIsRootJobOldDuplicate(
$job );
312 if ( $this->partitionRing->ejectFromLiveRing( $partition, 5 ) ) {
313 $partition = $this->partitionRing->getLiveLocation( $signature );
314 return $this->partitionQueues[$partition]->doIsRootJobOldDuplicate(
$job );
322 $signature =
$job->getRootJobParams()[
'rootJobSignature'];
323 $partition = $this->partitionRing->getLiveLocation( $signature );
325 return $this->partitionQueues[$partition]->doDeduplicateRootJob(
$job );
327 if ( $this->partitionRing->ejectFromLiveRing( $partition, 5 ) ) {
328 $partition = $this->partitionRing->getLiveLocation( $signature );
329 return $this->partitionQueues[$partition]->doDeduplicateRootJob(
$job );
339 foreach ( $this->partitionQueues as $queue ) {
354 foreach ( $this->partitionQueues as $queue ) {
356 $queue->waitForBackups();
367 foreach ( $this->partitionQueues as $queue ) {
368 $queue->doFlushCaches();
373 $iterator =
new AppendIterator();
376 foreach ( $this->partitionQueues as $queue ) {
377 $iterator->append( $queue->getAllQueuedJobs() );
384 $iterator =
new AppendIterator();
387 foreach ( $this->partitionQueues as $queue ) {
388 $iterator->append( $queue->getAllDelayedJobs() );
395 $iterator =
new AppendIterator();
398 foreach ( $this->partitionQueues as $queue ) {
399 $iterator->append( $queue->getAllAcquiredJobs() );
406 $iterator =
new AppendIterator();
409 foreach ( $this->partitionQueues as $queue ) {
410 $iterator->append( $queue->getAllAbandonedJobs() );
417 return "JobQueueFederated:wiki:{$this->domain}" .
418 sha1( serialize( array_keys( $this->partitionQueues ) ) );
426 foreach ( $this->partitionQueues as $queue ) {
428 $nonEmpty = $queue->doGetSiblingQueuesWithJobs( $types );
429 if ( is_array( $nonEmpty ) ) {
430 $result = array_unique( array_merge( $result, $nonEmpty ) );
434 if ( count( $result ) == count( $types ) ) {
444 return array_values( $result );
451 foreach ( $this->partitionQueues as $queue ) {
453 $sizes = $queue->doGetSiblingQueueSizes( $types );
454 if ( is_array( $sizes ) ) {
455 foreach ( $sizes as
$type => $size ) {
456 $result[
$type] = ( $result[
$type] ?? 0 ) + $size;
472 wfDebugLog(
'JobQueue', $e->getMessage() .
"\n" . $e->getTraceAsString() );
483 if ( $down >= count( $this->partitionQueues ) ) {
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.
getLiveLocationWeights()
Get the map of "live" locations to weight (does not include zero weight items)
getLiveLocation( $item)
Get the location of an item on the "live" ring.
ejectFromLiveRing( $location, $ttl)
Remove a location from the "live" hash ring.
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.
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.
static factory(array $params)
Get a job queue object of the specified type.
string $domain
DB domain ID.
Describe and execute a background job.
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