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}
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.
storage can be distributed across multiple servers
Definition memcached.txt:33
$params