Add 'mobile' target to 'mediawiki.raggett' module
[lhc/web/wiklou.git] / includes / jobqueue / aggregator / JobQueueAggregatorRedis.php
1 <?php
2 /**
3 * Job queue aggregator code that uses PhpRedis.
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 PhpRedis
26 *
27 * @ingroup JobQueue
28 * @ingroup Redis
29 * @since 1.21
30 */
31 class JobQueueAggregatorRedis extends JobQueueAggregator {
32 /** @var RedisConnectionPool */
33 protected $redisPool;
34 /** @var array List of Redis server addresses */
35 protected $servers;
36 /** @var bool */
37 protected $registeredQueue = false;
38
39 /**
40 * @param array $params Possible keys:
41 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
42 * - redisServers : Array of server entries, the first being the primary and the
43 * others being fallback servers. Each entry is either a hostname/port
44 * 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 */
48 public function __construct( array $params ) {
49 parent::__construct( $params );
50 $this->servers = isset( $params['redisServers'] )
51 ? $params['redisServers']
52 : array( $params['redisServer'] ); // b/c
53 $params['redisConfig']['serializer'] = 'none';
54 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
55 }
56
57 protected function doNotifyQueueEmpty( $wiki, $type ) {
58 $conn = $this->getConnection();
59 if ( !$conn ) {
60 return false;
61 }
62 try {
63 // Make sure doNotifyQueueNonEmpty() takes precedence to avoid races
64 $conn->watch( $this->getReadyQueueKey() );
65 $conn->multi()
66 ->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) )
67 ->exec();
68
69 return true;
70 } catch ( RedisException $e ) {
71 $this->handleException( $conn, $e );
72
73 return false;
74 }
75 }
76
77 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
78 $conn = $this->getConnection();
79 if ( !$conn ) {
80 return false;
81 }
82 try {
83 $conn->multi( Redis::PIPELINE );
84 if ( !$this->registeredQueue ) {
85 // Make sure the queue is registered as existing
86 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
87 $conn->sAdd( $this->getWikiSetKey(), $wiki );
88 }
89 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
90 $conn->exec();
91
92 $this->registeredQueue = true;
93
94 return true;
95 } catch ( RedisException $e ) {
96 $this->handleException( $conn, $e );
97
98 return false;
99 }
100 }
101
102 protected function doGetAllReadyWikiQueues() {
103 $conn = $this->getConnection();
104 if ( !$conn ) {
105 return array();
106 }
107 try {
108 $map = $conn->hGetAll( $this->getReadyQueueKey() );
109
110 if ( is_array( $map ) && isset( $map['_epoch'] ) ) {
111 unset( $map['_epoch'] ); // ignore
112 $pendingDBs = array(); // (type => list of wikis)
113 foreach ( $map as $key => $time ) {
114 list( $type, $wiki ) = $this->dencQueueName( $key );
115 $pendingDBs[$type][] = $wiki;
116 }
117 } else {
118 // Avoid duplicated effort
119 $rand = wfRandomString( 32 );
120 $conn->multi( Redis::MULTI );
121 $conn->setex( "{$rand}:lock", 3600, 1 );
122 $conn->renamenx( "{$rand}:lock", $this->getReadyQueueKey() . ":lock" );
123 if ( $conn->exec() !== array( true, true ) ) { // lock
124 $conn->delete( "{$rand}:lock" );
125 return array(); // already in progress
126 }
127
128 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
129
130 $conn->multi( Redis::PIPELINE );
131 $now = time();
132 $map = array( '_epoch' => time() ); // dummy key for empty Redis collections
133 foreach ( $pendingDBs as $type => $wikis ) {
134 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
135 foreach ( $wikis as $wiki ) {
136 $map[$this->encQueueName( $type, $wiki )] = $now;
137 }
138 }
139 $conn->hMSet( $this->getReadyQueueKey(), $map );
140 $conn->exec();
141
142 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
143 }
144
145 return $pendingDBs;
146 } catch ( RedisException $e ) {
147 $this->handleException( $conn, $e );
148
149 return array();
150 }
151 }
152
153 protected function doPurge() {
154 $conn = $this->getConnection();
155 if ( !$conn ) {
156 return false;
157 }
158 try {
159 $conn->delete( $this->getReadyQueueKey() );
160 // leave key at getQueueTypesKey() alone
161 } catch ( RedisException $e ) {
162 $this->handleException( $conn, $e );
163
164 return false;
165 }
166
167 return true;
168 }
169
170 /**
171 * Get a connection to the server that handles all sub-queues for this queue
172 *
173 * @return RedisConnRef|bool Returns false on failure
174 * @throws MWException
175 */
176 protected function getConnection() {
177 $conn = false;
178 foreach ( $this->servers as $server ) {
179 $conn = $this->redisPool->getConnection( $server );
180 if ( $conn ) {
181 break;
182 }
183 }
184
185 return $conn;
186 }
187
188 /**
189 * @param RedisConnRef $conn
190 * @param RedisException $e
191 * @return void
192 */
193 protected function handleException( RedisConnRef $conn, $e ) {
194 $this->redisPool->handleError( $conn, $e );
195 }
196
197 /**
198 * @return string
199 */
200 private function getReadyQueueKey() {
201 return "jobqueue:aggregator:h-ready-queues:v2"; // global
202 }
203
204 /**
205 * @return string
206 */
207 private function getQueueTypesKey() {
208 return "jobqueue:aggregator:h-queue-types:v2"; // global
209 }
210
211 /**
212 * @return string
213 */
214 private function getWikiSetKey() {
215 return "jobqueue:aggregator:s-wikis:v2"; // global
216 }
217
218 /**
219 * @param string $type
220 * @param string $wiki
221 * @return string
222 */
223 private function encQueueName( $type, $wiki ) {
224 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
225 }
226
227 /**
228 * @param string $name
229 * @return string
230 */
231 private function dencQueueName( $name ) {
232 list( $type, $wiki ) = explode( '/', $name, 2 );
233
234 return array( rawurldecode( $type ), rawurldecode( $wiki ) );
235 }
236 }