Merge "Make TransactionProfiler show the actual query times"
[lhc/web/wiklou.git] / includes / jobqueue / JobQueueRedis.php
1 <?php
2 /**
3 * Redis-backed job queue code.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle job queues stored in Redis
26 *
27 * This is a faster and less resource-intensive job queue than JobQueueDB.
28 * All data for a queue using this class is placed into one redis server.
29 *
30 * There are eight main redis keys used to track jobs:
31 * - l-unclaimed : A list of job IDs used for ready unclaimed jobs
32 * - z-claimed : A sorted set of (job ID, UNIX timestamp as score) used for job retries
33 * - z-abandoned : A sorted set of (job ID, UNIX timestamp as score) used for broken jobs
34 * - z-delayed : A sorted set of (job ID, UNIX timestamp as score) used for delayed jobs
35 * - h-idBySha1 : A hash of (SHA1 => job ID) for unclaimed jobs used for de-duplication
36 * - h-sha1ById : A hash of (job ID => SHA1) for unclaimed jobs used for de-duplication
37 * - h-attempts : A hash of (job ID => attempt count) used for job claiming/retries
38 * - h-data : A hash of (job ID => serialized blobs) for job storage
39 * A job ID can be in only one of z-delayed, l-unclaimed, z-claimed, and z-abandoned.
40 * If an ID appears in any of those lists, it should have a h-data entry for its ID.
41 * If a job has a SHA1 de-duplication value and its ID is in l-unclaimed or z-delayed, then
42 * there should be no other such jobs with that SHA1. Every h-idBySha1 entry has an h-sha1ById
43 * entry and every h-sha1ById must refer to an ID that is l-unclaimed. If a job has its
44 * ID in z-claimed or z-abandoned, then it must also have an h-attempts entry for its ID.
45 *
46 * Additionally, "rootjob:* keys track "root jobs" used for additional de-duplication.
47 * Aside from root job keys, all keys have no expiry, and are only removed when jobs are run.
48 * All the keys are prefixed with the relevant wiki ID information.
49 *
50 * This class requires Redis 2.6 as it makes use Lua scripts for fast atomic operations.
51 * Additionally, it should be noted that redis has different persistence modes, such
52 * as rdb snapshots, journaling, and no persistence. Appropriate configuration should be
53 * made on the servers based on what queues are using it and what tolerance they have.
54 *
55 * @ingroup JobQueue
56 * @ingroup Redis
57 * @since 1.22
58 */
59 class JobQueueRedis extends JobQueue {
60 /** @var RedisConnectionPool */
61 protected $redisPool;
62
63 /** @var string Server address */
64 protected $server;
65 /** @var string Compression method to use */
66 protected $compression;
67
68 const MAX_AGE_PRUNE = 604800; // integer; seconds a job can live once claimed (7 days)
69
70 /** @var string Key to prefix the queue keys with (used for testing) */
71 protected $key;
72
73 /**
74 * @param array $params Possible keys:
75 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
76 * Note that the serializer option is ignored as "none" is always used.
77 * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
78 * If a hostname is specified but no port, the standard port number
79 * 6379 will be used. Required.
80 * - compression : The type of compression to use; one of (none,gzip).
81 * - daemonized : Set to true if the redisJobRunnerService runs in the background.
82 * This will disable job recycling/undelaying from the MediaWiki side
83 * to avoid redundance and out-of-sync configuration.
84 * @throws InvalidArgumentException
85 */
86 public function __construct( array $params ) {
87 parent::__construct( $params );
88 $params['redisConfig']['serializer'] = 'none'; // make it easy to use Lua
89 $this->server = $params['redisServer'];
90 $this->compression = isset( $params['compression'] ) ? $params['compression'] : 'none';
91 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
92 if ( empty( $params['daemonized'] ) ) {
93 throw new InvalidArgumentException(
94 "Non-daemonized mode is no longer supported. Please install the " .
95 "mediawiki/services/jobrunner service and update \$wgJobTypeConf as needed." );
96 }
97 }
98
99 protected function supportedOrders() {
100 return array( 'timestamp', 'fifo' );
101 }
102
103 protected function optimalOrder() {
104 return 'fifo';
105 }
106
107 protected function supportsDelayedJobs() {
108 return true;
109 }
110
111 /**
112 * @see JobQueue::doIsEmpty()
113 * @return bool
114 * @throws JobQueueError
115 */
116 protected function doIsEmpty() {
117 return $this->doGetSize() == 0;
118 }
119
120 /**
121 * @see JobQueue::doGetSize()
122 * @return int
123 * @throws JobQueueError
124 */
125 protected function doGetSize() {
126 $conn = $this->getConnection();
127 try {
128 return $conn->lSize( $this->getQueueKey( 'l-unclaimed' ) );
129 } catch ( RedisException $e ) {
130 $this->throwRedisException( $conn, $e );
131 }
132 }
133
134 /**
135 * @see JobQueue::doGetAcquiredCount()
136 * @return int
137 * @throws JobQueueError
138 */
139 protected function doGetAcquiredCount() {
140 $conn = $this->getConnection();
141 try {
142 $conn->multi( Redis::PIPELINE );
143 $conn->zSize( $this->getQueueKey( 'z-claimed' ) );
144 $conn->zSize( $this->getQueueKey( 'z-abandoned' ) );
145
146 return array_sum( $conn->exec() );
147 } catch ( RedisException $e ) {
148 $this->throwRedisException( $conn, $e );
149 }
150 }
151
152 /**
153 * @see JobQueue::doGetDelayedCount()
154 * @return int
155 * @throws JobQueueError
156 */
157 protected function doGetDelayedCount() {
158 $conn = $this->getConnection();
159 try {
160 return $conn->zSize( $this->getQueueKey( 'z-delayed' ) );
161 } catch ( RedisException $e ) {
162 $this->throwRedisException( $conn, $e );
163 }
164 }
165
166 /**
167 * @see JobQueue::doGetAbandonedCount()
168 * @return int
169 * @throws JobQueueError
170 */
171 protected function doGetAbandonedCount() {
172 $conn = $this->getConnection();
173 try {
174 return $conn->zSize( $this->getQueueKey( 'z-abandoned' ) );
175 } catch ( RedisException $e ) {
176 $this->throwRedisException( $conn, $e );
177 }
178 }
179
180 /**
181 * @see JobQueue::doBatchPush()
182 * @param array $jobs
183 * @param int $flags
184 * @return void
185 * @throws JobQueueError
186 */
187 protected function doBatchPush( array $jobs, $flags ) {
188 // Convert the jobs into field maps (de-duplicated against each other)
189 $items = array(); // (job ID => job fields map)
190 foreach ( $jobs as $job ) {
191 $item = $this->getNewJobFields( $job );
192 if ( strlen( $item['sha1'] ) ) { // hash identifier => de-duplicate
193 $items[$item['sha1']] = $item;
194 } else {
195 $items[$item['uuid']] = $item;
196 }
197 }
198
199 if ( !count( $items ) ) {
200 return; // nothing to do
201 }
202
203 $conn = $this->getConnection();
204 try {
205 // Actually push the non-duplicate jobs into the queue...
206 if ( $flags & self::QOS_ATOMIC ) {
207 $batches = array( $items ); // all or nothing
208 } else {
209 $batches = array_chunk( $items, 100 ); // avoid tying up the server
210 }
211 $failed = 0;
212 $pushed = 0;
213 foreach ( $batches as $itemBatch ) {
214 $added = $this->pushBlobs( $conn, $itemBatch );
215 if ( is_int( $added ) ) {
216 $pushed += $added;
217 } else {
218 $failed += count( $itemBatch );
219 }
220 }
221 JobQueue::incrStats( 'inserts', $this->type, count( $items ) );
222 JobQueue::incrStats( 'inserts_actual', $pushed );
223 JobQueue::incrStats( 'dupe_inserts', $this->type,
224 count( $items ) - $failed - $pushed );
225 if ( $failed > 0 ) {
226 $err = "Could not insert {$failed} {$this->type} job(s).";
227 wfDebugLog( 'JobQueueRedis', $err );
228 throw new RedisException( $err );
229 }
230 } catch ( RedisException $e ) {
231 $this->throwRedisException( $conn, $e );
232 }
233 }
234
235 /**
236 * @param RedisConnRef $conn
237 * @param array $items List of results from JobQueueRedis::getNewJobFields()
238 * @return int Number of jobs inserted (duplicates are ignored)
239 * @throws RedisException
240 */
241 protected function pushBlobs( RedisConnRef $conn, array $items ) {
242 $args = array(); // ([id, sha1, rtime, blob [, id, sha1, rtime, blob ... ] ] )
243 foreach ( $items as $item ) {
244 $args[] = (string)$item['uuid'];
245 $args[] = (string)$item['sha1'];
246 $args[] = (string)$item['rtimestamp'];
247 $args[] = (string)$this->serialize( $item );
248 }
249 static $script =
250 <<<LUA
251 local kUnclaimed, kSha1ById, kIdBySha1, kDelayed, kData = unpack(KEYS)
252 if #ARGV % 4 ~= 0 then return redis.error_reply('Unmatched arguments') end
253 local pushed = 0
254 for i = 1,#ARGV,4 do
255 local id,sha1,rtimestamp,blob = ARGV[i],ARGV[i+1],ARGV[i+2],ARGV[i+3]
256 if sha1 == '' or redis.call('hExists',kIdBySha1,sha1) == 0 then
257 if 1*rtimestamp > 0 then
258 -- Insert into delayed queue (release time as score)
259 redis.call('zAdd',kDelayed,rtimestamp,id)
260 else
261 -- Insert into unclaimed queue
262 redis.call('lPush',kUnclaimed,id)
263 end
264 if sha1 ~= '' then
265 redis.call('hSet',kSha1ById,id,sha1)
266 redis.call('hSet',kIdBySha1,sha1,id)
267 end
268 redis.call('hSet',kData,id,blob)
269 pushed = pushed + 1
270 end
271 end
272 return pushed
273 LUA;
274 return $conn->luaEval( $script,
275 array_merge(
276 array(
277 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
278 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
279 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
280 $this->getQueueKey( 'z-delayed' ), # KEYS[4]
281 $this->getQueueKey( 'h-data' ), # KEYS[5]
282 ),
283 $args
284 ),
285 5 # number of first argument(s) that are keys
286 );
287 }
288
289 /**
290 * @see JobQueue::doPop()
291 * @return Job|bool
292 * @throws JobQueueError
293 */
294 protected function doPop() {
295 $job = false;
296
297 $conn = $this->getConnection();
298 try {
299 do {
300 $blob = $this->popAndAcquireBlob( $conn );
301 if ( !is_string( $blob ) ) {
302 break; // no jobs; nothing to do
303 }
304
305 JobQueue::incrStats( 'pops', $this->type );
306 $item = $this->unserialize( $blob );
307 if ( $item === false ) {
308 wfDebugLog( 'JobQueueRedis', "Could not unserialize {$this->type} job." );
309 continue;
310 }
311
312 // If $item is invalid, the runner loop recyling will cleanup as needed
313 $job = $this->getJobFromFields( $item ); // may be false
314 } while ( !$job ); // job may be false if invalid
315 } catch ( RedisException $e ) {
316 $this->throwRedisException( $conn, $e );
317 }
318
319 return $job;
320 }
321
322 /**
323 * @param RedisConnRef $conn
324 * @return array Serialized string or false
325 * @throws RedisException
326 */
327 protected function popAndAcquireBlob( RedisConnRef $conn ) {
328 static $script =
329 <<<LUA
330 local kUnclaimed, kSha1ById, kIdBySha1, kClaimed, kAttempts, kData = unpack(KEYS)
331 -- Pop an item off the queue
332 local id = redis.call('rPop',kUnclaimed)
333 if not id then return false end
334 -- Allow new duplicates of this job
335 local sha1 = redis.call('hGet',kSha1ById,id)
336 if sha1 then redis.call('hDel',kIdBySha1,sha1) end
337 redis.call('hDel',kSha1ById,id)
338 -- Mark the jobs as claimed and return it
339 redis.call('zAdd',kClaimed,ARGV[1],id)
340 redis.call('hIncrBy',kAttempts,id,1)
341 return redis.call('hGet',kData,id)
342 LUA;
343 return $conn->luaEval( $script,
344 array(
345 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
346 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
347 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
348 $this->getQueueKey( 'z-claimed' ), # KEYS[4]
349 $this->getQueueKey( 'h-attempts' ), # KEYS[5]
350 $this->getQueueKey( 'h-data' ), # KEYS[6]
351 time(), # ARGV[1] (injected to be replication-safe)
352 ),
353 6 # number of first argument(s) that are keys
354 );
355 }
356
357 /**
358 * @see JobQueue::doAck()
359 * @param Job $job
360 * @return Job|bool
361 * @throws UnexpectedValueException
362 * @throws JobQueueError
363 */
364 protected function doAck( Job $job ) {
365 if ( !isset( $job->metadata['uuid'] ) ) {
366 throw new UnexpectedValueException( "Job of type '{$job->getType()}' has no UUID." );
367 }
368
369 $uuid = $job->metadata['uuid'];
370 $conn = $this->getConnection();
371 try {
372 static $script =
373 <<<LUA
374 local kClaimed, kAttempts, kData = unpack(KEYS)
375 -- Unmark the job as claimed
376 redis.call('zRem',kClaimed,ARGV[1])
377 redis.call('hDel',kAttempts,ARGV[1])
378 -- Delete the job data itself
379 return redis.call('hDel',kData,ARGV[1])
380 LUA;
381 $res = $conn->luaEval( $script,
382 array(
383 $this->getQueueKey( 'z-claimed' ), # KEYS[1]
384 $this->getQueueKey( 'h-attempts' ), # KEYS[2]
385 $this->getQueueKey( 'h-data' ), # KEYS[3]
386 $uuid # ARGV[1]
387 ),
388 3 # number of first argument(s) that are keys
389 );
390
391 if ( !$res ) {
392 wfDebugLog( 'JobQueueRedis', "Could not acknowledge {$this->type} job $uuid." );
393
394 return false;
395 }
396
397 JobQueue::incrStats( 'acks', $this->type );
398 } catch ( RedisException $e ) {
399 $this->throwRedisException( $conn, $e );
400 }
401
402 return true;
403 }
404
405 /**
406 * @see JobQueue::doDeduplicateRootJob()
407 * @param IJobSpecification $job
408 * @return bool
409 * @throws JobQueueError
410 * @throws LogicException
411 */
412 protected function doDeduplicateRootJob( IJobSpecification $job ) {
413 if ( !$job->hasRootJobParams() ) {
414 throw new LogicException( "Cannot register root job; missing parameters." );
415 }
416 $params = $job->getRootJobParams();
417
418 $key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
419
420 $conn = $this->getConnection();
421 try {
422 $timestamp = $conn->get( $key ); // current last timestamp of this job
423 if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
424 return true; // a newer version of this root job was enqueued
425 }
426
427 // Update the timestamp of the last root job started at the location...
428 return $conn->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL ); // 2 weeks
429 } catch ( RedisException $e ) {
430 $this->throwRedisException( $conn, $e );
431 }
432 }
433
434 /**
435 * @see JobQueue::doIsRootJobOldDuplicate()
436 * @param Job $job
437 * @return bool
438 * @throws JobQueueError
439 */
440 protected function doIsRootJobOldDuplicate( Job $job ) {
441 if ( !$job->hasRootJobParams() ) {
442 return false; // job has no de-deplication info
443 }
444 $params = $job->getRootJobParams();
445
446 $conn = $this->getConnection();
447 try {
448 // Get the last time this root job was enqueued
449 $timestamp = $conn->get( $this->getRootJobCacheKey( $params['rootJobSignature'] ) );
450 } catch ( RedisException $e ) {
451 $timestamp = false;
452 $this->throwRedisException( $conn, $e );
453 }
454
455 // Check if a new root job was started at the location after this one's...
456 return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
457 }
458
459 /**
460 * @see JobQueue::doDelete()
461 * @return bool
462 * @throws JobQueueError
463 */
464 protected function doDelete() {
465 static $props = array( 'l-unclaimed', 'z-claimed', 'z-abandoned',
466 'z-delayed', 'h-idBySha1', 'h-sha1ById', 'h-attempts', 'h-data' );
467
468 $conn = $this->getConnection();
469 try {
470 $keys = array();
471 foreach ( $props as $prop ) {
472 $keys[] = $this->getQueueKey( $prop );
473 }
474
475 return ( $conn->delete( $keys ) !== false );
476 } catch ( RedisException $e ) {
477 $this->throwRedisException( $conn, $e );
478 }
479 }
480
481 /**
482 * @see JobQueue::getAllQueuedJobs()
483 * @return Iterator
484 * @throws JobQueueError
485 */
486 public function getAllQueuedJobs() {
487 $conn = $this->getConnection();
488 try {
489 $uids = $conn->lRange( $this->getQueueKey( 'l-unclaimed' ), 0, -1 );
490 } catch ( RedisException $e ) {
491 $this->throwRedisException( $conn, $e );
492 }
493
494 return $this->getJobIterator( $conn, $uids );
495 }
496
497 /**
498 * @see JobQueue::getAllDelayedJobs()
499 * @return Iterator
500 * @throws JobQueueError
501 */
502 public function getAllDelayedJobs() {
503 $conn = $this->getConnection();
504 try {
505 $uids = $conn->zRange( $this->getQueueKey( 'z-delayed' ), 0, -1 );
506 } catch ( RedisException $e ) {
507 $this->throwRedisException( $conn, $e );
508 }
509
510 return $this->getJobIterator( $conn, $uids );
511 }
512
513 /**
514 * @see JobQueue::getAllAcquiredJobs()
515 * @return Iterator
516 * @throws JobQueueError
517 */
518 public function getAllAcquiredJobs() {
519 $conn = $this->getConnection();
520 try {
521 $uids = $conn->zRange( $this->getQueueKey( 'z-claimed' ), 0, -1 );
522 } catch ( RedisException $e ) {
523 $this->throwRedisException( $conn, $e );
524 }
525
526 return $this->getJobIterator( $conn, $uids );
527 }
528
529 /**
530 * @see JobQueue::getAllAbandonedJobs()
531 * @return Iterator
532 * @throws JobQueueError
533 */
534 public function getAllAbandonedJobs() {
535 $conn = $this->getConnection();
536 try {
537 $uids = $conn->zRange( $this->getQueueKey( 'z-abandoned' ), 0, -1 );
538 } catch ( RedisException $e ) {
539 $this->throwRedisException( $conn, $e );
540 }
541
542 return $this->getJobIterator( $conn, $uids );
543 }
544
545 /**
546 * @param RedisConnRef $conn
547 * @param array $uids List of job UUIDs
548 * @return MappedIterator
549 */
550 protected function getJobIterator( RedisConnRef $conn, array $uids ) {
551 $that = $this;
552
553 return new MappedIterator(
554 $uids,
555 function ( $uid ) use ( $that, $conn ) {
556 return $that->getJobFromUidInternal( $uid, $conn );
557 },
558 array( 'accept' => function ( $job ) {
559 return is_object( $job );
560 } )
561 );
562 }
563
564 public function getCoalesceLocationInternal() {
565 return "RedisServer:" . $this->server;
566 }
567
568 protected function doGetSiblingQueuesWithJobs( array $types ) {
569 return array_keys( array_filter( $this->doGetSiblingQueueSizes( $types ) ) );
570 }
571
572 protected function doGetSiblingQueueSizes( array $types ) {
573 $sizes = array(); // (type => size)
574 $types = array_values( $types ); // reindex
575 $conn = $this->getConnection();
576 try {
577 $conn->multi( Redis::PIPELINE );
578 foreach ( $types as $type ) {
579 $conn->lSize( $this->getQueueKey( 'l-unclaimed', $type ) );
580 }
581 $res = $conn->exec();
582 if ( is_array( $res ) ) {
583 foreach ( $res as $i => $size ) {
584 $sizes[$types[$i]] = $size;
585 }
586 }
587 } catch ( RedisException $e ) {
588 $this->throwRedisException( $conn, $e );
589 }
590
591 return $sizes;
592 }
593
594 /**
595 * This function should not be called outside JobQueueRedis
596 *
597 * @param string $uid
598 * @param RedisConnRef $conn
599 * @return Job|bool Returns false if the job does not exist
600 * @throws JobQueueError
601 * @throws UnexpectedValueException
602 */
603 public function getJobFromUidInternal( $uid, RedisConnRef $conn ) {
604 try {
605 $data = $conn->hGet( $this->getQueueKey( 'h-data' ), $uid );
606 if ( $data === false ) {
607 return false; // not found
608 }
609 $item = $this->unserialize( $data );
610 if ( !is_array( $item ) ) { // this shouldn't happen
611 throw new UnexpectedValueException( "Could not find job with ID '$uid'." );
612 }
613 $title = Title::makeTitle( $item['namespace'], $item['title'] );
614 $job = Job::factory( $item['type'], $title, $item['params'] );
615 $job->metadata['uuid'] = $item['uuid'];
616 $job->metadata['timestamp'] = $item['timestamp'];
617 // Add in attempt count for debugging at showJobs.php
618 $job->metadata['attempts'] = $conn->hGet( $this->getQueueKey( 'h-attempts' ), $uid );
619
620 return $job;
621 } catch ( RedisException $e ) {
622 $this->throwRedisException( $conn, $e );
623 }
624 }
625
626 /**
627 * @param IJobSpecification $job
628 * @return array
629 */
630 protected function getNewJobFields( IJobSpecification $job ) {
631 return array(
632 // Fields that describe the nature of the job
633 'type' => $job->getType(),
634 'namespace' => $job->getTitle()->getNamespace(),
635 'title' => $job->getTitle()->getDBkey(),
636 'params' => $job->getParams(),
637 // Some jobs cannot run until a "release timestamp"
638 'rtimestamp' => $job->getReleaseTimestamp() ?: 0,
639 // Additional job metadata
640 'uuid' => UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND ),
641 'sha1' => $job->ignoreDuplicates()
642 ? wfBaseConvert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
643 : '',
644 'timestamp' => time() // UNIX timestamp
645 );
646 }
647
648 /**
649 * @param array $fields
650 * @return Job|bool
651 */
652 protected function getJobFromFields( array $fields ) {
653 $title = Title::makeTitle( $fields['namespace'], $fields['title'] );
654 $job = Job::factory( $fields['type'], $title, $fields['params'] );
655 $job->metadata['uuid'] = $fields['uuid'];
656 $job->metadata['timestamp'] = $fields['timestamp'];
657
658 return $job;
659 }
660
661 /**
662 * @param array $fields
663 * @return string Serialized and possibly compressed version of $fields
664 */
665 protected function serialize( array $fields ) {
666 $blob = serialize( $fields );
667 if ( $this->compression === 'gzip'
668 && strlen( $blob ) >= 1024
669 && function_exists( 'gzdeflate' )
670 ) {
671 $object = (object)array( 'blob' => gzdeflate( $blob ), 'enc' => 'gzip' );
672 $blobz = serialize( $object );
673
674 return ( strlen( $blobz ) < strlen( $blob ) ) ? $blobz : $blob;
675 } else {
676 return $blob;
677 }
678 }
679
680 /**
681 * @param string $blob
682 * @return array|bool Unserialized version of $blob or false
683 */
684 protected function unserialize( $blob ) {
685 $fields = unserialize( $blob );
686 if ( is_object( $fields ) ) {
687 if ( $fields->enc === 'gzip' && function_exists( 'gzinflate' ) ) {
688 $fields = unserialize( gzinflate( $fields->blob ) );
689 } else {
690 $fields = false;
691 }
692 }
693
694 return is_array( $fields ) ? $fields : false;
695 }
696
697 /**
698 * Get a connection to the server that handles all sub-queues for this queue
699 *
700 * @return RedisConnRef
701 * @throws JobQueueConnectionError
702 */
703 protected function getConnection() {
704 $conn = $this->redisPool->getConnection( $this->server );
705 if ( !$conn ) {
706 throw new JobQueueConnectionError( "Unable to connect to redis server." );
707 }
708
709 return $conn;
710 }
711
712 /**
713 * @param RedisConnRef $conn
714 * @param RedisException $e
715 * @throws JobQueueError
716 */
717 protected function throwRedisException( RedisConnRef $conn, $e ) {
718 $this->redisPool->handleError( $conn, $e );
719 throw new JobQueueError( "Redis server error: {$e->getMessage()}\n" );
720 }
721
722 /**
723 * @param string $prop
724 * @param string|null $type
725 * @return string
726 */
727 private function getQueueKey( $prop, $type = null ) {
728 $type = is_string( $type ) ? $type : $this->type;
729 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
730 if ( strlen( $this->key ) ) { // namespaced queue (for testing)
731 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $type, $this->key, $prop );
732 } else {
733 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $type, $prop );
734 }
735 }
736
737 /**
738 * @param string $key
739 * @return void
740 */
741 public function setTestingPrefix( $key ) {
742 $this->key = $key;
743 }
744 }