[JobQueue] Do not count claimed jobs in isEmpty().
[lhc/web/wiklou.git] / includes / job / JobQueueDB.php
index 67c5083..4d0f294 100644 (file)
@@ -49,15 +49,19 @@ class JobQueueDB extends JobQueue {
                        return false;
                }
 
-               $found = $this->getSlaveDB()->selectField(
-                       'job', '1', array( 'job_cmd' => $this->type ), __METHOD__
+               $found = $this->getSlaveDB()->selectField( // unclaimed job
+                       'job', '1', array( 'job_cmd' => $this->type, 'job_token' => '' ), __METHOD__
                );
 
                $wgMemc->add( $key, $found ? 'false' : 'true', self::CACHE_TTL );
+               return (bool)$found;
        }
 
        /**
         * @see JobQueue::doBatchPush()
+        * @param array $jobs
+        * @param $flags
+        * @throws DBError|Exception
         * @return bool
         */
        protected function doBatchPush( array $jobs, $flags ) {
@@ -85,11 +89,8 @@ class JobQueueDB extends JobQueue {
                        ) {
                                global $wgMemc;
 
-                               $autoTrx = $dbw->getFlag( DBO_TRX ); // automatic begin() enabled?
                                if ( $atomic ) {
                                        $dbw->begin( __METHOD__ ); // wrap all the job additions in one transaction
-                               } else {
-                                       $dbw->clearFlag( DBO_TRX ); // make each query its own transaction
                                }
                                try {
                                        // Strip out any duplicate jobs that are already in the queue...
@@ -116,15 +117,11 @@ class JobQueueDB extends JobQueue {
                                } catch ( DBError $e ) {
                                        if ( $atomic ) {
                                                $dbw->rollback( __METHOD__ );
-                                       } else {
-                                               $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
                                        }
                                        throw $e;
                                }
                                if ( $atomic ) {
                                        $dbw->commit( __METHOD__ );
-                               } else {
-                                       $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
                                }
 
                                $wgMemc->set( $key, 'false', $ttl ); // queue is not empty
@@ -150,51 +147,44 @@ class JobQueueDB extends JobQueue {
 
                $uuid = wfRandomString( 32 ); // pop attempt
                $job = false; // job popped off
-               $autoTrx = $dbw->getFlag( DBO_TRX ); // automatic begin() enabled?
-               $dbw->clearFlag( DBO_TRX ); // make each query its own transaction
-               try {
-                       // Occasionally recycle jobs back into the queue that have been claimed too long
-                       if ( mt_rand( 0, 99 ) == 0 ) {
-                               $this->recycleStaleJobs();
-                       }
-                       do { // retry when our row is invalid or deleted as a duplicate
-                               // Try to reserve a row in the DB...
-                               if ( in_array( $this->order, array( 'fifo', 'timestamp' ) ) ) {
-                                       $row = $this->claimOldest( $uuid );
-                               } else { // random first
-                                       $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs
-                                       $gte  = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand
-                                       $row  = $this->claimRandom( $uuid, $rand, $gte );
-                                       if ( !$row ) { // need to try the other direction
-                                               $row = $this->claimRandom( $uuid, $rand, !$gte );
-                                       }
-                               }
-                               // Check if we found a row to reserve...
-                               if ( !$row ) {
-                                       $wgMemc->set( $this->getEmptinessCacheKey(), 'true', self::CACHE_TTL );
-                                       break; // nothing to do
-                               }
-                               // Get the job object from the row...
-                               $title = Title::makeTitleSafe( $row->job_namespace, $row->job_title );
-                               if ( !$title ) {
-                                       $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
-                                       wfIncrStats( 'job-pop' );
-                                       wfDebugLog( 'JobQueueDB', "Row has invalid title '{$row->job_title}'." );
-                                       continue; // try again
-                               }
-                               $job = Job::factory( $row->job_cmd, $title,
-                                       self::extractBlob( $row->job_params ), $row->job_id );
-                               // Flag this job as an old duplicate based on its "root" job...
-                               if ( $this->isRootJobOldDuplicate( $job ) ) {
-                                       $job = DuplicateJob::newFromJob( $job ); // convert to a no-op
-                               }
-                               break; // done
-                       } while( true );
-               } catch ( DBError $e ) {
-                       $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
-                       throw $e;
+               // Occasionally recycle jobs back into the queue that have been claimed too long
+               if ( mt_rand( 0, 99 ) == 0 ) {
+                       $this->recycleStaleJobs();
                }
-               $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
+               do { // retry when our row is invalid or deleted as a duplicate
+                       // Try to reserve a row in the DB...
+                       if ( in_array( $this->order, array( 'fifo', 'timestamp' ) ) ) {
+                               $row = $this->claimOldest( $uuid );
+                       } else { // random first
+                               $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs
+                               $gte  = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand
+                               $row  = $this->claimRandom( $uuid, $rand, $gte );
+                               if ( !$row ) { // need to try the other direction
+                                       $row = $this->claimRandom( $uuid, $rand, !$gte );
+                               }
+                       }
+                       // Check if we found a row to reserve...
+                       if ( !$row ) {
+                               $wgMemc->set( $this->getEmptinessCacheKey(), 'true', self::CACHE_TTL );
+                               break; // nothing to do
+                       }
+                       // Get the job object from the row...
+                       $title = Title::makeTitleSafe( $row->job_namespace, $row->job_title );
+                       if ( !$title ) {
+                               $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
+                               wfIncrStats( 'job-pop' );
+                               wfDebugLog( 'JobQueueDB', "Row has invalid title '{$row->job_title}'." );
+                               continue; // try again
+                       }
+                       $job = Job::factory( $row->job_cmd, $title,
+                               self::extractBlob( $row->job_params ), $row->job_id );
+                       $job->id = $row->job_id; // XXX: work around broken subclasses
+                       // Flag this job as an old duplicate based on its "root" job...
+                       if ( $this->isRootJobOldDuplicate( $job ) ) {
+                               $job = DuplicateJob::newFromJob( $job ); // convert to a no-op
+                       }
+                       break; // done
+               } while( true );
 
                return $job;
        }
@@ -319,26 +309,42 @@ class JobQueueDB extends JobQueue {
                $dbw   = $this->getMasterDB();
                $count = 0; // affected rows
 
-               if ( $this->claimTTL > 0 ) { // re-try stale jobs...
+               if ( !$dbw->lock( "jobqueue-recycle-{$this->type}", __METHOD__, 1 ) ) {
+                       return $count; // already in progress
+               }
+
+               // Remove claims on jobs acquired for too long if enabled...
+               if ( $this->claimTTL > 0 ) {
                        $claimCutoff = $dbw->timestamp( $now - $this->claimTTL );
-                       // Reset job_token for these jobs so that other runners will pick them up.
-                       // Set the timestamp to the current time, as it is useful to now that the job
-                       // was already tried before.
-                       $dbw->update( 'job',
-                               array(
-                                       'job_token' => '',
-                                       'job_token_timestamp' => $dbw->timestamp( $now ) ), // time of release
+                       // Get the IDs of jobs that have be claimed but not finished after too long.
+                       // These jobs can be recycled into the queue by expiring the claim. Selecting
+                       // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
+                       $res = $dbw->select( 'job', 'job_id',
                                array(
                                        'job_cmd' => $this->type,
                                        "job_token != {$dbw->addQuotes( '' )}", // was acquired
                                        "job_token_timestamp < {$dbw->addQuotes( $claimCutoff )}", // stale
-                                       "job_attempts < {$dbw->addQuotes( self::MAX_ATTEMPTS )}" ),
+                                       "job_attempts < {$dbw->addQuotes( self::MAX_ATTEMPTS )}" ), // retries left
                                __METHOD__
                        );
-                       $count += $dbw->affectedRows();
+                       $ids = array_map( function( $o ) { return $o->job_id; }, iterator_to_array( $res ) );
+                       if ( count( $ids ) ) {
+                               // Reset job_token for these jobs so that other runners will pick them up.
+                               // Set the timestamp to the current time, as it is useful to now that the job
+                               // was already tried before (the timestamp becomes the "released" time).
+                               $dbw->update( 'job',
+                                       array(
+                                               'job_token' => '',
+                                               'job_token_timestamp' => $dbw->timestamp( $now ) ), // time of release
+                                       array(
+                                               'job_id' => $ids ),
+                                       __METHOD__
+                               );
+                               $count += $dbw->affectedRows();
+                       }
                }
 
-               // Just destroy stale jobs...
+               // Just destroy any stale jobs...
                $pruneCutoff = $dbw->timestamp( $now - self::MAX_AGE_PRUNE );
                $conds = array(
                        'job_cmd' => $this->type,
@@ -348,36 +354,45 @@ class JobQueueDB extends JobQueue {
                if ( $this->claimTTL > 0 ) { // only prune jobs attempted too many times...
                        $conds[] = "job_attempts >= {$dbw->addQuotes( self::MAX_ATTEMPTS )}";
                }
-               $dbw->delete( 'job', $conds, __METHOD__ );
-               $count += $dbw->affectedRows();
+               // Get the IDs of jobs that are considered stale and should be removed. Selecting
+               // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
+               $res = $dbw->select( 'job', 'job_id', $conds, __METHOD__ );
+               $ids = array_map( function( $o ) { return $o->job_id; }, iterator_to_array( $res ) );
+               if ( count( $ids ) ) {
+                       $dbw->delete( 'job', array( 'job_id' => $ids ), __METHOD__ );
+                       $count += $dbw->affectedRows();
+               }
+
+               $dbw->unlock( "jobqueue-recycle-{$this->type}", __METHOD__ );
 
                return $count;
        }
 
        /**
         * @see JobQueue::doAck()
+        * @param Job $job
+        * @throws MWException
         * @return Job|bool
         */
        protected function doAck( Job $job ) {
+               if ( !$job->getId() ) {
+                       throw new MWException( "Job of type '{$job->getType()}' has no ID." );
+               }
+
                $dbw = $this->getMasterDB();
                $dbw->commit( __METHOD__, 'flush' ); // flush existing transaction
 
-               $autoTrx = $dbw->getFlag( DBO_TRX ); // automatic begin() enabled?
-               $dbw->clearFlag( DBO_TRX ); // make each query its own transaction
-               try {
-                       // Delete a row with a single DELETE without holding row locks over RTTs...
-                       $dbw->delete( 'job', array( 'job_cmd' => $this->type, 'job_id' => $job->getId() ) );
-               } catch ( Exception $e ) {
-                       $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
-                       throw $e;
-               }
-               $dbw->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
+               // Delete a row with a single DELETE without holding row locks over RTTs...
+               $dbw->delete( 'job',
+                       array( 'job_cmd' => $this->type, 'job_id' => $job->getId() ), __METHOD__ );
 
                return true;
        }
 
        /**
         * @see JobQueue::doDeduplicateRootJob()
+        * @param Job $job
+        * @throws MWException
         * @return bool
         */
        protected function doDeduplicateRootJob( Job $job ) {