MediaWiki master
JobQueueRedis.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\JobQueue;
8
9use InvalidArgumentException;
10use LogicException;
16use Psr\Log\LoggerInterface;
17use Redis;
18use RedisException;
19use UnexpectedValueException;
22
69class JobQueueRedis extends JobQueue {
71 protected $redisPool;
73 protected $logger;
74
76 protected $server;
78 protected $compression;
79
80 private const MAX_PUSH_SIZE = 25; // avoid tying up the server
81
94 public function __construct( array $params ) {
95 parent::__construct( $params );
96 $params['redisConfig']['serializer'] = 'none'; // make it easy to use Lua
97 $this->server = $params['redisServer'];
98 $this->compression = $params['compression'] ?? 'none';
99 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
100 if ( empty( $params['daemonized'] ) ) {
101 throw new InvalidArgumentException(
102 "Non-daemonized mode is no longer supported. Please install the " .
103 "mediawiki/services/jobrunner service and update \$wgJobTypeConf as needed." );
104 }
105 $this->logger = LoggerFactory::getInstance( 'redis' );
106 }
107
109 protected function supportedOrders() {
110 return [ 'timestamp', 'fifo' ];
111 }
112
114 protected function optimalOrder() {
115 return 'fifo';
116 }
117
119 protected function supportsDelayedJobs() {
120 return true;
121 }
122
128 protected function doIsEmpty() {
129 return $this->doGetSize() == 0;
130 }
131
137 protected function doGetSize() {
138 $conn = $this->getConnection();
139 try {
140 return $conn->lLen( $this->getQueueKey( 'l-unclaimed' ) );
141 } catch ( RedisException $e ) {
142 throw $this->handleErrorAndMakeException( $conn, $e );
143 }
144 }
145
151 protected function doGetAcquiredCount() {
152 $conn = $this->getConnection();
153 try {
154 $conn->multi( Redis::PIPELINE );
155 $conn->zCard( $this->getQueueKey( 'z-claimed' ) );
156 $conn->zCard( $this->getQueueKey( 'z-abandoned' ) );
157
158 return array_sum( $conn->exec() );
159 } catch ( RedisException $e ) {
160 throw $this->handleErrorAndMakeException( $conn, $e );
161 }
162 }
163
169 protected function doGetDelayedCount() {
170 $conn = $this->getConnection();
171 try {
172 return $conn->zCard( $this->getQueueKey( 'z-delayed' ) );
173 } catch ( RedisException $e ) {
174 throw $this->handleErrorAndMakeException( $conn, $e );
175 }
176 }
177
183 protected function doGetAbandonedCount() {
184 $conn = $this->getConnection();
185 try {
186 return $conn->zCard( $this->getQueueKey( 'z-abandoned' ) );
187 } catch ( RedisException $e ) {
188 throw $this->handleErrorAndMakeException( $conn, $e );
189 }
190 }
191
199 protected function doBatchPush( array $jobs, $flags ) {
200 // Convert the jobs into field maps (de-duplicated against each other)
201 $items = []; // (job ID => job fields map)
202 foreach ( $jobs as $job ) {
203 $item = $this->getNewJobFields( $job );
204 if ( $item['sha1'] !== '' ) { // hash identifier => de-duplicate
205 $items[$item['sha1']] = $item;
206 } else {
207 $items[$item['uuid']] = $item;
208 }
209 }
210
211 if ( $items === [] ) {
212 return; // nothing to do
213 }
214
215 $conn = $this->getConnection();
216 try {
217 // Actually push the non-duplicate jobs into the queue...
218 if ( $flags & self::QOS_ATOMIC ) {
219 $batches = [ $items ]; // all or nothing
220 } else {
221 $batches = array_chunk( $items, self::MAX_PUSH_SIZE );
222 }
223 $failed = 0;
224 $pushed = 0;
225 foreach ( $batches as $itemBatch ) {
226 $added = $this->pushBlobs( $conn, $itemBatch );
227 if ( is_int( $added ) ) {
228 $pushed += $added;
229 } else {
230 $failed += count( $itemBatch );
231 }
232 }
233 $this->incrStats( 'inserts', $this->type, count( $items ) );
234 $this->incrStats( 'inserts_actual', $this->type, $pushed );
235 $this->incrStats( 'dupe_inserts', $this->type,
236 count( $items ) - $failed - $pushed );
237 if ( $failed > 0 ) {
238 $err = "Could not insert {$failed} {$this->type} job(s).";
239 wfDebugLog( 'JobQueue', $err );
240 throw new RedisException( $err );
241 }
242 } catch ( RedisException $e ) {
243 throw $this->handleErrorAndMakeException( $conn, $e );
244 }
245 }
246
253 protected function pushBlobs( RedisConnRef $conn, array $items ) {
254 $args = [ $this->encodeQueueName() ];
255 // Next args come in 4s ([id, sha1, rtime, blob [, id, sha1, rtime, blob ... ] ] )
256 foreach ( $items as $item ) {
257 $args[] = (string)$item['uuid'];
258 $args[] = (string)$item['sha1'];
259 $args[] = (string)$item['rtimestamp'];
260 $args[] = (string)$this->serialize( $item );
261 }
262 static $script =
264<<<LUA
265 local kUnclaimed, kSha1ById, kIdBySha1, kDelayed, kData, kQwJobs = unpack(KEYS)
266 -- First argument is the queue ID
267 local queueId = ARGV[1]
268 -- Next arguments all come in 4s (one per job)
269 local variadicArgCount = #ARGV - 1
270 if variadicArgCount % 4 ~= 0 then
271 return redis.error_reply('Unmatched arguments')
272 end
273 -- Insert each job into this queue as needed
274 local pushed = 0
275 for i = 2,#ARGV,4 do
276 local id,sha1,rtimestamp,blob = ARGV[i],ARGV[i+1],ARGV[i+2],ARGV[i+3]
277 if sha1 == '' or redis.call('hExists',kIdBySha1,sha1) == 0 then
278 if 1*rtimestamp > 0 then
279 -- Insert into delayed queue (release time as score)
280 redis.call('zAdd',kDelayed,rtimestamp,id)
281 else
282 -- Insert into unclaimed queue
283 redis.call('lPush',kUnclaimed,id)
284 end
285 if sha1 ~= '' then
286 redis.call('hSet',kSha1ById,id,sha1)
287 redis.call('hSet',kIdBySha1,sha1,id)
288 end
289 redis.call('hSet',kData,id,blob)
290 pushed = pushed + 1
291 end
292 end
293 if pushed > 0 then
294 -- Mark this queue as having jobs
295 redis.call('sAdd',kQwJobs,queueId)
296 end
297 return pushed
298LUA;
299 return $conn->luaEval( $script,
300 [
301 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
302 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
303 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
304 $this->getQueueKey( 'z-delayed' ), # KEYS[4]
305 $this->getQueueKey( 'h-data' ), # KEYS[5]
306 $this->getGlobalKey( 's-queuesWithJobs' ), # KEYS[6]
307 ...$args
308 ],
309 6 # number of first argument(s) that are keys
310 );
311 }
312
318 protected function doPop() {
319 $job = false;
320
321 $conn = $this->getConnection();
322 try {
323 do {
324 $blob = $this->popAndAcquireBlob( $conn );
325 if ( !is_string( $blob ) ) {
326 break; // no jobs; nothing to do
327 }
328
329 $this->incrStats( 'pops', $this->type );
330 $item = $this->unserialize( $blob );
331 if ( $item === false ) {
332 wfDebugLog( 'JobQueue', "Could not unserialize {$this->type} job." );
333 continue;
334 }
335
336 // If $item is invalid, the runner loop recycling will cleanup as needed
337 $job = $this->getJobFromFields( $item ); // may be false
338 } while ( !$job ); // job may be false if invalid
339 } catch ( RedisException $e ) {
340 throw $this->handleErrorAndMakeException( $conn, $e );
341 }
342
343 return $job;
344 }
345
351 protected function popAndAcquireBlob( RedisConnRef $conn ) {
352 static $script =
354<<<LUA
355 local kUnclaimed, kSha1ById, kIdBySha1, kClaimed, kAttempts, kData = unpack(KEYS)
356 local rTime = unpack(ARGV)
357 -- Pop an item off the queue
358 local id = redis.call('rPop',kUnclaimed)
359 if not id then
360 return false
361 end
362 -- Allow new duplicates of this job
363 local sha1 = redis.call('hGet',kSha1ById,id)
364 if sha1 then redis.call('hDel',kIdBySha1,sha1) end
365 redis.call('hDel',kSha1ById,id)
366 -- Mark the jobs as claimed and return it
367 redis.call('zAdd',kClaimed,rTime,id)
368 redis.call('hIncrBy',kAttempts,id,1)
369 return redis.call('hGet',kData,id)
370LUA;
371 return $conn->luaEval( $script,
372 [
373 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
374 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
375 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
376 $this->getQueueKey( 'z-claimed' ), # KEYS[4]
377 $this->getQueueKey( 'h-attempts' ), # KEYS[5]
378 $this->getQueueKey( 'h-data' ), # KEYS[6]
379 time(), # ARGV[1] (injected to be replication-safe)
380 ],
381 6 # number of first argument(s) that are keys
382 );
383 }
384
392 protected function doAck( RunnableJob $job ) {
393 $uuid = $job->getMetadata( 'uuid' );
394 if ( $uuid === null ) {
395 throw new UnexpectedValueException( "Job of type '{$job->getType()}' has no UUID." );
396 }
397
398 $conn = $this->getConnection();
399 try {
400 static $script =
402<<<LUA
403 local kClaimed, kAttempts, kData = unpack(KEYS)
404 local id = unpack(ARGV)
405 -- Unmark the job as claimed
406 local removed = redis.call('zRem',kClaimed,id)
407 -- Check if the job was recycled
408 if removed == 0 then
409 return 0
410 end
411 -- Delete the retry data
412 redis.call('hDel',kAttempts,id)
413 -- Delete the job data itself
414 return redis.call('hDel',kData,id)
415LUA;
416 $res = $conn->luaEval( $script,
417 [
418 $this->getQueueKey( 'z-claimed' ), # KEYS[1]
419 $this->getQueueKey( 'h-attempts' ), # KEYS[2]
420 $this->getQueueKey( 'h-data' ), # KEYS[3]
421 $uuid # ARGV[1]
422 ],
423 3 # number of first argument(s) that are keys
424 );
425
426 if ( !$res ) {
427 wfDebugLog( 'JobQueue', "Could not acknowledge {$this->type} job $uuid." );
428
429 return false;
430 }
431
432 $this->incrStats( 'acks', $this->type );
433 } catch ( RedisException $e ) {
434 throw $this->handleErrorAndMakeException( $conn, $e );
435 }
436
437 return true;
438 }
439
447 if ( !$job->hasRootJobParams() ) {
448 throw new LogicException( "Cannot register root job; missing parameters." );
449 }
450 $params = $job->getRootJobParams();
451
452 $key = $this->getRootJobCacheKey( $params['rootJobSignature'], $job->getType() );
453
454 $conn = $this->getConnection();
455 try {
456 $timestamp = $conn->get( $key ); // last known timestamp of such a root job
457 if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
458 return true; // a newer version of this root job was enqueued
459 }
460
461 // Update the timestamp of the last root job started at the location...
462 return $conn->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL ); // 2 weeks
463 } catch ( RedisException $e ) {
464 throw $this->handleErrorAndMakeException( $conn, $e );
465 }
466 }
467
475 if ( !$job->hasRootJobParams() ) {
476 return false; // job has no de-duplication info
477 }
478 $params = $job->getRootJobParams();
479
480 $conn = $this->getConnection();
481 try {
482 // Get the last time this root job was enqueued
483 $timestamp = $conn->get( $this->getRootJobCacheKey( $params['rootJobSignature'], $job->getType() ) );
484 } catch ( RedisException $e ) {
485 throw $this->handleErrorAndMakeException( $conn, $e );
486 }
487
488 // Check if a new root job was started at the location after this one's...
489 return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
490 }
491
497 protected function doDelete() {
498 static $props = [ 'l-unclaimed', 'z-claimed', 'z-abandoned',
499 'z-delayed', 'h-idBySha1', 'h-sha1ById', 'h-attempts', 'h-data' ];
500
501 $conn = $this->getConnection();
502 try {
503 $keys = [];
504 foreach ( $props as $prop ) {
505 $keys[] = $this->getQueueKey( $prop );
506 }
507
508 $ok = ( $conn->del( $keys ) !== false );
509 $conn->sRem( $this->getGlobalKey( 's-queuesWithJobs' ), $this->encodeQueueName() );
510
511 return $ok;
512 } catch ( RedisException $e ) {
513 throw $this->handleErrorAndMakeException( $conn, $e );
514 }
515 }
516
522 public function getAllQueuedJobs() {
523 $conn = $this->getConnection();
524 try {
525 $uids = $conn->lRange( $this->getQueueKey( 'l-unclaimed' ), 0, -1 );
526 } catch ( RedisException $e ) {
527 throw $this->handleErrorAndMakeException( $conn, $e );
528 }
529
530 return $this->getJobIterator( $conn, $uids );
531 }
532
538 public function getAllDelayedJobs() {
539 $conn = $this->getConnection();
540 try {
541 $uids = $conn->zRange( $this->getQueueKey( 'z-delayed' ), 0, -1 );
542 } catch ( RedisException $e ) {
543 throw $this->handleErrorAndMakeException( $conn, $e );
544 }
545
546 return $this->getJobIterator( $conn, $uids );
547 }
548
554 public function getAllAcquiredJobs() {
555 $conn = $this->getConnection();
556 try {
557 $uids = $conn->zRange( $this->getQueueKey( 'z-claimed' ), 0, -1 );
558 } catch ( RedisException $e ) {
559 throw $this->handleErrorAndMakeException( $conn, $e );
560 }
561
562 return $this->getJobIterator( $conn, $uids );
563 }
564
570 public function getAllAbandonedJobs() {
571 $conn = $this->getConnection();
572 try {
573 $uids = $conn->zRange( $this->getQueueKey( 'z-abandoned' ), 0, -1 );
574 } catch ( RedisException $e ) {
575 throw $this->handleErrorAndMakeException( $conn, $e );
576 }
577
578 return $this->getJobIterator( $conn, $uids );
579 }
580
586 protected function getJobIterator( RedisConnRef $conn, array $uids ) {
587 return new MappedIterator(
588 $uids,
589 function ( $uid ) use ( $conn ) {
590 return $this->getJobFromUidInternal( $uid, $conn );
591 },
592 [ 'accept' => static function ( $job ) {
593 return is_object( $job );
594 } ]
595 );
596 }
597
599 public function getCoalesceLocationInternal() {
600 return "RedisServer:" . $this->server;
601 }
602
604 protected function doGetSiblingQueuesWithJobs( array $types ) {
605 return array_keys( array_filter( $this->doGetSiblingQueueSizes( $types ) ) );
606 }
607
609 protected function doGetSiblingQueueSizes( array $types ) {
610 $sizes = []; // (type => size)
611 $types = array_values( $types ); // reindex
612 $conn = $this->getConnection();
613 try {
614 $conn->multi( Redis::PIPELINE );
615 foreach ( $types as $type ) {
616 $conn->lLen( $this->getQueueKey( 'l-unclaimed', $type ) );
617 }
618 $res = $conn->exec();
619 if ( is_array( $res ) ) {
620 foreach ( $res as $i => $size ) {
621 $sizes[$types[$i]] = $size;
622 }
623 }
624 } catch ( RedisException $e ) {
625 throw $this->handleErrorAndMakeException( $conn, $e );
626 }
627
628 return $sizes;
629 }
630
640 public function getJobFromUidInternal( $uid, $conn ) {
641 try {
642 $data = $conn->hGet( $this->getQueueKey( 'h-data' ), $uid );
643 if ( $data === false ) {
644 return false; // not found
645 }
646 $item = $this->unserialize( $data );
647 if ( !is_array( $item ) ) { // this shouldn't happen
648 throw new UnexpectedValueException( "Could not unserialize job with ID '$uid'." );
649 }
650
651 $params = $item['params'];
652 $params += [ 'namespace' => $item['namespace'], 'title' => $item['title'] ];
653 $job = $this->factoryJob( $item['type'], $params );
654 $job->setMetadata( 'uuid', $item['uuid'] );
655 $job->setMetadata( 'timestamp', $item['timestamp'] );
656 // Add in attempt count for debugging at showJobs.php
657 $job->setMetadata( 'attempts',
658 $conn->hGet( $this->getQueueKey( 'h-attempts' ), $uid ) );
659
660 return $job;
661 } catch ( RedisException $e ) {
662 throw $this->handleErrorAndMakeException( $conn, $e );
663 }
664 }
665
671 public function getServerQueuesWithJobs() {
672 $queues = [];
673
674 $conn = $this->getConnection();
675 try {
676 $set = $conn->sMembers( $this->getGlobalKey( 's-queuesWithJobs' ) );
677 foreach ( $set as $queue ) {
678 $queues[] = $this->decodeQueueName( $queue );
679 }
680 } catch ( RedisException $e ) {
681 throw $this->handleErrorAndMakeException( $conn, $e );
682 }
683
684 return $queues;
685 }
686
691 protected function getNewJobFields( IJobSpecification $job ) {
692 return [
693 // Fields that describe the nature of the job
694 'type' => $job->getType(),
695 'namespace' => $job->getParams()['namespace'] ?? NS_SPECIAL,
696 'title' => $job->getParams()['title'] ?? '',
697 'params' => $job->getParams(),
698 // Some jobs cannot run until a "release timestamp"
699 'rtimestamp' => $job->getReleaseTimestamp() ?: 0,
700 // Additional job metadata
701 'uuid' => $this->idGenerator->newRawUUIDv4(),
702 'sha1' => $job->ignoreDuplicates()
703 ? \Wikimedia\base_convert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
704 : '',
705 'timestamp' => time() // UNIX timestamp
706 ];
707 }
708
713 protected function getJobFromFields( array $fields ) {
714 $params = $fields['params'];
715 $params += [ 'namespace' => $fields['namespace'], 'title' => $fields['title'] ];
716
717 $job = $this->factoryJob( $fields['type'], $params );
718 $job->setMetadata( 'uuid', $fields['uuid'] );
719 $job->setMetadata( 'timestamp', $fields['timestamp'] );
720
721 return $job;
722 }
723
728 protected function serialize( array $fields ) {
729 $blob = serialize( $fields );
730 if ( $this->compression === 'gzip'
731 && strlen( $blob ) >= 1024
732 && function_exists( 'gzdeflate' )
733 ) {
734 $object = (object)[ 'blob' => gzdeflate( $blob ), 'enc' => 'gzip' ];
735 $blobz = serialize( $object );
736
737 return ( strlen( $blobz ) < strlen( $blob ) ) ? $blobz : $blob;
738 } else {
739 return $blob;
740 }
741 }
742
747 protected function unserialize( $blob ) {
748 $fields = unserialize( $blob );
749 if ( is_object( $fields ) ) {
750 if ( $fields->enc === 'gzip' && function_exists( 'gzinflate' ) ) {
751 $fields = unserialize( gzinflate( $fields->blob ) );
752 } else {
753 $fields = false;
754 }
755 }
756
757 return is_array( $fields ) ? $fields : false;
758 }
759
766 protected function getConnection() {
767 $conn = $this->redisPool->getConnection( $this->server, $this->logger );
768 if ( !$conn ) {
769 throw new JobQueueConnectionError(
770 "Unable to connect to redis server {$this->server}." );
771 }
772
773 return $conn;
774 }
775
781 protected function handleErrorAndMakeException( RedisConnRef $conn, $e ) {
782 $this->redisPool->handleError( $conn, $e );
783 return new JobQueueError( "Redis server error: {$e->getMessage()}\n" );
784 }
785
789 private function encodeQueueName() {
790 return json_encode( [ $this->type, $this->domain ] );
791 }
792
797 private function decodeQueueName( $name ) {
798 return json_decode( $name );
799 }
800
805 private function getGlobalKey( $name ) {
806 $parts = [ 'global', 'jobqueue', $name ];
807 foreach ( $parts as $part ) {
808 if ( !preg_match( '/[a-zA-Z0-9_-]+/', $part ) ) {
809 throw new InvalidArgumentException( "Key part characters are out of range." );
810 }
811 }
812
813 return implode( ':', $parts );
814 }
815
821 private function getQueueKey( $prop, $type = null ) {
822 $type = is_string( $type ) ? $type : $this->type;
823
824 // Use wiki ID for b/c
825 $keyspace = WikiMap::getWikiIdFromDbDomain( $this->domain );
826
827 $parts = [ $keyspace, 'jobqueue', $type, $prop ];
828
829 // Parts are typically ASCII, but encode to escape ":"
830 return implode( ':', array_map( 'rawurlencode', $parts ) );
831 }
832}
833
835class_alias( JobQueueRedis::class, 'JobQueueRedis' );
const NS_SPECIAL
Definition Defines.php:40
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Convenience class for generating iterators from iterators.
Redis-backed job queue storage.
handleErrorAndMakeException(RedisConnRef $conn, $e)
doGetSiblingQueueSizes(array $types)
to override JobQueue::getSiblingQueuesSize() array|null (list of queue types) or null if unsupported
pushBlobs(RedisConnRef $conn, array $items)
string $server
Server address.
optimalOrder()
Get the default queue order to use if configuration does not specify one.string One of (random,...
popAndAcquireBlob(RedisConnRef $conn)
getJobIterator(RedisConnRef $conn, array $uids)
doIsRootJobOldDuplicate(IJobSpecification $job)
string $compression
Compression method to use.
doGetSiblingQueuesWithJobs(array $types)
to override JobQueue::getSiblingQueuesWithJobs() array|null (list of queue types) or null if unsuppor...
getConnection()
Get a connection to the server that handles all sub-queues for this queue.
supportedOrders()
Get the allowed queue orders for configuration validation.array Subset of (random,...
doDeduplicateRootJob(IJobSpecification $job)
supportsDelayedJobs()
Find out if delayed jobs are supported for configuration validation.to override bool Whether delayed ...
getNewJobFields(IJobSpecification $job)
getJobFromUidInternal( $uid, $conn)
This function should not be called outside JobQueueRedis.
getCoalesceLocationInternal()
Do not use this function outside of JobQueue/JobQueueGroup.to override string|null 1....
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:36
incrStats( $event, $type, $delta=1)
Call StatsFactory::incrementBy() for the queue overall and for the queue type.
Definition JobQueue.php:770
string $type
Job type.
Definition JobQueue.php:40
factoryJob( $command, $params)
Definition JobQueue.php:736
getRootJobCacheKey( $signature, $type)
Definition JobQueue.php:554
Create PSR-3 logger objects.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:19
Wrapper class for Redis connections that automatically reuses connections (via RAII pattern)
luaEval( $script, array $params, $numKeys)
Manage one or more Redis client connection.
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