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