Merge "Added a PoolCounterWorkViaCallback convenience class."
[lhc/web/wiklou.git] / includes / job / 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 faster, less resource intensive, queue that JobQueueDB.
28 * All data for a queue using this class is placed into one redis server.
29 *
30 * There are seven main redis keys used to track jobs:
31 * - l-unclaimed : A list of job IDs used for push/pop
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 * - h-idBySha1 : A hash of (SHA1 => job ID) for unclaimed jobs used for de-duplication
35 * - h-sha1Byid : A hash of (job ID => SHA1) for unclaimed jobs used for de-duplication
36 * - h-attempts : A hash of (job ID => attempt count) used for job claiming/retries
37 * - h-data : A hash of (job ID => serialized blobs) for job storage
38 * Any given job ID can be in only one of l-unclaimed, z-claimed, and z-abandoned.
39 * If an ID appears in any of those lists, it should have a h-data entry for its ID.
40 * If a job has a non-empty SHA1 de-duplication value and its ID is in l-unclaimed,
41 * then there should be no other such jobs. Every h-idBySha1 entry has an h-sha1Byid
42 * entry and every h-sha1Byid must refer to an ID that is l-unclaimed. If a job has its
43 * ID in z-claimed or z-abandoned, then it must also have an h-attempts entry for its ID.
44 *
45 * Additionally, "rootjob:* keys to track "root jobs" used for additional de-duplication.
46 * Aside from root job keys, all keys have no expiry, and are only removed when jobs are run.
47 * All the keys are prefixed with the relevant wiki ID information.
48 *
49 * This class requires Redis 2.6 as it makes use Lua scripts for fast atomic operations.
50 * Additionally, it should be noted that redis has different persistence modes, such
51 * as rdb snapshots, journaling, and no persistent. Appropriate configuration should be
52 * made on the servers based on what queues are using it and what tolerance they have.
53 *
54 * @ingroup JobQueue
55 * @ingroup Redis
56 * @since 1.21
57 */
58 class JobQueueRedis extends JobQueue {
59 /** @var RedisConnectionPool */
60 protected $redisPool;
61
62 protected $server; // string; server address
63
64 const MAX_AGE_PRUNE = 604800; // integer; seconds a job can live once claimed (7 days)
65
66 protected $key; // string; key to prefix the queue keys with (used for testing)
67
68 /**
69 * @params include:
70 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
71 * Note that the serializer option is ignored "none" is always used.
72 * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
73 * If a hostname is specified but no port, the standard port number
74 * 6379 will be used. Required.
75 * @param array $params
76 */
77 public function __construct( array $params ) {
78 parent::__construct( $params );
79 $params['redisConfig']['serializer'] = 'none'; // make it easy to use Lua
80 $this->server = $params['redisServer'];
81 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
82 }
83
84 protected function supportedOrders() {
85 return array( 'timestamp', 'fifo' );
86 }
87
88 protected function optimalOrder() {
89 return 'fifo';
90 }
91
92 /**
93 * @see JobQueue::doIsEmpty()
94 * @return bool
95 * @throws MWException
96 */
97 protected function doIsEmpty() {
98 return $this->doGetSize() == 0;
99 }
100
101 /**
102 * @see JobQueue::doGetSize()
103 * @return integer
104 * @throws MWException
105 */
106 protected function doGetSize() {
107 $conn = $this->getConnection();
108 try {
109 return $conn->lSize( $this->getQueueKey( 'l-unclaimed' ) );
110 } catch ( RedisException $e ) {
111 $this->throwRedisException( $this->server, $conn, $e );
112 }
113 }
114
115 /**
116 * @see JobQueue::doGetAcquiredCount()
117 * @return integer
118 * @throws MWException
119 */
120 protected function doGetAcquiredCount() {
121 if ( $this->claimTTL <= 0 ) {
122 return 0; // no acknowledgements
123 }
124 $conn = $this->getConnection();
125 try {
126 $conn->multi( Redis::PIPELINE );
127 $conn->zSize( $this->getQueueKey( 'z-claimed' ) );
128 $conn->zSize( $this->getQueueKey( 'z-abandoned' ) );
129 return array_sum( $conn->exec() );
130 } catch ( RedisException $e ) {
131 $this->throwRedisException( $this->server, $conn, $e );
132 }
133 }
134
135 /**
136 * @see JobQueue::doBatchPush()
137 * @param array $jobs
138 * @param $flags
139 * @return bool
140 * @throws MWException
141 */
142 protected function doBatchPush( array $jobs, $flags ) {
143 // Convert the jobs into field maps (de-duplicated against each other)
144 $items = array(); // (job ID => job fields map)
145 foreach ( $jobs as $job ) {
146 $item = $this->getNewJobFields( $job );
147 if ( strlen( $item['sha1'] ) ) { // hash identifier => de-duplicate
148 $items[$item['sha1']] = $item;
149 } else {
150 $items[$item['uuid']] = $item;
151 }
152 }
153 // Convert the field maps into serialized blobs
154 $tuples = array();
155 foreach ( $items as $item ) {
156 $tuples[] = array( $item['uuid'], $item['sha1'], serialize( $item ) );
157 }
158
159 if ( !count( $tuples ) ) {
160 return true; // nothing to do
161 }
162
163 $conn = $this->getConnection();
164 try {
165 // Actually push the non-duplicate jobs into the queue...
166 if ( $flags & self::QoS_Atomic ) {
167 $batches = array( $tuples ); // all or nothing
168 } else {
169 $batches = array_chunk( $tuples, 500 ); // avoid tying up the server
170 }
171 $failed = 0;
172 $pushed = 0;
173 foreach ( $batches as $tupleBatch ) {
174 $added = $this->pushBlobs( $conn, $tupleBatch );
175 if ( is_int( $added ) ) {
176 $pushed += $added;
177 } else {
178 $failed += count( $tupleBatch );
179 }
180 }
181 if ( $failed > 0 ) {
182 wfDebugLog( 'JobQueueRedis', "Could not insert {$failed} {$this->type} job(s)." );
183 return false;
184 }
185 wfIncrStats( 'job-insert', count( $tuples ) );
186 wfIncrStats( 'job-insert-duplicate', count( $tuples ) - $failed - $pushed );
187 } catch ( RedisException $e ) {
188 $this->throwRedisException( $this->server, $conn, $e );
189 }
190
191 return true;
192 }
193
194 /**
195 * @param RedisConnRef $conn
196 * @param array $tuples List of tuples of (job ID, job SHA1 or '', serialized blob)
197 * @return integer Number of jobs inserted (duplicates are ignored)
198 * @throws RedisException
199 */
200 protected function pushBlobs( RedisConnRef $conn, array $tuples ) {
201 $args = array(); // ([id, sha1, blob [, id, sha1, blob ... ] ] )
202 foreach ( $tuples as $tuple ) {
203 $args[] = $tuple[0]; // id
204 $args[] = $tuple[1]; // sha1
205 $args[] = $tuple[2]; // blob
206 }
207 static $script =
208 <<<LUA
209 if #ARGV % 3 ~= 0 then return redis.error_reply('Unmatched arguments') end
210 local pushed = 0
211 for i = 1,#ARGV,3 do
212 local id,sha1,blob = ARGV[i],ARGV[i+1],ARGV[i+2]
213 if sha1 == '' or redis.call('hExists',KEYS[3],sha1) == 0 then
214 redis.call('lPush',KEYS[1],id)
215 if sha1 ~= '' then
216 redis.call('hSet',KEYS[2],id,sha1)
217 redis.call('hSet',KEYS[3],sha1,id)
218 end
219 redis.call('hSet',KEYS[4],id,blob)
220 pushed = pushed + 1
221 end
222 end
223 return pushed
224 LUA;
225 return $this->redisEval( $conn, $script,
226 array_merge(
227 array(
228 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
229 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
230 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
231 $this->getQueueKey( 'h-data' ), # KEYS[4]
232 ),
233 $args
234 ),
235 4 # number of first argument(s) that are keys
236 );
237 }
238
239 /**
240 * @see JobQueue::doPop()
241 * @return Job|bool
242 * @throws MWException
243 */
244 protected function doPop() {
245 $job = false;
246
247 $conn = $this->getConnection();
248 try {
249 do {
250 if ( $this->claimTTL > 0 ) {
251 // Keep the claimed job list down for high-traffic queues
252 if ( mt_rand( 0, 99 ) == 0 ) {
253 $this->recycleAndDeleteStaleJobs();
254 }
255 $blob = $this->popAndAcquireBlob( $conn );
256 } else {
257 $blob = $this->popAndDeleteBlob( $conn );
258 }
259 if ( $blob === false ) {
260 break; // no jobs; nothing to do
261 }
262
263 wfIncrStats( 'job-pop' );
264 $item = unserialize( $blob );
265 if ( $item === false ) {
266 wfDebugLog( 'JobQueueRedis', "Could not unserialize {$this->type} job." );
267 continue;
268 }
269
270 // If $item is invalid, recycleAndDeleteStaleJobs() will cleanup as needed
271 $job = $this->getJobFromFields( $item ); // may be false
272 } while ( !$job ); // job may be false if invalid
273 } catch ( RedisException $e ) {
274 $this->throwRedisException( $this->server, $conn, $e );
275 }
276
277 return $job;
278 }
279
280 /**
281 * @param RedisConnRef $conn
282 * @return array serialized string or false
283 * @throws RedisException
284 */
285 protected function popAndDeleteBlob( RedisConnRef $conn ) {
286 static $script =
287 <<<LUA
288 -- Pop an item off the queue
289 local id = redis.call('rpop',KEYS[1])
290 if not id then return false end
291 -- Get the job data and remove it
292 local item = redis.call('hGet',KEYS[4],id)
293 redis.call('hDel',KEYS[4],id)
294 -- Allow new duplicates of this job
295 local sha1 = redis.call('hGet',KEYS[2],id)
296 if sha1 then redis.call('hDel',KEYS[3],sha1) end
297 redis.call('hDel',KEYS[2],id)
298 -- Return the job data
299 return item
300 LUA;
301 return $this->redisEval( $conn, $script,
302 array(
303 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
304 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
305 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
306 $this->getQueueKey( 'h-data' ), # KEYS[4]
307 ),
308 4 # number of first argument(s) that are keys
309 );
310 }
311
312 /**
313 * @param RedisConnRef $conn
314 * @return array serialized string or false
315 * @throws RedisException
316 */
317 protected function popAndAcquireBlob( RedisConnRef $conn ) {
318 static $script =
319 <<<LUA
320 -- Pop an item off the queue
321 local id = redis.call('rPop',KEYS[1])
322 if not id then return false end
323 -- Allow new duplicates of this job
324 local sha1 = redis.call('hGet',KEYS[2],id)
325 if sha1 then redis.call('hDel',KEYS[3],sha1) end
326 redis.call('hDel',KEYS[2],id)
327 -- Mark the jobs as claimed and return it
328 redis.call('zAdd',KEYS[4],ARGV[1],id)
329 redis.call('hIncrBy',KEYS[5],id,1)
330 return redis.call('hGet',KEYS[6],id)
331 LUA;
332 return $this->redisEval( $conn, $script,
333 array(
334 $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
335 $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
336 $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
337 $this->getQueueKey( 'z-claimed' ), # KEYS[4]
338 $this->getQueueKey( 'h-attempts' ), # KEYS[5]
339 $this->getQueueKey( 'h-data' ), # KEYS[6]
340 time(), # ARGV[1] (injected to be replication-safe)
341 ),
342 6 # number of first argument(s) that are keys
343 );
344 }
345
346 /**
347 * @see JobQueue::doAck()
348 * @param Job $job
349 * @return Job|bool
350 * @throws MWException
351 */
352 protected function doAck( Job $job ) {
353 if ( $this->claimTTL > 0 ) {
354 $conn = $this->getConnection();
355 try {
356 // Get the exact field map this Job came from, regardless of whether
357 // the job was transformed into a DuplicateJob or anything of the sort.
358 $item = $job->metadata['sourceFields'];
359
360 static $script =
361 <<<LUA
362 -- Unmark the job as claimed
363 redis.call('zRem',KEYS[1],ARGV[1])
364 redis.call('hDel',KEYS[2],ARGV[1])
365 -- Delete the job data itself
366 return redis.call('hDel',KEYS[3],ARGV[1])
367 LUA;
368 $res = $this->redisEval( $conn, $script,
369 array(
370 $this->getQueueKey( 'z-claimed' ), # KEYS[1]
371 $this->getQueueKey( 'h-attempts' ), # KEYS[2]
372 $this->getQueueKey( 'h-data' ), # KEYS[3]
373 $item['uuid'] # ARGV[1]
374 ),
375 3 # number of first argument(s) that are keys
376 );
377
378 if ( !$res ) {
379 wfDebugLog( 'JobQueueRedis', "Could not acknowledge {$this->type} job." );
380 return false;
381 }
382 } catch ( RedisException $e ) {
383 $this->throwRedisException( $this->server, $conn, $e );
384 }
385 }
386 return true;
387 }
388
389 /**
390 * @see JobQueue::doDeduplicateRootJob()
391 * @param Job $job
392 * @return bool
393 * @throws MWException
394 */
395 protected function doDeduplicateRootJob( Job $job ) {
396 $params = $job->getParams();
397 if ( !isset( $params['rootJobSignature'] ) ) {
398 throw new MWException( "Cannot register root job; missing 'rootJobSignature'." );
399 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
400 throw new MWException( "Cannot register root job; missing 'rootJobTimestamp'." );
401 }
402 $key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
403
404 $conn = $this->getConnection();
405 try {
406 $timestamp = $conn->get( $key ); // current last timestamp of this job
407 if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
408 return true; // a newer version of this root job was enqueued
409 }
410 // Update the timestamp of the last root job started at the location...
411 return $conn->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL ); // 2 weeks
412 } catch ( RedisException $e ) {
413 $this->throwRedisException( $this->server, $conn, $e );
414 }
415 }
416
417 /**
418 * @see JobQueue::doIsRootJobOldDuplicate()
419 * @param Job $job
420 * @return bool
421 */
422 protected function doIsRootJobOldDuplicate( Job $job ) {
423 $params = $job->getParams();
424 if ( !isset( $params['rootJobSignature'] ) ) {
425 return false; // job has no de-deplication info
426 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
427 wfDebugLog( 'JobQueueRedis', "Cannot check root job; missing 'rootJobTimestamp'." );
428 return false;
429 }
430
431 $conn = $this->getConnection();
432 try {
433 // Get the last time this root job was enqueued
434 $timestamp = $conn->get( $this->getRootJobCacheKey( $params['rootJobSignature'] ) );
435 } catch ( RedisException $e ) {
436 $this->throwRedisException( $this->server, $conn, $e );
437 }
438
439 // Check if a new root job was started at the location after this one's...
440 return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
441 }
442
443 /**
444 * @see JobQueue::getAllQueuedJobs()
445 * @return Iterator
446 */
447 public function getAllQueuedJobs() {
448 $conn = $this->getConnection();
449 if ( !$conn ) {
450 throw new MWException( "Unable to connect to redis server." );
451 }
452 try {
453 $that = $this;
454 return new MappedIterator(
455 $conn->lRange( $this->getQueueKey( 'l-unclaimed' ), 0, -1 ),
456 function( $uid ) use ( $that, $conn ) {
457 return $that->getJobFromUidInternal( $uid, $conn );
458 }
459 );
460 } catch ( RedisException $e ) {
461 $this->throwRedisException( $this->server, $conn, $e );
462 }
463 }
464
465 /**
466 * This function should not be called outside RedisJobQueue
467 *
468 * @param $uid string
469 * @param $conn RedisConnRef
470 * @return Job
471 * @throws MWException
472 */
473 public function getJobFromUidInternal( $uid, RedisConnRef $conn ) {
474 try {
475 $item = unserialize( $conn->hGet( $this->getQueueKey( 'h-data' ), $uid ) );
476 if ( !is_array( $item ) ) { // this shouldn't happen
477 throw new MWException( "Could not find job with ID '$uid'." );
478 }
479 $title = Title::makeTitle( $item['namespace'], $item['title'] );
480 $job = Job::factory( $item['type'], $title, $item['params'] );
481 $job->metadata['sourceFields'] = $item;
482 return $job;
483 } catch ( RedisException $e ) {
484 $this->throwRedisException( $this->server, $conn, $e );
485 }
486 }
487
488 /**
489 * Recycle or destroy any jobs that have been claimed for too long
490 *
491 * @return integer Number of jobs recycled/deleted
492 * @throws MWException
493 */
494 public function recycleAndDeleteStaleJobs() {
495 if ( $this->claimTTL <= 0 ) { // sanity
496 throw new MWException( "Cannot recycle jobs since acknowledgements are disabled." );
497 }
498 $count = 0;
499 // For each job item that can be retried, we need to add it back to the
500 // main queue and remove it from the list of currenty claimed job items.
501 // For those that cannot, they are marked as dead and kept around for
502 // investigation and manual job restoration but are eventually deleted.
503 $conn = $this->getConnection();
504 try {
505 $now = time();
506 static $script =
507 <<<LUA
508 local released,abandoned,pruned = 0,0,0
509 -- Get all non-dead jobs that have an expired claim on them.
510 -- The score for each item is the last claim timestamp (UNIX).
511 local staleClaims = redis.call('zRangeByScore',KEYS[1],0,ARGV[1],'WITHSCORES')
512 for id,timestamp in ipairs(staleClaims) do
513 local attempts = redis.call('hGet',KEYS[2],id)
514 if attempts < ARGV[3] then
515 -- Claim expired and retries left: re-enqueue the job
516 redis.call('lPush',KEYS[3],id)
517 redis.call('hIncrBy',KEYS[2],id,1)
518 released = released + 1
519 else
520 -- Claim expired and no retries left: mark the job as dead
521 redis.call('zAdd',KEYS[5],timestamp,id)
522 abandoned = abandoned + 1
523 end
524 redis.call('zRem',KEYS[1],id)
525 end
526 -- Get all of the dead jobs that have been marked as dead for too long.
527 -- The score for each item is the last claim timestamp (UNIX).
528 local deadClaims = redis.call('zRangeByScore',KEYS[5],0,ARGV[2],'WITHSCORES')
529 for id,timestamp in ipairs(deadClaims) do
530 -- Stale and out of retries: remove any traces of the job
531 redis.call('zRem',KEYS[5],id)
532 redis.call('hDel',KEYS[2],id)
533 redis.call('hDel',KEYS[4],id)
534 pruned = pruned + 1
535 end
536 return {released,abandoned,pruned}
537 LUA;
538 $res = $this->redisEval( $conn, $script,
539 array(
540 $this->getQueueKey( 'z-claimed' ), # KEYS[1]
541 $this->getQueueKey( 'h-attempts' ), # KEYS[2]
542 $this->getQueueKey( 'l-unclaimed' ), # KEYS[3]
543 $this->getQueueKey( 'h-data' ), # KEYS[4]
544 $this->getQueueKey( 'z-abandoned' ), # KEYS[5]
545 $now - $this->claimTTL, # ARGV[1]
546 $now - self::MAX_AGE_PRUNE, # ARGV[2]
547 $this->maxTries # ARGV[3]
548 ),
549 5 # number of first argument(s) that are keys
550 );
551 if ( $res ) {
552 list( $released, $abandoned, $pruned ) = $res;
553 $count += $released + $pruned;
554 wfIncrStats( 'job-recycle', count( $released ) );
555 }
556 } catch ( RedisException $e ) {
557 $this->throwRedisException( $this->server, $conn, $e );
558 }
559
560 return $count;
561 }
562
563 /**
564 * @return Array
565 */
566 protected function doGetPeriodicTasks() {
567 if ( $this->claimTTL > 0 ) {
568 return array(
569 'recycleAndDeleteStaleJobs' => array(
570 'callback' => array( $this, 'recycleAndDeleteStaleJobs' ),
571 'period' => ceil( $this->claimTTL / 2 )
572 )
573 );
574 } else {
575 return array();
576 }
577 }
578
579 /**
580 * @param RedisConnRef $conn
581 * @param string $script
582 * @param array $params
583 * @param integer $numKeys
584 * @return mixed
585 */
586 protected function redisEval( RedisConnRef $conn, $script, array $params, $numKeys ) {
587 $res = $conn->evalSha( sha1( $script ), $params, $numKeys );
588 if ( $res === false && $conn->getLastError() != '' ) { // not in script cache?
589 wfDebugLog( 'JobQueueRedis', "Lua script error: " . $conn->getLastError() );
590 $res = $conn->eval( $script, $params, $numKeys );
591 }
592 return $res;
593 }
594
595 /**
596 * @param $job Job
597 * @return array
598 */
599 protected function getNewJobFields( Job $job ) {
600 return array(
601 // Fields that describe the nature of the job
602 'type' => $job->getType(),
603 'namespace' => $job->getTitle()->getNamespace(),
604 'title' => $job->getTitle()->getDBkey(),
605 'params' => $job->getParams(),
606 // Additional job metadata
607 'uuid' => UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND ),
608 'sha1' => $job->ignoreDuplicates()
609 ? wfBaseConvert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
610 : '',
611 'timestamp' => time() // UNIX timestamp
612 );
613 }
614
615 /**
616 * @param $fields array
617 * @return Job|bool
618 */
619 protected function getJobFromFields( array $fields ) {
620 $title = Title::makeTitleSafe( $fields['namespace'], $fields['title'] );
621 if ( $title ) {
622 $job = Job::factory( $fields['type'], $title, $fields['params'] );
623 $job->metadata['sourceFields'] = $fields;
624 return $job;
625 }
626 return false;
627 }
628
629 /**
630 * Get a connection to the server that handles all sub-queues for this queue
631 *
632 * @return Array (server name, Redis instance)
633 * @throws MWException
634 */
635 protected function getConnection() {
636 $conn = $this->redisPool->getConnection( $this->server );
637 if ( !$conn ) {
638 throw new MWException( "Unable to connect to redis server." );
639 }
640 return $conn;
641 }
642
643 /**
644 * @param $server string
645 * @param $conn RedisConnRef
646 * @param $e RedisException
647 * @throws MWException
648 */
649 protected function throwRedisException( $server, RedisConnRef $conn, $e ) {
650 $this->redisPool->handleException( $server, $conn, $e );
651 throw new MWException( "Redis server error: {$e->getMessage()}\n" );
652 }
653
654 /**
655 * @param $prop string
656 * @return string
657 */
658 private function getQueueKey( $prop ) {
659 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
660 if ( strlen( $this->key ) ) { // namespaced queue (for testing)
661 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type, $this->key, $prop );
662 } else {
663 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type, $prop );
664 }
665 }
666
667 /**
668 * @param $key string
669 * @return void
670 */
671 public function setTestingPrefix( $key ) {
672 $this->key = $key;
673 }
674 }