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