# NB: This file follows a connect on demand scheme. Do
# not access the $wgDatabase variable directly unless
# you intend to set it. Use wfGetDB().
+$wgDatabase = false;
$wgIsMySQL=false;
$wgIsPg=false;
$wgIsPg=true;
}
-# Query the database
-# $db: DB_READ = -1 read from slave (or only server)
-# DB_WRITE = -2 write to master (or only server)
-# 0,1,2,... query a database with a specific index
+
# Replication is not actually implemented just yet
# Usually aborts on failure
# If errors are explicitly ignored, returns success
function wfQuery( $sql, $db, $fname = "" )
{
- global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname,
- $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
-
if ( !is_numeric( $db ) ) {
# Someone has tried to call this the old way
$wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
}
-
- $db =& wfGetDB();
- return $db->query( $sql, $fname );
+ $c =& wfGetDB( $db );
+ return $c->query( $sql, $fname );
}
-# Connect on demand
-function &wfGetDB()
+function &wfGetDB( $db = DB_LAST )
{
- global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname,
- $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
- if ( !$wgDatabase ) {
- $wgDatabase = Database::newFromParams( $wgDBserver, $wgDBuser, $wgDBpassword,
- $wgDBname, false, $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors );
- }
- return $wgDatabase;
+ global $wgLoadBalancer;
+ return $wgLoadBalancer->getConnection( $db );
}
# Turns buffering of SQL result sets on (true) or off (false). Default is
# "on" and it should not be changed without good reasons.
# Returns the previous state.
-function wfBufferSQLResults( $newstate )
+function wfBufferSQLResults( $newstate, $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->setBufferResults( $newstate );
}
# situation as appropriate.
# Returns the previous state.
-function wfIgnoreSQLErrors( $newstate )
+function wfIgnoreSQLErrors( $newstate, $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->setIgnoreErrors( $newstate );
}
-function wfFreeResult( $res )
+function wfFreeResult( $res, $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
$db->freeResult( $res );
}
-function wfFetchObject( $res )
+function wfFetchObject( $res, $dbi = DB_LAST )
{
- $db =& wfGetDB();
- return $db->fetchObject( $res );
+ $db =& wfGetDB( $dbi );
+ return $db->fetchObject( $res, $dbi = DB_LAST );
}
-function wfFetchRow( $res )
+function wfFetchRow( $res, $dbi = DB_LAST )
{
- $db =& wfGetDB();
- return $db->fetchRow ( $res );
+ $db =& wfGetDB( $dbi );
+ return $db->fetchRow ( $res, $dbi = DB_LAST );
}
-function wfNumRows( $res )
+function wfNumRows( $res, $dbi = DB_LAST )
{
- $db =& wfGetDB();
- return $db->numRows( $res );
+ $db =& wfGetDB( $dbi );
+ return $db->numRows( $res, $dbi = DB_LAST );
}
-function wfNumFields( $res )
+function wfNumFields( $res, $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->numFields( $res );
}
-function wfFieldName( $res, $n )
+function wfFieldName( $res, $n, $dbi = DB_LAST )
{
- $db =& wfGetDB();
- return $db->fieldName( $res, $n );
+ $db =& wfGetDB( $dbi );
+ return $db->fieldName( $res, $n, $dbi = DB_LAST );
}
-function wfInsertId()
+function wfInsertId( $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->insertId();
}
-function wfDataSeek( $res, $row )
+function wfDataSeek( $res, $row, $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->dataSeek( $res, $row );
}
-function wfLastErrno()
+function wfLastErrno( $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->lastErrno();
}
-function wfLastError()
+function wfLastError( $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->lastError();
}
-function wfAffectedRows()
+function wfAffectedRows( $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->affectedRows();
}
-function wfLastDBquery()
+function wfLastDBquery( $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->lastQuery();
}
-function wfSetSQL( $table, $var, $value, $cond )
+function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_WRITE )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->set( $table, $var, $value, $cond );
}
-function wfGetSQL( $table, $var, $cond="" )
+function wfGetSQL( $table, $var, $cond="", $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->get( $table, $var, $cond );
}
-function wfFieldExists( $table, $field )
+function wfFieldExists( $table, $field, $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->fieldExists( $table, $field );
}
-function wfIndexExists( $table, $index )
+function wfIndexExists( $table, $index, $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->indexExists( $table, $index );
}
-function wfInsertArray( $table, $array )
+function wfInsertArray( $table, $array, $fname = "wfInsertArray", $dbi = DB_WRITE )
{
- $db =& wfGetDB();
- return $db->insertArray( $table, $array );
+ $db =& wfGetDB( $dbi );
+ return $db->insertArray( $table, $array, $fname );
}
-function wfGetArray( $table, $vars, $conds, $fname = "wfGetArray" )
+function wfGetArray( $table, $vars, $conds, $fname = "wfGetArray", $dbi = DB_LAST )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
return $db->getArray( $table, $vars, $conds, $fname );
}
-function wfUpdateArray( $table, $values, $conds, $fname = "wfUpdateArray" )
+function wfUpdateArray( $table, $values, $conds, $fname = "wfUpdateArray", $dbi = DB_WRITE )
{
- $db =& wfGetDB();
+ $db =& wfGetDB( $dbi );
$db->updateArray( $table, $values, $conds, $fname );
}
<?php
# Database load balancing object
+# Valid database indexes
+# Operation-based indexes
+define( "DB_READ", -1 ); # Read from the slave (or only server)
+define( "DB_WRITE", -2 ); # Write to master (or only server)
+define( "DB_LAST", -3 ); # Whatever database was used last
+
+# Task-based indexes
+# ***NOT USED YET, EXPERIMENTAL***
+# These may be defined in $wgDBservers. If they aren't, the default reader or writer will be used
+# Even numbers are always readers, odd numbers are writers
+define( "DB_TASK_FIRST", 1000 ); # First in list
+define( "DB_SEARCH_R", 1000 ); # Search read
+define( "DB_SEARCH_W", 1001 ); # Search write
+define( "DB_ASKSQL_R", 1002 ); # Special:Asksql read
+define( "DB_WATCHLIST_R", 1004 ); # Watchlist read
+define( "DB_TASK_LAST", 1004) ; # Last in list
+
class LoadBalancer {
/* private */ var $mServers, $mConnections, $mLoads;
/* private */ var $mUser, $mPassword, $mDbName, $mFailFunction;
- /* private */ var $mForce, $mReadIndex;
+ /* private */ var $mForce, $mReadIndex, $mLastConn;
function LoadBalancer()
{
$this->mFailFunction = false;
$this->mReadIndex = -1;
$this->mForce = -1;
+ $this->mLastConn = false;
}
function newFromParams( $servers, $loads, $user, $password, $dbName, $failFunction = false )
$this->mWriteIndex = -1;
$this->mForce = -1;
$this->mConnections = array();
+ $this->mLastConn = false;
wfSeedRandom();
}
}
return $conn;
}
-
+
function &getConnection( $i, $fail = false )
{
- if ( !array_key_exists( $i, $this->mConnections) || !$this->mConnections[$i]->isOpen() ) {
- $this->mConnections[$i] = Database::newFromParams( $this->mServers[$i], $this->mUser,
- $this->mPassword, $this->mDbName, 1 );
- }
- if ( !$this->mConnections[$i]->isOpen() ) {
- wfDebug( "Failed to connect to database $i at {$this->mServers[$i]}\n" );
- if ( $fail ) {
- $this->reportConnectionError( $this->mConnections[$i] );
+ /*
+ # Task-based index
+ if ( $i >= DB_TASK_FIRST && $i < DB_TASK_LAST ) {
+ if ( $i % 2 ) {
+ # Odd index use writer
+ $i = DB_WRITE;
+ } else {
+ # Even index use reader
+ $i = DB_READ;
+ }
+ }*/
+
+ # Operation-based index
+ # Note, getReader() and getWriter() will re-enter this function
+ if ( $i == DB_READ ) {
+ $this->mLastConn =& $this->getReader();
+ } elseif ( $i == DB_WRITE ) {
+ $this->mLastConn =& $this->getWriter();
+ } elseif ( $i == DB_LAST ) {
+ # Just use $this->mLastConn, which should already be set
+ if ( $this->mLastConn === false ) {
+ # Oh dear, not set, best to use the writer for safety
+ $this->mLastConn =& $this->getWriter();
}
- $this->mConnections[$i] = false;
+ } else {
+ # Explicit index
+ if ( !array_key_exists( $i, $this->mConnections) || !$this->mConnections[$i]->isOpen() ) {
+ $this->mConnections[$i] = Database::newFromParams( $this->mServers[$i], $this->mUser,
+ $this->mPassword, $this->mDbName, 1 );
+ }
+ if ( !$this->mConnections[$i]->isOpen() ) {
+ wfDebug( "Failed to connect to database $i at {$this->mServers[$i]}\n" );
+ if ( $fail ) {
+ $this->reportConnectionError( $this->mConnections[$i] );
+ }
+ $this->mConnections[$i] = false;
+ }
+ $this->mLastConn =& $this->mConnections[$i];
}
- return $this->mConnections[$i];
+ return $this->mLastConn;
}
function reportConnectionError( &$conn )
{
$this->mForce = $i;
}
+
+ function haveIndex( $i )
+ {
+ return array_key_exists( $i, $this->mServers );
+ }
}
require_once( 'Parser.php' );
require_once( 'ParserCache.php' );
require_once( 'WebRequest.php' );
+require_once( 'LoadBalancer.php' );
$wgRequest = new WebRequest();
global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
global $wgMsgCacheExpiry, $wgDBname, $wgCommandLineMode;
-global $wgBlockCache, $wgParserCache, $wgParser, $wgDontTrustMemcachedWithImportantStuff;
+global $wgBlockCache, $wgParserCache, $wgParser, $wgDBConnections;
+global $wgLoadBalancer, $wgDBservers, $wgDBloads, $wgDBuser, $wgDBpassword;
# Useful debug output
if ( $wgCommandLineMode ) {
}
wfProfileOut( $fname.'-memcached' );
+wfProfileIn( $fname.'-database' );
+
+if ( !$wgDBservers ) {
+ $wgDBservers = array( $wgDBserver );
+ $wgDBloads = array( 1 );
+}
+$wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers, $wgDBloads, $wgDBuser, $wgDBpassword, $wgDBname );
+$wgLoadBalancer->force(0);
+
+wfProfileOut( $fname.'-database' );
wfProfileIn( $fname.'-language' );
require_once( 'languages/Language.php' );
$wgParserCache = new ParserCache();
$wgParser = new Parser();
$wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
+$wgDBConnections = array();
# Placeholders in case of DB error
$wgTitle = Title::newFromText( wfMsg( 'badtitle' ) );