From 9f01cee8fa46aa5770feecea71be7db9ca64b1ae Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 24 Sep 2015 13:00:12 -0700 Subject: [PATCH] Moved all load balancer classes to /loadbalancer * Also split out LBFactory classes to their own files Change-Id: I9e649878afcffdb736c09c0574e5f99b1e72382f --- autoload.php | 22 +-- includes/db/{ => loadbalancer}/LBFactory.php | 161 ------------------ includes/db/loadbalancer/LBFactoryFake.php | 52 ++++++ .../db/{ => loadbalancer}/LBFactoryMulti.php | 0 includes/db/loadbalancer/LBFactorySimple.php | 153 +++++++++++++++++ .../db/{ => loadbalancer}/LBFactorySingle.php | 0 .../db/{ => loadbalancer}/LoadBalancer.php | 0 .../db/{ => loadbalancer}/LoadMonitor.php | 0 .../{ => loadbalancer}/LoadMonitorMySQL.php | 0 9 files changed, 216 insertions(+), 172 deletions(-) rename includes/db/{ => loadbalancer}/LBFactory.php (61%) create mode 100644 includes/db/loadbalancer/LBFactoryFake.php rename includes/db/{ => loadbalancer}/LBFactoryMulti.php (100%) create mode 100644 includes/db/loadbalancer/LBFactorySimple.php rename includes/db/{ => loadbalancer}/LBFactorySingle.php (100%) rename includes/db/{ => loadbalancer}/LoadBalancer.php (100%) rename includes/db/{ => loadbalancer}/LoadMonitor.php (100%) rename includes/db/{ => loadbalancer}/LoadMonitorMySQL.php (100%) diff --git a/autoload.php b/autoload.php index e5e7cda5bd..a2e51043e7 100644 --- a/autoload.php +++ b/autoload.php @@ -283,7 +283,7 @@ $wgAutoloadLocalClasses = array( 'CsvStatsOutput' => __DIR__ . '/maintenance/language/StatOutputs.php', 'CurlHttpRequest' => __DIR__ . '/includes/HttpFunctions.php', 'DBAccessBase' => __DIR__ . '/includes/dao/DBAccessBase.php', - 'DBAccessError' => __DIR__ . '/includes/db/LBFactory.php', + 'DBAccessError' => __DIR__ . '/includes/db/loadbalancer/LBFactory.php', 'DBAccessObjectUtils' => __DIR__ . '/includes/dao/DBAccessObjectUtils.php', 'DBConnRef' => __DIR__ . '/includes/db/DBConnRef.php', 'DBConnectionError' => __DIR__ . '/includes/db/DatabaseError.php', @@ -611,11 +611,11 @@ $wgAutoloadLocalClasses = array( 'JsonContentHandler' => __DIR__ . '/includes/content/JsonContentHandler.php', 'KkConverter' => __DIR__ . '/languages/classes/LanguageKk.php', 'KuConverter' => __DIR__ . '/languages/classes/LanguageKu.php', - 'LBFactory' => __DIR__ . '/includes/db/LBFactory.php', - 'LBFactoryFake' => __DIR__ . '/includes/db/LBFactory.php', - 'LBFactoryMulti' => __DIR__ . '/includes/db/LBFactoryMulti.php', - 'LBFactorySimple' => __DIR__ . '/includes/db/LBFactory.php', - 'LBFactorySingle' => __DIR__ . '/includes/db/LBFactorySingle.php', + 'LBFactory' => __DIR__ . '/includes/db/loadbalancer/LBFactory.php', + 'LBFactoryFake' => __DIR__ . '/includes/db/loadbalancer/LBFactoryFake.php', + 'LBFactoryMulti' => __DIR__ . '/includes/db/loadbalancer/LBFactoryMulti.php', + 'LBFactorySimple' => __DIR__ . '/includes/db/loadbalancer/LBFactorySimple.php', + 'LBFactorySingle' => __DIR__ . '/includes/db/loadbalancer/LBFactorySingle.php', 'LCStore' => __DIR__ . '/includes/cache/LocalisationCache.php', 'LCStoreCDB' => __DIR__ . '/includes/cache/LocalisationCache.php', 'LCStoreDB' => __DIR__ . '/includes/cache/LocalisationCache.php', @@ -686,11 +686,11 @@ $wgAutoloadLocalClasses = array( 'ListDuplicatedFilesPage' => __DIR__ . '/includes/specials/SpecialListDuplicatedFiles.php', 'ListVariants' => __DIR__ . '/maintenance/language/listVariants.php', 'ListredirectsPage' => __DIR__ . '/includes/specials/SpecialListredirects.php', - 'LoadBalancer' => __DIR__ . '/includes/db/LoadBalancer.php', - 'LoadBalancerSingle' => __DIR__ . '/includes/db/LBFactorySingle.php', - 'LoadMonitor' => __DIR__ . '/includes/db/LoadMonitor.php', - 'LoadMonitorMySQL' => __DIR__ . '/includes/db/LoadMonitorMySQL.php', - 'LoadMonitorNull' => __DIR__ . '/includes/db/LoadMonitor.php', + 'LoadBalancer' => __DIR__ . '/includes/db/loadbalancer/LoadBalancer.php', + 'LoadBalancerSingle' => __DIR__ . '/includes/db/loadbalancer/LBFactorySingle.php', + 'LoadMonitor' => __DIR__ . '/includes/db/loadbalancer/LoadMonitor.php', + 'LoadMonitorMySQL' => __DIR__ . '/includes/db/loadbalancer/LoadMonitorMySQL.php', + 'LoadMonitorNull' => __DIR__ . '/includes/db/loadbalancer/LoadMonitor.php', 'LocalFile' => __DIR__ . '/includes/filerepo/file/LocalFile.php', 'LocalFileDeleteBatch' => __DIR__ . '/includes/filerepo/file/LocalFile.php', 'LocalFileMoveBatch' => __DIR__ . '/includes/filerepo/file/LocalFile.php', diff --git a/includes/db/LBFactory.php b/includes/db/loadbalancer/LBFactory.php similarity index 61% rename from includes/db/LBFactory.php rename to includes/db/loadbalancer/LBFactory.php index cf522b2087..da0fe44c51 100644 --- a/includes/db/LBFactory.php +++ b/includes/db/loadbalancer/LBFactory.php @@ -215,167 +215,6 @@ abstract class LBFactory { } } -/** - * A simple single-master LBFactory that gets its configuration from the b/c globals - */ -class LBFactorySimple extends LBFactory { - /** @var LoadBalancer */ - private $mainLB; - - /** @var LoadBalancer[] */ - private $extLBs = array(); - - /** @var ChronologyProtector */ - private $chronProt; - - public function __construct( array $conf ) { - $this->chronProt = new ChronologyProtector; - } - - /** - * @param bool|string $wiki - * @return LoadBalancer - */ - public function newMainLB( $wiki = false ) { - global $wgDBservers; - if ( $wgDBservers ) { - $servers = $wgDBservers; - } else { - global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql; - global $wgDBssl, $wgDBcompress; - - $flags = DBO_DEFAULT; - if ( $wgDebugDumpSql ) { - $flags |= DBO_DEBUG; - } - if ( $wgDBssl ) { - $flags |= DBO_SSL; - } - if ( $wgDBcompress ) { - $flags |= DBO_COMPRESS; - } - - $servers = array( array( - 'host' => $wgDBserver, - 'user' => $wgDBuser, - 'password' => $wgDBpassword, - 'dbname' => $wgDBname, - 'type' => $wgDBtype, - 'load' => 1, - 'flags' => $flags - ) ); - } - - return new LoadBalancer( array( - 'servers' => $servers, - ) ); - } - - /** - * @param bool|string $wiki - * @return LoadBalancer - */ - public function getMainLB( $wiki = false ) { - if ( !isset( $this->mainLB ) ) { - $this->mainLB = $this->newMainLB( $wiki ); - $this->mainLB->parentInfo( array( 'id' => 'main' ) ); - $this->chronProt->initLB( $this->mainLB ); - } - - return $this->mainLB; - } - - /** - * @throws MWException - * @param string $cluster - * @param bool|string $wiki - * @return LoadBalancer - */ - protected function newExternalLB( $cluster, $wiki = false ) { - global $wgExternalServers; - if ( !isset( $wgExternalServers[$cluster] ) ) { - throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" ); - } - - return new LoadBalancer( array( - 'servers' => $wgExternalServers[$cluster] - ) ); - } - - /** - * @param string $cluster - * @param bool|string $wiki - * @return array - */ - public function &getExternalLB( $cluster, $wiki = false ) { - if ( !isset( $this->extLBs[$cluster] ) ) { - $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); - $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) ); - $this->chronProt->initLB( $this->extLBs[$cluster] ); - } - - return $this->extLBs[$cluster]; - } - - /** - * Execute a function for each tracked load balancer - * The callback is called with the load balancer as the first parameter, - * and $params passed as the subsequent parameters. - * - * @param callable $callback - * @param array $params - */ - public function forEachLB( $callback, array $params = array() ) { - if ( isset( $this->mainLB ) ) { - call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) ); - } - foreach ( $this->extLBs as $lb ) { - call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); - } - } - - public function shutdown() { - if ( $this->mainLB ) { - $this->chronProt->shutdownLB( $this->mainLB ); - } - foreach ( $this->extLBs as $extLB ) { - $this->chronProt->shutdownLB( $extLB ); - } - $this->chronProt->shutdown(); - $this->commitMasterChanges(); - } -} - -/** - * LBFactory class that throws an error on any attempt to use it. - * This will typically be done via wfGetDB(). - * Call LBFactory::disableBackend() to start using this, and - * LBFactory::enableBackend() to return to normal behavior - */ -class LBFactoryFake extends LBFactory { - public function __construct( array $conf ) { - } - - public function newMainLB( $wiki = false ) { - throw new DBAccessError; - } - - public function getMainLB( $wiki = false ) { - throw new DBAccessError; - } - - protected function newExternalLB( $cluster, $wiki = false ) { - throw new DBAccessError; - } - - public function &getExternalLB( $cluster, $wiki = false ) { - throw new DBAccessError; - } - - public function forEachLB( $callback, array $params = array() ) { - } -} - /** * Exception class for attempted DB access */ diff --git a/includes/db/loadbalancer/LBFactoryFake.php b/includes/db/loadbalancer/LBFactoryFake.php new file mode 100644 index 0000000000..d8becf50a7 --- /dev/null +++ b/includes/db/loadbalancer/LBFactoryFake.php @@ -0,0 +1,52 @@ +chronProt = new ChronologyProtector; + } + + /** + * @param bool|string $wiki + * @return LoadBalancer + */ + public function newMainLB( $wiki = false ) { + global $wgDBservers; + if ( $wgDBservers ) { + $servers = $wgDBservers; + } else { + global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql; + global $wgDBssl, $wgDBcompress; + + $flags = DBO_DEFAULT; + if ( $wgDebugDumpSql ) { + $flags |= DBO_DEBUG; + } + if ( $wgDBssl ) { + $flags |= DBO_SSL; + } + if ( $wgDBcompress ) { + $flags |= DBO_COMPRESS; + } + + $servers = array( array( + 'host' => $wgDBserver, + 'user' => $wgDBuser, + 'password' => $wgDBpassword, + 'dbname' => $wgDBname, + 'type' => $wgDBtype, + 'load' => 1, + 'flags' => $flags + ) ); + } + + return new LoadBalancer( array( + 'servers' => $servers, + ) ); + } + + /** + * @param bool|string $wiki + * @return LoadBalancer + */ + public function getMainLB( $wiki = false ) { + if ( !isset( $this->mainLB ) ) { + $this->mainLB = $this->newMainLB( $wiki ); + $this->mainLB->parentInfo( array( 'id' => 'main' ) ); + $this->chronProt->initLB( $this->mainLB ); + } + + return $this->mainLB; + } + + /** + * @throws MWException + * @param string $cluster + * @param bool|string $wiki + * @return LoadBalancer + */ + protected function newExternalLB( $cluster, $wiki = false ) { + global $wgExternalServers; + if ( !isset( $wgExternalServers[$cluster] ) ) { + throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" ); + } + + return new LoadBalancer( array( + 'servers' => $wgExternalServers[$cluster] + ) ); + } + + /** + * @param string $cluster + * @param bool|string $wiki + * @return array + */ + public function &getExternalLB( $cluster, $wiki = false ) { + if ( !isset( $this->extLBs[$cluster] ) ) { + $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); + $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) ); + $this->chronProt->initLB( $this->extLBs[$cluster] ); + } + + return $this->extLBs[$cluster]; + } + + /** + * Execute a function for each tracked load balancer + * The callback is called with the load balancer as the first parameter, + * and $params passed as the subsequent parameters. + * + * @param callable $callback + * @param array $params + */ + public function forEachLB( $callback, array $params = array() ) { + if ( isset( $this->mainLB ) ) { + call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) ); + } + foreach ( $this->extLBs as $lb ) { + call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); + } + } + + public function shutdown() { + if ( $this->mainLB ) { + $this->chronProt->shutdownLB( $this->mainLB ); + } + foreach ( $this->extLBs as $extLB ) { + $this->chronProt->shutdownLB( $extLB ); + } + $this->chronProt->shutdown(); + $this->commitMasterChanges(); + } +} diff --git a/includes/db/LBFactorySingle.php b/includes/db/loadbalancer/LBFactorySingle.php similarity index 100% rename from includes/db/LBFactorySingle.php rename to includes/db/loadbalancer/LBFactorySingle.php diff --git a/includes/db/LoadBalancer.php b/includes/db/loadbalancer/LoadBalancer.php similarity index 100% rename from includes/db/LoadBalancer.php rename to includes/db/loadbalancer/LoadBalancer.php diff --git a/includes/db/LoadMonitor.php b/includes/db/loadbalancer/LoadMonitor.php similarity index 100% rename from includes/db/LoadMonitor.php rename to includes/db/loadbalancer/LoadMonitor.php diff --git a/includes/db/LoadMonitorMySQL.php b/includes/db/loadbalancer/LoadMonitorMySQL.php similarity index 100% rename from includes/db/LoadMonitorMySQL.php rename to includes/db/loadbalancer/LoadMonitorMySQL.php -- 2.20.1