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