LBFactory_Multi.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup Database
  5. */
  6. /**
  7. * A multi-wiki, multi-master factory for Wikimedia and similar installations.
  8. * Ignores the old configuration globals
  9. *
  10. * Configuration:
  11. * sectionsByDB A map of database names to section names
  12. *
  13. * sectionLoads A 2-d map. For each section, gives a map of server names to load ratios.
  14. * For example: array( 'section1' => array( 'db1' => 100, 'db2' => 100 ) )
  15. *
  16. * serverTemplate A server info associative array as documented for $wgDBservers. The host,
  17. * hostName and load entries will be overridden.
  18. *
  19. * groupLoadsBySection A 3-d map giving server load ratios for each section and group. For example:
  20. * array( 'section1' => array( 'group1' => array( 'db1' => 100, 'db2' => 100 ) ) )
  21. *
  22. * groupLoadsByDB A 3-d map giving server load ratios by DB name.
  23. *
  24. * hostsByName A map of hostname to IP address.
  25. *
  26. * externalLoads A map of external storage cluster name to server load map
  27. *
  28. * externalTemplateOverrides A set of server info keys overriding serverTemplate for external storage
  29. *
  30. * templateOverridesByServer A 2-d map overriding serverTemplate and externalTemplateOverrides on a
  31. * server-by-server basis. Applies to both core and external storage.
  32. *
  33. * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster
  34. *
  35. * masterTemplateOverrides An override array for all master servers.
  36. *
  37. * readOnlyBySection A map of section name to read-only message. Missing or false for read/write.
  38. *
  39. * @ingroup Database
  40. */
  41. class LBFactory_Multi extends LBFactory {
  42. // Required settings
  43. var $sectionsByDB, $sectionLoads, $serverTemplate;
  44. // Optional settings
  45. var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
  46. var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
  47. var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
  48. // Other stuff
  49. var $conf, $mainLBs = array(), $extLBs = array();
  50. var $lastWiki, $lastSection;
  51. function __construct( $conf ) {
  52. $this->chronProt = new ChronologyProtector;
  53. $this->conf = $conf;
  54. $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
  55. $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
  56. 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
  57. 'templateOverridesByCluster', 'masterTemplateOverrides',
  58. 'readOnlyBySection' );
  59. foreach ( $required as $key ) {
  60. if ( !isset( $conf[$key] ) ) {
  61. throw new MWException( __CLASS__.": $key is required in configuration" );
  62. }
  63. $this->$key = $conf[$key];
  64. }
  65. foreach ( $optional as $key ) {
  66. if ( isset( $conf[$key] ) ) {
  67. $this->$key = $conf[$key];
  68. }
  69. }
  70. // Check for read-only mode
  71. $section = $this->getSectionForWiki();
  72. if ( !empty( $this->readOnlyBySection[$section] ) ) {
  73. global $wgReadOnly;
  74. $wgReadOnly = $this->readOnlyBySection[$section];
  75. }
  76. }
  77. function getSectionForWiki( $wiki = false ) {
  78. if ( $this->lastWiki === $wiki ) {
  79. return $this->lastSection;
  80. }
  81. list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
  82. if ( isset( $this->sectionsByDB[$dbName] ) ) {
  83. $section = $this->sectionsByDB[$dbName];
  84. } else {
  85. $section = 'DEFAULT';
  86. }
  87. $this->lastSection = $section;
  88. $this->lastWiki = $wiki;
  89. return $section;
  90. }
  91. function newMainLB( $wiki = false ) {
  92. list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
  93. $section = $this->getSectionForWiki( $wiki );
  94. $groupLoads = array();
  95. if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
  96. $groupLoads = $this->groupLoadsByDB[$dbName];
  97. }
  98. if ( isset( $this->groupLoadsBySection[$section] ) ) {
  99. $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
  100. }
  101. return $this->newLoadBalancer( $this->serverTemplate, $this->sectionLoads[$section], $groupLoads );
  102. }
  103. function getMainLB( $wiki = false ) {
  104. $section = $this->getSectionForWiki( $wiki );
  105. if ( !isset( $this->mainLBs[$section] ) ) {
  106. $lb = $this->newMainLB( $wiki, $section );
  107. $this->chronProt->initLB( $lb );
  108. $lb->parentInfo( array( 'id' => "main-$section" ) );
  109. $this->mainLBs[$section] = $lb;
  110. }
  111. return $this->mainLBs[$section];
  112. }
  113. function newExternalLB( $cluster, $wiki = false ) {
  114. if ( !isset( $this->externalLoads[$cluster] ) ) {
  115. throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
  116. }
  117. $template = $this->serverTemplate;
  118. if ( isset( $this->externalTemplateOverrides ) ) {
  119. $template = $this->externalTemplateOverrides + $template;
  120. }
  121. if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
  122. $template = $this->templateOverridesByCluster[$cluster] + $template;
  123. }
  124. return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
  125. }
  126. function &getExternalLB( $cluster, $wiki = false ) {
  127. if ( !isset( $this->extLBs[$cluster] ) ) {
  128. $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
  129. $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
  130. }
  131. return $this->extLBs[$cluster];
  132. }
  133. /**
  134. * Make a new load balancer object based on template and load array
  135. */
  136. function newLoadBalancer( $template, $loads, $groupLoads ) {
  137. global $wgMasterWaitTimeout;
  138. $servers = $this->makeServerArray( $template, $loads, $groupLoads );
  139. $lb = new LoadBalancer( array(
  140. 'servers' => $servers,
  141. 'masterWaitTimeout' => $wgMasterWaitTimeout
  142. ));
  143. return $lb;
  144. }
  145. /**
  146. * Make a server array as expected by LoadBalancer::__construct, using a template and load array
  147. */
  148. function makeServerArray( $template, $loads, $groupLoads ) {
  149. $servers = array();
  150. $master = true;
  151. $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
  152. foreach ( $groupLoadsByServer as $server => $stuff ) {
  153. if ( !isset( $loads[$server] ) ) {
  154. $loads[$server] = 0;
  155. }
  156. }
  157. foreach ( $loads as $serverName => $load ) {
  158. $serverInfo = $template;
  159. if ( $master ) {
  160. $serverInfo['master'] = true;
  161. if ( isset( $this->masterTemplateOverrides ) ) {
  162. $serverInfo = $this->masterTemplateOverrides + $serverInfo;
  163. }
  164. $master = false;
  165. }
  166. if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
  167. $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
  168. }
  169. if ( isset( $groupLoadsByServer[$serverName] ) ) {
  170. $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
  171. }
  172. if ( isset( $this->hostsByName[$serverName] ) ) {
  173. $serverInfo['host'] = $this->hostsByName[$serverName];
  174. } else {
  175. $serverInfo['host'] = $serverName;
  176. }
  177. $serverInfo['hostName'] = $serverName;
  178. $serverInfo['load'] = $load;
  179. $servers[] = $serverInfo;
  180. }
  181. return $servers;
  182. }
  183. /**
  184. * Take a group load array indexed by group then server, and reindex it by server then group
  185. */
  186. function reindexGroupLoads( $groupLoads ) {
  187. $reindexed = array();
  188. foreach ( $groupLoads as $group => $loads ) {
  189. foreach ( $loads as $server => $load ) {
  190. $reindexed[$server][$group] = $load;
  191. }
  192. }
  193. return $reindexed;
  194. }
  195. /**
  196. * Get the database name and prefix based on the wiki ID
  197. */
  198. function getDBNameAndPrefix( $wiki = false ) {
  199. if ( $wiki === false ) {
  200. global $wgDBname, $wgDBprefix;
  201. return array( $wgDBname, $wgDBprefix );
  202. } else {
  203. return wfSplitWikiID( $wiki );
  204. }
  205. }
  206. /**
  207. * Execute a function for each tracked load balancer
  208. * The callback is called with the load balancer as the first parameter,
  209. * and $params passed as the subsequent parameters.
  210. */
  211. function forEachLB( $callback, $params = array() ) {
  212. foreach ( $this->mainLBs as $lb ) {
  213. call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
  214. }
  215. foreach ( $this->extLBs as $lb ) {
  216. call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
  217. }
  218. }
  219. function shutdown() {
  220. foreach ( $this->mainLBs as $lb ) {
  221. $this->chronProt->shutdownLB( $lb );
  222. }
  223. $this->chronProt->shutdown();
  224. $this->commitMasterChanges();
  225. }
  226. }