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 */
39 * @param array $params Possible keys:
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.
47 public function __construct( array $params ) {
48 parent
::__construct( $params );
49 $this->servers
= isset( $params['redisServers'] )
50 ?
$params['redisServers']
51 : array( $params['redisServer'] ); // b/c
52 $params['redisConfig']['serializer'] = 'none';
53 $this->redisPool
= RedisConnectionPool
::singleton( $params['redisConfig'] );
56 protected function doNotifyQueueEmpty( $wiki, $type ) {
57 $conn = $this->getConnection();
62 // Make sure doNotifyQueueNonEmpty() takes precedence to avoid races
63 $conn->watch( $this->getReadyQueueKey() );
65 ->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) )
69 } catch ( RedisException
$e ) {
70 $this->handleException( $conn, $e );
76 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
77 $conn = $this->getConnection();
82 $conn->multi( Redis
::PIPELINE
);
83 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
84 $conn->sAdd( $this->getWikiSetKey(), $wiki );
85 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
89 } catch ( RedisException
$e ) {
90 $this->handleException( $conn, $e );
96 protected function doGetAllReadyWikiQueues() {
97 $conn = $this->getConnection();
102 $map = $conn->hGetAll( $this->getReadyQueueKey() );
104 if ( is_array( $map ) && isset( $map['_epoch'] ) ) {
105 unset( $map['_epoch'] ); // ignore
106 $pendingDBs = array(); // (type => list of wikis)
107 foreach ( $map as $key => $time ) {
108 list( $type, $wiki ) = $this->dencQueueName( $key );
109 $pendingDBs[$type][] = $wiki;
112 // Avoid duplicated effort
113 $rand = wfRandomString( 32 );
114 $conn->multi( Redis
::MULTI
);
115 $conn->setex( "{$rand}:lock", 3600, 1 );
116 $conn->renamenx( "{$rand}:lock", $this->getReadyQueueKey() . ":lock" );
117 if ( $conn->exec() !== array( true, true ) ) { // lock
118 $conn->delete( "{$rand}:lock" );
119 return array(); // already in progress
122 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
124 $conn->multi( Redis
::PIPELINE
);
126 $map = array( '_epoch' => time() ); // dummy key for empty Redis collections
127 foreach ( $pendingDBs as $type => $wikis ) {
128 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
129 foreach ( $wikis as $wiki ) {
130 $map[$this->encQueueName( $type, $wiki )] = $now;
133 $conn->hMSet( $this->getReadyQueueKey(), $map );
136 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
140 } catch ( RedisException
$e ) {
141 $this->handleException( $conn, $e );
147 protected function doPurge() {
148 $conn = $this->getConnection();
153 $conn->delete( $this->getReadyQueueKey() );
154 // leave key at getQueueTypesKey() alone
155 } catch ( RedisException
$e ) {
156 $this->handleException( $conn, $e );
165 * Get a connection to the server that handles all sub-queues for this queue
167 * @return RedisConnRef|bool Returns false on failure
168 * @throws MWException
170 protected function getConnection() {
172 foreach ( $this->servers
as $server ) {
173 $conn = $this->redisPool
->getConnection( $server );
183 * @param RedisConnRef $conn
184 * @param RedisException $e
187 protected function handleException( RedisConnRef
$conn, $e ) {
188 $this->redisPool
->handleError( $conn, $e );
194 private function getReadyQueueKey() {
195 return "jobqueue:aggregator:h-ready-queues:v2"; // global
201 private function getQueueTypesKey() {
202 return "jobqueue:aggregator:h-queue-types:v2"; // global
208 private function getWikiSetKey() {
209 return "jobqueue:aggregator:s-wikis:v2"; // global
213 * @param string $type
214 * @param string $wiki
217 private function encQueueName( $type, $wiki ) {
218 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
222 * @param string $name
225 private function dencQueueName( $name ) {
226 list( $type, $wiki ) = explode( '/', $name, 2 );
228 return array( rawurldecode( $type ), rawurldecode( $wiki ) );