<?php
/**
*
- * @package MediaWiki
*/
* Database load balancing object
*
* @todo document
- * @package MediaWiki
*/
class LoadBalancer {
/* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
*/
const AVG_STATUS_POLL = 2000;
- function LoadBalancer( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
+ function __construct( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
{
$this->mServers = $servers;
$this->mFailFunction = $failFunction;
$this->mWriteIndex = -1;
$this->mForce = -1;
$this->mConnections = array();
- $this->mLastIndex = 1;
+ $this->mLastIndex = -1;
$this->mLoads = array();
$this->mWaitForFile = false;
$this->mWaitForPos = false;
# Unset excessively lagged servers
$lags = $this->getLagTimes();
foreach ( $lags as $i => $lag ) {
- if ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) {
+ if ( $i != 0 && isset( $this->mServers[$i]['max lag'] ) &&
+ ( $lag === false || $lag > $this->mServers[$i]['max lag'] ) )
+ {
unset( $loads[$i] );
}
}
# Query groups
if ( !is_array( $groups ) ) {
- $groupIndex = $this->getGroupIndex( $groups, $i );
+ $groupIndex = $this->getGroupIndex( $groups );
if ( $groupIndex !== false ) {
$i = $groupIndex;
}
} else {
foreach ( $groups as $group ) {
- $groupIndex = $this->getGroupIndex( $group, $i );
+ $groupIndex = $this->getGroupIndex( $group );
if ( $groupIndex !== false ) {
$i = $groupIndex;
break;
return $db;
}
- function reportConnectionError( &$conn )
- {
+ function reportConnectionError( &$conn ) {
$fname = 'LoadBalancer::reportConnectionError';
wfProfileIn( $fname );
# Prevent infinite recursion
* Save master pos to the session and to memcached, if the session exists
*/
function saveMasterPos() {
- global $wgSessionStarted;
- if ( $wgSessionStarted && count( $this->mServers ) > 1 ) {
+ if ( session_id() != '' && count( $this->mServers ) > 1 ) {
# If this entire request was served from a slave without opening a connection to the
# master (however unlikely that may be), then we can fetch the position from the slave.
if ( empty( $this->mConnections[0] ) ) {
}
}
}
+
+ /* Issue COMMIT only on master, only if queries were done on connection */
+ function commitMasterChanges() {
+ // Always 0, but who knows.. :)
+ $i = $this->getWriterIndex();
+ if (array_key_exists($i,$this->mConnections)) {
+ if ($this->mConnections[$i]->lastQuery() != '') {
+ $this->mConnections[$i]->immediateCommit();
+ }
+ }
+ }
function waitTimeout( $value = NULL ) {
return wfSetVar( $this->mWaitTimeout, $value );
}
}
-?>
+