X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fdb%2FLoadBalancer.php;h=011c0831ccd720d2a37de034eca585a802f8ade6;hb=75bbe1bbffd6009959ba94ba44b2e4bc9551f7fc;hp=60c283304a477084307641d4854156f08ca7c8e4;hpb=627234a926ac89cad471c7076010c03fbd977a3c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index 60c283304a..011c0831cc 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -167,7 +167,7 @@ class LoadBalancer { #wfDebugLog( 'connect', var_export( $loads, true ) ); # Return a random representative of the remainder - return $this->pickRandom( $loads ); + return ArrayUtils::pickRandom( $loads ); } /** @@ -236,7 +236,7 @@ class LoadBalancer { $currentLoads = $nonErrorLoads; while ( count( $currentLoads ) ) { if ( $wgReadOnly || $this->mAllowLagged || $laggedSlaveMode ) { - $i = $this->pickRandom( $currentLoads ); + $i = ArrayUtils::pickRandom( $currentLoads ); } else { $i = $this->getRandomNonLagged( $currentLoads, $wiki ); if ( $i === false && count( $currentLoads ) != 0 ) { @@ -244,7 +244,7 @@ class LoadBalancer { wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode\n" ); $wgReadOnly = 'The database has been automatically locked ' . 'while the slave database servers catch up to the master'; - $i = $this->pickRandom( $currentLoads ); + $i = ArrayUtils::pickRandom( $currentLoads ); $laggedSlaveMode = true; } } @@ -373,7 +373,9 @@ class LoadBalancer { wfProfileIn( __METHOD__ ); $this->mWaitForPos = $pos; for ( $i = 1; $i < count( $this->mServers ); $i++ ) { - $this->doWait( $i, true ); + if ( $this->mLoads[$i] > 0 ) { + $this->doWait( $i, true ); + } } wfProfileOut( __METHOD__ ); } @@ -561,6 +563,22 @@ class LoadBalancer { return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) ); } + /** + * Get a database connection handle reference without connecting yet + * + * The handle's methods wrap simply wrap those of a DatabaseBase handle + * + * @see LoadBalancer::getConnection() for parameter information + * + * @param integer $db + * @param mixed $groups + * @param string $wiki + * @return DBConnRef + */ + public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) { + return new DBConnRef( $this, array( $db, $groups, $wiki ) ); + } + /** * Open a connection to the server given by the specified index * Index must be an actual index into the array. @@ -1088,6 +1106,7 @@ class LoadBalancer { /** * Helper class to handle automatically marking connectons as reusable (via RAII pattern) + * as well handling deferring the actual network connection until the handle is used * * @ingroup Database * @since 1.22 @@ -1095,23 +1114,35 @@ class LoadBalancer { class DBConnRef implements IDatabase { /** @var LoadBalancer */ protected $lb; - /** @var DatabaseBase */ + /** @var DatabaseBase|null */ protected $conn; + /** @var Array|null */ + protected $params; /** * @param $lb LoadBalancer - * @param $conn DatabaseBase + * @param $conn DatabaseBase|array Connection or (server index, group, wiki ID) array */ - public function __construct( LoadBalancer $lb, DatabaseBase $conn ) { + public function __construct( LoadBalancer $lb, $conn ) { $this->lb = $lb; - $this->conn = $conn; + if ( $conn instanceof DatabaseBase ) { + $this->conn = $conn; + } else { + $this->params = $conn; + } } public function __call( $name, $arguments ) { + if ( $this->conn === null ) { + list( $db, $groups, $wiki ) = $this->params; + $this->conn = $this->lb->getConnection( $db, $groups, $wiki ); + } return call_user_func_array( array( $this->conn, $name ), $arguments ); } function __destruct() { - $this->lb->reuseConnection( $this->conn ); + if ( $this->conn !== null ) { + $this->lb->reuseConnection( $this->conn ); + } } }