3 * Redis-backed job queue code.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Aaron Schulz
25 * Class to handle job queues stored in Redis
30 class JobQueueRedis
extends JobQueue
{
31 /** @var RedisConnectionPool */
34 protected $server; // string; server address
36 const ROOTJOB_TTL
= 1209600; // integer; seconds to remember root jobs (14 days)
37 const MAX_AGE_PRUNE
= 604800; // integer; seconds a job can live once claimed (7 days)
39 protected $key; // string; key to prefix the queue keys with (used for testing)
43 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
44 * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
45 * If a hostname is specified but no port, the standard port number
46 * 6379 will be used. Required.
47 * @param array $params
49 public function __construct( array $params ) {
50 parent
::__construct( $params );
51 $this->server
= $params['redisServer'];
52 $this->redisPool
= RedisConnectionPool
::singleton( $params['redisConfig'] );
55 protected function supportedOrders() {
56 return array( 'timestamp', 'fifo' );
59 protected function optimalOrder() {
64 * @see JobQueue::doIsEmpty()
68 protected function doIsEmpty() {
69 $conn = $this->getConnection();
71 return ( $conn->lSize( $this->getQueueKey( 'l-unclaimed' ) ) == 0 );
72 } catch ( RedisException
$e ) {
73 $this->throwRedisException( $this->server
, $conn, $e );
78 * @see JobQueue::doGetSize()
82 protected function doGetSize() {
83 $conn = $this->getConnection();
85 return $conn->lSize( $this->getQueueKey( 'l-unclaimed' ) );
86 } catch ( RedisException
$e ) {
87 $this->throwRedisException( $this->server
, $conn, $e );
92 * @see JobQueue::doGetAcquiredCount()
96 protected function doGetAcquiredCount() {
97 if ( $this->claimTTL
<= 0 ) {
98 return 0; // no acknowledgements
100 $conn = $this->getConnection();
102 return $conn->lSize( $this->getQueueKey( 'l-claimed' ) );
103 } catch ( RedisException
$e ) {
104 $this->throwRedisException( $this->server
, $conn, $e );
109 * @see JobQueue::doBatchPush()
113 * @throws MWException
115 protected function doBatchPush( array $jobs, $flags ) {
116 if ( !count( $jobs ) ) {
120 // Convert the jobs into a list of field maps
121 $items = array(); // (uid => job fields map)
122 foreach ( $jobs as $job ) {
123 $item = $this->getNewJobFields( $job );
124 $items[$item['uid']] = $item;
127 $dedupUids = array(); // list of uids to check for duplicates
128 foreach ( $items as $item ) {
129 if ( $this->isHashUid( $item['uid'] ) ) { // hash identifier => de-duplicate
130 $dedupUids[] = $item['uid'];
134 $conn = $this->getConnection();
136 // Find which of these jobs are duplicates of unclaimed jobs in the queue...
137 if ( count( $dedupUids ) ) {
138 $conn->multi( Redis
::PIPELINE
);
139 foreach ( $dedupUids as $uid ) { // check if job data exists
140 $conn->exists( $this->prefixWithQueueKey( 'data', $uid ) );
142 if ( $this->claimTTL
> 0 ) { // check which jobs were claimed
143 foreach ( $dedupUids as $uid ) {
144 $conn->hExists( $this->prefixWithQueueKey( 'h-meta', $uid ), 'ctime' );
146 list( $exists, $claimed ) = array_chunk( $conn->exec(), count( $dedupUids ) );
148 $exists = $conn->exec();
149 $claimed = array(); // no claim system
151 // Remove the duplicate jobs to cut down on pushing duplicate uids...
152 foreach ( $dedupUids as $k => $uid ) {
153 if ( $exists[$k] && empty( $claimed[$k] ) ) {
154 unset( $items[$uid] );
158 // Actually push the non-duplicate jobs into the queue...
159 if ( count( $items ) ) {
160 $uids = array_keys( $items );
161 $conn->multi( Redis
::MULTI
); // begin (atomic trx)
162 $conn->mSet( $this->prefixKeysWithQueueKey( 'data', $items ) );
163 call_user_func_array(
164 array( $conn, 'lPush' ),
165 array_merge( array( $this->getQueueKey( 'l-unclaimed' ) ), $uids )
167 $res = $conn->exec(); // commit (atomic trx)
168 if ( in_array( false, $res, true ) ) {
169 wfDebugLog( 'JobQueueRedis', "Could not insert {$this->type} job(s)." );
173 wfIncrStats( 'job-insert', count( $items ) );
174 wfIncrStats( 'job-insert-duplicate', count( $jobs ) - count( $items ) );
175 } catch ( RedisException
$e ) {
176 $this->throwRedisException( $this->server
, $conn, $e );
183 * @see JobQueue::doPop()
185 * @throws MWException
187 protected function doPop() {
190 if ( $this->claimTTL
<= 0 && mt_rand( 0, 99 ) == 0 ) {
191 $this->cleanupClaimedJobs(); // prune jobs and IDs from the "garbage" list
194 $conn = $this->getConnection();
197 // Atomically pop an item off the queue and onto the "claimed" list
198 $uid = $conn->rpoplpush(
199 $this->getQueueKey( 'l-unclaimed' ),
200 $this->getQueueKey( 'l-claimed' )
202 if ( $uid === false ) {
203 break; // no jobs; nothing to do
206 wfIncrStats( 'job-pop' );
207 $conn->multi( Redis
::PIPELINE
);
208 $conn->get( $this->prefixWithQueueKey( 'data', $uid ) );
209 if ( $this->claimTTL
> 0 ) {
210 // Set the claim timestamp metadata. If this step fails, then
211 // the timestamp will be assumed to be the current timestamp by
212 // recycleAndDeleteStaleJobs() as of the next time that it runs.
213 // If two runners claim duplicate jobs, one will abort here.
214 $conn->hSetNx( $this->prefixWithQueueKey( 'h-meta', $uid ), 'ctime', time() );
216 // If this fails, the message key will be deleted in cleanupClaimedJobs().
217 // If two runners claim duplicate jobs, one of them will abort here.
219 $this->prefixWithQueueKey( 'h-meta', $uid ),
220 $this->prefixWithQueueKey( 'data', $uid ) );
222 list( $item, $ok ) = $conn->exec();
223 if ( $item === false ||
( $this->claimTTL
&& !$ok ) ) {
224 wfDebug( "Could not find or delete job $uid; probably was a duplicate." );
225 continue; // job was probably a duplicate
228 // If $item is invalid, recycleAndDeleteStaleJobs() will cleanup as needed
229 $job = $this->getJobFromFields( $item ); // may be false
230 } while ( !$job ); // job may be false if invalid
231 } catch ( RedisException
$e ) {
232 $this->throwRedisException( $this->server
, $conn, $e );
235 // Flag this job as an old duplicate based on its "root" job...
237 if ( $job && $this->isRootJobOldDuplicate( $job ) ) {
238 wfIncrStats( 'job-pop-duplicate' );
239 return DuplicateJob
::newFromJob( $job ); // convert to a no-op
241 } catch ( MWException
$e ) {} // don't lose jobs over this
247 * @see JobQueue::doAck()
250 * @throws MWException
252 protected function doAck( Job
$job ) {
253 if ( $this->claimTTL
> 0 ) {
254 $conn = $this->getConnection();
256 // Get the exact field map this Job came from, regardless of whether
257 // the job was transformed into a DuplicateJob or anything of the sort.
258 $item = $job->metadata
['sourceFields'];
260 $conn->multi( Redis
::MULTI
); // begin (atomic trx)
261 // Remove the first instance of this job scanning right-to-left.
262 // This is O(N) in the worst case, but is likely to be much faster since
263 // jobs are pushed to the left and we are starting from the right, where
264 // the longest running jobs are likely to be. These should be the first
265 // jobs to be acknowledged assuming that job run times are roughly equal.
266 $conn->lRem( $this->getQueueKey( 'l-claimed' ), $item['uid'], -1 );
267 // Delete the job data and its claim metadata
269 $this->prefixWithQueueKey( 'h-meta', $item['uid'] ),
270 $this->prefixWithQueueKey( 'data', $item['uid'] ) );
271 $res = $conn->exec(); // commit (atomic trx)
273 if ( in_array( false, $res, true ) ) {
274 wfDebugLog( 'JobQueueRedis', "Could not acknowledge {$this->type} job." );
277 } catch ( RedisException
$e ) {
278 $this->throwRedisException( $this->server
, $conn, $e );
285 * @see JobQueue::doDeduplicateRootJob()
288 * @throws MWException
290 protected function doDeduplicateRootJob( Job
$job ) {
291 $params = $job->getParams();
292 if ( !isset( $params['rootJobSignature'] ) ) {
293 throw new MWException( "Cannot register root job; missing 'rootJobSignature'." );
294 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
295 throw new MWException( "Cannot register root job; missing 'rootJobTimestamp'." );
297 $key = $this->getRootJobKey( $params['rootJobSignature'] );
299 $conn = $this->getConnection();
301 $timestamp = $conn->get( $key ); // current last timestamp of this job
302 if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
303 return true; // a newer version of this root job was enqueued
305 // Update the timestamp of the last root job started at the location...
306 return $conn->set( $key, $params['rootJobTimestamp'], self
::ROOTJOB_TTL
); // 2 weeks
307 } catch ( RedisException
$e ) {
308 $this->throwRedisException( $this->server
, $conn, $e );
313 * Check if the "root" job of a given job has been superseded by a newer one
317 * @throws MWException
319 protected function isRootJobOldDuplicate( Job
$job ) {
320 $params = $job->getParams();
321 if ( !isset( $params['rootJobSignature'] ) ) {
322 return false; // job has no de-deplication info
323 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
324 wfDebugLog( 'JobQueueRedis', "Cannot check root job; missing 'rootJobTimestamp'." );
328 $conn = $this->getConnection();
330 // Get the last time this root job was enqueued
331 $timestamp = $conn->get( $this->getRootJobKey( $params['rootJobSignature'] ) );
332 } catch ( RedisException
$e ) {
333 $this->throwRedisException( $this->server
, $conn, $e );
336 // Check if a new root job was started at the location after this one's...
337 return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
341 * @see JobQueue::getAllQueuedJobs()
344 public function getAllQueuedJobs() {
345 $conn = $this->getConnection();
347 throw new MWException( "Unable to connect to redis server." );
351 return new MappedIterator(
352 $conn->lRange( $this->getQueueKey( 'l-unclaimed' ), 0, -1 ),
353 function( $uid ) use ( $that, $conn ) {
354 return $that->getJobFromUidInternal( $uid, $conn );
357 } catch ( RedisException
$e ) {
358 $this->throwRedisException( $this->server
, $conn, $e );
363 * This function should not be called outside RedisJobQueue
366 * @param $conn RedisConnRef
368 * @throws MWException
370 public function getJobFromUidInternal( $uid, RedisConnRef
$conn ) {
372 $fields = $conn->get( $this->prefixWithQueueKey( 'data', $uid ) );
373 if ( !is_array( $fields ) ) { // wtf?
374 $conn->delete( $this->prefixWithQueueKey( 'data', $uid ) );
375 throw new MWException( "Could not find job with UID '$uid'." );
377 $title = Title
::makeTitle( $fields['namespace'], $fields['title'] );
378 $job = Job
::factory( $fields['type'], $title, $fields['params'] );
379 $job->metadata
['sourceFields'] = $fields;
381 } catch ( RedisException
$e ) {
382 $this->throwRedisException( $this->server
, $conn, $e );
387 * Recycle or destroy any jobs that have been claimed for too long
389 * @return integer Number of jobs recycled/deleted
390 * @throws MWException
392 public function recycleAndDeleteStaleJobs() {
393 if ( $this->claimTTL
<= 0 ) { // sanity
394 throw new MWException( "Cannot recycle jobs since acknowledgements are disabled." );
397 // For each job item that can be retried, we need to add it back to the
398 // main queue and remove it from the list of currenty claimed job items.
399 $conn = $this->getConnection();
401 // Avoid duplicate insertions of items to be re-enqueued
402 $conn->multi( Redis
::MULTI
);
403 $conn->setnx( $this->getQueueKey( 'lock' ), 1 );
404 $conn->expire( $this->getQueueKey( 'lock' ), 3600 );
405 if ( $conn->exec() !== array( true, true ) ) { // lock
406 return $count; // already in progress
410 $claimCutoff = $now - $this->claimTTL
;
411 $pruneCutoff = $now - self
::MAX_AGE_PRUNE
;
413 // Get the list of all claimed jobs
414 $claimedUids = $conn->lRange( $this->getQueueKey( 'l-claimed' ), 0, -1 );
415 // Get a map of (uid => claim metadata) for all claimed jobs
416 $metadata = $conn->mGet( $this->prefixValuesWithQueueKey( 'h-meta', $claimedUids ) );
418 $uidsPush = array(); // items IDs to move to the "unclaimed" queue
419 $uidsRemove = array(); // item IDs to remove from "claimed" queue
420 foreach ( $claimedUids as $i => $uid ) { // all claimed items
421 $info = $metadata[$i] ?
$metadata[$i] : array();
422 if ( isset( $info['ctime'] ) ||
isset( $info['rctime'] ) ) {
423 // Prefer "ctime" (set by pop()) over "rctime" (set by this function)
424 $ctime = isset( $info['ctime'] ) ?
$info['ctime'] : $info['rctime'];
425 // Claimed job claimed for too long?
426 if ( $ctime < $claimCutoff ) {
427 // Get the number of failed attempts
428 $attempts = isset( $info['attempts'] ) ?
$info['attempts'] : 0;
429 if ( $attempts < $this->maxTries
) {
430 $uidsPush[] = $uid; // retry it
431 } elseif ( $ctime < $pruneCutoff ) {
432 $uidsRemove[] = $uid; // just remove it
436 // If pop() failed to set the claim timestamp, set it to the current time.
437 // Since that function sets this non-atomically *after* moving the job to
438 // the "claimed" queue, it may be the case that it just didn't set it yet.
439 $conn->hSet( $this->prefixWithQueueKey( 'h-meta', $uid ), 'rctime', $now );
443 $conn->multi( Redis
::MULTI
); // begin (atomic trx)
444 if ( count( $uidsPush ) ) { // move from "l-claimed" to "l-unclaimed"
445 call_user_func_array(
446 array( $conn, 'lPush' ),
447 array_merge( array( $this->getQueueKey( 'l-unclaimed' ) ), $uidsPush )
449 foreach ( $uidsPush as $uid ) {
450 $conn->lRem( $this->getQueueKey( 'l-claimed' ), $uid, -1 );
451 $conn->hDel( $this->prefixWithQueueKey( 'h-meta', $uid ), 'ctime', 'rctime' );
452 $conn->hIncrBy( $this->prefixWithQueueKey( 'h-meta', $uid ), 'attempts', 1 );
455 foreach ( $uidsRemove as $uid ) { // remove from "l-claimed"
456 $conn->lRem( $this->getQueueKey( 'l-claimed' ), $uid, -1 );
457 $conn->delete( // delete job data and metadata
458 $this->prefixWithQueueKey( 'h-meta', $uid ),
459 $this->prefixWithQueueKey( 'data', $uid ) );
461 $res = $conn->exec(); // commit (atomic trx)
463 if ( in_array( false, $res, true ) ) {
464 wfDebugLog( 'JobQueueRedis', "Could not recycle {$this->type} job(s)." );
466 $count +
= ( count( $uidsPush ) +
count( $uidsRemove ) );
467 wfIncrStats( 'job-recycle', count( $uidsPush ) );
470 $conn->delete( $this->getQueueKey( 'lock' ) ); // unlock
471 } catch ( RedisException
$e ) {
472 $this->throwRedisException( $this->server
, $conn, $e );
479 * Destroy any jobs that have been claimed
481 * @return integer Number of jobs deleted
482 * @throws MWException
484 protected function cleanupClaimedJobs() {
486 // Make sure the message for claimed jobs was deleted
487 // and remove the claimed job IDs from the "claimed" list.
488 $conn = $this->getConnection();
490 // Avoid races and duplicate effort
491 $conn->multi( Redis
::MULTI
);
492 $conn->setnx( $this->getQueueKey( 'lock' ), 1 );
493 $conn->expire( $this->getQueueKey( 'lock' ), 3600 );
494 if ( $conn->exec() !== array( true, true ) ) { // lock
495 return $count; // already in progress
497 // Get the list of all claimed jobs
498 $uids = $conn->lRange( $this->getQueueKey( 'l-claimed' ), 0, -1 );
499 if ( count( $uids ) ) {
500 // Delete the message keys and delist the corresponding ids.
501 // Since the only other changes to "l-claimed" are left pushes, we can just strip
502 // off the elements read here using a right trim based on the number of ids read.
503 $conn->multi( Redis
::MULTI
); // begin (atomic trx)
504 $conn->lTrim( $this->getQueueKey( 'l-claimed' ), 0, -count( $uids ) - 1 );
505 $conn->delete( array_merge(
506 $this->prefixValuesWithQueueKey( 'h-meta', $uids ),
507 $this->prefixValuesWithQueueKey( 'data', $uids )
509 $res = $conn->exec(); // commit (atomic trx)
511 if ( in_array( false, $res, true ) ) {
512 wfDebugLog( 'JobQueueRedis', "Could not purge {$this->type} job(s)." );
514 $count +
= count( $uids );
517 $conn->delete( $this->getQueueKey( 'lock' ) ); // unlock
518 } catch ( RedisException
$e ) {
519 $this->throwRedisException( $this->server
, $conn, $e );
528 protected function doGetPeriodicTasks() {
529 if ( $this->claimTTL
> 0 ) {
531 'recycleAndDeleteStaleJobs' => array(
532 'callback' => array( $this, 'recycleAndDeleteStaleJobs' ),
533 'period' => ceil( $this->claimTTL
/ 2 )
545 protected function getNewJobFields( Job
$job ) {
547 // Fields that describe the nature of the job
548 'type' => $job->getType(),
549 'namespace' => $job->getTitle()->getNamespace(),
550 'title' => $job->getTitle()->getDBkey(),
551 'params' => $job->getParams(),
552 // Additional metadata
553 'uid' => $job->ignoreDuplicates()
554 ?
wfBaseConvert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
555 : wfRandomString( 32 ),
556 'timestamp' => time() // UNIX timestamp
561 * @param $fields array
564 protected function getJobFromFields( array $fields ) {
565 $title = Title
::makeTitleSafe( $fields['namespace'], $fields['title'] );
567 $job = Job
::factory( $fields['type'], $title, $fields['params'] );
568 $job->metadata
['sourceFields'] = $fields;
575 * @param $uid string Job UID
576 * @return bool Whether $uid is a SHA-1 hash based identifier for de-duplication
578 protected function isHashUid( $uid ) {
579 return strlen( $uid ) == 31;
583 * Get a connection to the server that handles all sub-queues for this queue
585 * @return Array (server name, Redis instance)
586 * @throws MWException
588 protected function getConnection() {
589 $conn = $this->redisPool
->getConnection( $this->server
);
591 throw new MWException( "Unable to connect to redis server." );
597 * @param $server string
598 * @param $conn RedisConnRef
599 * @param $e RedisException
600 * @throws MWException
602 protected function throwRedisException( $server, RedisConnRef
$conn, $e ) {
603 $this->redisPool
->handleException( $server, $conn, $e );
604 throw new MWException( "Redis server error: {$e->getMessage()}\n" );
608 * @param $prop string
611 private function getQueueKey( $prop ) {
612 list( $db, $prefix ) = wfSplitWikiID( $this->wiki
);
613 if ( strlen( $this->key
) ) { // namespaced queue (for testing)
614 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type
, $this->key
, $prop );
616 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type
, $prop );
621 * @param string $signature Hash identifier of the root job
624 private function getRootJobKey( $signature ) {
625 list( $db, $prefix ) = wfSplitWikiID( $this->wiki
);
626 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type
, 'rootjob', $signature );
630 * @param $prop string
631 * @param $string string
634 private function prefixWithQueueKey( $prop, $string ) {
635 return $this->getQueueKey( $prop ) . ':' . $string;
639 * @param $prop string
640 * @param $items array
643 private function prefixValuesWithQueueKey( $prop, array $items ) {
645 foreach ( $items as $item ) {
646 $res[] = $this->prefixWithQueueKey( $prop, $item );
652 * @param $prop string
653 * @param $items array
656 private function prefixKeysWithQueueKey( $prop, array $items ) {
658 foreach ( $items as $key => $item ) {
659 $res[$this->prefixWithQueueKey( $prop, $key )] = $item;
668 public function setTestingPrefix( $key ) {