3 * Job queue aggregator code that uses PhpRedis.
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.
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.
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
21 * @author Aaron Schulz
25 * Class to handle tracking information about all queues using PhpRedis
31 class JobQueueAggregatorRedis
extends JobQueueAggregator
{
32 /** @var RedisConnectionPool */
34 /** @var array List of Redis server addresses */
37 protected $registeredQueue = false;
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.
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'] );
57 protected function doNotifyQueueEmpty( $wiki, $type ) {
58 $conn = $this->getConnection();
63 // Make sure doNotifyQueueNonEmpty() takes precedence to avoid races
64 $conn->watch( $this->getReadyQueueKey() );
66 ->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) )
70 } catch ( RedisException
$e ) {
71 $this->handleException( $conn, $e );
77 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
78 $conn = $this->getConnection();
83 $conn->multi( Redis
::PIPELINE
);
84 if ( !$this->registeredQueue
) {
85 // Make sure the queue is registered as existing
86 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
87 $conn->sAdd( $this->getWikiSetKey(), $wiki );
89 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
92 $this->registeredQueue
= true;
95 } catch ( RedisException
$e ) {
96 $this->handleException( $conn, $e );
102 protected function doGetAllReadyWikiQueues() {
103 $conn = $this->getConnection();
108 $map = $conn->hGetAll( $this->getReadyQueueKey() );
110 if ( is_array( $map ) && isset( $map['_epoch'] ) ) {
111 unset( $map['_epoch'] ); // ignore
112 $pendingDBs = array(); // (type => list of wikis)
113 foreach ( $map as $key => $time ) {
114 list( $type, $wiki ) = $this->dencQueueName( $key );
115 $pendingDBs[$type][] = $wiki;
118 // Avoid duplicated effort
119 $rand = wfRandomString( 32 );
120 $conn->multi( Redis
::MULTI
);
121 $conn->setex( "{$rand}:lock", 3600, 1 );
122 $conn->renamenx( "{$rand}:lock", $this->getReadyQueueKey() . ":lock" );
123 if ( $conn->exec() !== array( true, true ) ) { // lock
124 $conn->delete( "{$rand}:lock" );
125 return array(); // already in progress
128 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
130 $conn->multi( Redis
::PIPELINE
);
132 $map = array( '_epoch' => time() ); // dummy key for empty Redis collections
133 foreach ( $pendingDBs as $type => $wikis ) {
134 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
135 foreach ( $wikis as $wiki ) {
136 $map[$this->encQueueName( $type, $wiki )] = $now;
139 $conn->hMSet( $this->getReadyQueueKey(), $map );
142 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
146 } catch ( RedisException
$e ) {
147 $this->handleException( $conn, $e );
153 protected function doPurge() {
154 $conn = $this->getConnection();
159 $conn->delete( $this->getReadyQueueKey() );
160 // leave key at getQueueTypesKey() alone
161 } catch ( RedisException
$e ) {
162 $this->handleException( $conn, $e );
171 * Get a connection to the server that handles all sub-queues for this queue
173 * @return RedisConnRef|bool Returns false on failure
174 * @throws MWException
176 protected function getConnection() {
178 foreach ( $this->servers
as $server ) {
179 $conn = $this->redisPool
->getConnection( $server );
189 * @param RedisConnRef $conn
190 * @param RedisException $e
193 protected function handleException( RedisConnRef
$conn, $e ) {
194 $this->redisPool
->handleError( $conn, $e );
200 private function getReadyQueueKey() {
201 return "jobqueue:aggregator:h-ready-queues:v2"; // global
207 private function getQueueTypesKey() {
208 return "jobqueue:aggregator:h-queue-types:v2"; // global
214 private function getWikiSetKey() {
215 return "jobqueue:aggregator:s-wikis:v2"; // global
219 * @param string $type
220 * @param string $wiki
223 private function encQueueName( $type, $wiki ) {
224 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
228 * @param string $name
231 private function dencQueueName( $name ) {
232 list( $type, $wiki ) = explode( '/', $name, 2 );
234 return array( rawurldecode( $type ), rawurldecode( $wiki ) );