Aaron Schulz has uploaded a new change for review. https://gerrit.wikimedia.org/r/323883
Change subject: Add LoadBalancer::getMaintenanceConnectionRef() method ...................................................................... Add LoadBalancer::getMaintenanceConnectionRef() method This is useful when IMaintainableDatabase methods are needed for foreign wiki connections to things like external store. Change-Id: Ie35b1ff21032cc4e78912dc499486da23aeba041 --- M includes/externalstore/ExternalStoreDB.php M includes/libs/rdbms/database/DBConnRef.php A includes/libs/rdbms/database/MaintainableDBConnRef.php M includes/libs/rdbms/loadbalancer/ILoadBalancer.php M includes/libs/rdbms/loadbalancer/LoadBalancer.php 5 files changed, 114 insertions(+), 31 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/83/323883/1 diff --git a/includes/externalstore/ExternalStoreDB.php b/includes/externalstore/ExternalStoreDB.php index 7e93299..dc7d194 100644 --- a/includes/externalstore/ExternalStoreDB.php +++ b/includes/externalstore/ExternalStoreDB.php @@ -115,7 +115,7 @@ * Get a replica DB connection for the specified cluster * * @param string $cluster Cluster name - * @return IDatabase + * @return DBConnRef */ function getSlave( $cluster ) { global $wgDefaultExternalStore; @@ -140,7 +140,7 @@ * Get a master database connection for the specified cluster * * @param string $cluster Cluster name - * @return IDatabase + * @return DBConnRef */ function getMaster( $cluster ) { $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false; diff --git a/includes/libs/rdbms/database/DBConnRef.php b/includes/libs/rdbms/database/DBConnRef.php index 20198bf..b268b9f 100644 --- a/includes/libs/rdbms/database/DBConnRef.php +++ b/includes/libs/rdbms/database/DBConnRef.php @@ -10,10 +10,8 @@ class DBConnRef implements IDatabase { /** @var ILoadBalancer */ private $lb; - - /** @var IDatabase|null Live connection handle */ + /** @var Database|null Live connection handle */ private $conn; - /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */ private $params; @@ -22,12 +20,12 @@ const FLD_DOMAIN = 2; /** - * @param ILoadBalancer $lb - * @param IDatabase|array $conn Connection or (server index, group, DatabaseDomain|string) + * @param ILoadBalancer $lb Connection manager for $conn + * @param Database|array $conn New connection handle or (server index, query groups, domain) */ public function __construct( ILoadBalancer $lb, $conn ) { $this->lb = $lb; - if ( $conn instanceof IDatabase ) { + if ( $conn instanceof Database ) { $this->conn = $conn; // live handle } elseif ( count( $conn ) >= 3 && $conn[self::FLD_DOMAIN] !== false ) { $this->params = $conn; @@ -595,7 +593,7 @@ * Clean up the connection when out of scope */ function __destruct() { - if ( $this->conn !== null ) { + if ( $this->conn ) { $this->lb->reuseConnection( $this->conn ); } } diff --git a/includes/libs/rdbms/database/MaintainableDBConnRef.php b/includes/libs/rdbms/database/MaintainableDBConnRef.php new file mode 100644 index 0000000..ad54ad8 --- /dev/null +++ b/includes/libs/rdbms/database/MaintainableDBConnRef.php @@ -0,0 +1,62 @@ +<?php +/** + * Helper class to handle automatically marking connections as reusable (via RAII pattern) + * as well handling deferring the actual network connection until the handle is used + * + * @note: proxy methods are defined explicity to avoid interface errors + * @ingroup Database + * @since 1.29 + */ +class MaintainableDBConnRef extends DBConnRef implements IMaintainableDatabase { + public function tableName( $name, $format = 'quoted' ) { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function tableNames() { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function tableNamesN() { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function sourceFile( + $filename, + callable $lineCallback = null, + callable $resultCallback = null, + $fname = false, + callable $inputCallback = null + ) { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function sourceStream( + $fp, + callable $lineCallback = null, + callable $resultCallback = null, + $fname = __METHOD__, + callable $inputCallback = null + ) { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function dropTable( $tableName, $fName = __METHOD__ ) { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function deadlockLoop() { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function listViews( $prefix = null, $fname = __METHOD__ ) { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function textFieldSize( $table, $field ) { + return $this->__call( __FUNCTION__, func_get_args() ); + } + + public function streamStatementEnd( &$sql, &$newLine ) { + return $this->__call( __FUNCTION__, func_get_args() ); + } +} diff --git a/includes/libs/rdbms/loadbalancer/ILoadBalancer.php b/includes/libs/rdbms/loadbalancer/ILoadBalancer.php index 8854479..fc306b4 100644 --- a/includes/libs/rdbms/loadbalancer/ILoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/ILoadBalancer.php @@ -108,8 +108,9 @@ /** * Get the index of the reader connection, which may be a replica DB + * * This takes into account load ratios and lag times. It should - * always return a consistent index during a given invocation + * always return a consistent index during a given invocation. * * Side effect: opens connections to databases * @param string|bool $group Query group, or false for the generic reader @@ -121,8 +122,10 @@ /** * Set the master wait position - * If a DB_REPLICA connection has been opened already, waits - * Otherwise sets a variable telling it to wait if such a connection is opened + * + * If a DB_REPLICA connection has been opened already, then wait immediately. + * Otherwise sets a variable telling it to wait if such a connection is opened. + * * @param DBMasterPos $pos */ public function waitFor( $pos ); @@ -140,6 +143,7 @@ /** * Set the master wait position and wait for ALL replica DBs to catch up to it + * * @param DBMasterPos $pos * @param int $timeout Max seconds to wait; default is mWaitTimeout * @return bool Success (able to connect and no timeouts reached) @@ -148,30 +152,29 @@ /** * Get any open connection to a given server index, local or foreign - * Returns false if there is no connection open * - * @param int $i Server index - * @return IDatabase|bool False on failure + * @param int $i Server index or DB_MASTER/DB_REPLICA + * @return Database|bool False if no such connection is open */ public function getAnyOpenConnection( $i ); /** * Get a connection by index - * This is the main entry point for this class. * - * @param int $i Server index + * @param int $i Server index or DB_MASTER/DB_REPLICA * @param array|string|bool $groups Query group(s), or false for the generic reader * @param string|bool $domain Domain ID, or false for the current domain * * @throws DBError - * @return IDatabase + * @return Database */ public function getConnection( $i, $groups = [], $domain = false ); /** - * Mark a foreign connection as being available for reuse under a different - * DB name or prefix. This mechanism is reference-counted, and must be called - * the same number of times as getConnection() to work. + * Mark a foreign connection as being available for reuse under a different DB domain + * + * This mechanism is reference-counted, and must be called the same number of times + * as getConnection() to work. * * @param IDatabase $conn * @throws InvalidArgumentException @@ -181,30 +184,44 @@ /** * Get a database connection handle reference * - * The handle's methods wrap simply wrap those of a IDatabase handle + * The handle's methods simply wrap those of a Database handle * - * @see LoadBalancer::getConnection() for parameter information + * @see ILoadBalancer::getConnection() for parameter information * - * @param int $db + * @param int $i Server index or DB_MASTER/DB_REPLICA * @param array|string|bool $groups Query group(s), or false for the generic reader * @param string|bool $domain Domain ID, or false for the current domain * @return DBConnRef */ - public function getConnectionRef( $db, $groups = [], $domain = false ); + public function getConnectionRef( $i, $groups = [], $domain = false ); /** * Get a database connection handle reference without connecting yet * - * The handle's methods wrap simply wrap those of a IDatabase handle + * The handle's methods simply wrap those of a Database handle * - * @see LoadBalancer::getConnection() for parameter information + * @see ILoadBalancer::getConnection() for parameter information * - * @param int $db + * @param int $i Server index or DB_MASTER/DB_REPLICA * @param array|string|bool $groups Query group(s), or false for the generic reader * @param string|bool $domain Domain ID, or false for the current domain * @return DBConnRef */ - public function getLazyConnectionRef( $db, $groups = [], $domain = false ); + public function getLazyConnectionRef( $i, $groups = [], $domain = false ); + + /** + * Get a maintenance database connection handle reference for migrations and schema changes + * + * The handle's methods simply wrap those of a Database handle + * + * @see ILoadBalancer::getConnection() for parameter information + * + * @param int $db Server index or DB_MASTER/DB_REPLICA + * @param array|string|bool $groups Query group(s), or false for the generic reader + * @param string|bool $domain Domain ID, or false for the current domain + * @return MaintainableDBConnRef + */ + public function getMaintenanceConnectionRef( $db, $groups = [], $domain = false ); /** * Open a connection to the server given by the specified index @@ -216,9 +233,9 @@ * * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. * - * @param int $i Server index + * @param int $i Server index or DB_MASTER/DB_REPLICA * @param string|bool $domain Domain ID, or false for the current domain - * @return IDatabase|bool Returns false on errors + * @return Database|bool Returns false on errors * @throws DBAccessError */ public function openConnection( $i, $domain = false ); diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php b/includes/libs/rdbms/loadbalancer/LoadBalancer.php index d42fed9..8b4a312 100644 --- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php @@ -650,6 +650,12 @@ return new DBConnRef( $this, [ $db, $groups, $domain ] ); } + public function getMaintenanceConnectionRef( $db, $groups = [], $domain = false ) { + $domain = ( $domain !== false ) ? $domain : $this->localDomain; + + return new MaintainableDBConnRef( $this, $this->getConnection( $db, $groups, $domain ) ); + } + /** * @see ILoadBalancer::openConnection() * -- To view, visit https://gerrit.wikimedia.org/r/323883 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie35b1ff21032cc4e78912dc499486da23aeba041 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits