Merge "Validates wgArticlePath does start with slash (/)."
[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 // Make sure doNotifyQueueNonEmpty() takes precedence to avoid races
63 $conn->watch( $this->getReadyQueueKey() );
64 $conn->multi()
65 ->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) )
66 ->exec();
67
68 return true;
69 } catch ( RedisException $e ) {
70 $this->handleException( $conn, $e );
71
72 return false;
73 }
74 }
75
76 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
77 $conn = $this->getConnection();
78 if ( !$conn ) {
79 return false;
80 }
81 try {
82 $conn->multi( Redis::PIPELINE );
83 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
84 $conn->sAdd( $this->getWikiSetKey(), $wiki );
85 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
86 $conn->exec();
87
88 return true;
89 } catch ( RedisException $e ) {
90 $this->handleException( $conn, $e );
91
92 return false;
93 }
94 }
95
96 protected function doGetAllReadyWikiQueues() {
97 $conn = $this->getConnection();
98 if ( !$conn ) {
99 return array();
100 }
101 try {
102 $map = $conn->hGetAll( $this->getReadyQueueKey() );
103
104 if ( is_array( $map ) && isset( $map['_epoch'] ) ) {
105 unset( $map['_epoch'] ); // ignore
106 $pendingDBs = array(); // (type => list of wikis)
107 foreach ( $map as $key => $time ) {
108 list( $type, $wiki ) = $this->dencQueueName( $key );
109 $pendingDBs[$type][] = $wiki;
110 }
111 } else {
112 // Avoid duplicated effort
113 $rand = wfRandomString( 32 );
114 $conn->multi( Redis::MULTI );
115 $conn->setex( "{$rand}:lock", 3600, 1 );
116 $conn->renamenx( "{$rand}:lock", $this->getReadyQueueKey() . ":lock" );
117 if ( $conn->exec() !== array( true, true ) ) { // lock
118 $conn->delete( "{$rand}:lock" );
119 return array(); // already in progress
120 }
121
122 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
123
124 $conn->multi( Redis::PIPELINE );
125 $now = time();
126 $map = array( '_epoch' => time() ); // dummy key for empty Redis collections
127 foreach ( $pendingDBs as $type => $wikis ) {
128 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
129 foreach ( $wikis as $wiki ) {
130 $map[$this->encQueueName( $type, $wiki )] = $now;
131 }
132 }
133 $conn->hMSet( $this->getReadyQueueKey(), $map );
134 $conn->exec();
135
136 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
137 }
138
139 return $pendingDBs;
140 } catch ( RedisException $e ) {
141 $this->handleException( $conn, $e );
142
143 return array();
144 }
145 }
146
147 protected function doPurge() {
148 $conn = $this->getConnection();
149 if ( !$conn ) {
150 return false;
151 }
152 try {
153 $conn->delete( $this->getReadyQueueKey() );
154 // leave key at getQueueTypesKey() alone
155 } catch ( RedisException $e ) {
156 $this->handleException( $conn, $e );
157
158 return false;
159 }
160
161 return true;
162 }
163
164 /**
165 * Get a connection to the server that handles all sub-queues for this queue
166 *
167 * @return RedisConnRef|bool Returns false on failure
168 * @throws MWException
169 */
170 protected function getConnection() {
171 $conn = false;
172 foreach ( $this->servers as $server ) {
173 $conn = $this->redisPool->getConnection( $server );
174 if ( $conn ) {
175 break;
176 }
177 }
178
179 return $conn;
180 }
181
182 /**
183 * @param RedisConnRef $conn
184 * @param RedisException $e
185 * @return void
186 */
187 protected function handleException( RedisConnRef $conn, $e ) {
188 $this->redisPool->handleError( $conn, $e );
189 }
190
191 /**
192 * @return string
193 */
194 private function getReadyQueueKey() {
195 return "jobqueue:aggregator:h-ready-queues:v2"; // global
196 }
197
198 /**
199 * @return string
200 */
201 private function getQueueTypesKey() {
202 return "jobqueue:aggregator:h-queue-types:v2"; // global
203 }
204
205 /**
206 * @return string
207 */
208 private function getWikiSetKey() {
209 return "jobqueue:aggregator:s-wikis:v2"; // global
210 }
211
212 /**
213 * @param string $type
214 * @param string $wiki
215 * @return string
216 */
217 private function encQueueName( $type, $wiki ) {
218 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
219 }
220
221 /**
222 * @param string $name
223 * @return string
224 */
225 private function dencQueueName( $name ) {
226 list( $type, $wiki ) = explode( '/', $name, 2 );
227
228 return array( rawurldecode( $type ), rawurldecode( $wiki ) );
229 }
230 }