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 */
35 /** @var Array List of Redis server addresses */
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
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'] );
56 protected function doNotifyQueueEmpty( $wiki, $type ) {
57 $conn = $this->getConnection();
62 $conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
64 } catch ( RedisException
$e ) {
65 $this->handleException( $conn, $e );
70 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
71 $conn = $this->getConnection();
76 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
78 } catch ( RedisException
$e ) {
79 $this->handleException( $conn, $e );
84 protected function doGetAllReadyWikiQueues() {
85 $conn = $this->getConnection();
90 $conn->multi( Redis
::PIPELINE
);
91 $conn->exists( $this->getReadyQueueKey() );
92 $conn->hGetAll( $this->getReadyQueueKey() );
93 list( $exists, $map ) = $conn->exec();
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;
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
110 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
112 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
116 foreach ( $pendingDBs as $type => $wikis ) {
117 foreach ( $wikis as $wiki ) {
118 $map[$this->encQueueName( $type, $wiki )] = $now;
121 $conn->hMSet( $this->getReadyQueueKey(), $map );
125 } catch ( RedisException
$e ) {
126 $this->handleException( $conn, $e );
131 protected function doPurge() {
132 $conn = $this->getConnection();
137 $conn->delete( $this->getReadyQueueKey() );
138 } catch ( RedisException
$e ) {
139 $this->handleException( $conn, $e );
146 * Get a connection to the server that handles all sub-queues for this queue
148 * @return RedisConnRef|bool Returns false on failure
149 * @throws MWException
151 protected function getConnection() {
153 foreach ( $this->servers
as $server ) {
154 $conn = $this->redisPool
->getConnection( $server );
163 * @param RedisConnRef $conn
164 * @param RedisException $e
167 protected function handleException( RedisConnRef
$conn, $e ) {
168 $this->redisPool
->handleException( $conn->getServer(), $conn, $e );
174 private function getReadyQueueKey() {
175 return "jobqueue:aggregator:h-ready-queues:v1"; // global
179 * @param string $type
180 * @param string $wiki
183 private function encQueueName( $type, $wiki ) {
184 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
188 * @param string $name
191 private function dencQueueName( $name ) {
192 list( $type, $wiki ) = explode( '/', $name, 2 );
193 return array( rawurldecode( $type ), rawurldecode( $wiki ) );