Add Release notes for I1e24733c
[lhc/web/wiklou.git] / includes / job / JobQueueAggregatorMemc.php
1 <?php
2 /**
3 * Job queue aggregator code that uses BagOStuff.
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 tracking information about all queues using BagOStuff
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueAggregatorMemc extends JobQueueAggregator {
31 /** @var BagOStuff */
32 protected $cache;
33
34 protected $cacheTTL; // integer; seconds
35
36 /**
37 * @params include:
38 * - objectCache : Name of an object cache registered in $wgObjectCaches.
39 * This defaults to the one specified by $wgMainCacheType.
40 * - cacheTTL : Seconds to cache the aggregate data before regenerating.
41 * @param array $params
42 */
43 protected function __construct( array $params ) {
44 parent::__construct( $params );
45 $this->cache = isset( $params['objectCache'] )
46 ? wfGetCache( $params['objectCache'] )
47 : wfGetMainCache();
48 $this->cacheTTL = isset( $params['cacheTTL'] ) ? $params['cacheTTL'] : 180; // 3 min
49 }
50
51 /**
52 * @see JobQueueAggregator::doNotifyQueueEmpty()
53 */
54 protected function doNotifyQueueEmpty( $wiki, $type ) {
55 $key = $this->getReadyQueueCacheKey();
56 // Delist the queue from the "ready queue" list
57 if ( $this->cache->add( "$key:lock", 1, 60 ) ) { // lock
58 $curInfo = $this->cache->get( $key );
59 if ( is_array( $curInfo ) && isset( $curInfo['pendingDBs'][$type] ) ) {
60 if ( in_array( $wiki, $curInfo['pendingDBs'][$type] ) ) {
61 $curInfo['pendingDBs'][$type] = array_diff(
62 $curInfo['pendingDBs'][$type], array( $wiki ) );
63 $this->cache->set( $key, $curInfo );
64 }
65 }
66 $this->cache->delete( "$key:lock" ); // unlock
67 }
68 return true;
69 }
70
71 /**
72 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
73 */
74 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
75 return true; // updated periodically
76 }
77
78 /**
79 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
80 */
81 protected function doGetAllReadyWikiQueues() {
82 $key = $this->getReadyQueueCacheKey();
83 // If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
84 // regenerate the cache. Use any available stale cache if another process is
85 // currently regenerating the pending DB information.
86 $pendingDbInfo = $this->cache->get( $key );
87 if ( !is_array( $pendingDbInfo )
88 || ( time() - $pendingDbInfo['timestamp'] ) > $this->cacheTTL
89 || mt_rand( 0, 999 ) == 0
90 ) {
91 if ( $this->cache->add( "$key:rebuild", 1, 1800 ) ) { // lock
92 $pendingDbInfo = array(
93 'pendingDBs' => $this->findPendingWikiQueues(),
94 'timestamp' => time()
95 );
96 for ( $attempts = 1; $attempts <= 25; ++$attempts ) {
97 if ( $this->cache->add( "$key:lock", 1, 60 ) ) { // lock
98 $this->cache->set( $key, $pendingDbInfo );
99 $this->cache->delete( "$key:lock" ); // unlock
100 break;
101 }
102 }
103 $this->cache->delete( "$key:rebuild" ); // unlock
104 }
105 }
106 return is_array( $pendingDbInfo )
107 ? $pendingDbInfo['pendingDBs']
108 : array(); // cache is both empty and locked
109 }
110
111 /**
112 * @see JobQueueAggregator::doPurge()
113 */
114 protected function doPurge() {
115 return $this->cache->delete( $this->getReadyQueueCacheKey() );
116 }
117
118 /**
119 * @return string
120 */
121 private function getReadyQueueCacheKey() {
122 return "jobqueue:aggregator:ready-queues:v1"; // global
123 }
124 }