--- /dev/null
+<?php
+/**
+ * This file deals with RAII style scoped callbacks.
+ *
+ * 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
+ */
+
+/**
+ * Class for asserting that a callback happens when an dummy object leaves scope
+ */
+class ScopedCallback {
+ /** @var Closure */
+ protected $callback;
+
+ /**
+ * @param $callback Closure
+ */
+ public function __construct( Closure $callback ) {
+ $this->callback = $callback;
+ }
+
+ function __destruct() {
+ call_user_func( $this->callback );
+ }
+}
const MAX_JOB_RANDOM = 2147483647; // integer; 2^31 - 1, used for job_random
const MAX_OFFSET = 255; // integer; maximum number of rows to skip
+ protected $cluster = false; // string; name of an external DB cluster
+
+ /**
+ * Additional parameters include:
+ * - cluster : The name of an external cluster registered via LBFactory.
+ * If not specified, the primary DB cluster for the wiki will be used.
+ * This can be overridden with a custom cluster so that DB handles will
+ * be retrieved via LBFactory::getExternalLB() and getConnection().
+ * @param $params array
+ */
+ protected function __construct( array $params ) {
+ parent::__construct( $params );
+ $this->cluster = isset( $params['cluster'] ) ? $params['cluster'] : false;
+ }
+
/**
* @see JobQueue::doIsEmpty()
* @return bool
return false;
}
- $found = $this->getSlaveDB()->selectField( // unclaimed job
+ list( $dbr, $scope ) = $this->getSlaveDB();
+ $found = $dbr->selectField( // unclaimed job
'job', '1', array( 'job_cmd' => $this->type, 'job_token' => '' ), __METHOD__
);
$wgMemc->add( $key, $found ? 'false' : 'true', self::CACHE_TTL_LONG );
*/
protected function doBatchPush( array $jobs, $flags ) {
if ( count( $jobs ) ) {
- $dbw = $this->getMasterDB();
+ list( $dbw, $scope ) = $this->getMasterDB();
$rowSet = array(); // (sha1 => job) map for jobs that are de-duplicated
$rowList = array(); // list of jobs for jobs that are are not de-duplicated
$ttl = self::CACHE_TTL_LONG;
$dbw->onTransactionIdle(
- function() use ( $dbw, $rowSet, $rowList, $atomic, $key, $ttl
+ function() use ( $dbw, $rowSet, $rowList, $atomic, $key, $ttl, $scope
) {
global $wgMemc;
return false; // queue is empty
}
- $dbw = $this->getMasterDB();
+ list( $dbw, $scope ) = $this->getMasterDB();
$dbw->commit( __METHOD__, 'flush' ); // flush existing transaction
$uuid = wfRandomString( 32 ); // pop attempt
protected function claimRandom( $uuid, $rand, $gte ) {
global $wgMemc;
- $dbw = $this->getMasterDB();
+ list( $dbw, $scope ) = $this->getMasterDB();
// Check cache to see if the queue has <= OFFSET items
$tinyQueue = $wgMemc->get( $this->getCacheKey( 'small' ) );
* @return Row|false
*/
protected function claimOldest( $uuid ) {
- $dbw = $this->getMasterDB();
+ list( $dbw, $scope ) = $this->getMasterDB();
$row = false; // the row acquired
do {
*/
protected function recycleStaleJobs() {
$now = time();
- $dbw = $this->getMasterDB();
+ list( $dbw, $scope ) = $this->getMasterDB();
$count = 0; // affected rows
if ( !$dbw->lock( "jobqueue-recycle-{$this->type}", __METHOD__, 1 ) ) {
throw new MWException( "Job of type '{$job->getType()}' has no ID." );
}
- $dbw = $this->getMasterDB();
+ list( $dbw, $scope ) = $this->getMasterDB();
$dbw->commit( __METHOD__, 'flush' ); // flush existing transaction
// Delete a row with a single DELETE without holding row locks over RTTs...
// deferred till "transaction idle", do the same here, so that the ordering is
// maintained. Having only the de-duplication registration succeed would cause
// jobs to become no-ops without any actual jobs that made them redundant.
- $this->getMasterDB()->onTransactionIdle( function() use ( $params, $key ) {
+ list( $dbw, $scope ) = $this->getMasterDB();
+ $dbw->onTransactionIdle( function() use ( $params, $key, $scope ) {
global $wgMemc;
$timestamp = $wgMemc->get( $key ); // current last timestamp of this job
}
/**
- * @return DatabaseBase
+ * @return Array (DatabaseBase, ScopedCallback)
*/
protected function getSlaveDB() {
- return wfGetDB( DB_SLAVE, array(), $this->wiki );
+ return $this->getDB( DB_SLAVE );
}
/**
- * @return DatabaseBase
+ * @return Array (DatabaseBase, ScopedCallback)
*/
protected function getMasterDB() {
- return wfGetDB( DB_MASTER, array(), $this->wiki );
+ return $this->getDB( DB_MASTER );
+ }
+
+ /**
+ * @param $index integer (DB_SLAVE/DB_MASTER)
+ * @return Array (DatabaseBase, ScopedCallback)
+ */
+ protected function getDB( $index ) {
+ $lb = ( $this->cluster !== false )
+ ? wfGetLBFactory()->getExternalLB( $this->cluster, $this->wiki )
+ : wfGetLB( $this->wiki );
+ $conn = $lb->getConnection( $index );
+ return array(
+ $conn,
+ new ScopedCallback( function() use ( $lb, $conn ) {
+ $lb->reuseConnection( $conn );
+ } )
+ );
}
/**
* @return array
*/
protected function insertFields( Job $job ) {
- $dbw = $this->getMasterDB();
+ list( $dbw, $scope ) = $this->getMasterDB();
return array(
// Fields that describe the nature of the job
'job_cmd' => $job->getType(),