From df4ad11d29e1fd96e9dc313a25559658e49b7f0f Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Mon, 5 Mar 2007 12:42:19 +0000 Subject: [PATCH] Bringing foreign connection logic into the core. Committing for test on server. --- includes/DefaultSettings.php | 8 ++++++ includes/LoadBalancer.php | 52 +++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 690d9c2acd..98b94d4b5f 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -592,6 +592,14 @@ $wgDBmysql5 = false; */ $wgLocalDatabases = array(); +/** + * A map of database name (key) to connection index (value), describing the + * master database server for each database. The connection index is an + * index into the $wgDBservers array. If a database is not listed here, the + * default load balancing settings will be used. + */ +$wgAlternateMaster = array(); + /** * Object cache settings * See Defines.php for types diff --git a/includes/LoadBalancer.php b/includes/LoadBalancer.php index d3d072eed3..b9fda222e5 100644 --- a/includes/LoadBalancer.php +++ b/includes/LoadBalancer.php @@ -240,6 +240,53 @@ class LoadBalancer { return $i; } + /** + * Get a connection to a foreign DB. Will use the following types of + * connection in order of precedence: + * * The alternate master, if there is one and DB_MASTER is given + * * The query group + * * The alternate master, if there is one + * * The default writer, if DB_MASTER was specified + * * The default reader. + * + * Connection error will *not* lead to progression down this list. Selection + * of a group depends only on configuration. + * + * @param string $dbName The database name + * @param mixed $group The query group, or DB_MASTER for the foreign master + * @return Database object with the database selected + */ + function getForeignConnection( $dbName, $group = false ) { + global $wgAlternateMaster; + + // Try cache + if ( isset( $this->mForeignConnections[$dbName][$group] ) ) { + return $this->mForeignConnections[$dbName][$group]; + } + + // Try the precedence list described in the function description + if ( $group === DB_MASTER && isset( $wgAlternateMaster[$dbName] ) ) { + $index = $this->mServers[$wgAlternateMaster[$dbName]]; + } if ( $group && $group !== DB_MASTER ) { + $index = $this->getGroupIndex( $group ); + } elseif ( isset( $wgAlternateMaster[$dbName] ) ) { + $index = $this->mServers[$wgAlternateMaster[$dbName]]; + } elseif ( $group === DB_MASTER ) { + $index = $this->getWriterIndex(); + } else { + $index = $this->getReaderIndex(); + } + + if ( $index === false || !isset( $this->mServers[$index] ) ) { + throw new MWException( "No configured server available for foreign connection to $dbName" ); + } + + $dbInfo = $this->mServers[$index]; + $dbc = $this->reallyOpenConnection( $dbInfo, $dbName ); + $this->mForeignConnections[$dbName][$group] = $dbc; + return $dbc; + } + /** * Set the master wait position * If a DB_SLAVE connection has been opened already, waits @@ -415,12 +462,15 @@ class LoadBalancer { * Really opens a connection * @access private */ - function reallyOpenConnection( &$server ) { + function reallyOpenConnection( &$server, $overrideDBname = false ) { if( !is_array( $server ) ) { throw new MWException( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' ); } extract( $server ); + if ( $overrideDBname ) { + $dbname = $overrideDBname; + } # Get class for this database type $class = 'Database' . ucfirst( $type ); -- 2.20.1