Bringing the load balancer to the main branch. Still doesn't do much. I needed a...
authorTim Starling <tstarling@users.mediawiki.org>
Tue, 15 Jun 2004 15:00:54 +0000 (15:00 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Tue, 15 Jun 2004 15:00:54 +0000 (15:00 +0000)
includes/Database.php
includes/DatabaseFunctions.php
includes/DefaultSettings.php
includes/LoadBalancer.php
includes/Setup.php

index d465c5e..0d48fe4 100644 (file)
@@ -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) {
index 3b92eaa..80e02be 100644 (file)
@@ -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 );
 }
 
index 0445c4b..1e0e635 100644 (file)
@@ -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';
index 3df2ac5..baea60a 100644 (file)
@@ -1,10 +1,27 @@
 <?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()
        {
@@ -17,6 +34,7 @@ class LoadBalancer {
                $this->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 );
+       }
 }
index d9b3404..3268788 100644 (file)
@@ -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' ) );