Capitalization fix in memcached setting
[lhc/web/wiklou.git] / includes / LoadBalancer.php
index 455844c..9813c2a 100644 (file)
@@ -1,31 +1,43 @@
 <?php
-# Database load balancing object
+/**
+ *
+ * @package MediaWiki
+ */
 
-require_once( "Database.php" );
+/**
+ * Depends on the database object
+ */
+require_once( 'Database.php' );
 
 # Valid database indexes
 # Operation-based indexes
-define( "DB_SLAVE", -1 );     # Read from the slave (or only server)
-define( "DB_MASTER", -2 );    # Write to master (or only server)
-define( "DB_LAST", -3 );     # Whatever database was used last
+define( 'DB_SLAVE', -1 );     # Read from the slave (or only server)
+define( 'DB_MASTER', -2 );    # Write to master (or only server)
+define( 'DB_LAST', -3 );     # Whatever database was used last
 
 # Obsolete aliases
-define( "DB_READ", -1 );
-define( "DB_WRITE", -2 );
+define( 'DB_READ', -1 );
+define( 'DB_WRITE', -2 );
 
 # 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
-
-define( "MASTER_WAIT_TIMEOUT", 15 ); # Time to wait for a slave to synchronise
-
+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
+
+define( 'MASTER_WAIT_TIMEOUT', 15 ); # Time to wait for a slave to synchronise
+
+/**
+ * Database load balancing object
+ *
+ * @todo document
+ * @package MediaWiki
+ */
 class LoadBalancer {
        /* private */ var $mServers, $mConnections, $mLoads;
        /* private */ var $mFailFunction;
@@ -67,8 +79,10 @@ class LoadBalancer {
                }
        }
        
-       # Given an array of non-normalised probabilities, this function will select
-       # an element and return the appropriate key
+       /**
+        * Given an array of non-normalised probabilities, this function will select
+        * an element and return the appropriate key
+        */
        function pickRandom( $weights )
        {
                if ( !is_array( $weights ) || count( $weights ) == 0 ) {
@@ -131,11 +145,13 @@ class LoadBalancer {
                return $i;
        }
 
-       # Set the master wait position
-       # If a DB_SLAVE connection has been opened already, waits
-       # Otherwise sets a variable telling it to wait if such a connection is opened
+       /**
+        * Set the master wait position
+        * If a DB_SLAVE connection has been opened already, waits
+        * Otherwise sets a variable telling it to wait if such a connection is opened
+        */
        function waitFor( $file, $pos ) {
-               $fname = "LoadBalancer::waitFor";
+               $fname = 'LoadBalancer::waitFor';
                wfProfileIn( $fname );
 
                wfDebug( "User master pos: $file $pos\n" );
@@ -156,13 +172,15 @@ class LoadBalancer {
                wfProfileOut( $fname );
        }
 
-       # Wait for a given slave to catch up to the master pos stored in $this
+       /**
+        * Wait for a given slave to catch up to the master pos stored in $this
+        */
        function doWait( $index ) {
                global $wgMemc;
                
                $retVal = false;
 
-               $key = "masterpos:" . $index;
+               $key = 'masterpos:' . $index;
                $memcPos = $wgMemc->get( $key );
                if ( $memcPos ) {
                        list( $file, $pos ) = explode( ' ', $memcPos );
@@ -189,10 +207,12 @@ class LoadBalancer {
                return $retVal;
        }               
 
-       # Get a connection by index
-       function &getConnection( $i, $fail = false )
+       /**
+        * Get a connection by index
+        */
+       function &getConnection( $i, $fail = true )
        {
-               $fname = "LoadBalancer::getConnection";
+               $fname = 'LoadBalancer::getConnection';
                wfProfileIn( $fname );
                /*
                # Task-based index
@@ -226,9 +246,12 @@ class LoadBalancer {
                return $this->mConnections[$i];
        }
 
-       # Open a connection to the server given by the specified index
-       # Index must be an actual index into the array
-       /* private */ function openConnection( $i, $fail = false ) {
+       /**
+        * Open a connection to the server given by the specified index
+        * Index must be an actual index into the array
+        * @private
+        */
+       function openConnection( $i, $fail = false ) {
                $fname = 'LoadBalancer::openConnection';
                wfProfileIn( $fname );
 
@@ -258,8 +281,14 @@ class LoadBalancer {
                wfProfileOut( $fname );
        }
 
-       # Test if the specified index represents an open connection
-       /* private */ function isOpen( $index ) {
+       /**
+        * Test if the specified index represents an open connection
+        * @private
+        */
+       function isOpen( $index ) {
+               if( !is_integer( $index ) ) {
+                       return false;
+               }
                if ( array_key_exists( $index, $this->mConnections ) && is_object( $this->mConnections[$index] ) && 
                  $this->mConnections[$index]->isOpen() ) 
                {
@@ -269,8 +298,11 @@ class LoadBalancer {
                }
        }
        
-       # Really opens a connection
-       /* private */ function reallyOpenConnection( &$server ) {
+       /**
+        * Really opens a connection
+        * @private
+        */
+       function reallyOpenConnection( &$server ) {
                        extract( $server );
                        # Get class for this database type
                        $class = 'Database' . ucfirst( $type );
@@ -295,9 +327,9 @@ class LoadBalancer {
                                $conn = new Database;
                        }
                        if ( $this->mFailFunction ) {
-                               $conn->setFailFunction( $this->mFailFunction );
+                               $conn->failFunction( $this->mFailFunction );
                        } else {
-                               $conn->setFailFunction( "wfEmergencyAbort" );
+                               $conn->failFunction( 'wfEmergencyAbort' );
                        }
                        $conn->reportConnectionError();
                        $reporting = false;
@@ -320,12 +352,16 @@ class LoadBalancer {
                return array_key_exists( $i, $this->mServers );
        }
 
-       # Get the number of defined servers (not the number of open connections)
+       /**
+        * Get the number of defined servers (not the number of open connections)
+        */
        function getServerCount() {
                return count( $this->mServers );
        }
 
-       # Save master pos to the session and to memcached, if the session exists
+       /**
+        * Save master pos to the session and to memcached, if the session exists
+        */
        function saveMasterPos() {
                global $wgSessionStarted;
                if ( $wgSessionStarted && count( $this->mServers ) > 1 ) {
@@ -347,14 +383,18 @@ class LoadBalancer {
                }
        }
 
-       # Loads the master pos from the session, waits for it if necessary
+       /**
+        * Loads the master pos from the session, waits for it if necessary
+        */
        function loadMasterPos() {
                if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
                        $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
                }
        }
 
-       # Close all open connections
+       /**
+        * Close all open connections
+        */
        function closeAll() {
                foreach( $this->mConnections as $i => $conn ) {
                        if ( $this->isOpen( $i ) ) {