2013-02-05 20:00:24 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Job queue aggregator code that uses PhpRedis.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Aaron Schulz
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to handle tracking information about all queues using PhpRedis
|
|
|
|
|
*
|
|
|
|
|
* @ingroup JobQueue
|
2013-03-18 22:14:55 +00:00
|
|
|
* @ingroup Redis
|
2013-02-05 20:00:24 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
*/
|
|
|
|
|
class JobQueueAggregatorRedis extends JobQueueAggregator {
|
|
|
|
|
/** @var RedisConnectionPool */
|
|
|
|
|
protected $redisPool;
|
|
|
|
|
|
2013-11-25 17:12:42 +00:00
|
|
|
/** @var array List of Redis server addresses */
|
2013-11-17 22:04:04 +00:00
|
|
|
protected $servers;
|
|
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
/**
|
|
|
|
|
* @params include:
|
2013-11-17 22:04:04 +00:00
|
|
|
* - redisConfig : An array of parameters to RedisConnectionPool::__construct().
|
|
|
|
|
* - redisServers : Array of server entries, the first being the primary and the
|
|
|
|
|
* others being fallback servers. Each entry is either a hostname/port
|
|
|
|
|
* combination or the absolute path of a UNIX socket.
|
|
|
|
|
* If a hostname is specified but no port, the standard port number
|
|
|
|
|
* 6379 will be used. Required.
|
2013-02-05 20:00:24 +00:00
|
|
|
* @param array $params
|
|
|
|
|
*/
|
|
|
|
|
protected function __construct( array $params ) {
|
|
|
|
|
parent::__construct( $params );
|
2013-11-17 22:04:04 +00:00
|
|
|
$this->servers = isset( $params['redisServers'] )
|
|
|
|
|
? $params['redisServers']
|
|
|
|
|
: array( $params['redisServer'] ); // b/c
|
2014-07-15 20:43:35 +00:00
|
|
|
$params['redisConfig']['serializer'] = 'none';
|
2013-02-05 20:00:24 +00:00
|
|
|
$this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doNotifyQueueEmpty( $wiki, $type ) {
|
|
|
|
|
$conn = $this->getConnection();
|
|
|
|
|
if ( !$conn ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
return true;
|
|
|
|
|
} catch ( RedisException $e ) {
|
|
|
|
|
$this->handleException( $conn, $e );
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doNotifyQueueNonEmpty( $wiki, $type ) {
|
|
|
|
|
$conn = $this->getConnection();
|
|
|
|
|
if ( !$conn ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2014-06-03 20:53:17 +00:00
|
|
|
$conn->multi( Redis::PIPELINE );
|
|
|
|
|
$conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
|
2013-02-05 20:00:24 +00:00
|
|
|
$conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
|
2014-06-03 20:53:17 +00:00
|
|
|
$conn->exec();
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
return true;
|
|
|
|
|
} catch ( RedisException $e ) {
|
|
|
|
|
$this->handleException( $conn, $e );
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doGetAllReadyWikiQueues() {
|
|
|
|
|
$conn = $this->getConnection();
|
|
|
|
|
if ( !$conn ) {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
try {
|
2014-07-15 20:43:35 +00:00
|
|
|
$map = $conn->hGetAll( $this->getReadyQueueKey() );
|
2013-02-05 20:00:24 +00:00
|
|
|
|
2014-07-15 20:43:35 +00:00
|
|
|
if ( is_array( $map ) && isset( $map['_epoch'] ) ) {
|
|
|
|
|
unset( $map['_epoch'] ); // ignore
|
2013-02-05 20:00:24 +00:00
|
|
|
$pendingDBs = array(); // (type => list of wikis)
|
|
|
|
|
foreach ( $map as $key => $time ) {
|
|
|
|
|
list( $type, $wiki ) = $this->dencQueueName( $key );
|
|
|
|
|
$pendingDBs[$type][] = $wiki;
|
|
|
|
|
}
|
2014-07-15 20:43:35 +00:00
|
|
|
} else {
|
2013-03-14 20:39:46 +00:00
|
|
|
// Avoid duplicated effort
|
2013-12-29 21:44:33 +00:00
|
|
|
$rand = wfRandomString( 32 );
|
2013-03-14 20:39:46 +00:00
|
|
|
$conn->multi( Redis::MULTI );
|
2013-12-29 21:44:33 +00:00
|
|
|
$conn->setex( "{$rand}:lock", 3600, 1 );
|
|
|
|
|
$conn->renamenx( "{$rand}:lock", $this->getReadyQueueKey() . ":lock" );
|
2013-03-14 20:39:46 +00:00
|
|
|
if ( $conn->exec() !== array( true, true ) ) { // lock
|
2013-12-29 21:44:33 +00:00
|
|
|
$conn->delete( "{$rand}:lock" );
|
2013-03-14 20:39:46 +00:00
|
|
|
return array(); // already in progress
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
$pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
|
|
|
|
|
|
2014-06-05 16:24:59 +00:00
|
|
|
$conn->multi( Redis::PIPELINE );
|
2013-02-05 20:00:24 +00:00
|
|
|
$now = time();
|
2014-07-15 20:43:35 +00:00
|
|
|
$map = array( '_epoch' => time() ); // dummy key for empty Redis collections
|
2013-02-05 20:00:24 +00:00
|
|
|
foreach ( $pendingDBs as $type => $wikis ) {
|
2014-06-05 16:24:59 +00:00
|
|
|
$conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
|
2013-02-05 20:00:24 +00:00
|
|
|
foreach ( $wikis as $wiki ) {
|
|
|
|
|
$map[$this->encQueueName( $type, $wiki )] = $now;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$conn->hMSet( $this->getReadyQueueKey(), $map );
|
2014-06-05 16:24:59 +00:00
|
|
|
$conn->exec();
|
|
|
|
|
|
|
|
|
|
$conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
|
2013-02-05 20:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $pendingDBs;
|
|
|
|
|
} catch ( RedisException $e ) {
|
|
|
|
|
$this->handleException( $conn, $e );
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 20:03:13 +00:00
|
|
|
protected function doPurge() {
|
|
|
|
|
$conn = $this->getConnection();
|
|
|
|
|
if ( !$conn ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$conn->delete( $this->getReadyQueueKey() );
|
2014-06-05 16:24:59 +00:00
|
|
|
// leave key at getQueueTypesKey() alone
|
2013-03-14 20:03:13 +00:00
|
|
|
} catch ( RedisException $e ) {
|
|
|
|
|
$this->handleException( $conn, $e );
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-03-14 20:03:13 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-03-14 20:03:13 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-02-05 20:00:24 +00:00
|
|
|
* Get a connection to the server that handles all sub-queues for this queue
|
|
|
|
|
*
|
2013-11-17 22:04:04 +00:00
|
|
|
* @return RedisConnRef|bool Returns false on failure
|
2013-02-05 20:00:24 +00:00
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
protected function getConnection() {
|
2013-11-17 22:04:04 +00:00
|
|
|
$conn = false;
|
|
|
|
|
foreach ( $this->servers as $server ) {
|
|
|
|
|
$conn = $this->redisPool->getConnection( $server );
|
|
|
|
|
if ( $conn ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-11-17 22:04:04 +00:00
|
|
|
return $conn;
|
2013-02-05 20:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param RedisConnRef $conn
|
|
|
|
|
* @param RedisException $e
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function handleException( RedisConnRef $conn, $e ) {
|
2013-12-18 00:55:09 +00:00
|
|
|
$this->redisPool->handleError( $conn, $e );
|
2013-02-05 20:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function getReadyQueueKey() {
|
2014-07-15 20:43:35 +00:00
|
|
|
return "jobqueue:aggregator:h-ready-queues:v2"; // global
|
2013-02-05 20:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
2014-06-03 20:53:17 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function getQueueTypesKey() {
|
2014-07-15 20:43:35 +00:00
|
|
|
return "jobqueue:aggregator:h-queue-types:v2"; // global
|
2014-06-03 20:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param string $wiki
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function encQueueName( $type, $wiki ) {
|
|
|
|
|
return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function dencQueueName( $name ) {
|
|
|
|
|
list( $type, $wiki ) = explode( '/', $name, 2 );
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
return array( rawurldecode( $type ), rawurldecode( $wiki ) );
|
|
|
|
|
}
|
|
|
|
|
}
|