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 */
37 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
38 * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
39 * If a hostname is specified but no port, the standard port number
40 * 6379 will be used. Required.
41 * @param array $params
43 protected function __construct( array $params ) {
44 parent
::__construct( $params );
45 $this->server
= $params['redisServer'];
46 $this->redisPool
= RedisConnectionPool
::singleton( $params['redisConfig'] );
50 * @see JobQueueAggregator::doNotifyQueueEmpty()
52 protected function doNotifyQueueEmpty( $wiki, $type ) {
53 $conn = $this->getConnection();
58 $conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
60 } catch ( RedisException
$e ) {
61 $this->handleException( $conn, $e );
67 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
69 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
70 $conn = $this->getConnection();
75 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
77 } catch ( RedisException
$e ) {
78 $this->handleException( $conn, $e );
84 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
86 protected function doGetAllReadyWikiQueues() {
87 $conn = $this->getConnection();
92 $conn->multi( Redis
::PIPELINE
);
93 $conn->exists( $this->getReadyQueueKey() );
94 $conn->hGetAll( $this->getReadyQueueKey() );
95 list( $exists, $map ) = $conn->exec();
97 if ( $exists ) { // cache hit
98 $pendingDBs = array(); // (type => list of wikis)
99 foreach ( $map as $key => $time ) {
100 list( $type, $wiki ) = $this->dencQueueName( $key );
101 $pendingDBs[$type][] = $wiki;
103 } else { // cache miss
104 // Avoid duplicated effort
105 $conn->multi( Redis
::MULTI
);
106 $conn->setnx( $this->getReadyQueueKey() . ":lock", 1 );
107 $conn->expire( $this->getReadyQueueKey() . ":lock", 3600 );
108 if ( $conn->exec() !== array( true, true ) ) { // lock
109 return array(); // already in progress
112 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
114 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
118 foreach ( $pendingDBs as $type => $wikis ) {
119 foreach ( $wikis as $wiki ) {
120 $map[$this->encQueueName( $type, $wiki )] = $now;
123 $conn->hMSet( $this->getReadyQueueKey(), $map );
127 } catch ( RedisException
$e ) {
128 $this->handleException( $conn, $e );
134 * @see JobQueueAggregator::doPurge()
136 protected function doPurge() {
137 $conn = $this->getConnection();
142 $conn->delete( $this->getReadyQueueKey() );
143 } catch ( RedisException
$e ) {
144 $this->handleException( $conn, $e );
151 * Get a connection to the server that handles all sub-queues for this queue
153 * @return Array (server name, Redis instance)
154 * @throws MWException
156 protected function getConnection() {
157 return $this->redisPool
->getConnection( $this->server
);
161 * @param RedisConnRef $conn
162 * @param RedisException $e
165 protected function handleException( RedisConnRef
$conn, $e ) {
166 $this->redisPool
->handleException( $this->server
, $conn, $e );
172 private function getReadyQueueKey() {
173 return "jobqueue:aggregator:h-ready-queues:v1"; // global
177 * @param string $type
178 * @param string $wiki
181 private function encQueueName( $type, $wiki ) {
182 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
186 * @param string $name
189 private function dencQueueName( $name ) {
190 list( $type, $wiki ) = explode( '/', $name, 2 );
191 return array( rawurldecode( $type ), rawurldecode( $wiki ) );