X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fexternalstore%2FExternalStoreDB.php;h=22a2d2bc8e27631893eaa22a8db16bdebf3e62ca;hb=c8defe4272683625caf1b5a58697f1085481b8a3;hp=e5d36e10cbcf7d772ef77f4759fa91cd36fa2ee9;hpb=587d08c5a626f8195def9f82ee83e17c85824db8;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/externalstore/ExternalStoreDB.php b/includes/externalstore/ExternalStoreDB.php index e5d36e10cb..22a2d2bc8e 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,42 @@ 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( $this->params['wiki'] ) ) { + return $this->params['wiki']; // explicit domain + } + + 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 false; // local LB domain + } + /** * Get the 'blobs' table name for this database *