Merge "TableDiffFormatter: Don't repeatedly call array_shift()"
[lhc/web/wiklou.git] / includes / db / loadbalancer / LoadBalancer.php
1 <?php
2 /**
3 * Database load balancing.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database load balancing object
26 *
27 * @todo document
28 * @ingroup Database
29 */
30 class LoadBalancer {
31 /** @var array[] Map of (server index => server config array) */
32 private $mServers;
33 /** @var array[] Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) */
34 private $mConns;
35 /** @var array Map of (server index => weight) */
36 private $mLoads;
37 /** @var array[] Map of (group => server index => weight) */
38 private $mGroupLoads;
39 /** @var bool Whether to disregard slave lag as a factor in slave selection */
40 private $mAllowLagged;
41 /** @var integer Seconds to spend waiting on slave lag to resolve */
42 private $mWaitTimeout;
43 /** @var array LBFactory information */
44 private $mParentInfo;
45
46 /** @var string The LoadMonitor subclass name */
47 private $mLoadMonitorClass;
48 /** @var LoadMonitor */
49 private $mLoadMonitor;
50 /** @var BagOStuff */
51 private $srvCache;
52
53 /** @var bool|DatabaseBase Database connection that caused a problem */
54 private $mErrorConnection;
55 /** @var integer The generic (not query grouped) slave index (of $mServers) */
56 private $mReadIndex;
57 /** @var bool|DBMasterPos False if not set */
58 private $mWaitForPos;
59 /** @var bool Whether the generic reader fell back to a lagged slave */
60 private $laggedSlaveMode = false;
61 /** @var bool Whether the generic reader fell back to a lagged slave */
62 private $slavesDownMode = false;
63 /** @var string The last DB selection or connection error */
64 private $mLastError = 'Unknown error';
65 /** @var string|bool Reason the LB is read-only or false if not */
66 private $readOnlyReason = false;
67 /** @var integer Total connections opened */
68 private $connsOpened = 0;
69
70 /** @var TransactionProfiler */
71 protected $trxProfiler;
72
73 /** @var integer Warn when this many connection are held */
74 const CONN_HELD_WARN_THRESHOLD = 10;
75 /** @var integer Default 'max lag' when unspecified */
76 const MAX_LAG = 10;
77 /** @var integer Max time to wait for a slave to catch up (e.g. ChronologyProtector) */
78 const POS_WAIT_TIMEOUT = 10;
79
80 /**
81 * @param array $params Array with keys:
82 * - servers : Required. Array of server info structures.
83 * - loadMonitor : Name of a class used to fetch server lag and load.
84 * - readOnlyReason : Reason the master DB is read-only if so [optional]
85 * @throws MWException
86 */
87 public function __construct( array $params ) {
88 if ( !isset( $params['servers'] ) ) {
89 throw new MWException( __CLASS__ . ': missing servers parameter' );
90 }
91 $this->mServers = $params['servers'];
92 $this->mWaitTimeout = self::POS_WAIT_TIMEOUT;
93
94 $this->mReadIndex = -1;
95 $this->mWriteIndex = -1;
96 $this->mConns = [
97 'local' => [],
98 'foreignUsed' => [],
99 'foreignFree' => [] ];
100 $this->mLoads = [];
101 $this->mWaitForPos = false;
102 $this->mErrorConnection = false;
103 $this->mAllowLagged = false;
104
105 if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) {
106 $this->readOnlyReason = $params['readOnlyReason'];
107 }
108
109 if ( isset( $params['loadMonitor'] ) ) {
110 $this->mLoadMonitorClass = $params['loadMonitor'];
111 } else {
112 $master = reset( $params['servers'] );
113 if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
114 $this->mLoadMonitorClass = 'LoadMonitorMySQL';
115 } else {
116 $this->mLoadMonitorClass = 'LoadMonitorNull';
117 }
118 }
119
120 foreach ( $params['servers'] as $i => $server ) {
121 $this->mLoads[$i] = $server['load'];
122 if ( isset( $server['groupLoads'] ) ) {
123 foreach ( $server['groupLoads'] as $group => $ratio ) {
124 if ( !isset( $this->mGroupLoads[$group] ) ) {
125 $this->mGroupLoads[$group] = [];
126 }
127 $this->mGroupLoads[$group][$i] = $ratio;
128 }
129 }
130 }
131
132 $this->srvCache = ObjectCache::getLocalServerInstance();
133
134 if ( isset( $params['trxProfiler'] ) ) {
135 $this->trxProfiler = $params['trxProfiler'];
136 } else {
137 $this->trxProfiler = new TransactionProfiler();
138 }
139 }
140
141 /**
142 * Get a LoadMonitor instance
143 *
144 * @return LoadMonitor
145 */
146 private function getLoadMonitor() {
147 if ( !isset( $this->mLoadMonitor ) ) {
148 $class = $this->mLoadMonitorClass;
149 $this->mLoadMonitor = new $class( $this );
150 }
151
152 return $this->mLoadMonitor;
153 }
154
155 /**
156 * Get or set arbitrary data used by the parent object, usually an LBFactory
157 * @param mixed $x
158 * @return mixed
159 */
160 public function parentInfo( $x = null ) {
161 return wfSetVar( $this->mParentInfo, $x );
162 }
163
164 /**
165 * @param array $loads
166 * @param bool|string $wiki Wiki to get non-lagged for
167 * @param int $maxLag Restrict the maximum allowed lag to this many seconds
168 * @return bool|int|string
169 */
170 private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self::MAX_LAG ) {
171 $lags = $this->getLagTimes( $wiki );
172
173 # Unset excessively lagged servers
174 foreach ( $lags as $i => $lag ) {
175 if ( $i != 0 ) {
176 $maxServerLag = $maxLag;
177 if ( isset( $this->mServers[$i]['max lag'] ) ) {
178 $maxServerLag = min( $maxServerLag, $this->mServers[$i]['max lag'] );
179 }
180
181 $host = $this->getServerName( $i );
182 if ( $lag === false ) {
183 wfDebugLog( 'replication', "Server $host (#$i) is not replicating?" );
184 unset( $loads[$i] );
185 } elseif ( $lag > $maxServerLag ) {
186 wfDebugLog( 'replication', "Server $host (#$i) has >= $lag seconds of lag" );
187 unset( $loads[$i] );
188 }
189 }
190 }
191
192 # Find out if all the slaves with non-zero load are lagged
193 $sum = 0;
194 foreach ( $loads as $load ) {
195 $sum += $load;
196 }
197 if ( $sum == 0 ) {
198 # No appropriate DB servers except maybe the master and some slaves with zero load
199 # Do NOT use the master
200 # Instead, this function will return false, triggering read-only mode,
201 # and a lagged slave will be used instead.
202 return false;
203 }
204
205 if ( count( $loads ) == 0 ) {
206 return false;
207 }
208
209 # Return a random representative of the remainder
210 return ArrayUtils::pickRandom( $loads );
211 }
212
213 /**
214 * Get the index of the reader connection, which may be a slave
215 * This takes into account load ratios and lag times. It should
216 * always return a consistent index during a given invocation
217 *
218 * Side effect: opens connections to databases
219 * @param string|bool $group Query group, or false for the generic reader
220 * @param string|bool $wiki Wiki ID, or false for the current wiki
221 * @throws MWException
222 * @return bool|int|string
223 */
224 public function getReaderIndex( $group = false, $wiki = false ) {
225 global $wgDBtype;
226
227 # @todo FIXME: For now, only go through all this for mysql databases
228 if ( $wgDBtype != 'mysql' ) {
229 return $this->getWriterIndex();
230 }
231
232 if ( count( $this->mServers ) == 1 ) {
233 # Skip the load balancing if there's only one server
234 return 0;
235 } elseif ( $group === false && $this->mReadIndex >= 0 ) {
236 # Shortcut if generic reader exists already
237 return $this->mReadIndex;
238 }
239
240 # Find the relevant load array
241 if ( $group !== false ) {
242 if ( isset( $this->mGroupLoads[$group] ) ) {
243 $nonErrorLoads = $this->mGroupLoads[$group];
244 } else {
245 # No loads for this group, return false and the caller can use some other group
246 wfDebugLog( 'connect', __METHOD__ . ": no loads for group $group\n" );
247
248 return false;
249 }
250 } else {
251 $nonErrorLoads = $this->mLoads;
252 }
253
254 if ( !count( $nonErrorLoads ) ) {
255 throw new MWException( "Empty server array given to LoadBalancer" );
256 }
257
258 # Scale the configured load ratios according to the dynamic load (if the load monitor supports it)
259 $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $wiki );
260
261 $laggedSlaveMode = false;
262
263 # No server found yet
264 $i = false;
265 $conn = false;
266 # First try quickly looking through the available servers for a server that
267 # meets our criteria
268 $currentLoads = $nonErrorLoads;
269 while ( count( $currentLoads ) ) {
270 if ( $this->mAllowLagged || $laggedSlaveMode ) {
271 $i = ArrayUtils::pickRandom( $currentLoads );
272 } else {
273 $i = false;
274 if ( $this->mWaitForPos && $this->mWaitForPos->asOfTime() ) {
275 # ChronologyProtecter causes mWaitForPos to be set via sessions.
276 # This triggers doWait() after connect, so it's especially good to
277 # avoid lagged servers so as to avoid just blocking in that method.
278 $ago = microtime( true ) - $this->mWaitForPos->asOfTime();
279 # Aim for <= 1 second of waiting (being too picky can backfire)
280 $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago + 1 );
281 }
282 if ( $i === false ) {
283 # Any server with less lag than it's 'max lag' param is preferable
284 $i = $this->getRandomNonLagged( $currentLoads, $wiki );
285 }
286 if ( $i === false && count( $currentLoads ) != 0 ) {
287 # All slaves lagged. Switch to read-only mode
288 wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" );
289 $i = ArrayUtils::pickRandom( $currentLoads );
290 $laggedSlaveMode = true;
291 }
292 }
293
294 if ( $i === false ) {
295 # pickRandom() returned false
296 # This is permanent and means the configuration or the load monitor
297 # wants us to return false.
298 wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false" );
299
300 return false;
301 }
302
303 $serverName = $this->getServerName( $i );
304 wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: $serverName..." );
305
306 $conn = $this->openConnection( $i, $wiki );
307 if ( !$conn ) {
308 wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki" );
309 unset( $nonErrorLoads[$i] );
310 unset( $currentLoads[$i] );
311 $i = false;
312 continue;
313 }
314
315 // Decrement reference counter, we are finished with this connection.
316 // It will be incremented for the caller later.
317 if ( $wiki !== false ) {
318 $this->reuseConnection( $conn );
319 }
320
321 # Return this server
322 break;
323 }
324
325 # If all servers were down, quit now
326 if ( !count( $nonErrorLoads ) ) {
327 wfDebugLog( 'connect', "All servers down" );
328 }
329
330 if ( $i !== false ) {
331 # Slave connection successful
332 # Wait for the session master pos for a short time
333 if ( $this->mWaitForPos && $i > 0 ) {
334 if ( !$this->doWait( $i ) ) {
335 $this->mServers[$i]['slave pos'] = $conn->getSlavePos();
336 }
337 }
338 if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) {
339 $this->mReadIndex = $i;
340 # Record if the generic reader index is in "lagged slave" mode
341 if ( $laggedSlaveMode ) {
342 $this->laggedSlaveMode = true;
343 }
344 }
345 $serverName = $this->getServerName( $i );
346 wfDebugLog( 'connect', __METHOD__ .
347 ": using server $serverName for group '$group'\n" );
348 }
349
350 return $i;
351 }
352
353 /**
354 * Set the master wait position
355 * If a DB_SLAVE connection has been opened already, waits
356 * Otherwise sets a variable telling it to wait if such a connection is opened
357 * @param DBMasterPos $pos
358 */
359 public function waitFor( $pos ) {
360 $this->mWaitForPos = $pos;
361 $i = $this->mReadIndex;
362
363 if ( $i > 0 ) {
364 if ( !$this->doWait( $i ) ) {
365 $this->mServers[$i]['slave pos'] = $this->getAnyOpenConnection( $i )->getSlavePos();
366 $this->laggedSlaveMode = true;
367 }
368 }
369 }
370
371 /**
372 * Set the master wait position and wait for a "generic" slave to catch up to it
373 *
374 * This can be used a faster proxy for waitForAll()
375 *
376 * @param DBMasterPos $pos
377 * @param int $timeout Max seconds to wait; default is mWaitTimeout
378 * @return bool Success (able to connect and no timeouts reached)
379 * @since 1.26
380 */
381 public function waitForOne( $pos, $timeout = null ) {
382 $this->mWaitForPos = $pos;
383
384 $i = $this->mReadIndex;
385 if ( $i <= 0 ) {
386 // Pick a generic slave if there isn't one yet
387 $readLoads = $this->mLoads;
388 unset( $readLoads[$this->getWriterIndex()] ); // slaves only
389 $readLoads = array_filter( $readLoads ); // with non-zero load
390 $i = ArrayUtils::pickRandom( $readLoads );
391 }
392
393 if ( $i > 0 ) {
394 $ok = $this->doWait( $i, true, $timeout );
395 } else {
396 $ok = true; // no applicable loads
397 }
398
399 return $ok;
400 }
401
402 /**
403 * Set the master wait position and wait for ALL slaves to catch up to it
404 * @param DBMasterPos $pos
405 * @param int $timeout Max seconds to wait; default is mWaitTimeout
406 * @return bool Success (able to connect and no timeouts reached)
407 */
408 public function waitForAll( $pos, $timeout = null ) {
409 $this->mWaitForPos = $pos;
410 $serverCount = count( $this->mServers );
411
412 $ok = true;
413 for ( $i = 1; $i < $serverCount; $i++ ) {
414 if ( $this->mLoads[$i] > 0 ) {
415 $ok = $this->doWait( $i, true, $timeout ) && $ok;
416 }
417 }
418
419 return $ok;
420 }
421
422 /**
423 * Get any open connection to a given server index, local or foreign
424 * Returns false if there is no connection open
425 *
426 * @param int $i
427 * @return DatabaseBase|bool False on failure
428 */
429 public function getAnyOpenConnection( $i ) {
430 foreach ( $this->mConns as $conns ) {
431 if ( !empty( $conns[$i] ) ) {
432 return reset( $conns[$i] );
433 }
434 }
435
436 return false;
437 }
438
439 /**
440 * Wait for a given slave to catch up to the master pos stored in $this
441 * @param int $index Server index
442 * @param bool $open Check the server even if a new connection has to be made
443 * @param int $timeout Max seconds to wait; default is mWaitTimeout
444 * @return bool
445 */
446 protected function doWait( $index, $open = false, $timeout = null ) {
447 $close = false; // close the connection afterwards
448
449 // Check if we already know that the DB has reached this point
450 $server = $this->getServerName( $index );
451 $key = $this->srvCache->makeGlobalKey( __CLASS__, 'last-known-pos', $server );
452 /** @var DBMasterPos $knownReachedPos */
453 $knownReachedPos = $this->srvCache->get( $key );
454 if ( $knownReachedPos && $knownReachedPos->hasReached( $this->mWaitForPos ) ) {
455 wfDebugLog( 'replication', __METHOD__ .
456 ": slave $server known to be caught up (pos >= $knownReachedPos).\n" );
457 return true;
458 }
459
460 // Find a connection to wait on, creating one if needed and allowed
461 $conn = $this->getAnyOpenConnection( $index );
462 if ( !$conn ) {
463 if ( !$open ) {
464 wfDebugLog( 'replication', __METHOD__ . ": no connection open for $server\n" );
465
466 return false;
467 } else {
468 $conn = $this->openConnection( $index, '' );
469 if ( !$conn ) {
470 wfDebugLog( 'replication', __METHOD__ . ": failed to connect to $server\n" );
471
472 return false;
473 }
474 // Avoid connection spam in waitForAll() when connections
475 // are made just for the sake of doing this lag check.
476 $close = true;
477 }
478 }
479
480 wfDebugLog( 'replication', __METHOD__ . ": Waiting for slave $server to catch up...\n" );
481 $timeout = $timeout ?: $this->mWaitTimeout;
482 $result = $conn->masterPosWait( $this->mWaitForPos, $timeout );
483
484 if ( $result == -1 || is_null( $result ) ) {
485 // Timed out waiting for slave, use master instead
486 $msg = __METHOD__ . ": Timed out waiting on $server pos {$this->mWaitForPos}";
487 wfDebugLog( 'replication', "$msg\n" );
488 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
489 $ok = false;
490 } else {
491 wfDebugLog( 'replication', __METHOD__ . ": Done\n" );
492 $ok = true;
493 // Remember that the DB reached this point
494 $this->srvCache->set( $key, $this->mWaitForPos, BagOStuff::TTL_DAY );
495 }
496
497 if ( $close ) {
498 $this->closeConnection( $conn );
499 }
500
501 return $ok;
502 }
503
504 /**
505 * Get a connection by index
506 * This is the main entry point for this class.
507 *
508 * @param int $i Server index
509 * @param array|string|bool $groups Query group(s), or false for the generic reader
510 * @param string|bool $wiki Wiki ID, or false for the current wiki
511 *
512 * @throws MWException
513 * @return DatabaseBase
514 */
515 public function getConnection( $i, $groups = [], $wiki = false ) {
516 if ( $i === null || $i === false ) {
517 throw new MWException( 'Attempt to call ' . __METHOD__ .
518 ' with invalid server index' );
519 }
520
521 if ( $wiki === wfWikiID() ) {
522 $wiki = false;
523 }
524
525 $groups = ( $groups === false || $groups === [] )
526 ? [ false ] // check one "group": the generic pool
527 : (array)$groups;
528
529 $masterOnly = ( $i == DB_MASTER || $i == $this->getWriterIndex() );
530 $oldConnsOpened = $this->connsOpened; // connections open now
531
532 if ( $i == DB_MASTER ) {
533 $i = $this->getWriterIndex();
534 } else {
535 # Try to find an available server in any the query groups (in order)
536 foreach ( $groups as $group ) {
537 $groupIndex = $this->getReaderIndex( $group, $wiki );
538 if ( $groupIndex !== false ) {
539 $i = $groupIndex;
540 break;
541 }
542 }
543 }
544
545 # Operation-based index
546 if ( $i == DB_SLAVE ) {
547 $this->mLastError = 'Unknown error'; // reset error string
548 # Try the general server pool if $groups are unavailable.
549 $i = in_array( false, $groups, true )
550 ? false // don't bother with this if that is what was tried above
551 : $this->getReaderIndex( false, $wiki );
552 # Couldn't find a working server in getReaderIndex()?
553 if ( $i === false ) {
554 $this->mLastError = 'No working slave server: ' . $this->mLastError;
555
556 return $this->reportConnectionError();
557 }
558 }
559
560 # Now we have an explicit index into the servers array
561 $conn = $this->openConnection( $i, $wiki );
562 if ( !$conn ) {
563 return $this->reportConnectionError();
564 }
565
566 # Profile any new connections that happen
567 if ( $this->connsOpened > $oldConnsOpened ) {
568 $host = $conn->getServer();
569 $dbname = $conn->getDBname();
570 $trxProf = Profiler::instance()->getTransactionProfiler();
571 $trxProf->recordConnection( $host, $dbname, $masterOnly );
572 }
573
574 if ( $masterOnly ) {
575 # Make master-requested DB handles inherit any read-only mode setting
576 $conn->setLBInfo( 'readOnlyReason', $this->getReadOnlyReason( $wiki ) );
577 }
578
579 return $conn;
580 }
581
582 /**
583 * Mark a foreign connection as being available for reuse under a different
584 * DB name or prefix. This mechanism is reference-counted, and must be called
585 * the same number of times as getConnection() to work.
586 *
587 * @param DatabaseBase $conn
588 * @throws MWException
589 */
590 public function reuseConnection( $conn ) {
591 $serverIndex = $conn->getLBInfo( 'serverIndex' );
592 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
593 if ( $serverIndex === null || $refCount === null ) {
594 wfDebug( __METHOD__ . ": this connection was not opened as a foreign connection\n" );
595 /**
596 * This can happen in code like:
597 * foreach ( $dbs as $db ) {
598 * $conn = $lb->getConnection( DB_SLAVE, array(), $db );
599 * ...
600 * $lb->reuseConnection( $conn );
601 * }
602 * When a connection to the local DB is opened in this way, reuseConnection()
603 * should be ignored
604 */
605 return;
606 }
607
608 $dbName = $conn->getDBname();
609 $prefix = $conn->tablePrefix();
610 if ( strval( $prefix ) !== '' ) {
611 $wiki = "$dbName-$prefix";
612 } else {
613 $wiki = $dbName;
614 }
615 if ( $this->mConns['foreignUsed'][$serverIndex][$wiki] !== $conn ) {
616 throw new MWException( __METHOD__ . ": connection not found, has " .
617 "the connection been freed already?" );
618 }
619 $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
620 if ( $refCount <= 0 ) {
621 $this->mConns['foreignFree'][$serverIndex][$wiki] = $conn;
622 unset( $this->mConns['foreignUsed'][$serverIndex][$wiki] );
623 wfDebug( __METHOD__ . ": freed connection $serverIndex/$wiki\n" );
624 } else {
625 wfDebug( __METHOD__ . ": reference count for $serverIndex/$wiki reduced to $refCount\n" );
626 }
627 }
628
629 /**
630 * Get a database connection handle reference
631 *
632 * The handle's methods wrap simply wrap those of a DatabaseBase handle
633 *
634 * @see LoadBalancer::getConnection() for parameter information
635 *
636 * @param int $db
637 * @param array|string|bool $groups Query group(s), or false for the generic reader
638 * @param string|bool $wiki Wiki ID, or false for the current wiki
639 * @return DBConnRef
640 */
641 public function getConnectionRef( $db, $groups = [], $wiki = false ) {
642 return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
643 }
644
645 /**
646 * Get a database connection handle reference without connecting yet
647 *
648 * The handle's methods wrap simply wrap those of a DatabaseBase handle
649 *
650 * @see LoadBalancer::getConnection() for parameter information
651 *
652 * @param int $db
653 * @param array|string|bool $groups Query group(s), or false for the generic reader
654 * @param string|bool $wiki Wiki ID, or false for the current wiki
655 * @return DBConnRef
656 */
657 public function getLazyConnectionRef( $db, $groups = [], $wiki = false ) {
658 return new DBConnRef( $this, [ $db, $groups, $wiki ] );
659 }
660
661 /**
662 * Open a connection to the server given by the specified index
663 * Index must be an actual index into the array.
664 * If the server is already open, returns it.
665 *
666 * On error, returns false, and the connection which caused the
667 * error will be available via $this->mErrorConnection.
668 *
669 * @param int $i Server index
670 * @param string|bool $wiki Wiki ID, or false for the current wiki
671 * @return DatabaseBase|bool Returns false on errors
672 */
673 public function openConnection( $i, $wiki = false ) {
674 if ( $wiki !== false ) {
675 $conn = $this->openForeignConnection( $i, $wiki );
676 } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
677 $conn = $this->mConns['local'][$i][0];
678 } else {
679 $server = $this->mServers[$i];
680 $server['serverIndex'] = $i;
681 $conn = $this->reallyOpenConnection( $server, false );
682 $serverName = $this->getServerName( $i );
683 if ( $conn->isOpen() ) {
684 wfDebugLog( 'connect', "Connected to database $i at $serverName\n" );
685 $this->mConns['local'][$i][0] = $conn;
686 } else {
687 wfDebugLog( 'connect', "Failed to connect to database $i at $serverName\n" );
688 $this->mErrorConnection = $conn;
689 $conn = false;
690 }
691 }
692
693 if ( $conn && !$conn->isOpen() ) {
694 // Connection was made but later unrecoverably lost for some reason.
695 // Do not return a handle that will just throw exceptions on use,
696 // but let the calling code (e.g. getReaderIndex) try another server.
697 // See DatabaseMyslBase::ping() for how this can happen.
698 $this->mErrorConnection = $conn;
699 $conn = false;
700 }
701
702 return $conn;
703 }
704
705 /**
706 * Open a connection to a foreign DB, or return one if it is already open.
707 *
708 * Increments a reference count on the returned connection which locks the
709 * connection to the requested wiki. This reference count can be
710 * decremented by calling reuseConnection().
711 *
712 * If a connection is open to the appropriate server already, but with the wrong
713 * database, it will be switched to the right database and returned, as long as
714 * it has been freed first with reuseConnection().
715 *
716 * On error, returns false, and the connection which caused the
717 * error will be available via $this->mErrorConnection.
718 *
719 * @param int $i Server index
720 * @param string $wiki Wiki ID to open
721 * @return DatabaseBase
722 */
723 private function openForeignConnection( $i, $wiki ) {
724 list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
725 if ( isset( $this->mConns['foreignUsed'][$i][$wiki] ) ) {
726 // Reuse an already-used connection
727 $conn = $this->mConns['foreignUsed'][$i][$wiki];
728 wfDebug( __METHOD__ . ": reusing connection $i/$wiki\n" );
729 } elseif ( isset( $this->mConns['foreignFree'][$i][$wiki] ) ) {
730 // Reuse a free connection for the same wiki
731 $conn = $this->mConns['foreignFree'][$i][$wiki];
732 unset( $this->mConns['foreignFree'][$i][$wiki] );
733 $this->mConns['foreignUsed'][$i][$wiki] = $conn;
734 wfDebug( __METHOD__ . ": reusing free connection $i/$wiki\n" );
735 } elseif ( !empty( $this->mConns['foreignFree'][$i] ) ) {
736 // Reuse a connection from another wiki
737 $conn = reset( $this->mConns['foreignFree'][$i] );
738 $oldWiki = key( $this->mConns['foreignFree'][$i] );
739
740 // The empty string as a DB name means "don't care".
741 // DatabaseMysqlBase::open() already handle this on connection.
742 if ( $dbName !== '' && !$conn->selectDB( $dbName ) ) {
743 $this->mLastError = "Error selecting database $dbName on server " .
744 $conn->getServer() . " from client host " . wfHostname() . "\n";
745 $this->mErrorConnection = $conn;
746 $conn = false;
747 } else {
748 $conn->tablePrefix( $prefix );
749 unset( $this->mConns['foreignFree'][$i][$oldWiki] );
750 $this->mConns['foreignUsed'][$i][$wiki] = $conn;
751 wfDebug( __METHOD__ . ": reusing free connection from $oldWiki for $wiki\n" );
752 }
753 } else {
754 // Open a new connection
755 $server = $this->mServers[$i];
756 $server['serverIndex'] = $i;
757 $server['foreignPoolRefCount'] = 0;
758 $server['foreign'] = true;
759 $conn = $this->reallyOpenConnection( $server, $dbName );
760 if ( !$conn->isOpen() ) {
761 wfDebug( __METHOD__ . ": error opening connection for $i/$wiki\n" );
762 $this->mErrorConnection = $conn;
763 $conn = false;
764 } else {
765 $conn->tablePrefix( $prefix );
766 $this->mConns['foreignUsed'][$i][$wiki] = $conn;
767 wfDebug( __METHOD__ . ": opened new connection for $i/$wiki\n" );
768 }
769 }
770
771 // Increment reference count
772 if ( $conn ) {
773 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
774 $conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
775 }
776
777 return $conn;
778 }
779
780 /**
781 * Test if the specified index represents an open connection
782 *
783 * @param int $index Server index
784 * @access private
785 * @return bool
786 */
787 private function isOpen( $index ) {
788 if ( !is_integer( $index ) ) {
789 return false;
790 }
791
792 return (bool)$this->getAnyOpenConnection( $index );
793 }
794
795 /**
796 * Really opens a connection. Uncached.
797 * Returns a Database object whether or not the connection was successful.
798 * @access private
799 *
800 * @param array $server
801 * @param bool $dbNameOverride
802 * @throws MWException
803 * @return DatabaseBase
804 */
805 protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
806 if ( !is_array( $server ) ) {
807 throw new MWException( 'You must update your load-balancing configuration. ' .
808 'See DefaultSettings.php entry for $wgDBservers.' );
809 }
810
811 if ( $dbNameOverride !== false ) {
812 $server['dbname'] = $dbNameOverride;
813 }
814
815 // Let the handle know what the cluster master is (e.g. "db1052")
816 $masterName = $this->getServerName( 0 );
817 $server['clusterMasterHost'] = $masterName;
818
819 // Log when many connection are made on requests
820 if ( ++$this->connsOpened >= self::CONN_HELD_WARN_THRESHOLD ) {
821 wfDebugLog( 'DBPerformance', __METHOD__ . ": " .
822 "{$this->connsOpened}+ connections made (master=$masterName)\n" .
823 wfBacktrace( true ) );
824 }
825
826 # Create object
827 try {
828 $db = DatabaseBase::factory( $server['type'], $server );
829 } catch ( DBConnectionError $e ) {
830 // FIXME: This is probably the ugliest thing I have ever done to
831 // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
832 $db = $e->db;
833 }
834
835 $db->setLBInfo( $server );
836 $db->setLazyMasterHandle(
837 $this->getLazyConnectionRef( DB_MASTER, [], $db->getWikiID() )
838 );
839 $db->setTransactionProfiler( $this->trxProfiler );
840
841 return $db;
842 }
843
844 /**
845 * @throws DBConnectionError
846 * @return bool
847 */
848 private function reportConnectionError() {
849 $conn = $this->mErrorConnection; // The connection which caused the error
850 $context = [
851 'method' => __METHOD__,
852 'last_error' => $this->mLastError,
853 ];
854
855 if ( !is_object( $conn ) ) {
856 // No last connection, probably due to all servers being too busy
857 wfLogDBError(
858 "LB failure with no last connection. Connection error: {last_error}",
859 $context
860 );
861
862 // If all servers were busy, mLastError will contain something sensible
863 throw new DBConnectionError( null, $this->mLastError );
864 } else {
865 $context['db_server'] = $conn->getProperty( 'mServer' );
866 wfLogDBError(
867 "Connection error: {last_error} ({db_server})",
868 $context
869 );
870
871 // throws DBConnectionError
872 $conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" );
873 }
874
875 return false; /* not reached */
876 }
877
878 /**
879 * @return int
880 * @since 1.26
881 */
882 public function getWriterIndex() {
883 return 0;
884 }
885
886 /**
887 * Returns true if the specified index is a valid server index
888 *
889 * @param string $i
890 * @return bool
891 */
892 public function haveIndex( $i ) {
893 return array_key_exists( $i, $this->mServers );
894 }
895
896 /**
897 * Returns true if the specified index is valid and has non-zero load
898 *
899 * @param string $i
900 * @return bool
901 */
902 public function isNonZeroLoad( $i ) {
903 return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
904 }
905
906 /**
907 * Get the number of defined servers (not the number of open connections)
908 *
909 * @return int
910 */
911 public function getServerCount() {
912 return count( $this->mServers );
913 }
914
915 /**
916 * Get the host name or IP address of the server with the specified index
917 * Prefer a readable name if available.
918 * @param string $i
919 * @return string
920 */
921 public function getServerName( $i ) {
922 if ( isset( $this->mServers[$i]['hostName'] ) ) {
923 $name = $this->mServers[$i]['hostName'];
924 } elseif ( isset( $this->mServers[$i]['host'] ) ) {
925 $name = $this->mServers[$i]['host'];
926 } else {
927 $name = '';
928 }
929
930 return ( $name != '' ) ? $name : 'localhost';
931 }
932
933 /**
934 * Return the server info structure for a given index, or false if the index is invalid.
935 * @param int $i
936 * @return array|bool
937 */
938 public function getServerInfo( $i ) {
939 if ( isset( $this->mServers[$i] ) ) {
940 return $this->mServers[$i];
941 } else {
942 return false;
943 }
944 }
945
946 /**
947 * Sets the server info structure for the given index. Entry at index $i
948 * is created if it doesn't exist
949 * @param int $i
950 * @param array $serverInfo
951 */
952 public function setServerInfo( $i, array $serverInfo ) {
953 $this->mServers[$i] = $serverInfo;
954 }
955
956 /**
957 * Get the current master position for chronology control purposes
958 * @return mixed
959 */
960 public function getMasterPos() {
961 # If this entire request was served from a slave without opening a connection to the
962 # master (however unlikely that may be), then we can fetch the position from the slave.
963 $masterConn = $this->getAnyOpenConnection( 0 );
964 if ( !$masterConn ) {
965 $serverCount = count( $this->mServers );
966 for ( $i = 1; $i < $serverCount; $i++ ) {
967 $conn = $this->getAnyOpenConnection( $i );
968 if ( $conn ) {
969 return $conn->getSlavePos();
970 }
971 }
972 } else {
973 return $masterConn->getMasterPos();
974 }
975
976 return false;
977 }
978
979 /**
980 * Close all open connections
981 */
982 public function closeAll() {
983 foreach ( $this->mConns as $conns2 ) {
984 foreach ( $conns2 as $conns3 ) {
985 /** @var DatabaseBase $conn */
986 foreach ( $conns3 as $conn ) {
987 $conn->close();
988 }
989 }
990 }
991 $this->mConns = [
992 'local' => [],
993 'foreignFree' => [],
994 'foreignUsed' => [],
995 ];
996 $this->connsOpened = 0;
997 }
998
999 /**
1000 * Close a connection
1001 * Using this function makes sure the LoadBalancer knows the connection is closed.
1002 * If you use $conn->close() directly, the load balancer won't update its state.
1003 * @param DatabaseBase $conn
1004 */
1005 public function closeConnection( $conn ) {
1006 $done = false;
1007 foreach ( $this->mConns as $i1 => $conns2 ) {
1008 foreach ( $conns2 as $i2 => $conns3 ) {
1009 foreach ( $conns3 as $i3 => $candidateConn ) {
1010 if ( $conn === $candidateConn ) {
1011 $conn->close();
1012 unset( $this->mConns[$i1][$i2][$i3] );
1013 --$this->connsOpened;
1014 $done = true;
1015 break;
1016 }
1017 }
1018 }
1019 }
1020 if ( !$done ) {
1021 $conn->close();
1022 }
1023 }
1024
1025 /**
1026 * Commit transactions on all open connections
1027 * @param string $fname Caller name
1028 */
1029 public function commitAll( $fname = __METHOD__ ) {
1030 foreach ( $this->mConns as $conns2 ) {
1031 foreach ( $conns2 as $conns3 ) {
1032 /** @var DatabaseBase[] $conns3 */
1033 foreach ( $conns3 as $conn ) {
1034 if ( $conn->trxLevel() ) {
1035 $conn->commit( $fname, 'flush' );
1036 }
1037 }
1038 }
1039 }
1040 }
1041
1042 /**
1043 * Issue COMMIT only on master, only if queries were done on connection
1044 * @param string $fname Caller name
1045 */
1046 public function commitMasterChanges( $fname = __METHOD__ ) {
1047 $masterIndex = $this->getWriterIndex();
1048 foreach ( $this->mConns as $conns2 ) {
1049 if ( empty( $conns2[$masterIndex] ) ) {
1050 continue;
1051 }
1052 /** @var DatabaseBase $conn */
1053 foreach ( $conns2[$masterIndex] as $conn ) {
1054 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1055 $conn->commit( $fname, 'flush' );
1056 }
1057 }
1058 }
1059 }
1060
1061 /**
1062 * Issue ROLLBACK only on master, only if queries were done on connection
1063 * @param string $fname Caller name
1064 * @throws DBExpectedError
1065 * @since 1.23
1066 */
1067 public function rollbackMasterChanges( $fname = __METHOD__ ) {
1068 $failedServers = [];
1069
1070 $masterIndex = $this->getWriterIndex();
1071 foreach ( $this->mConns as $conns2 ) {
1072 if ( empty( $conns2[$masterIndex] ) ) {
1073 continue;
1074 }
1075 /** @var DatabaseBase $conn */
1076 foreach ( $conns2[$masterIndex] as $conn ) {
1077 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1078 try {
1079 $conn->rollback( $fname, 'flush' );
1080 } catch ( DBError $e ) {
1081 MWExceptionHandler::logException( $e );
1082 $failedServers[] = $conn->getServer();
1083 }
1084 }
1085 }
1086 }
1087
1088 if ( $failedServers ) {
1089 throw new DBExpectedError( null, "Rollback failed on server(s) " .
1090 implode( ', ', array_unique( $failedServers ) ) );
1091 }
1092 }
1093
1094 /**
1095 * @return bool Whether a master connection is already open
1096 * @since 1.24
1097 */
1098 public function hasMasterConnection() {
1099 return $this->isOpen( $this->getWriterIndex() );
1100 }
1101
1102 /**
1103 * Determine if there are pending changes in a transaction by this thread
1104 * @since 1.23
1105 * @return bool
1106 */
1107 public function hasMasterChanges() {
1108 $masterIndex = $this->getWriterIndex();
1109 foreach ( $this->mConns as $conns2 ) {
1110 if ( empty( $conns2[$masterIndex] ) ) {
1111 continue;
1112 }
1113 /** @var DatabaseBase $conn */
1114 foreach ( $conns2[$masterIndex] as $conn ) {
1115 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1116 return true;
1117 }
1118 }
1119 }
1120 return false;
1121 }
1122
1123 /**
1124 * Get the timestamp of the latest write query done by this thread
1125 * @since 1.25
1126 * @return float|bool UNIX timestamp or false
1127 */
1128 public function lastMasterChangeTimestamp() {
1129 $lastTime = false;
1130 $masterIndex = $this->getWriterIndex();
1131 foreach ( $this->mConns as $conns2 ) {
1132 if ( empty( $conns2[$masterIndex] ) ) {
1133 continue;
1134 }
1135 /** @var DatabaseBase $conn */
1136 foreach ( $conns2[$masterIndex] as $conn ) {
1137 $lastTime = max( $lastTime, $conn->lastDoneWrites() );
1138 }
1139 }
1140 return $lastTime;
1141 }
1142
1143 /**
1144 * Check if this load balancer object had any recent or still
1145 * pending writes issued against it by this PHP thread
1146 *
1147 * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
1148 * @return bool
1149 * @since 1.25
1150 */
1151 public function hasOrMadeRecentMasterChanges( $age = null ) {
1152 $age = ( $age === null ) ? $this->mWaitTimeout : $age;
1153
1154 return ( $this->hasMasterChanges()
1155 || $this->lastMasterChangeTimestamp() > microtime( true ) - $age );
1156 }
1157
1158 /**
1159 * Get the list of callers that have pending master changes
1160 *
1161 * @return array
1162 * @since 1.27
1163 */
1164 public function pendingMasterChangeCallers() {
1165 $fnames = [];
1166
1167 $masterIndex = $this->getWriterIndex();
1168 foreach ( $this->mConns as $conns2 ) {
1169 if ( empty( $conns2[$masterIndex] ) ) {
1170 continue;
1171 }
1172 /** @var DatabaseBase $conn */
1173 foreach ( $conns2[$masterIndex] as $conn ) {
1174 $fnames = array_merge( $fnames, $conn->pendingWriteCallers() );
1175 }
1176 }
1177
1178 return $fnames;
1179 }
1180
1181 /**
1182 * @param mixed $value
1183 * @return mixed
1184 */
1185 public function waitTimeout( $value = null ) {
1186 return wfSetVar( $this->mWaitTimeout, $value );
1187 }
1188
1189 /**
1190 * @note This method will trigger a DB connection if not yet done
1191 *
1192 * @param string|bool $wiki Wiki ID, or false for the current wiki
1193 * @return bool Whether the generic connection for reads is highly "lagged"
1194 */
1195 public function getLaggedSlaveMode( $wiki = false ) {
1196 // No-op if there is only one DB (also avoids recursion)
1197 if ( !$this->laggedSlaveMode && $this->getServerCount() > 1 ) {
1198 try {
1199 // See if laggedSlaveMode gets set
1200 $conn = $this->getConnection( DB_SLAVE, false, $wiki );
1201 $this->reuseConnection( $conn );
1202 } catch ( DBConnectionError $e ) {
1203 // Avoid expensive re-connect attempts and failures
1204 $this->slavesDownMode = true;
1205 $this->laggedSlaveMode = true;
1206 }
1207 }
1208
1209 return $this->laggedSlaveMode;
1210 }
1211
1212 /**
1213 * @note This method will never cause a new DB connection
1214 * @return bool Whether any generic connection used for reads was highly "lagged"
1215 * @since 1.27
1216 */
1217 public function laggedSlaveUsed() {
1218 return $this->laggedSlaveMode;
1219 }
1220
1221 /**
1222 * @note This method may trigger a DB connection if not yet done
1223 * @param string|bool $wiki Wiki ID, or false for the current wiki
1224 * @return string|bool Reason the master is read-only or false if it is not
1225 * @since 1.27
1226 */
1227 public function getReadOnlyReason( $wiki = false ) {
1228 if ( $this->readOnlyReason !== false ) {
1229 return $this->readOnlyReason;
1230 } elseif ( $this->getLaggedSlaveMode( $wiki ) ) {
1231 if ( $this->slavesDownMode ) {
1232 return 'The database has been automatically locked ' .
1233 'until the slave database servers become available';
1234 } else {
1235 return 'The database has been automatically locked ' .
1236 'while the slave database servers catch up to the master.';
1237 }
1238 }
1239
1240 return false;
1241 }
1242
1243 /**
1244 * Disables/enables lag checks
1245 * @param null|bool $mode
1246 * @return bool
1247 */
1248 public function allowLagged( $mode = null ) {
1249 if ( $mode === null ) {
1250 return $this->mAllowLagged;
1251 }
1252 $this->mAllowLagged = $mode;
1253
1254 return $this->mAllowLagged;
1255 }
1256
1257 /**
1258 * @return bool
1259 */
1260 public function pingAll() {
1261 $success = true;
1262 foreach ( $this->mConns as $conns2 ) {
1263 foreach ( $conns2 as $conns3 ) {
1264 /** @var DatabaseBase[] $conns3 */
1265 foreach ( $conns3 as $conn ) {
1266 if ( !$conn->ping() ) {
1267 $success = false;
1268 }
1269 }
1270 }
1271 }
1272
1273 return $success;
1274 }
1275
1276 /**
1277 * Call a function with each open connection object
1278 * @param callable $callback
1279 * @param array $params
1280 */
1281 public function forEachOpenConnection( $callback, array $params = [] ) {
1282 foreach ( $this->mConns as $conns2 ) {
1283 foreach ( $conns2 as $conns3 ) {
1284 foreach ( $conns3 as $conn ) {
1285 $mergedParams = array_merge( [ $conn ], $params );
1286 call_user_func_array( $callback, $mergedParams );
1287 }
1288 }
1289 }
1290 }
1291
1292 /**
1293 * Get the hostname and lag time of the most-lagged slave
1294 *
1295 * This is useful for maintenance scripts that need to throttle their updates.
1296 * May attempt to open connections to slaves on the default DB. If there is
1297 * no lag, the maximum lag will be reported as -1.
1298 *
1299 * @param bool|string $wiki Wiki ID, or false for the default database
1300 * @return array ( host, max lag, index of max lagged host )
1301 */
1302 public function getMaxLag( $wiki = false ) {
1303 $maxLag = -1;
1304 $host = '';
1305 $maxIndex = 0;
1306
1307 if ( $this->getServerCount() <= 1 ) {
1308 return [ $host, $maxLag, $maxIndex ]; // no replication = no lag
1309 }
1310
1311 $lagTimes = $this->getLagTimes( $wiki );
1312 foreach ( $lagTimes as $i => $lag ) {
1313 if ( $lag > $maxLag ) {
1314 $maxLag = $lag;
1315 $host = $this->mServers[$i]['host'];
1316 $maxIndex = $i;
1317 }
1318 }
1319
1320 return [ $host, $maxLag, $maxIndex ];
1321 }
1322
1323 /**
1324 * Get an estimate of replication lag (in seconds) for each server
1325 *
1326 * Results are cached for a short time in memcached/process cache
1327 *
1328 * Values may be "false" if replication is too broken to estimate
1329 *
1330 * @param string|bool $wiki
1331 * @return int[] Map of (server index => float|int|bool)
1332 */
1333 public function getLagTimes( $wiki = false ) {
1334 if ( $this->getServerCount() <= 1 ) {
1335 return [ 0 => 0 ]; // no replication = no lag
1336 }
1337
1338 # Send the request to the load monitor
1339 return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki );
1340 }
1341
1342 /**
1343 * Get the lag in seconds for a given connection, or zero if this load
1344 * balancer does not have replication enabled.
1345 *
1346 * This should be used in preference to Database::getLag() in cases where
1347 * replication may not be in use, since there is no way to determine if
1348 * replication is in use at the connection level without running
1349 * potentially restricted queries such as SHOW SLAVE STATUS. Using this
1350 * function instead of Database::getLag() avoids a fatal error in this
1351 * case on many installations.
1352 *
1353 * @param IDatabase $conn
1354 * @return int|bool Returns false on error
1355 */
1356 public function safeGetLag( IDatabase $conn ) {
1357 if ( $this->getServerCount() == 1 ) {
1358 return 0;
1359 } else {
1360 return $conn->getLag();
1361 }
1362 }
1363
1364 /**
1365 * Wait for a slave DB to reach a specified master position
1366 *
1367 * This will connect to the master to get an accurate position if $pos is not given
1368 *
1369 * @param IDatabase $conn Slave DB
1370 * @param DBMasterPos|bool $pos Master position; default: current position
1371 * @param integer $timeout Timeout in seconds
1372 * @return bool Success
1373 * @since 1.27
1374 */
1375 public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) {
1376 if ( $this->getServerCount() == 1 || !$conn->getLBInfo( 'slave' ) ) {
1377 return true; // server is not a slave DB
1378 }
1379
1380 $pos = $pos ?: $this->getConnection( DB_MASTER )->getMasterPos();
1381 if ( !$pos ) {
1382 return false; // something is misconfigured
1383 }
1384
1385 $result = $conn->masterPosWait( $pos, $timeout );
1386 if ( $result == -1 || is_null( $result ) ) {
1387 $msg = __METHOD__ . ": Timed out waiting on {$conn->getServer()} pos {$pos}";
1388 wfDebugLog( 'replication', "$msg\n" );
1389 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
1390 $ok = false;
1391 } else {
1392 wfDebugLog( 'replication', __METHOD__ . ": Done\n" );
1393 $ok = true;
1394 }
1395
1396 return $ok;
1397 }
1398
1399 /**
1400 * Clear the cache for slag lag delay times
1401 *
1402 * This is only used for testing
1403 */
1404 public function clearLagTimeCache() {
1405 $this->getLoadMonitor()->clearCaches();
1406 }
1407 }