MediaWiki master
JobQueueRedis.php
Go to the documentation of this file.
1<?php
23use Psr\Log\LoggerInterface;
26
73class JobQueueRedis extends JobQueue {
75 protected $redisPool;
77 protected $logger;
78
80 protected $server;
82 protected $compression;
83
84 private const MAX_PUSH_SIZE = 25; // avoid tying up the server
85
98 public function __construct( array $params ) {
99 parent::__construct( $params );
100 $params['redisConfig']['serializer'] = 'none'; // make it easy to use Lua
101 $this->server = $params['redisServer'];
102 $this->compression = $params['compression'] ?? 'none';
103 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
104 if ( empty( $params['daemonized'] ) ) {
105 throw new InvalidArgumentException(
106 "Non-daemonized mode is no longer supported. Please install the " .
107 "mediawiki/services/jobrunner service and update \$wgJobTypeConf as needed." );
108 }
109 $this->logger = LoggerFactory::getInstance( 'redis' );
110 }
111
112 protected function supportedOrders() {
113 return [ 'timestamp', 'fifo' ];
114 }
115
116 protected function optimalOrder() {
117 return 'fifo';
118 }
119
120 protected function supportsDelayedJobs() {
121 return true;
122 }
123
129 protected function doIsEmpty() {
130 return $this->doGetSize() == 0;
131 }
132
138 protected function doGetSize() {
139 $conn = $this->getConnection();
140 try {
141 return $conn->lLen( $this->getQueueKey( 'l-unclaimed' ) );
142 } catch ( RedisException $e ) {
143 throw $this->handleErrorAndMakeException( $conn, $e );
144 }
145 }
146
152 protected function doGetAcquiredCount() {
153 $conn = $this->getConnection();
154 try {
155 $conn->multi( Redis::PIPELINE );
156 $conn->zCard( $this->getQueueKey( 'z-claimed' ) );
157 $conn->zCard( $this->getQueueKey( 'z-abandoned' ) );
158
159 return array_sum( $conn->exec() );
160 } catch ( RedisException $e ) {
161 throw $this->handleErrorAndMakeException( $conn, $e );
162 }
163 }
164
170 protected function doGetDelayedCount() {
171 $conn = $this->getConnection();
172 try {
173 return $conn->zCard( $this->getQueueKey( 'z-delayed' ) );
174 } catch ( RedisException $e ) {
175 throw $this->handleErrorAndMakeException( $conn, $e );
176 }
177 }
178
184 protected function doGetAbandonedCount() {
185 $conn = $this->getConnection();
186 try {
187 return $conn->zCard( $this->getQueueKey( 'z-abandoned' ) );
188 } catch ( RedisException $e ) {
189 throw $this->handleErrorAndMakeException( $conn, $e );
190 }
191 }
192
200 protected function doBatchPush( array $jobs, $flags ) {
201 // Convert the jobs into field maps (de-duplicated against each other)
202 $items = []; // (job ID => job fields map)
203 foreach ( $jobs as $job ) {
204 $item = $this->getNewJobFields( $job );
205 if ( strlen( $item['sha1'] ) ) { // hash identifier => de-duplicate
206 $items[$item['sha1']] = $item;
207 } else {
208 $items[$item['uuid']] = $item;
209 }
210 }
211
212 if ( $items === [] ) {
213 return; // nothing to do
214 }
215
216 $conn = $this->getConnection();
217 try {
218 // Actually push the non-duplicate jobs into the queue...
219 if ( $flags & self::QOS_ATOMIC ) {
220 $batches = [ $items ]; // all or nothing
221 } else {
222 $batches = array_chunk( $items, self::MAX_PUSH_SIZE );
223 }
224 $failed = 0;
225 $pushed = 0;
226 foreach ( $batches as $itemBatch ) {
227 $added = $this->pushBlobs( $conn, $itemBatch );
228 if ( is_int( $added ) ) {
229 $pushed += $added;
230 } else {
231 $failed += count( $itemBatch );
232 }
233 }
234 $this->incrStats( 'inserts', $this->type, count( $items ) );
235 $this->incrStats( 'inserts_actual', $this->type, $pushed );
236 $this->incrStats( 'dupe_inserts', $this->type,
237 count( $items ) - $failed - $pushed );
238 if ( $failed > 0 ) {
239 $err = "Could not insert {$failed} {$this->type} job(s).";
240 wfDebugLog( 'JobQueue', $err );
241 throw new RedisException( $err );
242 }
243 } catch ( RedisException $e ) {
244 throw $this->handleErrorAndMakeException( $conn, $e );
245 }
246 }
247
254 protected function pushBlobs( RedisConnRef $conn, array $items ) {
255 $args = [ $this->encodeQueueName() ];
256 // Next args come in 4s ([id, sha1, rtime, blob [, id, sha1, rtime, blob ... ] ] )
257 foreach ( $items as $item ) {
258 $args[] = (string)$item['uuid'];
259 $args[] = (string)$item['sha1'];
260 $args[] = (string)$item['rtimestamp'];
261 $args[] = (string)$this->serialize( $item );
262 }
263 static $script =
265<<<LUA
266 local kUnclaimed, kSha1ById, kIdBySha1, kDelayed, kData, kQwJobs = unpack(KEYS)
267 -- First argument is the queue ID
268 local queueId = ARGV[1]
269 -- Next arguments all come in 4s (one per job)
270 local variadicArgCount = #ARGV - 1
271 if variadicArgCount % 4 ~= 0 then
272 return redis.error_reply('Unmatched arguments')
273 end
274 -- Insert each job into this queue as needed
275 local pushed = 0
276 for i = 2,#ARGV,4 do
277 local id,sha1,rtimestamp,blob = ARGV[i],ARGV[i+1],ARGV[i+2],ARGV[i+3]
278 if sha1 == '' or redis.call('hExists',kIdBySha1,sha1) == 0 then
279 if 1*rtimestamp > 0 then
280 -- Insert into delayed queue (release time as score)
281 redis.call('zAdd',kDelayed,rtimestamp,id)
282 else
283 -- Insert into unclaimed queue
284 redis.call('lPush',kUnclaimed,id)
285 end
286 if sha1 ~= '' then
287 redis.call('hSet',kSha1ById,id,sha1)
288 redis.call('hSet',kIdBySha1,sha1,id)
289 end
290 redis.call('hSet',kData,id,blob)
291 pushed = pushed + 1
292 end
293 end
294 -- Mark this queue as having jobs
295 redis.call('sAdd',kQwJobs,queueId)
296 return pushed
297LUA;
298 return $conn->luaEval( $script,
299 array_merge(
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 ],
308 $args
309 ),
310 6 # number of first argument(s) that are keys
311 );
312 }
313
319 protected function doPop() {
320 $job = false;
321
322 $conn = $this->getConnection();
323 try {
324 do {
325 $blob = $this->popAndAcquireBlob( $conn );
326 if ( !is_string( $blob ) ) {
327 break; // no jobs; nothing to do
328 }
329
330 $this->incrStats( 'pops', $this->type );
331 $item = $this->unserialize( $blob );
332 if ( $item === false ) {
333 wfDebugLog( 'JobQueue', "Could not unserialize {$this->type} job." );
334 continue;
335 }
336
337 // If $item is invalid, the runner loop recycling will cleanup as needed
338 $job = $this->getJobFromFields( $item ); // may be false
339 } while ( !$job ); // job may be false if invalid
340 } catch ( RedisException $e ) {
341 throw $this->handleErrorAndMakeException( $conn, $e );
342 }
343
344 return $job;
345 }
346
352 protected function popAndAcquireBlob( RedisConnRef $conn ) {
353 static $script =
355<<<LUA
356 local kUnclaimed, kSha1ById, kIdBySha1, kClaimed, kAttempts, kData = unpack(KEYS)
357 local rTime = unpack(ARGV)
358 -- Pop an item off the queue
359 local id = redis.call('rPop',kUnclaimed)
360 if not id then
361 return false
362 end
363 -- Allow new duplicates of this job
364 local sha1 = redis.call('hGet',kSha1ById,id)
365 if sha1 then redis.call('hDel',kIdBySha1,sha1) end
366 redis.call('hDel',kSha1ById,id)
367 -- Mark the jobs as claimed and return it
368 redis.call('zAdd',kClaimed,rTime,id)
369 redis.call('hIncrBy',kAttempts,id,1)
370 return redis.call('hGet',kData,id)
371LUA;
372 return $conn->luaEval( $script,
373 [
374 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
375 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
376 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
377 $this->getQueueKey( 'z-claimed' ), # KEYS[4]
378 $this->getQueueKey( 'h-attempts' ), # KEYS[5]
379 $this->getQueueKey( 'h-data' ), # KEYS[6]
380 time(), # ARGV[1] (injected to be replication-safe)
381 ],
382 6 # number of first argument(s) that are keys
383 );
384 }
385
393 protected function doAck( RunnableJob $job ) {
394 $uuid = $job->getMetadata( 'uuid' );
395 if ( $uuid === null ) {
396 throw new UnexpectedValueException( "Job of type '{$job->getType()}' has no UUID." );
397 }
398
399 $conn = $this->getConnection();
400 try {
401 static $script =
403<<<LUA
404 local kClaimed, kAttempts, kData = unpack(KEYS)
405 local id = unpack(ARGV)
406 -- Unmark the job as claimed
407 local removed = redis.call('zRem',kClaimed,id)
408 -- Check if the job was recycled
409 if removed == 0 then
410 return 0
411 end
412 -- Delete the retry data
413 redis.call('hDel',kAttempts,id)
414 -- Delete the job data itself
415 return redis.call('hDel',kData,id)
416LUA;
417 $res = $conn->luaEval( $script,
418 [
419 $this->getQueueKey( 'z-claimed' ), # KEYS[1]
420 $this->getQueueKey( 'h-attempts' ), # KEYS[2]
421 $this->getQueueKey( 'h-data' ), # KEYS[3]
422 $uuid # ARGV[1]
423 ],
424 3 # number of first argument(s) that are keys
425 );
426
427 if ( !$res ) {
428 wfDebugLog( 'JobQueue', "Could not acknowledge {$this->type} job $uuid." );
429
430 return false;
431 }
432
433 $this->incrStats( 'acks', $this->type );
434 } catch ( RedisException $e ) {
435 throw $this->handleErrorAndMakeException( $conn, $e );
436 }
437
438 return true;
439 }
440
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:54
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:45
incrStats( $key, $type, $delta=1)
Call StatsdDataFactoryInterface::updateCount() for the queue overall and for the queue type.
Definition JobQueue.php:779
string $type
Job type.
Definition JobQueue.php:49
factoryJob( $command, $params)
Definition JobQueue.php:745
getRootJobCacheKey( $signature, $type)
Definition JobQueue.php:563
Convenience class for generating iterators from iterators.
Create PSR-3 logger objects.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
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