From b5ef8d221b701c4eda4900fcff8f35811fa651e5 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Tue, 15 Jun 2004 15:00:54 +0000 Subject: [PATCH] Bringing the load balancer to the main branch. Still doesn't do much. I needed a DB connection cache for my Article.php alterations, which LoadBalancer provides. --- includes/Database.php | 13 ++-- includes/DatabaseFunctions.php | 120 +++++++++++++++------------------ includes/DefaultSettings.php | 4 ++ includes/LoadBalancer.php | 76 +++++++++++++++++---- includes/Setup.php | 15 ++++- 5 files changed, 141 insertions(+), 87 deletions(-) diff --git a/includes/Database.php b/includes/Database.php index d465c5e38d..0d48fe4431 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -5,10 +5,6 @@ # require_once( "CacheManager.php" ); -define( "DB_READ", -1 ); -define( "DB_WRITE", -2 ); -define( "DB_LAST", -3 ); - define( "LIST_COMMA", 0 ); define( "LIST_AND", 1 ); define( "LIST_SET", 2 ); @@ -143,10 +139,11 @@ class Database { { if ( $this->mFailFunction ) { if ( !is_int( $this->mFailFunction ) ) { - $this->$mFailFunction( $this ); + $ff = $this->mFailFunction; + $ff( $this, mysql_error() ); } } else { - wfEmergencyAbort( $this ); + wfEmergencyAbort( $this, mysql_error() ); } } @@ -459,7 +456,7 @@ class Database { /* Standard fail function, called by default when a connection cannot be established Displays the file cache if possible */ -function wfEmergencyAbort( &$conn ) { +function wfEmergencyAbort( &$conn, $error ) { global $wgTitle, $wgUseFileCache, $title, $wgInputEncoding, $wgSiteNotice, $wgOutputEncoding; if( !headers_sent() ) { @@ -470,7 +467,7 @@ function wfEmergencyAbort( &$conn ) { header( "Pragma: nocache" ); } $msg = $wgSiteNotice; - if($msg == "") $msg = wfMsgNoDB( "noconnect" ); + if($msg == "") $msg = wfMsgNoDB( "noconnect", $error ); $text = $msg; if($wgUseFileCache) { diff --git a/includes/DatabaseFunctions.php b/includes/DatabaseFunctions.php index 3b92eaa1eb..80e02beafe 100644 --- a/includes/DatabaseFunctions.php +++ b/includes/DatabaseFunctions.php @@ -11,6 +11,7 @@ # 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; @@ -23,46 +24,33 @@ if ($wgDBtype=="mysql") { $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 ); } @@ -73,122 +61,122 @@ function wfBufferSQLResults( $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 ); } diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 0445c4ba4e..1e0e635f71 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -60,6 +60,10 @@ $wgDBconnection = ''; $wgDBuser = 'wikiuser'; $wgDBtype = "mysql"; # "mysql" for working code and "pgsql" for development/broken code +# Database load balancer +$wgDBservers = false; # e.g. array(0 => "larousse", 1 => "pliny") +$wgDBloads = false; # e.g. array(0 => 0.6, 1 => 0.4); + # Sysop SQL queries $wgAllowSysopQueries = false; # Dangerous if not configured properly. $wgDBsqluser = 'sqluser'; diff --git a/includes/LoadBalancer.php b/includes/LoadBalancer.php index 3df2ac58c6..baea60ac32 100644 --- a/includes/LoadBalancer.php +++ b/includes/LoadBalancer.php @@ -1,10 +1,27 @@ mFailFunction = false; $this->mReadIndex = -1; $this->mForce = -1; + $this->mLastConn = false; } function newFromParams( $servers, $loads, $user, $password, $dbName, $failFunction = false ) @@ -38,6 +56,7 @@ class LoadBalancer { $this->mWriteIndex = -1; $this->mForce = -1; $this->mConnections = array(); + $this->mLastConn = false; wfSeedRandom(); } @@ -96,21 +115,49 @@ class LoadBalancer { } 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 ) @@ -140,4 +187,9 @@ class LoadBalancer { { $this->mForce = $i; } + + function haveIndex( $i ) + { + return array_key_exists( $i, $this->mServers ); + } } diff --git a/includes/Setup.php b/includes/Setup.php index d9b34040d2..3268788f73 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -58,6 +58,7 @@ require_once( 'BlockCache.php' ); require_once( 'Parser.php' ); require_once( 'ParserCache.php' ); require_once( 'WebRequest.php' ); +require_once( 'LoadBalancer.php' ); $wgRequest = new WebRequest(); @@ -70,7 +71,8 @@ global $wgArticle, $wgDeferredUpdateList, $wgLinkCache; 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 ) { @@ -141,6 +143,16 @@ if( $wgUseMemCached ) { } 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' ); @@ -218,6 +230,7 @@ $wgMwRedir =& MagicWord::get( MAG_REDIRECT ); $wgParserCache = new ParserCache(); $wgParser = new Parser(); $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) ); +$wgDBConnections = array(); # Placeholders in case of DB error $wgTitle = Title::newFromText( wfMsg( 'badtitle' ) ); -- 2.20.1