MediaWiki master
JobQueueRedis.php
Go to the documentation of this file.
1<?php
23use Psr\Log\LoggerInterface;
24
71class JobQueueRedis extends JobQueue {
73 protected $redisPool;
75 protected $logger;
76
78 protected $server;
80 protected $compression;
81
82 private const MAX_PUSH_SIZE = 25; // avoid tying up the server
83
97 public function __construct( array $params ) {
98 parent::__construct( $params );
99 $params['redisConfig']['serializer'] = 'none'; // make it easy to use Lua
100 $this->server = $params['redisServer'];
101 $this->compression = $params['compression'] ?? 'none';
102 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
103 if ( empty( $params['daemonized'] ) ) {
104 throw new InvalidArgumentException(
105 "Non-daemonized mode is no longer supported. Please install the " .
106 "mediawiki/services/jobrunner service and update \$wgJobTypeConf as needed." );
107 }
108 $this->logger = LoggerFactory::getInstance( 'redis' );
109 }
110
111 protected function supportedOrders() {
112 return [ 'timestamp', 'fifo' ];
113 }
114
115 protected function optimalOrder() {
116 return 'fifo';
117 }
118
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 ( strlen( $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 -- Mark this queue as having jobs
294 redis.call('sAdd',kQwJobs,queueId)
295 return pushed
296LUA;
297 return $conn->luaEval( $script,
298 array_merge(
299 [
300 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
301 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
302 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
303 $this->getQueueKey( 'z-delayed' ), # KEYS[4]
304 $this->getQueueKey( 'h-data' ), # KEYS[5]
305 $this->getGlobalKey( 's-queuesWithJobs' ), # KEYS[6]
306 ],
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
448 if ( !$job->hasRootJobParams() ) {
449 throw new LogicException( "Cannot register root job; missing parameters." );
450 }
451 $params = $job->getRootJobParams();
452
453 $key = $this->getRootJobCacheKey( $params['rootJobSignature'], $job->getType() );
454
455 $conn = $this->getConnection();
456 try {
457 $timestamp = $conn->get( $key ); // last known timestamp of such a root job
458 if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
459 return true; // a newer version of this root job was enqueued
460 }
461
462 // Update the timestamp of the last root job started at the location...
463 return $conn->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL ); // 2 weeks
464 } catch ( RedisException $e ) {
465 throw $this->handleErrorAndMakeException( $conn, $e );
466 }
467 }
468
476 if ( !$job->hasRootJobParams() ) {
477 return false; // job has no de-duplication info
478 }
479 $params = $job->getRootJobParams();
480
481 $conn = $this->getConnection();
482 try {
483 // Get the last time this root job was enqueued
484 $timestamp = $conn->get( $this->getRootJobCacheKey( $params['rootJobSignature'], $job->getType() ) );
485 } catch ( RedisException $e ) {
486 throw $this->handleErrorAndMakeException( $conn, $e );
487 }
488
489 // Check if a new root job was started at the location after this one's...
490 return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
491 }
492
498 protected function doDelete() {
499 static $props = [ 'l-unclaimed', 'z-claimed', 'z-abandoned',
500 'z-delayed', 'h-idBySha1', 'h-sha1ById', 'h-attempts', 'h-data' ];
501
502 $conn = $this->getConnection();
503 try {
504 $keys = [];
505 foreach ( $props as $prop ) {
506 $keys[] = $this->getQueueKey( $prop );
507 }
508
509 $ok = ( $conn->del( $keys ) !== false );
510 $conn->sRem( $this->getGlobalKey( 's-queuesWithJobs' ), $this->encodeQueueName() );
511
512 return $ok;
513 } catch ( RedisException $e ) {
514 throw $this->handleErrorAndMakeException( $conn, $e );
515 }
516 }
517
523 public function getAllQueuedJobs() {
524 $conn = $this->getConnection();
525 try {
526 $uids = $conn->lRange( $this->getQueueKey( 'l-unclaimed' ), 0, -1 );
527 } catch ( RedisException $e ) {
528 throw $this->handleErrorAndMakeException( $conn, $e );
529 }
530
531 return $this->getJobIterator( $conn, $uids );
532 }
533
539 public function getAllDelayedJobs() {
540 $conn = $this->getConnection();
541 try {
542 $uids = $conn->zRange( $this->getQueueKey( 'z-delayed' ), 0, -1 );
543 } catch ( RedisException $e ) {
544 throw $this->handleErrorAndMakeException( $conn, $e );
545 }
546
547 return $this->getJobIterator( $conn, $uids );
548 }
549
555 public function getAllAcquiredJobs() {
556 $conn = $this->getConnection();
557 try {
558 $uids = $conn->zRange( $this->getQueueKey( 'z-claimed' ), 0, -1 );
559 } catch ( RedisException $e ) {
560 throw $this->handleErrorAndMakeException( $conn, $e );
561 }
562
563 return $this->getJobIterator( $conn, $uids );
564 }
565
571 public function getAllAbandonedJobs() {
572 $conn = $this->getConnection();
573 try {
574 $uids = $conn->zRange( $this->getQueueKey( 'z-abandoned' ), 0, -1 );
575 } catch ( RedisException $e ) {
576 throw $this->handleErrorAndMakeException( $conn, $e );
577 }
578
579 return $this->getJobIterator( $conn, $uids );
580 }
581
587 protected function getJobIterator( RedisConnRef $conn, array $uids ) {
588 return new MappedIterator(
589 $uids,
590 function ( $uid ) use ( $conn ) {
591 return $this->getJobFromUidInternal( $uid, $conn );
592 },
593 [ 'accept' => static function ( $job ) {
594 return is_object( $job );
595 } ]
596 );
597 }
598
599 public function getCoalesceLocationInternal() {
600 return "RedisServer:" . $this->server;
601 }
602
603 protected function doGetSiblingQueuesWithJobs( array $types ) {
604 return array_keys( array_filter( $this->doGetSiblingQueueSizes( $types ) ) );
605 }
606
607 protected function doGetSiblingQueueSizes( array $types ) {
608 $sizes = []; // (type => size)
609 $types = array_values( $types ); // reindex
610 $conn = $this->getConnection();
611 try {
612 $conn->multi( Redis::PIPELINE );
613 foreach ( $types as $type ) {
614 $conn->lLen( $this->getQueueKey( 'l-unclaimed', $type ) );
615 }
616 $res = $conn->exec();
617 if ( is_array( $res ) ) {
618 foreach ( $res as $i => $size ) {
619 $sizes[$types[$i]] = $size;
620 }
621 }
622 } catch ( RedisException $e ) {
623 throw $this->handleErrorAndMakeException( $conn, $e );
624 }
625
626 return $sizes;
627 }
628
638 public function getJobFromUidInternal( $uid, $conn ) {
639 try {
640 $data = $conn->hGet( $this->getQueueKey( 'h-data' ), $uid );
641 if ( $data === false ) {
642 return false; // not found
643 }
644 $item = $this->unserialize( $data );
645 if ( !is_array( $item ) ) { // this shouldn't happen
646 throw new UnexpectedValueException( "Could not unserialize job with ID '$uid'." );
647 }
648
649 $params = $item['params'];
650 $params += [ 'namespace' => $item['namespace'], 'title' => $item['title'] ];
651 $job = $this->factoryJob( $item['type'], $params );
652 $job->setMetadata( 'uuid', $item['uuid'] );
653 $job->setMetadata( 'timestamp', $item['timestamp'] );
654 // Add in attempt count for debugging at showJobs.php
655 $job->setMetadata( 'attempts',
656 $conn->hGet( $this->getQueueKey( 'h-attempts' ), $uid ) );
657
658 return $job;
659 } catch ( RedisException $e ) {
660 throw $this->handleErrorAndMakeException( $conn, $e );
661 }
662 }
663
669 public function getServerQueuesWithJobs() {
670 $queues = [];
671
672 $conn = $this->getConnection();
673 try {
674 $set = $conn->sMembers( $this->getGlobalKey( 's-queuesWithJobs' ) );
675 foreach ( $set as $queue ) {
676 $queues[] = $this->decodeQueueName( $queue );
677 }
678 } catch ( RedisException $e ) {
679 throw $this->handleErrorAndMakeException( $conn, $e );
680 }
681
682 return $queues;
683 }
684
689 protected function getNewJobFields( IJobSpecification $job ) {
690 return [
691 // Fields that describe the nature of the job
692 'type' => $job->getType(),
693 'namespace' => $job->getParams()['namespace'] ?? NS_SPECIAL,
694 'title' => $job->getParams()['title'] ?? '',
695 'params' => $job->getParams(),
696 // Some jobs cannot run until a "release timestamp"
697 'rtimestamp' => $job->getReleaseTimestamp() ?: 0,
698 // Additional job metadata
699 'uuid' => $this->idGenerator->newRawUUIDv4(),
700 'sha1' => $job->ignoreDuplicates()
701 ? Wikimedia\base_convert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
702 : '',
703 'timestamp' => time() // UNIX timestamp
704 ];
705 }
706
711 protected function getJobFromFields( array $fields ) {
712 $params = $fields['params'];
713 $params += [ 'namespace' => $fields['namespace'], 'title' => $fields['title'] ];
714
715 $job = $this->factoryJob( $fields['type'], $params );
716 $job->setMetadata( 'uuid', $fields['uuid'] );
717 $job->setMetadata( 'timestamp', $fields['timestamp'] );
718
719 return $job;
720 }
721
726 protected function serialize( array $fields ) {
727 $blob = serialize( $fields );
728 if ( $this->compression === 'gzip'
729 && strlen( $blob ) >= 1024
730 && function_exists( 'gzdeflate' )
731 ) {
732 $object = (object)[ 'blob' => gzdeflate( $blob ), 'enc' => 'gzip' ];
733 $blobz = serialize( $object );
734
735 return ( strlen( $blobz ) < strlen( $blob ) ) ? $blobz : $blob;
736 } else {
737 return $blob;
738 }
739 }
740
745 protected function unserialize( $blob ) {
746 $fields = unserialize( $blob );
747 if ( is_object( $fields ) ) {
748 if ( $fields->enc === 'gzip' && function_exists( 'gzinflate' ) ) {
749 $fields = unserialize( gzinflate( $fields->blob ) );
750 } else {
751 $fields = false;
752 }
753 }
754
755 return is_array( $fields ) ? $fields : false;
756 }
757
764 protected function getConnection() {
765 $conn = $this->redisPool->getConnection( $this->server, $this->logger );
766 if ( !$conn ) {
767 throw new JobQueueConnectionError(
768 "Unable to connect to redis server {$this->server}." );
769 }
770
771 return $conn;
772 }
773
779 protected function handleErrorAndMakeException( RedisConnRef $conn, $e ) {
780 $this->redisPool->handleError( $conn, $e );
781 return new JobQueueError( "Redis server error: {$e->getMessage()}\n" );
782 }
783
787 private function encodeQueueName() {
788 return json_encode( [ $this->type, $this->domain ] );
789 }
790
795 private function decodeQueueName( $name ) {
796 return json_decode( $name );
797 }
798
803 private function getGlobalKey( $name ) {
804 $parts = [ 'global', 'jobqueue', $name ];
805 foreach ( $parts as $part ) {
806 if ( !preg_match( '/[a-zA-Z0-9_-]+/', $part ) ) {
807 throw new InvalidArgumentException( "Key part characters are out of range." );
808 }
809 }
810
811 return implode( ':', $parts );
812 }
813
819 private function getQueueKey( $prop, $type = null ) {
820 $type = is_string( $type ) ? $type : $this->type;
821
822 // Use wiki ID for b/c
823 $keyspace = WikiMap::getWikiIdFromDbDomain( $this->domain );
824
825 $parts = [ $keyspace, 'jobqueue', $type, $prop ];
826
827 // Parts are typically ASCII, but encode to escape ":"
828 return implode( ':', array_map( 'rawurlencode', $parts ) );
829 }
830}
const NS_SPECIAL
Definition Defines.php:53
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.
Redis-backed job queue storage.
doDeduplicateRootJob(IJobSpecification $job)
__construct(array $params)
popAndAcquireBlob(RedisConnRef $conn)
doAck(RunnableJob $job)
getCoalesceLocationInternal()
Do not use this function outside of JobQueue/JobQueueGroup.
getJobFromUidInternal( $uid, $conn)
This function should not be called outside JobQueueRedis.
doGetSiblingQueuesWithJobs(array $types)
doGetSiblingQueueSizes(array $types)
RedisConnectionPool $redisPool
pushBlobs(RedisConnRef $conn, array $items)
string $server
Server address.
supportedOrders()
Get the allowed queue orders for configuration validation.
supportsDelayedJobs()
Find out if delayed jobs are supported for configuration validation.
string $compression
Compression method to use.
LoggerInterface $logger
serialize(array $fields)
doIsRootJobOldDuplicate(IJobSpecification $job)
doBatchPush(array $jobs, $flags)
getJobIterator(RedisConnRef $conn, array $uids)
getConnection()
Get a connection to the server that handles all sub-queues for this queue.
getJobFromFields(array $fields)
optimalOrder()
Get the default queue order to use if configuration does not specify one.
handleErrorAndMakeException(RedisConnRef $conn, $e)
getNewJobFields(IJobSpecification $job)
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:43
incrStats( $key, $type, $delta=1)
Call StatsdDataFactoryInterface::updateCount() for the queue overall and for the queue type.
Definition JobQueue.php:777
string $type
Job type.
Definition JobQueue.php:47
factoryJob( $command, $params)
Definition JobQueue.php:743
getRootJobCacheKey( $signature, $type)
Definition JobQueue.php:561
Convenience class for generating iterators from iterators.
Create PSR-3 logger objects.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Helper class to handle automatically marking connections as reusable (via RAII pattern)
luaEval( $script, array $params, $numKeys)
Helper class to manage Redis connections.
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