MediaWiki REL1_33
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
44
57 public function __construct( array $conf ) {
58 parent::__construct( $conf );
59
60 $this->servers = $conf['servers'] ?? [];
61 foreach ( $this->servers as $i => $server ) {
62 if ( $i == 0 ) {
63 $this->servers[$i]['master'] = true;
64 } else {
65 $this->servers[$i]['replica'] = true;
66 }
67 }
68
69 $this->externalClusters = $conf['externalClusters'] ?? [];
70 $this->loadMonitorClass = $conf['loadMonitorClass'] ?? 'LoadMonitor';
71 }
72
77 public function newMainLB( $domain = false ) {
78 return $this->newLoadBalancer( $this->servers );
79 }
80
85 public function getMainLB( $domain = false ) {
86 if ( !isset( $this->mainLB ) ) {
87 $this->mainLB = $this->newMainLB( $domain );
88 }
89
90 return $this->mainLB;
91 }
92
93 public function newExternalLB( $cluster ) {
94 if ( !isset( $this->externalClusters[$cluster] ) ) {
95 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"." );
96 }
97
98 return $this->newLoadBalancer( $this->externalClusters[$cluster] );
99 }
100
101 public function getExternalLB( $cluster ) {
102 if ( !isset( $this->extLBs[$cluster] ) ) {
103 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
104 }
105
106 return $this->extLBs[$cluster];
107 }
108
109 public function getAllMainLBs() {
110 return [ 'DEFAULT' => $this->getMainLB() ];
111 }
112
113 public function getAllExternalLBs() {
114 $lbs = [];
115 foreach ( $this->externalClusters as $cluster => $unused ) {
116 $lbs[$cluster] = $this->getExternalLB( $cluster );
117 }
118
119 return $lbs;
120 }
121
122 private function newLoadBalancer( array $servers ) {
123 $lb = new LoadBalancer( array_merge(
124 $this->baseLoadBalancerParams(),
125 [
126 'servers' => $servers,
127 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
128 ]
129 ) );
130 $this->initLoadBalancer( $lb );
131
132 return $lb;
133 }
134
143 public function forEachLB( $callback, array $params = [] ) {
144 if ( isset( $this->mainLB ) ) {
145 $callback( $this->mainLB, ...$params );
146 }
147 foreach ( $this->extLBs as $lb ) {
148 $callback( $lb, ...$params );
149 }
150 }
151}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
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 ILoadBalancer::__construct()
Database connection, tracking, load balancing, and transaction manager for a cluster.
storage can be distributed across multiple servers
Definition memcached.txt:33
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params