X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fexternalstore%2FExternalStoreDB.php;h=6ed5047947c28aa9cce7fbfc2f2db118aa28bd7c;hb=633252c3e9a3e462282c94ccad71f808175394b2;hp=e5d36e10cbcf7d772ef77f4759fa91cd36fa2ee9;hpb=7babd362babcbf7f20adb8e12edb4f4bc1d4249f;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/externalstore/ExternalStoreDB.php b/includes/externalstore/ExternalStoreDB.php index e5d36e10cb..6ed5047947 100644 --- a/includes/externalstore/ExternalStoreDB.php +++ b/includes/externalstore/ExternalStoreDB.php @@ -20,13 +20,15 @@ * @file */ -use Wikimedia\Rdbms\LoadBalancer; +use MediaWiki\MediaWikiServices; +use Wikimedia\Rdbms\ILoadBalancer; use Wikimedia\Rdbms\IDatabase; use Wikimedia\Rdbms\DBConnRef; use Wikimedia\Rdbms\MaintainableDBConnRef; +use Wikimedia\Rdbms\DatabaseDomain; /** - * DB accessable external objects. + * DB accessible external objects. * * In this system, each store "location" maps to a database "cluster". * The clusters must be defined in the normal LBFactory configuration. @@ -103,14 +105,19 @@ class ExternalStoreDB extends ExternalStoreMedium { return "DB://$location/$id"; } + public function isReadOnly( $location ) { + return ( $this->getLoadBalancer( $location )->getReadOnlyReason() !== false ); + } + /** * Get a LoadBalancer for the specified cluster * * @param string $cluster Cluster name - * @return LoadBalancer + * @return ILoadBalancer */ private function getLoadBalancer( $cluster ) { - return wfGetLBFactory()->getExternalLB( $cluster ); + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + return $lbFactory->getExternalLB( $cluster ); } /** @@ -122,8 +129,8 @@ class ExternalStoreDB extends ExternalStoreMedium { public function getSlave( $cluster ) { global $wgDefaultExternalStore; - $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false; $lb = $this->getLoadBalancer( $cluster ); + $domainId = $this->getDomainId( $lb->getServerInfo( $lb->getWriterIndex() ) ); if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) { wfDebug( "read only external store\n" ); @@ -132,7 +139,7 @@ class ExternalStoreDB extends ExternalStoreMedium { wfDebug( "writable external store\n" ); } - $db = $lb->getConnectionRef( DB_REPLICA, [], $wiki ); + $db = $lb->getConnectionRef( DB_REPLICA, [], $domainId ); $db->clearFlag( DBO_TRX ); // sanity return $db; @@ -145,15 +152,38 @@ class ExternalStoreDB extends ExternalStoreMedium { * @return MaintainableDBConnRef */ public function getMaster( $cluster ) { - $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false; $lb = $this->getLoadBalancer( $cluster ); + $domainId = $this->getDomainId( $lb->getServerInfo( $lb->getWriterIndex() ) ); - $db = $lb->getMaintenanceConnectionRef( DB_MASTER, [], $wiki ); + $db = $lb->getMaintenanceConnectionRef( DB_MASTER, [], $domainId ); $db->clearFlag( DBO_TRX ); // sanity return $db; } + /** + * @param array $server Master DB server configuration array for LoadBalancer + * @return string|bool Database domain ID or false + */ + private function getDomainId( array $server ) { + if ( isset( $server['dbname'] ) ) { + // T200471: for b/c, treat any "dbname" field as forcing which database to use. + // MediaWiki/LoadBalancer previously did not enforce any concept of a local DB + // domain, but rather assumed that the LB server configuration matched $wgDBname. + // This check is useful when the external storage DB for this cluster does not use + // the same name as the corresponding "main" DB(s) for wikis. + $domain = new DatabaseDomain( + $server['dbname'], + $server['schema'] ?? null, + $server['tablePrefix'] ?? '' + ); + + return $domain->getId(); + } + + return $this->params['wiki'] ?? false; // local domain unless explictly given + } + /** * Get the 'blobs' table name for this database *