MediaWiki REL1_31
LBFactorySimple.php
Go to the documentation of this file.
1<?php
24namespace Wikimedia\Rdbms;
25
26use InvalidArgumentException;
27
33 private $mainLB;
35 private $extLBs = [];
36
38 private $servers = [];
40 private $externalClusters = [];
41
45 private $maxLag;
46
48 const MAX_LAG_DEFAULT = 10;
49
62 public function __construct( array $conf ) {
63 parent::__construct( $conf );
64
65 $this->servers = isset( $conf['servers'] ) ? $conf['servers'] : [];
66 foreach ( $this->servers as $i => $server ) {
67 if ( $i == 0 ) {
68 $this->servers[$i]['master'] = true;
69 } else {
70 $this->servers[$i]['replica'] = true;
71 }
72 }
73
74 $this->externalClusters = isset( $conf['externalClusters'] )
75 ? $conf['externalClusters']
76 : [];
77 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
78 ? $conf['loadMonitorClass']
79 : 'LoadMonitor';
80 $this->maxLag = isset( $conf['maxLag'] ) ? $conf['maxLag'] : self::MAX_LAG_DEFAULT;
81 }
82
87 public function newMainLB( $domain = false ) {
88 return $this->newLoadBalancer( $this->servers );
89 }
90
95 public function getMainLB( $domain = false ) {
96 if ( !isset( $this->mainLB ) ) {
97 $this->mainLB = $this->newMainLB( $domain );
98 }
99
100 return $this->mainLB;
101 }
102
103 public function newExternalLB( $cluster ) {
104 if ( !isset( $this->externalClusters[$cluster] ) ) {
105 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"." );
106 }
107
108 return $this->newLoadBalancer( $this->externalClusters[$cluster] );
109 }
110
111 public function getExternalLB( $cluster ) {
112 if ( !isset( $this->extLBs[$cluster] ) ) {
113 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
114 }
115
116 return $this->extLBs[$cluster];
117 }
118
119 public function getAllMainLBs() {
120 return [ 'DEFAULT' => $this->getMainLB() ];
121 }
122
123 public function getAllExternalLBs() {
124 $lbs = [];
125 foreach ( $this->externalClusters as $cluster => $unused ) {
126 $lbs[$cluster] = $this->getExternalLB( $cluster );
127 }
128
129 return $lbs;
130 }
131
132 private function newLoadBalancer( array $servers ) {
133 $lb = new LoadBalancer( array_merge(
134 $this->baseLoadBalancerParams(),
135 [
136 'servers' => $servers,
137 'maxLag' => $this->maxLag,
138 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
139 ]
140 ) );
141 $this->initLoadBalancer( $lb );
142
143 return $lb;
144 }
145
154 public function forEachLB( $callback, array $params = [] ) {
155 if ( isset( $this->mainLB ) ) {
156 call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
157 }
158 foreach ( $this->extLBs as $lb ) {
159 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
160 }
161 }
162}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
A simple single-master LBFactory that gets its configuration from the b/c globals.
getAllMainLBs()
Get cached (tracked) load balancers for all main database clusters.
getAllExternalLBs()
Get cached (tracked) load balancers for all external database clusters.
array[] $servers
Map of (server index => server config)
array[] $externalClusters
Map of (cluster => (server index => server config))
forEachLB( $callback, array $params=[])
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
An interface for generating database load balancers.
Definition LBFactory.php:39
initLoadBalancer(ILoadBalancer $lb)
baseLoadBalancerParams()
Base parameters to LoadBalancer::__construct()
Database connection, tracking, load balancing, and transaction manager for a cluster.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
storage can be distributed across multiple servers
Definition memcached.txt:33
$params