Merge "Misc follow-ups to I2fc3966e (a161c5e)"
[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 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueRedis extends JobQueue {
31 /** @var RedisConnectionPool */
32 protected $redisPool;
33
34 protected $server; // string; server address
35
36 const ROOTJOB_TTL = 1209600; // integer; seconds to remember root jobs (14 days)
37 const MAX_AGE_PRUNE = 604800; // integer; seconds a job can live once claimed (7 days)
38
39 protected $key; // string; key to prefix the queue keys with (used for testing)
40
41 /**
42 * @params include:
43 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
44 * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
45 * If a hostname is specified but no port, the standard port number
46 * 6379 will be used. Required.
47 * @param array $params
48 */
49 public function __construct( array $params ) {
50 parent::__construct( $params );
51 $this->server = $params['redisServer'];
52 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
53 }
54
55 /**
56 * @see JobQueue::doIsEmpty()
57 * @return bool
58 * @throws MWException
59 */
60 protected function doIsEmpty() {
61 $conn = $this->getConnection();
62 try {
63 return ( $conn->lSize( $this->getQueueKey( 'l-unclaimed' ) ) == 0 );
64 } catch ( RedisException $e ) {
65 $this->throwRedisException( $this->server, $conn, $e );
66 }
67 }
68
69 /**
70 * @see JobQueue::doGetSize()
71 * @return integer
72 * @throws MWException
73 */
74 protected function doGetSize() {
75 $conn = $this->getConnection();
76 try {
77 return $conn->lSize( $this->getQueueKey( 'l-unclaimed' ) );
78 } catch ( RedisException $e ) {
79 $this->throwRedisException( $this->server, $conn, $e );
80 }
81 }
82
83 /**
84 * @see JobQueue::doGetAcquiredCount()
85 * @return integer
86 * @throws MWException
87 */
88 protected function doGetAcquiredCount() {
89 if ( $this->claimTTL <= 0 ) {
90 return 0; // no acknowledgements
91 }
92 $conn = $this->getConnection();
93 try {
94 return $conn->lSize( $this->getQueueKey( 'l-claimed' ) );
95 } catch ( RedisException $e ) {
96 $this->throwRedisException( $this->server, $conn, $e );
97 }
98 }
99
100 /**
101 * @see JobQueue::doBatchPush()
102 * @param array $jobs
103 * @param $flags
104 * @return bool
105 * @throws MWException
106 */
107 protected function doBatchPush( array $jobs, $flags ) {
108 if ( !count( $jobs ) ) {
109 return true;
110 }
111
112 // Convert the jobs into a list of field maps
113 $items = array(); // (uid => job fields map)
114 foreach ( $jobs as $job ) {
115 $item = $this->getNewJobFields( $job );
116 $items[$item['uid']] = $item;
117 }
118
119 $dedupUids = array(); // list of uids to check for duplicates
120 foreach ( $items as $item ) {
121 if ( $this->isHashUid( $item['uid'] ) ) { // hash identifier => de-duplicate
122 $dedupUids[] = $item['uid'];
123 }
124 }
125
126 $conn = $this->getConnection();
127 try {
128 // Find which of these jobs are duplicates of unclaimed jobs in the queue...
129 if ( count( $dedupUids ) ) {
130 $conn->multi( Redis::PIPELINE );
131 foreach ( $dedupUids as $uid ) { // check if job data exists
132 $conn->exists( $this->prefixWithQueueKey( 'data', $uid ) );
133 }
134 if ( $this->claimTTL > 0 ) { // check which jobs were claimed
135 foreach ( $dedupUids as $uid ) {
136 $conn->hExists( $this->prefixWithQueueKey( 'h-meta', $uid ), 'ctime' );
137 }
138 list( $exists, $claimed ) = array_chunk( $conn->exec(), count( $dedupUids ) );
139 } else {
140 $exists = $conn->exec();
141 $claimed = array(); // no claim system
142 }
143 // Remove the duplicate jobs to cut down on pushing duplicate uids...
144 foreach ( $dedupUids as $k => $uid ) {
145 if ( $exists[$k] && empty( $claimed[$k] ) ) {
146 unset( $items[$uid] );
147 }
148 }
149 }
150 // Actually push the non-duplicate jobs into the queue...
151 if ( count( $items ) ) {
152 $uids = array_keys( $items );
153 $conn->multi( Redis::MULTI ); // begin (atomic trx)
154 $conn->mSet( $this->prefixKeysWithQueueKey( 'data', $items ) );
155 call_user_func_array(
156 array( $conn, 'lPush' ),
157 array_merge( array( $this->getQueueKey( 'l-unclaimed' ) ), $uids )
158 );
159 $res = $conn->exec(); // commit (atomic trx)
160 if ( in_array( false, $res, true ) ) {
161 wfDebugLog( 'JobQueueRedis', "Could not insert {$this->type} job(s)." );
162 return false;
163 }
164 }
165 wfIncrStats( 'job-insert', count( $items ) );
166 wfIncrStats( 'job-insert-duplicate', count( $jobs ) - count( $items ) );
167 } catch ( RedisException $e ) {
168 $this->throwRedisException( $this->server, $conn, $e );
169 }
170
171 return true;
172 }
173
174 /**
175 * @see JobQueue::doPop()
176 * @return Job|bool
177 * @throws MWException
178 */
179 protected function doPop() {
180 $job = false;
181
182 if ( $this->claimTTL <= 0 && mt_rand( 0, 99 ) == 0 ) {
183 $this->cleanupClaimedJobs(); // prune jobs and IDs from the "garbage" list
184 }
185
186 $conn = $this->getConnection();
187 try {
188 do {
189 // Atomically pop an item off the queue and onto the "claimed" list
190 $uid = $conn->rpoplpush(
191 $this->getQueueKey( 'l-unclaimed' ),
192 $this->getQueueKey( 'l-claimed' )
193 );
194 if ( $uid === false ) {
195 break; // no jobs; nothing to do
196 }
197
198 wfIncrStats( 'job-pop' );
199 $conn->multi( Redis::PIPELINE );
200 $conn->get( $this->prefixWithQueueKey( 'data', $uid ) );
201 if ( $this->claimTTL > 0 ) {
202 // Set the claim timestamp metadata. If this step fails, then
203 // the timestamp will be assumed to be the current timestamp by
204 // recycleAndDeleteStaleJobs() as of the next time that it runs.
205 // If two runners claim duplicate jobs, one will abort here.
206 $conn->hSetNx( $this->prefixWithQueueKey( 'h-meta', $uid ), 'ctime', time() );
207 } else {
208 // If this fails, the message key will be deleted in cleanupClaimedJobs().
209 // If two runners claim duplicate jobs, one of them will abort here.
210 $conn->delete(
211 $this->prefixWithQueueKey( 'h-meta', $uid ),
212 $this->prefixWithQueueKey( 'data', $uid ) );
213 }
214 list( $item, $ok ) = $conn->exec();
215 if ( $item === false || ( $this->claimTTL && !$ok ) ) {
216 wfDebug( "Could not find or delete job $uid; probably was a duplicate." );
217 continue; // job was probably a duplicate
218 }
219
220 // If $item is invalid, recycleAndDeleteStaleJobs() will cleanup as needed
221 $job = $this->getJobFromFields( $item ); // may be false
222 } while ( !$job ); // job may be false if invalid
223 } catch ( RedisException $e ) {
224 $this->throwRedisException( $this->server, $conn, $e );
225 }
226
227 // Flag this job as an old duplicate based on its "root" job...
228 try {
229 if ( $job && $this->isRootJobOldDuplicate( $job ) ) {
230 wfIncrStats( 'job-pop-duplicate' );
231 return DuplicateJob::newFromJob( $job ); // convert to a no-op
232 }
233 } catch ( MWException $e ) {} // don't lose jobs over this
234
235 return $job;
236 }
237
238 /**
239 * @see JobQueue::doAck()
240 * @param Job $job
241 * @return Job|bool
242 * @throws MWException
243 */
244 protected function doAck( Job $job ) {
245 if ( $this->claimTTL > 0 ) {
246 $conn = $this->getConnection();
247 try {
248 // Get the exact field map this Job came from, regardless of whether
249 // the job was transformed into a DuplicateJob or anything of the sort.
250 $item = $job->metadata['sourceFields'];
251
252 $conn->multi( Redis::MULTI ); // begin (atomic trx)
253 // Remove the first instance of this job scanning right-to-left.
254 // This is O(N) in the worst case, but is likely to be much faster since
255 // jobs are pushed to the left and we are starting from the right, where
256 // the longest running jobs are likely to be. These should be the first
257 // jobs to be acknowledged assuming that job run times are roughly equal.
258 $conn->lRem( $this->getQueueKey( 'l-claimed' ), $item['uid'], -1 );
259 // Delete the job data and its claim metadata
260 $conn->delete(
261 $this->prefixWithQueueKey( 'h-meta', $item['uid'] ),
262 $this->prefixWithQueueKey( 'data', $item['uid'] ) );
263 $res = $conn->exec(); // commit (atomic trx)
264
265 if ( in_array( false, $res, true ) ) {
266 wfDebugLog( 'JobQueueRedis', "Could not acknowledge {$this->type} job." );
267 return false;
268 }
269 } catch ( RedisException $e ) {
270 $this->throwRedisException( $this->server, $conn, $e );
271 }
272 }
273 return true;
274 }
275
276 /**
277 * @see JobQueue::doDeduplicateRootJob()
278 * @param Job $job
279 * @return bool
280 * @throws MWException
281 */
282 protected function doDeduplicateRootJob( Job $job ) {
283 $params = $job->getParams();
284 if ( !isset( $params['rootJobSignature'] ) ) {
285 throw new MWException( "Cannot register root job; missing 'rootJobSignature'." );
286 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
287 throw new MWException( "Cannot register root job; missing 'rootJobTimestamp'." );
288 }
289 $key = $this->getRootJobKey( $params['rootJobSignature'] );
290
291 $conn = $this->getConnection();
292 try {
293 $timestamp = $conn->get( $key ); // current last timestamp of this job
294 if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
295 return true; // a newer version of this root job was enqueued
296 }
297 // Update the timestamp of the last root job started at the location...
298 return $conn->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL ); // 2 weeks
299 } catch ( RedisException $e ) {
300 $this->throwRedisException( $this->server, $conn, $e );
301 }
302 }
303
304 /**
305 * Check if the "root" job of a given job has been superseded by a newer one
306 *
307 * @param $job Job
308 * @return bool
309 * @throws MWException
310 */
311 protected function isRootJobOldDuplicate( Job $job ) {
312 $params = $job->getParams();
313 if ( !isset( $params['rootJobSignature'] ) ) {
314 return false; // job has no de-deplication info
315 } elseif ( !isset( $params['rootJobTimestamp'] ) ) {
316 wfDebugLog( 'JobQueueRedis', "Cannot check root job; missing 'rootJobTimestamp'." );
317 return false;
318 }
319
320 $conn = $this->getConnection();
321 try {
322 // Get the last time this root job was enqueued
323 $timestamp = $conn->get( $this->getRootJobKey( $params['rootJobSignature'] ) );
324 } catch ( RedisException $e ) {
325 $this->throwRedisException( $this->server, $conn, $e );
326 }
327
328 // Check if a new root job was started at the location after this one's...
329 return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
330 }
331
332 /**
333 * Recycle or destroy any jobs that have been claimed for too long
334 *
335 * @return integer Number of jobs recycled/deleted
336 * @throws MWException
337 */
338 public function recycleAndDeleteStaleJobs() {
339 if ( $this->claimTTL <= 0 ) { // sanity
340 throw new MWException( "Cannot recycle jobs since acknowledgements are disabled." );
341 }
342 $count = 0;
343 // For each job item that can be retried, we need to add it back to the
344 // main queue and remove it from the list of currenty claimed job items.
345 $conn = $this->getConnection();
346 try {
347 // Avoid duplicate insertions of items to be re-enqueued
348 $conn->multi( Redis::MULTI );
349 $conn->setnx( $this->getQueueKey( 'lock' ), 1 );
350 $conn->expire( $this->getQueueKey( 'lock' ), 3600 );
351 if ( $conn->exec() !== array( true, true ) ) { // lock
352 return $count; // already in progress
353 }
354
355 $now = time();
356 $claimCutoff = $now - $this->claimTTL;
357 $pruneCutoff = $now - self::MAX_AGE_PRUNE;
358
359 // Get the list of all claimed jobs
360 $claimedUids = $conn->lRange( $this->getQueueKey( 'l-claimed' ), 0, -1 );
361 // Get a map of (uid => claim metadata) for all claimed jobs
362 $metadata = $conn->mGet( $this->prefixValuesWithQueueKey( 'h-meta', $claimedUids ) );
363
364 $uidsPush = array(); // items IDs to move to the "unclaimed" queue
365 $uidsRemove = array(); // item IDs to remove from "claimed" queue
366 foreach ( $claimedUids as $i => $uid ) { // all claimed items
367 $info = $metadata[$i] ? $metadata[$i] : array();
368 if ( isset( $info['ctime'] ) || isset( $info['rctime'] ) ) {
369 // Prefer "ctime" (set by pop()) over "rctime" (set by this function)
370 $ctime = isset( $info['ctime'] ) ? $info['ctime'] : $info['rctime'];
371 // Claimed job claimed for too long?
372 if ( $ctime < $claimCutoff ) {
373 // Get the number of failed attempts
374 $attempts = isset( $info['attempts'] ) ? $info['attempts'] : 0;
375 if ( $attempts < $this->maxTries ) {
376 $uidsPush[] = $uid; // retry it
377 } elseif ( $ctime < $pruneCutoff ) {
378 $uidsRemove[] = $uid; // just remove it
379 }
380 }
381 } else {
382 // If pop() failed to set the claim timestamp, set it to the current time.
383 // Since that function sets this non-atomically *after* moving the job to
384 // the "claimed" queue, it may be the case that it just didn't set it yet.
385 $conn->hSet( $this->prefixWithQueueKey( 'h-meta', $uid ), 'rctime', $now );
386 }
387 }
388
389 $conn->multi( Redis::MULTI ); // begin (atomic trx)
390 if ( count( $uidsPush ) ) { // move from "l-claimed" to "l-unclaimed"
391 call_user_func_array(
392 array( $conn, 'lPush' ),
393 array_merge( array( $this->getQueueKey( 'l-unclaimed' ) ), $uidsPush )
394 );
395 foreach ( $uidsPush as $uid ) {
396 $conn->lRem( $this->getQueueKey( 'l-claimed' ), $uid, -1 );
397 $conn->hDel( $this->prefixWithQueueKey( 'h-meta', $uid ), 'ctime', 'rctime' );
398 $conn->hIncrBy( $this->prefixWithQueueKey( 'h-meta', $uid ), 'attempts', 1 );
399 }
400 }
401 foreach ( $uidsRemove as $uid ) { // remove from "l-claimed"
402 $conn->lRem( $this->getQueueKey( 'l-claimed' ), $uid, -1 );
403 $conn->delete( // delete job data and metadata
404 $this->prefixWithQueueKey( 'h-meta', $uid ),
405 $this->prefixWithQueueKey( 'data', $uid ) );
406 }
407 $res = $conn->exec(); // commit (atomic trx)
408
409 if ( in_array( false, $res, true ) ) {
410 wfDebugLog( 'JobQueueRedis', "Could not recycle {$this->type} job(s)." );
411 } else {
412 $count += ( count( $uidsPush ) + count( $uidsRemove ) );
413 wfIncrStats( 'job-recycle', count( $uidsPush ) );
414 }
415
416 $conn->delete( $this->getQueueKey( 'lock' ) ); // unlock
417 } catch ( RedisException $e ) {
418 $this->throwRedisException( $this->server, $conn, $e );
419 }
420
421 return $count;
422 }
423
424 /**
425 * Destroy any jobs that have been claimed
426 *
427 * @return integer Number of jobs deleted
428 * @throws MWException
429 */
430 protected function cleanupClaimedJobs() {
431 $count = 0;
432 // Make sure the message for claimed jobs was deleted
433 // and remove the claimed job IDs from the "claimed" list.
434 $conn = $this->getConnection();
435 try {
436 // Avoid races and duplicate effort
437 $conn->multi( Redis::MULTI );
438 $conn->setnx( $this->getQueueKey( 'lock' ), 1 );
439 $conn->expire( $this->getQueueKey( 'lock' ), 3600 );
440 if ( $conn->exec() !== array( true, true ) ) { // lock
441 return $count; // already in progress
442 }
443 // Get the list of all claimed jobs
444 $uids = $conn->lRange( $this->getQueueKey( 'l-claimed' ), 0, -1 );
445 if ( count( $uids ) ) {
446 // Delete the message keys and delist the corresponding ids.
447 // Since the only other changes to "l-claimed" are left pushes, we can just strip
448 // off the elements read here using a right trim based on the number of ids read.
449 $conn->multi( Redis::MULTI ); // begin (atomic trx)
450 $conn->lTrim( $this->getQueueKey( 'l-claimed' ), 0, -count( $uids ) - 1 );
451 $conn->delete( array_merge(
452 $this->prefixValuesWithQueueKey( 'h-meta', $uids ),
453 $this->prefixValuesWithQueueKey( 'data', $uids )
454 ) );
455 $res = $conn->exec(); // commit (atomic trx)
456
457 if ( in_array( false, $res, true ) ) {
458 wfDebugLog( 'JobQueueRedis', "Could not purge {$this->type} job(s)." );
459 } else {
460 $count += count( $uids );
461 }
462 }
463 $conn->delete( $this->getQueueKey( 'lock' ) ); // unlock
464 } catch ( RedisException $e ) {
465 $this->throwRedisException( $this->server, $conn, $e );
466 }
467
468 return $count;
469 }
470
471 /**
472 * @return Array
473 */
474 protected function doGetPeriodicTasks() {
475 if ( $this->claimTTL > 0 ) {
476 return array(
477 'recycleAndDeleteStaleJobs' => array(
478 'callback' => array( $this, 'recycleAndDeleteStaleJobs' ),
479 'period' => ceil( $this->claimTTL / 2 )
480 )
481 );
482 } else {
483 return array();
484 }
485 }
486
487 /**
488 * @param $job Job
489 * @return array
490 */
491 protected function getNewJobFields( Job $job ) {
492 return array(
493 // Fields that describe the nature of the job
494 'type' => $job->getType(),
495 'namespace' => $job->getTitle()->getNamespace(),
496 'title' => $job->getTitle()->getDBkey(),
497 'params' => $job->getParams(),
498 // Additional metadata
499 'uid' => $job->ignoreDuplicates()
500 ? wfBaseConvert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
501 : wfRandomString( 32 ),
502 'timestamp' => time() // UNIX timestamp
503 );
504 }
505
506 /**
507 * @param $fields array
508 * @return Job|bool
509 */
510 protected function getJobFromFields( array $fields ) {
511 $title = Title::makeTitleSafe( $fields['namespace'], $fields['title'] );
512 if ( $title ) {
513 $job = Job::factory( $fields['type'], $title, $fields['params'] );
514 $job->metadata['sourceFields'] = $fields;
515 return $job;
516 }
517 return false;
518 }
519
520 /**
521 * @param $uid string Job UID
522 * @return bool Whether $uid is a SHA-1 hash based identifier for de-duplication
523 */
524 protected function isHashUid( $uid ) {
525 return strlen( $uid ) == 31;
526 }
527
528 /**
529 * Get a connection to the server that handles all sub-queues for this queue
530 *
531 * @return Array (server name, Redis instance)
532 * @throws MWException
533 */
534 protected function getConnection() {
535 $conn = $this->redisPool->getConnection( $this->server );
536 if ( !$conn ) {
537 throw new MWException( "Unable to connect to redis server." );
538 }
539 return $conn;
540 }
541
542 /**
543 * @param $server string
544 * @param $conn RedisConnRef
545 * @param $e RedisException
546 * @throws MWException
547 */
548 protected function throwRedisException( $server, RedisConnRef $conn, $e ) {
549 $this->redisPool->handleException( $server, $conn, $e );
550 throw new MWException( "Redis server error: {$e->getMessage()}\n" );
551 }
552
553 /**
554 * @param $prop string
555 * @return string
556 */
557 private function getQueueKey( $prop ) {
558 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
559 if ( strlen( $this->key ) ) { // namespaced queue (for testing)
560 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type, $this->key, $prop );
561 } else {
562 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type, $prop );
563 }
564 }
565
566 /**
567 * @param string $signature Hash identifier of the root job
568 * @return string
569 */
570 private function getRootJobKey( $signature ) {
571 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
572 return wfForeignMemcKey( $db, $prefix, 'jobqueue', $this->type, 'rootjob', $signature );
573 }
574
575 /**
576 * @param $prop string
577 * @param $string string
578 * @return string
579 */
580 private function prefixWithQueueKey( $prop, $string ) {
581 return $this->getQueueKey( $prop ) . ':' . $string;
582 }
583
584 /**
585 * @param $prop string
586 * @param $items array
587 * @return Array
588 */
589 private function prefixValuesWithQueueKey( $prop, array $items ) {
590 $res = array();
591 foreach ( $items as $item ) {
592 $res[] = $this->prefixWithQueueKey( $prop, $item );
593 }
594 return $res;
595 }
596
597 /**
598 * @param $prop string
599 * @param $items array
600 * @return Array
601 */
602 private function prefixKeysWithQueueKey( $prop, array $items ) {
603 $res = array();
604 foreach ( $items as $key => $item ) {
605 $res[$this->prefixWithQueueKey( $prop, $key )] = $item;
606 }
607 return $res;
608 }
609
610 /**
611 * @param $key string
612 * @return void
613 */
614 public function setTestingPrefix( $key ) {
615 $this->key = $key;
616 }
617 }