Merge "Made pool counter better handled nested calls"
[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 * @params include:
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 * @param array $params
47 */
48 protected function __construct( array $params ) {
49 parent::__construct( $params );
50 $this->servers = isset( $params['redisServers'] )
51 ? $params['redisServers']
52 : array( $params['redisServer'] ); // b/c
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->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
81 $conn->exec();
82
83 return true;
84 } catch ( RedisException $e ) {
85 $this->handleException( $conn, $e );
86
87 return false;
88 }
89 }
90
91 protected function doGetAllReadyWikiQueues() {
92 $conn = $this->getConnection();
93 if ( !$conn ) {
94 return array();
95 }
96 try {
97 $conn->multi( Redis::PIPELINE );
98 $conn->exists( $this->getReadyQueueKey() );
99 $conn->hGetAll( $this->getReadyQueueKey() );
100 list( $exists, $map ) = $conn->exec();
101
102 if ( $exists ) { // cache hit
103 $pendingDBs = array(); // (type => list of wikis)
104 foreach ( $map as $key => $time ) {
105 list( $type, $wiki ) = $this->dencQueueName( $key );
106 $pendingDBs[$type][] = $wiki;
107 }
108 } else { // cache miss
109 // Avoid duplicated effort
110 $rand = wfRandomString( 32 );
111 $conn->multi( Redis::MULTI );
112 $conn->setex( "{$rand}:lock", 3600, 1 );
113 $conn->renamenx( "{$rand}:lock", $this->getReadyQueueKey() . ":lock" );
114 if ( $conn->exec() !== array( true, true ) ) { // lock
115 $conn->delete( "{$rand}:lock" );
116 return array(); // already in progress
117 }
118
119 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
120
121 $conn->multi( Redis::PIPELINE );
122 $now = time();
123 $map = array();
124 foreach ( $pendingDBs as $type => $wikis ) {
125 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
126 foreach ( $wikis as $wiki ) {
127 $map[$this->encQueueName( $type, $wiki )] = $now;
128 }
129 }
130 $conn->hMSet( $this->getReadyQueueKey(), $map );
131 $conn->exec();
132
133 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
134 }
135
136 return $pendingDBs;
137 } catch ( RedisException $e ) {
138 $this->handleException( $conn, $e );
139
140 return array();
141 }
142 }
143
144 protected function doPurge() {
145 $conn = $this->getConnection();
146 if ( !$conn ) {
147 return false;
148 }
149 try {
150 $conn->delete( $this->getReadyQueueKey() );
151 // leave key at getQueueTypesKey() alone
152 } catch ( RedisException $e ) {
153 $this->handleException( $conn, $e );
154
155 return false;
156 }
157
158 return true;
159 }
160
161 /**
162 * Get a connection to the server that handles all sub-queues for this queue
163 *
164 * @return RedisConnRef|bool Returns false on failure
165 * @throws MWException
166 */
167 protected function getConnection() {
168 $conn = false;
169 foreach ( $this->servers as $server ) {
170 $conn = $this->redisPool->getConnection( $server );
171 if ( $conn ) {
172 break;
173 }
174 }
175
176 return $conn;
177 }
178
179 /**
180 * @param RedisConnRef $conn
181 * @param RedisException $e
182 * @return void
183 */
184 protected function handleException( RedisConnRef $conn, $e ) {
185 $this->redisPool->handleError( $conn, $e );
186 }
187
188 /**
189 * @return string
190 */
191 private function getReadyQueueKey() {
192 return "jobqueue:aggregator:h-ready-queues:v1"; // global
193 }
194
195 /**
196 * @return string
197 */
198 private function getQueueTypesKey() {
199 return "jobqueue:aggregator:h-queue-types:v1"; // global
200 }
201
202 /**
203 * @param string $type
204 * @param string $wiki
205 * @return string
206 */
207 private function encQueueName( $type, $wiki ) {
208 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
209 }
210
211 /**
212 * @param string $name
213 * @return string
214 */
215 private function dencQueueName( $name ) {
216 list( $type, $wiki ) = explode( '/', $name, 2 );
217
218 return array( rawurldecode( $type ), rawurldecode( $wiki ) );
219 }
220 }