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