Update Codex from v1.20.0 to v1.20.1
[mediawiki.git] / includes / jobqueue / JobQueueDB.php
blob1c9eac6d906fe6fff0cb5e04d0dd1df91e0fa06d
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.
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.
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
18 * @file
20 use MediaWiki\MediaWikiServices;
21 use Wikimedia\Rdbms\DBConnectionError;
22 use Wikimedia\Rdbms\DBError;
23 use Wikimedia\Rdbms\IDatabase;
24 use Wikimedia\Rdbms\IMaintainableDatabase;
25 use Wikimedia\Rdbms\IReadableDatabase;
26 use Wikimedia\Rdbms\RawSQLValue;
27 use Wikimedia\Rdbms\SelectQueryBuilder;
28 use Wikimedia\Rdbms\ServerInfo;
29 use Wikimedia\ScopedCallback;
31 /**
32 * Database-backed job queue storage.
34 * @since 1.21
35 * @ingroup JobQueue
37 class JobQueueDB extends JobQueue {
38 /* seconds to cache info without re-validating */
39 private const CACHE_TTL_SHORT = 30;
40 /* seconds a job can live once claimed */
41 private const MAX_AGE_PRUNE = 7 * 24 * 3600;
42 /**
43 * Used for job_random, the highest safe 32-bit signed integer.
44 * Equivalent to `( 2 ** 31 ) - 1` on 64-bit.
46 private const MAX_JOB_RANDOM = 2_147_483_647;
47 /* maximum number of rows to skip */
48 private const MAX_OFFSET = 255;
50 /** @var IMaintainableDatabase|DBError|null */
51 protected $conn;
53 /** @var array|null Server configuration array */
54 protected $server;
55 /** @var string|null Name of an external DB cluster or null for the local DB cluster */
56 protected $cluster;
58 /**
59 * Additional parameters include:
60 * - server : Server configuration array for Database::factory. Overrides "cluster".
61 * - cluster : The name of an external cluster registered via LBFactory.
62 * If not specified, the primary DB cluster for the wiki will be used.
63 * This can be overridden with a custom cluster so that DB handles will
64 * be retrieved via LBFactory::getExternalLB() and getConnection().
66 protected function __construct( array $params ) {
67 parent::__construct( $params );
69 if ( isset( $params['server'] ) ) {
70 $this->server = $params['server'];
71 // Always use autocommit mode, even if DBO_TRX is configured
72 $this->server['flags'] ??= 0;
73 $this->server['flags'] &= ~( IDatabase::DBO_TRX | IDatabase::DBO_DEFAULT );
74 } elseif ( isset( $params['cluster'] ) && is_string( $params['cluster'] ) ) {
75 $this->cluster = $params['cluster'];
79 protected function supportedOrders() {
80 return [ 'random', 'timestamp', 'fifo' ];
83 protected function optimalOrder() {
84 return 'random';
87 /**
88 * @see JobQueue::doIsEmpty()
89 * @return bool
91 protected function doIsEmpty() {
92 $dbr = $this->getReplicaDB();
93 try {
94 // unclaimed job
95 $found = (bool)$dbr->newSelectQueryBuilder()
96 ->select( '1' )
97 ->from( 'job' )
98 ->where( [ 'job_cmd' => $this->type, 'job_token' => '' ] )
99 ->caller( __METHOD__ )->fetchField();
100 } catch ( DBError $e ) {
101 throw $this->getDBException( $e );
104 return !$found;
108 * @see JobQueue::doGetSize()
109 * @return int
111 protected function doGetSize() {
112 $key = $this->getCacheKey( 'size' );
114 $size = $this->wanCache->get( $key );
115 if ( is_int( $size ) ) {
116 return $size;
119 $dbr = $this->getReplicaDB();
120 try {
121 $size = $dbr->newSelectQueryBuilder()
122 ->from( 'job' )
123 ->where( [ 'job_cmd' => $this->type, 'job_token' => '' ] )
124 ->caller( __METHOD__ )->fetchRowCount();
125 } catch ( DBError $e ) {
126 throw $this->getDBException( $e );
128 $this->wanCache->set( $key, $size, self::CACHE_TTL_SHORT );
130 return $size;
134 * @see JobQueue::doGetAcquiredCount()
135 * @return int
137 protected function doGetAcquiredCount() {
138 if ( $this->claimTTL <= 0 ) {
139 return 0; // no acknowledgements
142 $key = $this->getCacheKey( 'acquiredcount' );
144 $count = $this->wanCache->get( $key );
145 if ( is_int( $count ) ) {
146 return $count;
149 $dbr = $this->getReplicaDB();
150 try {
151 $count = $dbr->newSelectQueryBuilder()
152 ->from( 'job' )
153 ->where( [
154 'job_cmd' => $this->type,
155 $dbr->expr( 'job_token', '!=', '' ),
157 ->caller( __METHOD__ )->fetchRowCount();
158 } catch ( DBError $e ) {
159 throw $this->getDBException( $e );
161 $this->wanCache->set( $key, $count, self::CACHE_TTL_SHORT );
163 return $count;
167 * @see JobQueue::doGetAbandonedCount()
168 * @return int
169 * @throws JobQueueConnectionError
170 * @throws JobQueueError
172 protected function doGetAbandonedCount() {
173 if ( $this->claimTTL <= 0 ) {
174 return 0; // no acknowledgements
177 $key = $this->getCacheKey( 'abandonedcount' );
179 $count = $this->wanCache->get( $key );
180 if ( is_int( $count ) ) {
181 return $count;
184 $dbr = $this->getReplicaDB();
185 try {
186 $count = $dbr->newSelectQueryBuilder()
187 ->from( 'job' )
188 ->where(
190 'job_cmd' => $this->type,
191 $dbr->expr( 'job_token', '!=', '' ),
192 $dbr->expr( 'job_attempts', '>=', $this->maxTries ),
195 ->caller( __METHOD__ )->fetchRowCount();
196 } catch ( DBError $e ) {
197 throw $this->getDBException( $e );
200 $this->wanCache->set( $key, $count, self::CACHE_TTL_SHORT );
202 return $count;
206 * @see JobQueue::doBatchPush()
207 * @param IJobSpecification[] $jobs
208 * @param int $flags
209 * @throws DBError|Exception
210 * @return void
212 protected function doBatchPush( array $jobs, $flags ) {
213 // Silence expectations related to getting a primary DB, as we have to get a primary DB to insert the job.
214 $transactionProfiler = Profiler::instance()->getTransactionProfiler();
215 $scope = $transactionProfiler->silenceForScope();
216 $dbw = $this->getPrimaryDB();
217 ScopedCallback::consume( $scope );
218 // In general, there will be two cases here:
219 // a) sqlite; DB connection is probably a regular round-aware handle.
220 // If the connection is busy with a transaction, then defer the job writes
221 // until right before the main round commit step. Any errors that bubble
222 // up will rollback the main commit round.
223 // b) mysql/postgres; DB connection is generally a separate CONN_TRX_AUTOCOMMIT handle.
224 // No transaction is active nor will be started by writes, so enqueue the jobs
225 // now so that any errors will show up immediately as the interface expects. Any
226 // errors that bubble up will rollback the main commit round.
227 $fname = __METHOD__;
228 $dbw->onTransactionPreCommitOrIdle(
229 function ( IDatabase $dbw ) use ( $jobs, $flags, $fname ) {
230 $this->doBatchPushInternal( $dbw, $jobs, $flags, $fname );
232 $fname
237 * This function should *not* be called outside of JobQueueDB
239 * @suppress SecurityCheck-SQLInjection Bug in phan-taint-check handling bulk inserts
240 * @param IDatabase $dbw
241 * @param IJobSpecification[] $jobs
242 * @param int $flags
243 * @param string $method
244 * @throws DBError
245 * @return void
247 public function doBatchPushInternal( IDatabase $dbw, array $jobs, $flags, $method ) {
248 if ( $jobs === [] ) {
249 return;
252 $rowSet = []; // (sha1 => job) map for jobs that are de-duplicated
253 $rowList = []; // list of jobs for jobs that are not de-duplicated
254 foreach ( $jobs as $job ) {
255 $row = $this->insertFields( $job, $dbw );
256 if ( $job->ignoreDuplicates() ) {
257 $rowSet[$row['job_sha1']] = $row;
258 } else {
259 $rowList[] = $row;
263 if ( $flags & self::QOS_ATOMIC ) {
264 $dbw->startAtomic( $method ); // wrap all the job additions in one transaction
266 try {
267 // Strip out any duplicate jobs that are already in the queue...
268 if ( count( $rowSet ) ) {
269 $res = $dbw->newSelectQueryBuilder()
270 ->select( 'job_sha1' )
271 ->from( 'job' )
272 ->where(
274 // No job_type condition since it's part of the job_sha1 hash
275 'job_sha1' => array_map( 'strval', array_keys( $rowSet ) ),
276 'job_token' => '' // unclaimed
279 ->caller( $method )->fetchResultSet();
280 foreach ( $res as $row ) {
281 wfDebug( "Job with hash '{$row->job_sha1}' is a duplicate." );
282 unset( $rowSet[$row->job_sha1] ); // already enqueued
285 // Build the full list of job rows to insert
286 $rows = array_merge( $rowList, array_values( $rowSet ) );
287 // Silence expectations related to inserting to the job table, because we have to perform the inserts to
288 // track the job.
289 $transactionProfiler = Profiler::instance()->getTransactionProfiler();
290 $scope = $transactionProfiler->silenceForScope();
291 // Insert the job rows in chunks to avoid replica DB lag...
292 foreach ( array_chunk( $rows, 50 ) as $rowBatch ) {
293 $dbw->newInsertQueryBuilder()
294 ->insertInto( 'job' )
295 ->rows( $rowBatch )
296 ->caller( $method )->execute();
298 ScopedCallback::consume( $scope );
299 $this->incrStats( 'inserts', $this->type, count( $rows ) );
300 $this->incrStats( 'dupe_inserts', $this->type,
301 count( $rowSet ) + count( $rowList ) - count( $rows )
303 } catch ( DBError $e ) {
304 throw $this->getDBException( $e );
306 if ( $flags & self::QOS_ATOMIC ) {
307 $dbw->endAtomic( $method );
312 * @see JobQueue::doPop()
313 * @return RunnableJob|false
315 protected function doPop() {
316 $job = false; // job popped off
317 try {
318 $uuid = wfRandomString( 32 ); // pop attempt
319 do { // retry when our row is invalid or deleted as a duplicate
320 // Try to reserve a row in the DB...
321 if ( in_array( $this->order, [ 'fifo', 'timestamp' ] ) ) {
322 $row = $this->claimOldest( $uuid );
323 } else { // random first
324 $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs
325 $gte = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand
326 $row = $this->claimRandom( $uuid, $rand, $gte );
328 // Check if we found a row to reserve...
329 if ( !$row ) {
330 break; // nothing to do
332 $this->incrStats( 'pops', $this->type );
334 // Get the job object from the row...
335 $job = $this->jobFromRow( $row );
336 break; // done
337 } while ( true );
339 if ( !$job || mt_rand( 0, 9 ) == 0 ) {
340 // Handled jobs that need to be recycled/deleted;
341 // any recycled jobs will be picked up next attempt
342 $this->recycleAndDeleteStaleJobs();
344 } catch ( DBError $e ) {
345 throw $this->getDBException( $e );
348 return $job;
352 * Reserve a row with a single UPDATE without holding row locks over RTTs...
354 * @param string $uuid 32 char hex string
355 * @param int $rand Random unsigned integer (31 bits)
356 * @param bool $gte Search for job_random >= $random (otherwise job_random <= $random)
357 * @return stdClass|false Row|false
359 protected function claimRandom( $uuid, $rand, $gte ) {
360 $dbw = $this->getPrimaryDB();
361 // Check cache to see if the queue has <= OFFSET items
362 $tinyQueue = $this->wanCache->get( $this->getCacheKey( 'small' ) );
364 $invertedDirection = false; // whether one job_random direction was already scanned
365 // This uses a replication safe method for acquiring jobs. One could use UPDATE+LIMIT
366 // instead, but that either uses ORDER BY (in which case it deadlocks in MySQL) or is
367 // not replication safe. Due to https://bugs.mysql.com/bug.php?id=6980, subqueries cannot
368 // be used here with MySQL.
369 do {
370 if ( $tinyQueue ) { // queue has <= MAX_OFFSET rows
371 // For small queues, using OFFSET will overshoot and return no rows more often.
372 // Instead, this uses job_random to pick a row (possibly checking both directions).
373 $row = $dbw->newSelectQueryBuilder()
374 ->select( self::selectFields() )
375 ->from( 'job' )
376 ->where(
378 'job_cmd' => $this->type,
379 'job_token' => '', // unclaimed
380 $dbw->expr( 'job_random', $gte ? '>=' : '<=', $rand )
383 ->orderBy(
384 'job_random',
385 $gte ? SelectQueryBuilder::SORT_ASC : SelectQueryBuilder::SORT_DESC
387 ->caller( __METHOD__ )->fetchRow();
388 if ( !$row && !$invertedDirection ) {
389 $gte = !$gte;
390 $invertedDirection = true;
391 continue; // try the other direction
393 } else { // table *may* have >= MAX_OFFSET rows
394 // T44614: "ORDER BY job_random" with a job_random inequality causes high CPU
395 // in MySQL if there are many rows for some reason. This uses a small OFFSET
396 // instead of job_random for reducing excess claim retries.
397 $row = $dbw->newSelectQueryBuilder()
398 ->select( self::selectFields() )
399 ->from( 'job' )
400 ->where(
402 'job_cmd' => $this->type,
403 'job_token' => '', // unclaimed
406 ->offset( mt_rand( 0, self::MAX_OFFSET ) )
407 ->caller( __METHOD__ )->fetchRow();
408 if ( !$row ) {
409 $tinyQueue = true; // we know the queue must have <= MAX_OFFSET rows
410 $this->wanCache->set( $this->getCacheKey( 'small' ), 1, 30 );
411 continue; // use job_random
415 if ( !$row ) {
416 break;
419 $dbw->newUpdateQueryBuilder()
420 ->update( 'job' ) // update by PK
421 ->set( [
422 'job_token' => $uuid,
423 'job_token_timestamp' => $dbw->timestamp(),
424 'job_attempts' => new RawSQLValue( 'job_attempts+1' ),
426 ->where( [
427 'job_cmd' => $this->type,
428 'job_id' => $row->job_id,
429 'job_token' => ''
431 ->caller( __METHOD__ )->execute();
433 // This might get raced out by another runner when claiming the previously
434 // selected row. The use of job_random should minimize this problem, however.
435 if ( !$dbw->affectedRows() ) {
436 $row = false; // raced out
438 } while ( !$row );
440 return $row;
444 * Reserve a row with a single UPDATE without holding row locks over RTTs...
446 * @param string $uuid 32 char hex string
447 * @return stdClass|false Row|false
449 protected function claimOldest( $uuid ) {
450 $dbw = $this->getPrimaryDB();
452 $row = false; // the row acquired
453 do {
454 if ( $dbw->getType() === 'mysql' ) {
455 // Per https://bugs.mysql.com/bug.php?id=6980, we can't use subqueries on the
456 // same table being changed in an UPDATE query in MySQL (gives Error: 1093).
457 // Postgres has no such limitation. However, MySQL offers an
458 // alternative here by supporting ORDER BY + LIMIT for UPDATE queries.
459 $dbw->query( "UPDATE {$dbw->tableName( 'job' )} " .
460 "SET " .
461 "job_token = {$dbw->addQuotes( $uuid ) }, " .
462 "job_token_timestamp = {$dbw->addQuotes( $dbw->timestamp() )}, " .
463 "job_attempts = job_attempts+1 " .
464 "WHERE ( " .
465 "job_cmd = {$dbw->addQuotes( $this->type )} " .
466 "AND job_token = {$dbw->addQuotes( '' )} " .
467 ") ORDER BY job_id ASC LIMIT 1",
468 __METHOD__
470 } else {
471 // Use a subquery to find the job, within an UPDATE to claim it.
472 // This uses as much of the DB wrapper functions as possible.
473 $qb = $dbw->newSelectQueryBuilder()
474 ->select( 'job_id' )
475 ->from( 'job' )
476 ->where( [ 'job_cmd' => $this->type, 'job_token' => '' ] )
477 ->orderBy( 'job_id', SelectQueryBuilder::SORT_ASC )
478 ->limit( 1 );
480 $dbw->newUpdateQueryBuilder()
481 ->update( 'job' )
482 ->set( [
483 'job_token' => $uuid,
484 'job_token_timestamp' => $dbw->timestamp(),
485 'job_attempts' => new RawSQLValue( 'job_attempts+1' ),
487 ->where( [ 'job_id' => new RawSQLValue( '(' . $qb->getSQL() . ')' ) ] )
488 ->caller( __METHOD__ )->execute();
491 if ( !$dbw->affectedRows() ) {
492 break;
495 // Fetch any row that we just reserved...
496 $row = $dbw->newSelectQueryBuilder()
497 ->select( self::selectFields() )
498 ->from( 'job' )
499 ->where( [ 'job_cmd' => $this->type, 'job_token' => $uuid ] )
500 ->caller( __METHOD__ )->fetchRow();
501 if ( !$row ) { // raced out by duplicate job removal
502 wfDebug( "Row deleted as duplicate by another process." );
504 } while ( !$row );
506 return $row;
510 * @see JobQueue::doAck()
511 * @param RunnableJob $job
512 * @throws JobQueueConnectionError
513 * @throws JobQueueError
515 protected function doAck( RunnableJob $job ) {
516 $id = $job->getMetadata( 'id' );
517 if ( $id === null ) {
518 throw new UnexpectedValueException( "Job of type '{$job->getType()}' has no ID." );
521 $dbw = $this->getPrimaryDB();
522 try {
523 // Delete a row with a single DELETE without holding row locks over RTTs...
524 $dbw->newDeleteQueryBuilder()
525 ->deleteFrom( 'job' )
526 ->where( [ 'job_cmd' => $this->type, 'job_id' => $id ] )
527 ->caller( __METHOD__ )->execute();
529 $this->incrStats( 'acks', $this->type );
530 } catch ( DBError $e ) {
531 throw $this->getDBException( $e );
536 * @see JobQueue::doDeduplicateRootJob()
537 * @param IJobSpecification $job
538 * @throws JobQueueConnectionError
539 * @return bool
541 protected function doDeduplicateRootJob( IJobSpecification $job ) {
542 // Callers should call JobQueueGroup::push() before this method so that if the
543 // insert fails, the de-duplication registration will be aborted. Since the insert
544 // is deferred till "transaction idle", do the same here, so that the ordering is
545 // maintained. Having only the de-duplication registration succeed would cause
546 // jobs to become no-ops without any actual jobs that made them redundant.
547 $dbw = $this->getPrimaryDB();
548 $dbw->onTransactionCommitOrIdle(
549 function () use ( $job ) {
550 parent::doDeduplicateRootJob( $job );
552 __METHOD__
555 return true;
559 * @see JobQueue::doDelete()
560 * @return bool
562 protected function doDelete() {
563 $dbw = $this->getPrimaryDB();
564 try {
565 $dbw->newDeleteQueryBuilder()
566 ->deleteFrom( 'job' )
567 ->where( [ 'job_cmd' => $this->type ] )
568 ->caller( __METHOD__ )->execute();
569 } catch ( DBError $e ) {
570 throw $this->getDBException( $e );
573 return true;
577 * @see JobQueue::doWaitForBackups()
578 * @return void
580 protected function doWaitForBackups() {
581 if ( $this->server ) {
582 return; // not using LBFactory instance
585 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
586 $lbFactory->waitForReplication();
590 * @return void
592 protected function doFlushCaches() {
593 foreach ( [ 'size', 'acquiredcount' ] as $type ) {
594 $this->wanCache->delete( $this->getCacheKey( $type ) );
599 * @see JobQueue::getAllQueuedJobs()
600 * @return Iterator<RunnableJob>
602 public function getAllQueuedJobs() {
603 return $this->getJobIterator( [ 'job_cmd' => $this->getType(), 'job_token' => '' ] );
607 * @see JobQueue::getAllAcquiredJobs()
608 * @return Iterator<RunnableJob>
610 public function getAllAcquiredJobs() {
611 $dbr = $this->getReplicaDB();
612 return $this->getJobIterator( [ 'job_cmd' => $this->getType(), $dbr->expr( 'job_token', '>', '' ) ] );
616 * @see JobQueue::getAllAbandonedJobs()
617 * @return Iterator<RunnableJob>
619 public function getAllAbandonedJobs() {
620 $dbr = $this->getReplicaDB();
621 return $this->getJobIterator( [
622 'job_cmd' => $this->getType(),
623 $dbr->expr( 'job_token', '>', '' ),
624 $dbr->expr( 'job_attempts', '>=', intval( $this->maxTries ) ),
625 ] );
629 * @param array $conds Query conditions
630 * @return Iterator<RunnableJob>
632 protected function getJobIterator( array $conds ) {
633 $dbr = $this->getReplicaDB();
634 $qb = $dbr->newSelectQueryBuilder()
635 ->select( self::selectFields() )
636 ->from( 'job' )
637 ->where( $conds );
638 try {
639 return new MappedIterator(
640 $qb->caller( __METHOD__ )->fetchResultSet(),
641 function ( $row ) {
642 return $this->jobFromRow( $row );
645 } catch ( DBError $e ) {
646 throw $this->getDBException( $e );
650 public function getCoalesceLocationInternal() {
651 if ( $this->server ) {
652 return null; // not using the LBFactory instance
655 return is_string( $this->cluster )
656 ? "DBCluster:{$this->cluster}:{$this->domain}"
657 : "LBFactory:{$this->domain}";
660 protected function doGetSiblingQueuesWithJobs( array $types ) {
661 $dbr = $this->getReplicaDB();
662 // @note: this does not check whether the jobs are claimed or not.
663 // This is useful so JobQueueGroup::pop() also sees queues that only
664 // have stale jobs. This lets recycleAndDeleteStaleJobs() re-enqueue
665 // failed jobs so that they can be popped again for that edge case.
666 $res = $dbr->newSelectQueryBuilder()
667 ->select( 'job_cmd' )
668 ->distinct()
669 ->from( 'job' )
670 ->where( [ 'job_cmd' => $types ] )
671 ->caller( __METHOD__ )->fetchResultSet();
673 $types = [];
674 foreach ( $res as $row ) {
675 $types[] = $row->job_cmd;
678 return $types;
681 protected function doGetSiblingQueueSizes( array $types ) {
682 $dbr = $this->getReplicaDB();
684 $res = $dbr->newSelectQueryBuilder()
685 ->select( [ 'job_cmd', 'count' => 'COUNT(*)' ] )
686 ->from( 'job' )
687 ->where( [ 'job_cmd' => $types ] )
688 ->groupBy( 'job_cmd' )
689 ->caller( __METHOD__ )->fetchResultSet();
691 $sizes = [];
692 foreach ( $res as $row ) {
693 $sizes[$row->job_cmd] = (int)$row->count;
696 return $sizes;
700 * Recycle or destroy any jobs that have been claimed for too long
702 * @return int Number of jobs recycled/deleted
704 public function recycleAndDeleteStaleJobs() {
705 $now = time();
706 $count = 0; // affected rows
707 $dbw = $this->getPrimaryDB();
709 try {
710 if ( !$dbw->lock( "jobqueue-recycle-{$this->type}", __METHOD__, 1 ) ) {
711 return $count; // already in progress
714 // Remove claims on jobs acquired for too long if enabled...
715 if ( $this->claimTTL > 0 ) {
716 $claimCutoff = $dbw->timestamp( $now - $this->claimTTL );
717 // Get the IDs of jobs that have be claimed but not finished after too long.
718 // These jobs can be recycled into the queue by expiring the claim. Selecting
719 // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
720 $res = $dbw->newSelectQueryBuilder()
721 ->select( 'job_id' )
722 ->from( 'job' )
723 ->where(
725 'job_cmd' => $this->type,
726 $dbw->expr( 'job_token', '!=', '' ), // was acquired
727 $dbw->expr( 'job_token_timestamp', '<', $claimCutoff ), // stale
728 $dbw->expr( 'job_attempts', '<', $this->maxTries ), // retries left
731 ->caller( __METHOD__ )->fetchResultSet();
732 $ids = array_map(
733 static function ( $o ) {
734 return $o->job_id;
735 }, iterator_to_array( $res )
737 if ( count( $ids ) ) {
738 // Reset job_token for these jobs so that other runners will pick them up.
739 // Set the timestamp to the current time, as it is useful to now that the job
740 // was already tried before (the timestamp becomes the "released" time).
741 $dbw->newUpdateQueryBuilder()
742 ->update( 'job' )
743 ->set( [
744 'job_token' => '',
745 'job_token_timestamp' => $dbw->timestamp( $now ) // time of release
747 ->where( [
748 'job_id' => $ids,
749 $dbw->expr( 'job_token', '!=', '' ),
751 ->caller( __METHOD__ )->execute();
753 $affected = $dbw->affectedRows();
754 $count += $affected;
755 $this->incrStats( 'recycles', $this->type, $affected );
759 // Just destroy any stale jobs...
760 $pruneCutoff = $dbw->timestamp( $now - self::MAX_AGE_PRUNE );
761 $qb = $dbw->newSelectQueryBuilder()
762 ->select( 'job_id' )
763 ->from( 'job' )
764 ->where(
766 'job_cmd' => $this->type,
767 $dbw->expr( 'job_token', '!=', '' ), // was acquired
768 $dbw->expr( 'job_token_timestamp', '<', $pruneCutoff ) // stale
771 if ( $this->claimTTL > 0 ) { // only prune jobs attempted too many times...
772 $qb->andWhere( $dbw->expr( 'job_attempts', '>=', $this->maxTries ) );
774 // Get the IDs of jobs that are considered stale and should be removed. Selecting
775 // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
776 $res = $qb->caller( __METHOD__ )->fetchResultSet();
777 $ids = array_map(
778 static function ( $o ) {
779 return $o->job_id;
780 }, iterator_to_array( $res )
782 if ( count( $ids ) ) {
783 $dbw->newDeleteQueryBuilder()
784 ->deleteFrom( 'job' )
785 ->where( [ 'job_id' => $ids ] )
786 ->caller( __METHOD__ )->execute();
787 $affected = $dbw->affectedRows();
788 $count += $affected;
789 $this->incrStats( 'abandons', $this->type, $affected );
792 $dbw->unlock( "jobqueue-recycle-{$this->type}", __METHOD__ );
793 } catch ( DBError $e ) {
794 throw $this->getDBException( $e );
797 return $count;
801 * @param IJobSpecification $job
802 * @param IReadableDatabase $db
803 * @return array
805 protected function insertFields( IJobSpecification $job, IReadableDatabase $db ) {
806 return [
807 // Fields that describe the nature of the job
808 'job_cmd' => $job->getType(),
809 'job_namespace' => $job->getParams()['namespace'] ?? NS_SPECIAL,
810 'job_title' => $job->getParams()['title'] ?? '',
811 'job_params' => self::makeBlob( $job->getParams() ),
812 // Additional job metadata
813 'job_timestamp' => $db->timestamp(),
814 'job_sha1' => Wikimedia\base_convert(
815 sha1( serialize( $job->getDeduplicationInfo() ) ),
816 16, 36, 31
818 'job_random' => mt_rand( 0, self::MAX_JOB_RANDOM )
823 * @throws JobQueueConnectionError
824 * @return IDatabase
826 protected function getReplicaDB() {
827 try {
828 return $this->getDB( DB_REPLICA );
829 } catch ( DBConnectionError $e ) {
830 throw new JobQueueConnectionError( "DBConnectionError:" . $e->getMessage() );
835 * @throws JobQueueConnectionError
836 * @return IMaintainableDatabase
837 * @since 1.37
839 protected function getPrimaryDB() {
840 try {
841 return $this->getDB( DB_PRIMARY );
842 } catch ( DBConnectionError $e ) {
843 throw new JobQueueConnectionError( "DBConnectionError:" . $e->getMessage() );
848 * @param int $index (DB_REPLICA/DB_PRIMARY)
849 * @return IMaintainableDatabase
851 protected function getDB( $index ) {
852 if ( $this->server ) {
853 if ( $this->conn instanceof IDatabase ) {
854 return $this->conn;
855 } elseif ( $this->conn instanceof DBError ) {
856 throw $this->conn;
859 try {
860 $this->conn = MediaWikiServices::getInstance()->getDatabaseFactory()->create(
861 $this->server['type'],
862 $this->server
864 } catch ( DBError $e ) {
865 $this->conn = $e;
866 throw $e;
869 return $this->conn;
870 } else {
871 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
872 $lb = is_string( $this->cluster )
873 ? $lbFactory->getExternalLB( $this->cluster )
874 : $lbFactory->getMainLB( $this->domain );
876 if ( $lb->getServerType( ServerInfo::WRITER_INDEX ) !== 'sqlite' ) {
877 // Keep a separate connection to avoid contention and deadlocks;
878 // However, SQLite has the opposite behavior due to DB-level locking.
879 $flags = $lb::CONN_TRX_AUTOCOMMIT;
880 } else {
881 // Jobs insertion will be deferred until the PRESEND stage to reduce contention.
882 $flags = 0;
885 return $lb->getMaintenanceConnectionRef( $index, [], $this->domain, $flags );
890 * @param string $property
891 * @return string
893 private function getCacheKey( $property ) {
894 $cluster = is_string( $this->cluster ) ? $this->cluster : 'main';
896 return $this->wanCache->makeGlobalKey(
897 'jobqueue',
898 $this->domain,
899 $cluster,
900 $this->type,
901 $property
906 * @param array|false $params
907 * @return string
909 protected static function makeBlob( $params ) {
910 if ( $params !== false ) {
911 return serialize( $params );
912 } else {
913 return '';
918 * @param stdClass $row
919 * @return RunnableJob
921 protected function jobFromRow( $row ) {
922 $params = ( (string)$row->job_params !== '' ) ? unserialize( $row->job_params ) : [];
923 if ( !is_array( $params ) ) { // this shouldn't happen
924 throw new UnexpectedValueException(
925 "Could not unserialize job with ID '{$row->job_id}'." );
928 $params += [ 'namespace' => $row->job_namespace, 'title' => $row->job_title ];
929 $job = $this->factoryJob( $row->job_cmd, $params );
930 $job->setMetadata( 'id', $row->job_id );
931 $job->setMetadata( 'timestamp', $row->job_timestamp );
933 return $job;
937 * @param DBError $e
938 * @return JobQueueError
940 protected function getDBException( DBError $e ) {
941 return new JobQueueError( get_class( $e ) . ": " . $e->getMessage() );
945 * Return the list of job fields that should be selected.
946 * @since 1.23
947 * @return array
949 public static function selectFields() {
950 return [
951 'job_id',
952 'job_cmd',
953 'job_namespace',
954 'job_title',
955 'job_timestamp',
956 'job_params',
957 'job_random',
958 'job_attempts',
959 'job_token',
960 'job_token_timestamp',
961 'job_sha1',