Change $wiki => $domain in LoadBalancer
[lhc/web/wiklou.git] / includes / db / loadbalancer / LoadBalancer.php
1 <?php
2 /**
3 * Database load balancing manager
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 use Psr\Log\LoggerInterface;
24
25 /**
26 * Database load balancing, tracking, and transaction management object
27 *
28 * @ingroup Database
29 */
30 class LoadBalancer implements ILoadBalancer {
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 replica DB lag as a factor in replica DB selection */
40 private $mAllowLagged;
41 /** @var integer Seconds to spend waiting on replica DB lag to resolve */
42 private $mWaitTimeout;
43 /** @var string The LoadMonitor subclass name */
44 private $mLoadMonitorClass;
45
46 /** @var LoadMonitor */
47 private $mLoadMonitor;
48 /** @var BagOStuff */
49 private $srvCache;
50 /** @var BagOStuff */
51 private $memCache;
52 /** @var WANObjectCache */
53 private $wanCache;
54 /** @var TransactionProfiler */
55 protected $trxProfiler;
56 /** @var LoggerInterface */
57 protected $replLogger;
58 /** @var LoggerInterface */
59 protected $connLogger;
60 /** @var LoggerInterface */
61 protected $queryLogger;
62 /** @var LoggerInterface */
63 protected $perfLogger;
64
65 /** @var bool|DatabaseBase Database connection that caused a problem */
66 private $mErrorConnection;
67 /** @var integer The generic (not query grouped) replica DB index (of $mServers) */
68 private $mReadIndex;
69 /** @var bool|DBMasterPos False if not set */
70 private $mWaitForPos;
71 /** @var bool Whether the generic reader fell back to a lagged replica DB */
72 private $laggedReplicaMode = false;
73 /** @var bool Whether the generic reader fell back to a lagged replica DB */
74 private $allReplicasDownMode = false;
75 /** @var string The last DB selection or connection error */
76 private $mLastError = 'Unknown error';
77 /** @var string|bool Reason the LB is read-only or false if not */
78 private $readOnlyReason = false;
79 /** @var integer Total connections opened */
80 private $connsOpened = 0;
81 /** @var string|bool String if a requested DBO_TRX transaction round is active */
82 private $trxRoundId = false;
83 /** @var array[] Map of (name => callable) */
84 private $trxRecurringCallbacks = [];
85 /** @var string Local Domain ID and default for selectDB() calls */
86 private $localDomain;
87 /** @var string Current server name */
88 private $host;
89
90 /** @var callable Exception logger */
91 private $errorLogger;
92
93 /** @var boolean */
94 private $disabled = false;
95
96 /** @var integer Warn when this many connection are held */
97 const CONN_HELD_WARN_THRESHOLD = 10;
98 /** @var integer Default 'max lag' when unspecified */
99 const MAX_LAG_DEFAULT = 10;
100 /** @var integer Max time to wait for a replica DB to catch up (e.g. ChronologyProtector) */
101 const POS_WAIT_TIMEOUT = 10;
102 /** @var integer Seconds to cache master server read-only status */
103 const TTL_CACHE_READONLY = 5;
104
105 public function __construct( array $params ) {
106 if ( !isset( $params['servers'] ) ) {
107 throw new InvalidArgumentException( __CLASS__ . ': missing servers parameter' );
108 }
109 $this->mServers = $params['servers'];
110 $this->mWaitTimeout = isset( $params['waitTimeout'] )
111 ? $params['waitTimeout']
112 : self::POS_WAIT_TIMEOUT;
113 $this->localDomain = isset( $params['localDomain'] ) ? $params['localDomain'] : '';
114
115 $this->mReadIndex = -1;
116 $this->mConns = [
117 'local' => [],
118 'foreignUsed' => [],
119 'foreignFree' => [] ];
120 $this->mLoads = [];
121 $this->mWaitForPos = false;
122 $this->mErrorConnection = false;
123 $this->mAllowLagged = false;
124
125 if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) {
126 $this->readOnlyReason = $params['readOnlyReason'];
127 }
128
129 if ( isset( $params['loadMonitor'] ) ) {
130 $this->mLoadMonitorClass = $params['loadMonitor'];
131 } else {
132 $master = reset( $params['servers'] );
133 if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
134 $this->mLoadMonitorClass = 'LoadMonitorMySQL';
135 } else {
136 $this->mLoadMonitorClass = 'LoadMonitorNull';
137 }
138 }
139
140 foreach ( $params['servers'] as $i => $server ) {
141 $this->mLoads[$i] = $server['load'];
142 if ( isset( $server['groupLoads'] ) ) {
143 foreach ( $server['groupLoads'] as $group => $ratio ) {
144 if ( !isset( $this->mGroupLoads[$group] ) ) {
145 $this->mGroupLoads[$group] = [];
146 }
147 $this->mGroupLoads[$group][$i] = $ratio;
148 }
149 }
150 }
151
152 if ( isset( $params['srvCache'] ) ) {
153 $this->srvCache = $params['srvCache'];
154 } else {
155 $this->srvCache = new EmptyBagOStuff();
156 }
157 if ( isset( $params['memCache'] ) ) {
158 $this->memCache = $params['memCache'];
159 } else {
160 $this->memCache = new EmptyBagOStuff();
161 }
162 if ( isset( $params['wanCache'] ) ) {
163 $this->wanCache = $params['wanCache'];
164 } else {
165 $this->wanCache = WANObjectCache::newEmpty();
166 }
167 if ( isset( $params['trxProfiler'] ) ) {
168 $this->trxProfiler = $params['trxProfiler'];
169 } else {
170 $this->trxProfiler = new TransactionProfiler();
171 }
172
173 $this->errorLogger = isset( $params['errorLogger'] )
174 ? $params['errorLogger']
175 : function ( Exception $e ) {
176 trigger_error( E_WARNING, $e->getMessage() );
177 };
178
179 foreach ( [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ] as $key ) {
180 $this->$key = isset( $params[$key] ) ? $params[$key] : new \Psr\Log\NullLogger();
181 }
182
183 $this->host = isset( $params['hostname'] )
184 ? $params['hostname']
185 : ( gethostname() ?: 'unknown' );
186 }
187
188 /**
189 * Get a LoadMonitor instance
190 *
191 * @return LoadMonitor
192 */
193 private function getLoadMonitor() {
194 if ( !isset( $this->mLoadMonitor ) ) {
195 $class = $this->mLoadMonitorClass;
196 $this->mLoadMonitor = new $class( $this, $this->srvCache, $this->memCache );
197 $this->mLoadMonitor->setLogger( $this->replLogger );
198 }
199
200 return $this->mLoadMonitor;
201 }
202
203 /**
204 * @param array $loads
205 * @param bool|string $domain Domain to get non-lagged for
206 * @param int $maxLag Restrict the maximum allowed lag to this many seconds
207 * @return bool|int|string
208 */
209 private function getRandomNonLagged( array $loads, $domain = false, $maxLag = INF ) {
210 $lags = $this->getLagTimes( $domain );
211
212 # Unset excessively lagged servers
213 foreach ( $lags as $i => $lag ) {
214 if ( $i != 0 ) {
215 # How much lag this server nominally is allowed to have
216 $maxServerLag = isset( $this->mServers[$i]['max lag'] )
217 ? $this->mServers[$i]['max lag']
218 : self::MAX_LAG_DEFAULT; // default
219 # Constrain that futher by $maxLag argument
220 $maxServerLag = min( $maxServerLag, $maxLag );
221
222 $host = $this->getServerName( $i );
223 if ( $lag === false && !is_infinite( $maxServerLag ) ) {
224 $this->replLogger->error( "Server $host (#$i) is not replicating?" );
225 unset( $loads[$i] );
226 } elseif ( $lag > $maxServerLag ) {
227 $this->replLogger->warning( "Server $host (#$i) has >= $lag seconds of lag" );
228 unset( $loads[$i] );
229 }
230 }
231 }
232
233 # Find out if all the replica DBs with non-zero load are lagged
234 $sum = 0;
235 foreach ( $loads as $load ) {
236 $sum += $load;
237 }
238 if ( $sum == 0 ) {
239 # No appropriate DB servers except maybe the master and some replica DBs with zero load
240 # Do NOT use the master
241 # Instead, this function will return false, triggering read-only mode,
242 # and a lagged replica DB will be used instead.
243 return false;
244 }
245
246 if ( count( $loads ) == 0 ) {
247 return false;
248 }
249
250 # Return a random representative of the remainder
251 return ArrayUtils::pickRandom( $loads );
252 }
253
254 public function getReaderIndex( $group = false, $domain = false ) {
255 if ( count( $this->mServers ) == 1 ) {
256 # Skip the load balancing if there's only one server
257 return $this->getWriterIndex();
258 } elseif ( $group === false && $this->mReadIndex >= 0 ) {
259 # Shortcut if generic reader exists already
260 return $this->mReadIndex;
261 }
262
263 # Find the relevant load array
264 if ( $group !== false ) {
265 if ( isset( $this->mGroupLoads[$group] ) ) {
266 $nonErrorLoads = $this->mGroupLoads[$group];
267 } else {
268 # No loads for this group, return false and the caller can use some other group
269 $this->connLogger->info( __METHOD__ . ": no loads for group $group" );
270
271 return false;
272 }
273 } else {
274 $nonErrorLoads = $this->mLoads;
275 }
276
277 if ( !count( $nonErrorLoads ) ) {
278 throw new InvalidArgumentException( "Empty server array given to LoadBalancer" );
279 }
280
281 # Scale the configured load ratios according to the dynamic load if supported
282 $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $domain );
283
284 $laggedReplicaMode = false;
285
286 # No server found yet
287 $i = false;
288 # First try quickly looking through the available servers for a server that
289 # meets our criteria
290 $currentLoads = $nonErrorLoads;
291 while ( count( $currentLoads ) ) {
292 if ( $this->mAllowLagged || $laggedReplicaMode ) {
293 $i = ArrayUtils::pickRandom( $currentLoads );
294 } else {
295 $i = false;
296 if ( $this->mWaitForPos && $this->mWaitForPos->asOfTime() ) {
297 # ChronologyProtecter causes mWaitForPos to be set via sessions.
298 # This triggers doWait() after connect, so it's especially good to
299 # avoid lagged servers so as to avoid just blocking in that method.
300 $ago = microtime( true ) - $this->mWaitForPos->asOfTime();
301 # Aim for <= 1 second of waiting (being too picky can backfire)
302 $i = $this->getRandomNonLagged( $currentLoads, $domain, $ago + 1 );
303 }
304 if ( $i === false ) {
305 # Any server with less lag than it's 'max lag' param is preferable
306 $i = $this->getRandomNonLagged( $currentLoads, $domain );
307 }
308 if ( $i === false && count( $currentLoads ) != 0 ) {
309 # All replica DBs lagged. Switch to read-only mode
310 $this->replLogger->error( "All replica DBs lagged. Switch to read-only mode" );
311 $i = ArrayUtils::pickRandom( $currentLoads );
312 $laggedReplicaMode = true;
313 }
314 }
315
316 if ( $i === false ) {
317 # pickRandom() returned false
318 # This is permanent and means the configuration or the load monitor
319 # wants us to return false.
320 $this->connLogger->debug( __METHOD__ . ": pickRandom() returned false" );
321
322 return false;
323 }
324
325 $serverName = $this->getServerName( $i );
326 $this->connLogger->debug( __METHOD__ . ": Using reader #$i: $serverName..." );
327
328 $conn = $this->openConnection( $i, $domain );
329 if ( !$conn ) {
330 $this->connLogger->warning( __METHOD__ . ": Failed connecting to $i/$domain" );
331 unset( $nonErrorLoads[$i] );
332 unset( $currentLoads[$i] );
333 $i = false;
334 continue;
335 }
336
337 // Decrement reference counter, we are finished with this connection.
338 // It will be incremented for the caller later.
339 if ( $domain !== false ) {
340 $this->reuseConnection( $conn );
341 }
342
343 # Return this server
344 break;
345 }
346
347 # If all servers were down, quit now
348 if ( !count( $nonErrorLoads ) ) {
349 $this->connLogger->error( "All servers down" );
350 }
351
352 if ( $i !== false ) {
353 # Replica DB connection successful.
354 # Wait for the session master pos for a short time.
355 if ( $this->mWaitForPos && $i > 0 ) {
356 $this->doWait( $i );
357 }
358 if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) {
359 $this->mReadIndex = $i;
360 # Record if the generic reader index is in "lagged replica DB" mode
361 if ( $laggedReplicaMode ) {
362 $this->laggedReplicaMode = true;
363 }
364 }
365 $serverName = $this->getServerName( $i );
366 $this->connLogger->debug(
367 __METHOD__ . ": using server $serverName for group '$group'" );
368 }
369
370 return $i;
371 }
372
373 public function waitFor( $pos ) {
374 $this->mWaitForPos = $pos;
375 $i = $this->mReadIndex;
376
377 if ( $i > 0 ) {
378 if ( !$this->doWait( $i ) ) {
379 $this->laggedReplicaMode = true;
380 }
381 }
382 }
383
384 /**
385 * Set the master wait position and wait for a "generic" replica DB to catch up to it
386 *
387 * This can be used a faster proxy for waitForAll()
388 *
389 * @param DBMasterPos $pos
390 * @param int $timeout Max seconds to wait; default is mWaitTimeout
391 * @return bool Success (able to connect and no timeouts reached)
392 * @since 1.26
393 */
394 public function waitForOne( $pos, $timeout = null ) {
395 $this->mWaitForPos = $pos;
396
397 $i = $this->mReadIndex;
398 if ( $i <= 0 ) {
399 // Pick a generic replica DB if there isn't one yet
400 $readLoads = $this->mLoads;
401 unset( $readLoads[$this->getWriterIndex()] ); // replica DBs only
402 $readLoads = array_filter( $readLoads ); // with non-zero load
403 $i = ArrayUtils::pickRandom( $readLoads );
404 }
405
406 if ( $i > 0 ) {
407 $ok = $this->doWait( $i, true, $timeout );
408 } else {
409 $ok = true; // no applicable loads
410 }
411
412 return $ok;
413 }
414
415 public function waitForAll( $pos, $timeout = null ) {
416 $this->mWaitForPos = $pos;
417 $serverCount = count( $this->mServers );
418
419 $ok = true;
420 for ( $i = 1; $i < $serverCount; $i++ ) {
421 if ( $this->mLoads[$i] > 0 ) {
422 $ok = $this->doWait( $i, true, $timeout ) && $ok;
423 }
424 }
425
426 return $ok;
427 }
428
429 public function getAnyOpenConnection( $i ) {
430 foreach ( $this->mConns as $connsByServer ) {
431 if ( !empty( $connsByServer[$i] ) ) {
432 return reset( $connsByServer[$i] );
433 }
434 }
435
436 return false;
437 }
438
439 /**
440 * Wait for a given replica DB 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 $this->replLogger->debug( __METHOD__ .
456 ": replica DB $server known to be caught up (pos >= $knownReachedPos)." );
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 $this->replLogger->debug( __METHOD__ . ": no connection open for $server" );
465
466 return false;
467 } else {
468 $conn = $this->openConnection( $index, '' );
469 if ( !$conn ) {
470 $this->replLogger->warning( __METHOD__ . ": failed to connect to $server" );
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 $this->replLogger->info( __METHOD__ . ": Waiting for replica DB $server to catch up..." );
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 replica DB, use master instead
486 $msg = __METHOD__ . ": Timed out waiting on $server pos {$this->mWaitForPos}";
487 $this->replLogger->warning( "$msg" );
488 $ok = false;
489 } else {
490 $this->replLogger->info( __METHOD__ . ": Done" );
491 $ok = true;
492 // Remember that the DB reached this point
493 $this->srvCache->set( $key, $this->mWaitForPos, BagOStuff::TTL_DAY );
494 }
495
496 if ( $close ) {
497 $this->closeConnection( $conn );
498 }
499
500 return $ok;
501 }
502
503 public function getConnection( $i, $groups = [], $domain = false ) {
504 if ( $i === null || $i === false ) {
505 throw new InvalidArgumentException( 'Attempt to call ' . __METHOD__ .
506 ' with invalid server index' );
507 }
508
509 if ( $domain === $this->localDomain ) {
510 $domain = false;
511 }
512
513 $groups = ( $groups === false || $groups === [] )
514 ? [ false ] // check one "group": the generic pool
515 : (array)$groups;
516
517 $masterOnly = ( $i == DB_MASTER || $i == $this->getWriterIndex() );
518 $oldConnsOpened = $this->connsOpened; // connections open now
519
520 if ( $i == DB_MASTER ) {
521 $i = $this->getWriterIndex();
522 } else {
523 # Try to find an available server in any the query groups (in order)
524 foreach ( $groups as $group ) {
525 $groupIndex = $this->getReaderIndex( $group, $domain );
526 if ( $groupIndex !== false ) {
527 $i = $groupIndex;
528 break;
529 }
530 }
531 }
532
533 # Operation-based index
534 if ( $i == DB_REPLICA ) {
535 $this->mLastError = 'Unknown error'; // reset error string
536 # Try the general server pool if $groups are unavailable.
537 $i = in_array( false, $groups, true )
538 ? false // don't bother with this if that is what was tried above
539 : $this->getReaderIndex( false, $domain );
540 # Couldn't find a working server in getReaderIndex()?
541 if ( $i === false ) {
542 $this->mLastError = 'No working replica DB server: ' . $this->mLastError;
543
544 return $this->reportConnectionError();
545 }
546 }
547
548 # Now we have an explicit index into the servers array
549 $conn = $this->openConnection( $i, $domain );
550 if ( !$conn ) {
551 return $this->reportConnectionError();
552 }
553
554 # Profile any new connections that happen
555 if ( $this->connsOpened > $oldConnsOpened ) {
556 $host = $conn->getServer();
557 $dbname = $conn->getDBname();
558 $this->trxProfiler->recordConnection( $host, $dbname, $masterOnly );
559 }
560
561 if ( $masterOnly ) {
562 # Make master-requested DB handles inherit any read-only mode setting
563 $conn->setLBInfo( 'readOnlyReason', $this->getReadOnlyReason( $domain, $conn ) );
564 }
565
566 return $conn;
567 }
568
569 public function reuseConnection( $conn ) {
570 $serverIndex = $conn->getLBInfo( 'serverIndex' );
571 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
572 if ( $serverIndex === null || $refCount === null ) {
573 /**
574 * This can happen in code like:
575 * foreach ( $dbs as $db ) {
576 * $conn = $lb->getConnection( DB_REPLICA, [], $db );
577 * ...
578 * $lb->reuseConnection( $conn );
579 * }
580 * When a connection to the local DB is opened in this way, reuseConnection()
581 * should be ignored
582 */
583 return;
584 }
585
586 $dbName = $conn->getDBname();
587 $prefix = $conn->tablePrefix();
588 if ( strval( $prefix ) !== '' ) {
589 $domain = "$dbName-$prefix";
590 } else {
591 $domain = $dbName;
592 }
593 if ( $this->mConns['foreignUsed'][$serverIndex][$domain] !== $conn ) {
594 throw new InvalidArgumentException( __METHOD__ . ": connection not found, has " .
595 "the connection been freed already?" );
596 }
597 $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
598 if ( $refCount <= 0 ) {
599 $this->mConns['foreignFree'][$serverIndex][$domain] = $conn;
600 unset( $this->mConns['foreignUsed'][$serverIndex][$domain] );
601 $this->connLogger->debug( __METHOD__ . ": freed connection $serverIndex/$domain" );
602 } else {
603 $this->connLogger->debug( __METHOD__ .
604 ": reference count for $serverIndex/$domain reduced to $refCount" );
605 }
606 }
607
608 /**
609 * Get a database connection handle reference
610 *
611 * The handle's methods wrap simply wrap those of a DatabaseBase handle
612 *
613 * @see LoadBalancer::getConnection() for parameter information
614 *
615 * @param int $db
616 * @param array|string|bool $groups Query group(s), or false for the generic reader
617 * @param string|bool $domain Domain ID, or false for the current domain
618 * @return DBConnRef
619 * @since 1.22
620 */
621 public function getConnectionRef( $db, $groups = [], $domain = false ) {
622 return new DBConnRef( $this, $this->getConnection( $db, $groups, $domain ) );
623 }
624
625 /**
626 * Get a database connection handle reference without connecting yet
627 *
628 * The handle's methods wrap simply wrap those of a DatabaseBase handle
629 *
630 * @see LoadBalancer::getConnection() for parameter information
631 *
632 * @param int $db
633 * @param array|string|bool $groups Query group(s), or false for the generic reader
634 * @param string|bool $domain Domain ID, or false for the current domain
635 * @return DBConnRef
636 * @since 1.22
637 */
638 public function getLazyConnectionRef( $db, $groups = [], $domain = false ) {
639 $domain = ( $domain !== false ) ? $domain : $this->localDomain;
640
641 return new DBConnRef( $this, [ $db, $groups, $domain ] );
642 }
643
644 public function openConnection( $i, $domain = false ) {
645 if ( $domain !== false ) {
646 $conn = $this->openForeignConnection( $i, $domain );
647 } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
648 $conn = $this->mConns['local'][$i][0];
649 } else {
650 $server = $this->mServers[$i];
651 $server['serverIndex'] = $i;
652 $conn = $this->reallyOpenConnection( $server, false );
653 $serverName = $this->getServerName( $i );
654 if ( $conn->isOpen() ) {
655 $this->connLogger->debug( "Connected to database $i at '$serverName'." );
656 $this->mConns['local'][$i][0] = $conn;
657 } else {
658 $this->connLogger->warning( "Failed to connect to database $i at '$serverName'." );
659 $this->mErrorConnection = $conn;
660 $conn = false;
661 }
662 }
663
664 if ( $conn && !$conn->isOpen() ) {
665 // Connection was made but later unrecoverably lost for some reason.
666 // Do not return a handle that will just throw exceptions on use,
667 // but let the calling code (e.g. getReaderIndex) try another server.
668 // See DatabaseMyslBase::ping() for how this can happen.
669 $this->mErrorConnection = $conn;
670 $conn = false;
671 }
672
673 return $conn;
674 }
675
676 /**
677 * Open a connection to a foreign DB, or return one if it is already open.
678 *
679 * Increments a reference count on the returned connection which locks the
680 * connection to the requested domain. This reference count can be
681 * decremented by calling reuseConnection().
682 *
683 * If a connection is open to the appropriate server already, but with the wrong
684 * database, it will be switched to the right database and returned, as long as
685 * it has been freed first with reuseConnection().
686 *
687 * On error, returns false, and the connection which caused the
688 * error will be available via $this->mErrorConnection.
689 *
690 * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError.
691 *
692 * @param int $i Server index
693 * @param string $domain Domain ID to open
694 * @return DatabaseBase
695 */
696 private function openForeignConnection( $i, $domain ) {
697 list( $dbName, $prefix ) = explode( '-', $domain, 2 ) + [ '', '' ];
698
699 if ( isset( $this->mConns['foreignUsed'][$i][$domain] ) ) {
700 // Reuse an already-used connection
701 $conn = $this->mConns['foreignUsed'][$i][$domain];
702 $this->connLogger->debug( __METHOD__ . ": reusing connection $i/$domain" );
703 } elseif ( isset( $this->mConns['foreignFree'][$i][$domain] ) ) {
704 // Reuse a free connection for the same domain
705 $conn = $this->mConns['foreignFree'][$i][$domain];
706 unset( $this->mConns['foreignFree'][$i][$domain] );
707 $this->mConns['foreignUsed'][$i][$domain] = $conn;
708 $this->connLogger->debug( __METHOD__ . ": reusing free connection $i/$domain" );
709 } elseif ( !empty( $this->mConns['foreignFree'][$i] ) ) {
710 // Reuse a connection from another domain
711 $conn = reset( $this->mConns['foreignFree'][$i] );
712 $oldDomain = key( $this->mConns['foreignFree'][$i] );
713
714 // The empty string as a DB name means "don't care".
715 // DatabaseMysqlBase::open() already handle this on connection.
716 if ( $dbName !== '' && !$conn->selectDB( $dbName ) ) {
717 $this->mLastError = "Error selecting database $dbName on server " .
718 $conn->getServer() . " from client host {$this->host}";
719 $this->mErrorConnection = $conn;
720 $conn = false;
721 } else {
722 $conn->tablePrefix( $prefix );
723 unset( $this->mConns['foreignFree'][$i][$oldDomain] );
724 $this->mConns['foreignUsed'][$i][$domain] = $conn;
725 $this->connLogger->debug( __METHOD__ .
726 ": reusing free connection from $oldDomain for $domain" );
727 }
728 } else {
729 // Open a new connection
730 $server = $this->mServers[$i];
731 $server['serverIndex'] = $i;
732 $server['foreignPoolRefCount'] = 0;
733 $server['foreign'] = true;
734 $conn = $this->reallyOpenConnection( $server, $dbName );
735 if ( !$conn->isOpen() ) {
736 $this->connLogger->warning( __METHOD__ . ": connection error for $i/$domain" );
737 $this->mErrorConnection = $conn;
738 $conn = false;
739 } else {
740 $conn->tablePrefix( $prefix );
741 $this->mConns['foreignUsed'][$i][$domain] = $conn;
742 $this->connLogger->debug( __METHOD__ . ": opened new connection for $i/$domain" );
743 }
744 }
745
746 // Increment reference count
747 if ( $conn ) {
748 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
749 $conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
750 }
751
752 return $conn;
753 }
754
755 /**
756 * Test if the specified index represents an open connection
757 *
758 * @param int $index Server index
759 * @access private
760 * @return bool
761 */
762 private function isOpen( $index ) {
763 if ( !is_integer( $index ) ) {
764 return false;
765 }
766
767 return (bool)$this->getAnyOpenConnection( $index );
768 }
769
770 /**
771 * Really opens a connection. Uncached.
772 * Returns a Database object whether or not the connection was successful.
773 * @access private
774 *
775 * @param array $server
776 * @param bool $dbNameOverride
777 * @return DatabaseBase
778 * @throws DBAccessError
779 * @throws MWException
780 */
781 protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
782 if ( $this->disabled ) {
783 throw new DBAccessError();
784 }
785
786 if ( !is_array( $server ) ) {
787 throw new InvalidArgumentException( 'You must update your load-balancing configuration. ' .
788 'See DefaultSettings.php entry for $wgDBservers.' );
789 }
790
791 if ( $dbNameOverride !== false ) {
792 $server['dbname'] = $dbNameOverride;
793 }
794
795 // Let the handle know what the cluster master is (e.g. "db1052")
796 $masterName = $this->getServerName( $this->getWriterIndex() );
797 $server['clusterMasterHost'] = $masterName;
798
799 // Log when many connection are made on requests
800 if ( ++$this->connsOpened >= self::CONN_HELD_WARN_THRESHOLD ) {
801 $this->perfLogger->warning( __METHOD__ . ": " .
802 "{$this->connsOpened}+ connections made (master=$masterName)" );
803 }
804
805 // Set loggers
806 $server['connLogger'] = $this->connLogger;
807 $server['queryLogger'] = $this->queryLogger;
808
809 // Create a live connection object
810 try {
811 $db = DatabaseBase::factory( $server['type'], $server );
812 } catch ( DBConnectionError $e ) {
813 // FIXME: This is probably the ugliest thing I have ever done to
814 // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
815 $db = $e->db;
816 }
817
818 $db->setLBInfo( $server );
819 $db->setLazyMasterHandle(
820 $this->getLazyConnectionRef( DB_MASTER, [], $db->getWikiID() )
821 );
822 $db->setTransactionProfiler( $this->trxProfiler );
823
824 if ( $server['serverIndex'] === $this->getWriterIndex() ) {
825 if ( $this->trxRoundId !== false ) {
826 $this->applyTransactionRoundFlags( $db );
827 }
828 foreach ( $this->trxRecurringCallbacks as $name => $callback ) {
829 $db->setTransactionListener( $name, $callback );
830 }
831 }
832
833 return $db;
834 }
835
836 /**
837 * @throws DBConnectionError
838 * @return bool
839 */
840 private function reportConnectionError() {
841 $conn = $this->mErrorConnection; // The connection which caused the error
842 $context = [
843 'method' => __METHOD__,
844 'last_error' => $this->mLastError,
845 ];
846
847 if ( !is_object( $conn ) ) {
848 // No last connection, probably due to all servers being too busy
849 $this->connLogger->error(
850 "LB failure with no last connection. Connection error: {last_error}",
851 $context
852 );
853
854 // If all servers were busy, mLastError will contain something sensible
855 throw new DBConnectionError( null, $this->mLastError );
856 } else {
857 $context['db_server'] = $conn->getProperty( 'mServer' );
858 $this->connLogger->warning(
859 "Connection error: {last_error} ({db_server})",
860 $context
861 );
862
863 // throws DBConnectionError
864 $conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" );
865 }
866
867 return false; /* not reached */
868 }
869
870 public function getWriterIndex() {
871 return 0;
872 }
873
874 public function haveIndex( $i ) {
875 return array_key_exists( $i, $this->mServers );
876 }
877
878 public function isNonZeroLoad( $i ) {
879 return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
880 }
881
882 public function getServerCount() {
883 return count( $this->mServers );
884 }
885
886 public function getServerName( $i ) {
887 if ( isset( $this->mServers[$i]['hostName'] ) ) {
888 $name = $this->mServers[$i]['hostName'];
889 } elseif ( isset( $this->mServers[$i]['host'] ) ) {
890 $name = $this->mServers[$i]['host'];
891 } else {
892 $name = '';
893 }
894
895 return ( $name != '' ) ? $name : 'localhost';
896 }
897
898 public function getServerInfo( $i ) {
899 if ( isset( $this->mServers[$i] ) ) {
900 return $this->mServers[$i];
901 } else {
902 return false;
903 }
904 }
905
906 public function setServerInfo( $i, array $serverInfo ) {
907 $this->mServers[$i] = $serverInfo;
908 }
909
910 public function getMasterPos() {
911 # If this entire request was served from a replica DB without opening a connection to the
912 # master (however unlikely that may be), then we can fetch the position from the replica DB.
913 $masterConn = $this->getAnyOpenConnection( $this->getWriterIndex() );
914 if ( !$masterConn ) {
915 $serverCount = count( $this->mServers );
916 for ( $i = 1; $i < $serverCount; $i++ ) {
917 $conn = $this->getAnyOpenConnection( $i );
918 if ( $conn ) {
919 return $conn->getSlavePos();
920 }
921 }
922 } else {
923 return $masterConn->getMasterPos();
924 }
925
926 return false;
927 }
928
929 /**
930 * Disable this load balancer. All connections are closed, and any attempt to
931 * open a new connection will result in a DBAccessError.
932 *
933 * @since 1.27
934 */
935 public function disable() {
936 $this->closeAll();
937 $this->disabled = true;
938 }
939
940 public function closeAll() {
941 $this->forEachOpenConnection( function ( DatabaseBase $conn ) {
942 $conn->close();
943 } );
944
945 $this->mConns = [
946 'local' => [],
947 'foreignFree' => [],
948 'foreignUsed' => [],
949 ];
950 $this->connsOpened = 0;
951 }
952
953 public function closeConnection( IDatabase $conn ) {
954 $serverIndex = $conn->getLBInfo( 'serverIndex' ); // second index level of mConns
955 foreach ( $this->mConns as $type => $connsByServer ) {
956 if ( !isset( $connsByServer[$serverIndex] ) ) {
957 continue;
958 }
959
960 foreach ( $connsByServer[$serverIndex] as $i => $trackedConn ) {
961 if ( $conn === $trackedConn ) {
962 unset( $this->mConns[$type][$serverIndex][$i] );
963 --$this->connsOpened;
964 break 2;
965 }
966 }
967 }
968
969 $conn->close();
970 }
971
972 public function commitAll( $fname = __METHOD__ ) {
973 $failures = [];
974
975 $restore = ( $this->trxRoundId !== false );
976 $this->trxRoundId = false;
977 $this->forEachOpenConnection(
978 function ( DatabaseBase $conn ) use ( $fname, $restore, &$failures ) {
979 try {
980 $conn->commit( $fname, $conn::FLUSHING_ALL_PEERS );
981 } catch ( DBError $e ) {
982 call_user_func( $this->errorLogger, $e );
983 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
984 }
985 if ( $restore && $conn->getLBInfo( 'master' ) ) {
986 $this->undoTransactionRoundFlags( $conn );
987 }
988 }
989 );
990
991 if ( $failures ) {
992 throw new DBExpectedError(
993 null,
994 "Commit failed on server(s) " . implode( "\n", array_unique( $failures ) )
995 );
996 }
997 }
998
999 /**
1000 * Perform all pre-commit callbacks that remain part of the atomic transactions
1001 * and disable any post-commit callbacks until runMasterPostTrxCallbacks()
1002 * @since 1.28
1003 */
1004 public function finalizeMasterChanges() {
1005 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) {
1006 // Any error should cause all DB transactions to be rolled back together
1007 $conn->setTrxEndCallbackSuppression( false );
1008 $conn->runOnTransactionPreCommitCallbacks();
1009 // Defer post-commit callbacks until COMMIT finishes for all DBs
1010 $conn->setTrxEndCallbackSuppression( true );
1011 } );
1012 }
1013
1014 /**
1015 * Perform all pre-commit checks for things like replication safety
1016 * @param array $options Includes:
1017 * - maxWriteDuration : max write query duration time in seconds
1018 * @throws DBTransactionError
1019 * @since 1.28
1020 */
1021 public function approveMasterChanges( array $options ) {
1022 $limit = isset( $options['maxWriteDuration'] ) ? $options['maxWriteDuration'] : 0;
1023 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) use ( $limit ) {
1024 // If atomic sections or explicit transactions are still open, some caller must have
1025 // caught an exception but failed to properly rollback any changes. Detect that and
1026 // throw and error (causing rollback).
1027 if ( $conn->explicitTrxActive() ) {
1028 throw new DBTransactionError(
1029 $conn,
1030 "Explicit transaction still active. A caller may have caught an error."
1031 );
1032 }
1033 // Assert that the time to replicate the transaction will be sane.
1034 // If this fails, then all DB transactions will be rollback back together.
1035 $time = $conn->pendingWriteQueryDuration( $conn::ESTIMATE_DB_APPLY );
1036 if ( $limit > 0 && $time > $limit ) {
1037 throw new DBTransactionError(
1038 $conn,
1039 "Transaction spent $time second(s) in writes, exceeding the $limit limit.",
1040 wfMessage( 'transaction-duration-limit-exceeded', $time, $limit )->text()
1041 );
1042 }
1043 // If a connection sits idle while slow queries execute on another, that connection
1044 // may end up dropped before the commit round is reached. Ping servers to detect this.
1045 if ( $conn->writesOrCallbacksPending() && !$conn->ping() ) {
1046 throw new DBTransactionError(
1047 $conn,
1048 "A connection to the {$conn->getDBname()} database was lost before commit."
1049 );
1050 }
1051 } );
1052 }
1053
1054 /**
1055 * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)
1056 *
1057 * The DBO_TRX setting will be reverted to the default in each of these methods:
1058 * - commitMasterChanges()
1059 * - rollbackMasterChanges()
1060 * - commitAll()
1061 * This allows for custom transaction rounds from any outer transaction scope.
1062 *
1063 * @param string $fname
1064 * @throws DBExpectedError
1065 * @since 1.28
1066 */
1067 public function beginMasterChanges( $fname = __METHOD__ ) {
1068 if ( $this->trxRoundId !== false ) {
1069 throw new DBTransactionError(
1070 null,
1071 "$fname: Transaction round '{$this->trxRoundId}' already started."
1072 );
1073 }
1074 $this->trxRoundId = $fname;
1075
1076 $failures = [];
1077 $this->forEachOpenMasterConnection(
1078 function ( DatabaseBase $conn ) use ( $fname, &$failures ) {
1079 $conn->setTrxEndCallbackSuppression( true );
1080 try {
1081 $conn->flushSnapshot( $fname );
1082 } catch ( DBError $e ) {
1083 call_user_func( $this->errorLogger, $e );
1084 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
1085 }
1086 $conn->setTrxEndCallbackSuppression( false );
1087 $this->applyTransactionRoundFlags( $conn );
1088 }
1089 );
1090
1091 if ( $failures ) {
1092 throw new DBExpectedError(
1093 null,
1094 "$fname: Flush failed on server(s) " . implode( "\n", array_unique( $failures ) )
1095 );
1096 }
1097 }
1098
1099 public function commitMasterChanges( $fname = __METHOD__ ) {
1100 $failures = [];
1101
1102 $restore = ( $this->trxRoundId !== false );
1103 $this->trxRoundId = false;
1104 $this->forEachOpenMasterConnection(
1105 function ( DatabaseBase $conn ) use ( $fname, $restore, &$failures ) {
1106 try {
1107 if ( $conn->writesOrCallbacksPending() ) {
1108 $conn->commit( $fname, $conn::FLUSHING_ALL_PEERS );
1109 } elseif ( $restore ) {
1110 $conn->flushSnapshot( $fname );
1111 }
1112 } catch ( DBError $e ) {
1113 call_user_func( $this->errorLogger, $e );
1114 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
1115 }
1116 if ( $restore ) {
1117 $this->undoTransactionRoundFlags( $conn );
1118 }
1119 }
1120 );
1121
1122 if ( $failures ) {
1123 throw new DBExpectedError(
1124 null,
1125 "$fname: Commit failed on server(s) " . implode( "\n", array_unique( $failures ) )
1126 );
1127 }
1128 }
1129
1130 /**
1131 * Issue all pending post-COMMIT/ROLLBACK callbacks
1132 * @param integer $type IDatabase::TRIGGER_* constant
1133 * @return Exception|null The first exception or null if there were none
1134 * @since 1.28
1135 */
1136 public function runMasterPostTrxCallbacks( $type ) {
1137 $e = null; // first exception
1138 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) use ( $type, &$e ) {
1139 $conn->setTrxEndCallbackSuppression( false );
1140 if ( $conn->writesOrCallbacksPending() ) {
1141 // This happens if onTransactionIdle() callbacks leave callbacks on *another* DB
1142 // (which finished its callbacks already). Warn and recover in this case. Let the
1143 // callbacks run in the final commitMasterChanges() in LBFactory::shutdown().
1144 $this->queryLogger->error( __METHOD__ . ": found writes/callbacks pending." );
1145 return;
1146 } elseif ( $conn->trxLevel() ) {
1147 // This happens for single-DB setups where DB_REPLICA uses the master DB,
1148 // thus leaving an implicit read-only transaction open at this point. It
1149 // also happens if onTransactionIdle() callbacks leave implicit transactions
1150 // open on *other* DBs (which is slightly improper). Let these COMMIT on the
1151 // next call to commitMasterChanges(), possibly in LBFactory::shutdown().
1152 return;
1153 }
1154 try {
1155 $conn->runOnTransactionIdleCallbacks( $type );
1156 } catch ( Exception $ex ) {
1157 $e = $e ?: $ex;
1158 }
1159 try {
1160 $conn->runTransactionListenerCallbacks( $type );
1161 } catch ( Exception $ex ) {
1162 $e = $e ?: $ex;
1163 }
1164 } );
1165
1166 return $e;
1167 }
1168
1169 /**
1170 * Issue ROLLBACK only on master, only if queries were done on connection
1171 * @param string $fname Caller name
1172 * @throws DBExpectedError
1173 * @since 1.23
1174 */
1175 public function rollbackMasterChanges( $fname = __METHOD__ ) {
1176 $restore = ( $this->trxRoundId !== false );
1177 $this->trxRoundId = false;
1178 $this->forEachOpenMasterConnection(
1179 function ( DatabaseBase $conn ) use ( $fname, $restore ) {
1180 if ( $conn->writesOrCallbacksPending() ) {
1181 $conn->rollback( $fname, $conn::FLUSHING_ALL_PEERS );
1182 }
1183 if ( $restore ) {
1184 $this->undoTransactionRoundFlags( $conn );
1185 }
1186 }
1187 );
1188 }
1189
1190 /**
1191 * Suppress all pending post-COMMIT/ROLLBACK callbacks
1192 * @return Exception|null The first exception or null if there were none
1193 * @since 1.28
1194 */
1195 public function suppressTransactionEndCallbacks() {
1196 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) {
1197 $conn->setTrxEndCallbackSuppression( true );
1198 } );
1199 }
1200
1201 /**
1202 * @param IDatabase $conn
1203 */
1204 private function applyTransactionRoundFlags( IDatabase $conn ) {
1205 if ( $conn->getFlag( DBO_DEFAULT ) ) {
1206 // DBO_TRX is controlled entirely by CLI mode presence with DBO_DEFAULT.
1207 // Force DBO_TRX even in CLI mode since a commit round is expected soon.
1208 $conn->setFlag( DBO_TRX, $conn::REMEMBER_PRIOR );
1209 // If config has explicitly requested DBO_TRX be either on or off by not
1210 // setting DBO_DEFAULT, then respect that. Forcing no transactions is useful
1211 // for things like blob stores (ExternalStore) which want auto-commit mode.
1212 }
1213 }
1214
1215 /**
1216 * @param IDatabase $conn
1217 */
1218 private function undoTransactionRoundFlags( IDatabase $conn ) {
1219 if ( $conn->getFlag( DBO_DEFAULT ) ) {
1220 $conn->restoreFlags( $conn::RESTORE_PRIOR );
1221 }
1222 }
1223
1224 /**
1225 * Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot
1226 *
1227 * @param string $fname Caller name
1228 * @since 1.28
1229 */
1230 public function flushReplicaSnapshots( $fname = __METHOD__ ) {
1231 $this->forEachOpenReplicaConnection( function ( DatabaseBase $conn ) {
1232 $conn->flushSnapshot( __METHOD__ );
1233 } );
1234 }
1235
1236 /**
1237 * @return bool Whether a master connection is already open
1238 * @since 1.24
1239 */
1240 public function hasMasterConnection() {
1241 return $this->isOpen( $this->getWriterIndex() );
1242 }
1243
1244 /**
1245 * Determine if there are pending changes in a transaction by this thread
1246 * @since 1.23
1247 * @return bool
1248 */
1249 public function hasMasterChanges() {
1250 $pending = 0;
1251 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) use ( &$pending ) {
1252 $pending |= $conn->writesOrCallbacksPending();
1253 } );
1254
1255 return (bool)$pending;
1256 }
1257
1258 /**
1259 * Get the timestamp of the latest write query done by this thread
1260 * @since 1.25
1261 * @return float|bool UNIX timestamp or false
1262 */
1263 public function lastMasterChangeTimestamp() {
1264 $lastTime = false;
1265 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) use ( &$lastTime ) {
1266 $lastTime = max( $lastTime, $conn->lastDoneWrites() );
1267 } );
1268
1269 return $lastTime;
1270 }
1271
1272 /**
1273 * Check if this load balancer object had any recent or still
1274 * pending writes issued against it by this PHP thread
1275 *
1276 * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
1277 * @return bool
1278 * @since 1.25
1279 */
1280 public function hasOrMadeRecentMasterChanges( $age = null ) {
1281 $age = ( $age === null ) ? $this->mWaitTimeout : $age;
1282
1283 return ( $this->hasMasterChanges()
1284 || $this->lastMasterChangeTimestamp() > microtime( true ) - $age );
1285 }
1286
1287 /**
1288 * Get the list of callers that have pending master changes
1289 *
1290 * @return string[] List of method names
1291 * @since 1.27
1292 */
1293 public function pendingMasterChangeCallers() {
1294 $fnames = [];
1295 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) use ( &$fnames ) {
1296 $fnames = array_merge( $fnames, $conn->pendingWriteCallers() );
1297 } );
1298
1299 return $fnames;
1300 }
1301
1302 public function getLaggedReplicaMode( $domain = false ) {
1303 // No-op if there is only one DB (also avoids recursion)
1304 if ( !$this->laggedReplicaMode && $this->getServerCount() > 1 ) {
1305 try {
1306 // See if laggedReplicaMode gets set
1307 $conn = $this->getConnection( DB_REPLICA, false, $domain );
1308 $this->reuseConnection( $conn );
1309 } catch ( DBConnectionError $e ) {
1310 // Avoid expensive re-connect attempts and failures
1311 $this->allReplicasDownMode = true;
1312 $this->laggedReplicaMode = true;
1313 }
1314 }
1315
1316 return $this->laggedReplicaMode;
1317 }
1318
1319 /**
1320 * @param bool $domain
1321 * @return bool
1322 * @deprecated 1.28; use getLaggedReplicaMode()
1323 */
1324 public function getLaggedSlaveMode( $domain = false ) {
1325 return $this->getLaggedReplicaMode( $domain );
1326 }
1327
1328 /**
1329 * @note This method will never cause a new DB connection
1330 * @return bool Whether any generic connection used for reads was highly "lagged"
1331 * @since 1.28
1332 */
1333 public function laggedReplicaUsed() {
1334 return $this->laggedReplicaMode;
1335 }
1336
1337 /**
1338 * @return bool
1339 * @since 1.27
1340 * @deprecated Since 1.28; use laggedReplicaUsed()
1341 */
1342 public function laggedSlaveUsed() {
1343 return $this->laggedReplicaUsed();
1344 }
1345
1346 /**
1347 * @note This method may trigger a DB connection if not yet done
1348 * @param string|bool $domain Domain ID, or false for the current domain
1349 * @param IDatabase|null DB master connection; used to avoid loops [optional]
1350 * @return string|bool Reason the master is read-only or false if it is not
1351 * @since 1.27
1352 */
1353 public function getReadOnlyReason( $domain = false, IDatabase $conn = null ) {
1354 if ( $this->readOnlyReason !== false ) {
1355 return $this->readOnlyReason;
1356 } elseif ( $this->getLaggedReplicaMode( $domain ) ) {
1357 if ( $this->allReplicasDownMode ) {
1358 return 'The database has been automatically locked ' .
1359 'until the replica database servers become available';
1360 } else {
1361 return 'The database has been automatically locked ' .
1362 'while the replica database servers catch up to the master.';
1363 }
1364 } elseif ( $this->masterRunningReadOnly( $domain, $conn ) ) {
1365 return 'The database master is running in read-only mode.';
1366 }
1367
1368 return false;
1369 }
1370
1371 /**
1372 * @param string $domain Domain ID, or false for the current domain
1373 * @param IDatabase|null DB master connectionl used to avoid loops [optional]
1374 * @return bool
1375 */
1376 private function masterRunningReadOnly( $domain, IDatabase $conn = null ) {
1377 $cache = $this->wanCache;
1378 $masterServer = $this->getServerName( $this->getWriterIndex() );
1379
1380 return (bool)$cache->getWithSetCallback(
1381 $cache->makeGlobalKey( __CLASS__, 'server-read-only', $masterServer ),
1382 self::TTL_CACHE_READONLY,
1383 function () use ( $domain, $conn ) {
1384 $this->trxProfiler->setSilenced( true );
1385 try {
1386 $dbw = $conn ?: $this->getConnection( DB_MASTER, [], $domain );
1387 $readOnly = (int)$dbw->serverIsReadOnly();
1388 } catch ( DBError $e ) {
1389 $readOnly = 0;
1390 }
1391 $this->trxProfiler->setSilenced( false );
1392 return $readOnly;
1393 },
1394 [ 'pcTTL' => $cache::TTL_PROC_LONG, 'busyValue' => 0 ]
1395 );
1396 }
1397
1398 public function allowLagged( $mode = null ) {
1399 if ( $mode === null ) {
1400 return $this->mAllowLagged;
1401 }
1402 $this->mAllowLagged = $mode;
1403
1404 return $this->mAllowLagged;
1405 }
1406
1407 public function pingAll() {
1408 $success = true;
1409 $this->forEachOpenConnection( function ( DatabaseBase $conn ) use ( &$success ) {
1410 if ( !$conn->ping() ) {
1411 $success = false;
1412 }
1413 } );
1414
1415 return $success;
1416 }
1417
1418 public function forEachOpenConnection( $callback, array $params = [] ) {
1419 foreach ( $this->mConns as $connsByServer ) {
1420 foreach ( $connsByServer as $serverConns ) {
1421 foreach ( $serverConns as $conn ) {
1422 $mergedParams = array_merge( [ $conn ], $params );
1423 call_user_func_array( $callback, $mergedParams );
1424 }
1425 }
1426 }
1427 }
1428
1429 /**
1430 * Call a function with each open connection object to a master
1431 * @param callable $callback
1432 * @param array $params
1433 * @since 1.28
1434 */
1435 public function forEachOpenMasterConnection( $callback, array $params = [] ) {
1436 $masterIndex = $this->getWriterIndex();
1437 foreach ( $this->mConns as $connsByServer ) {
1438 if ( isset( $connsByServer[$masterIndex] ) ) {
1439 /** @var DatabaseBase $conn */
1440 foreach ( $connsByServer[$masterIndex] as $conn ) {
1441 $mergedParams = array_merge( [ $conn ], $params );
1442 call_user_func_array( $callback, $mergedParams );
1443 }
1444 }
1445 }
1446 }
1447
1448 /**
1449 * Call a function with each open replica DB connection object
1450 * @param callable $callback
1451 * @param array $params
1452 * @since 1.28
1453 */
1454 public function forEachOpenReplicaConnection( $callback, array $params = [] ) {
1455 foreach ( $this->mConns as $connsByServer ) {
1456 foreach ( $connsByServer as $i => $serverConns ) {
1457 if ( $i === $this->getWriterIndex() ) {
1458 continue; // skip master
1459 }
1460 foreach ( $serverConns as $conn ) {
1461 $mergedParams = array_merge( [ $conn ], $params );
1462 call_user_func_array( $callback, $mergedParams );
1463 }
1464 }
1465 }
1466 }
1467
1468 public function getMaxLag( $domain = false ) {
1469 $maxLag = -1;
1470 $host = '';
1471 $maxIndex = 0;
1472
1473 if ( $this->getServerCount() <= 1 ) {
1474 return [ $host, $maxLag, $maxIndex ]; // no replication = no lag
1475 }
1476
1477 $lagTimes = $this->getLagTimes( $domain );
1478 foreach ( $lagTimes as $i => $lag ) {
1479 if ( $this->mLoads[$i] > 0 && $lag > $maxLag ) {
1480 $maxLag = $lag;
1481 $host = $this->mServers[$i]['host'];
1482 $maxIndex = $i;
1483 }
1484 }
1485
1486 return [ $host, $maxLag, $maxIndex ];
1487 }
1488
1489 public function getLagTimes( $domain = false ) {
1490 if ( $this->getServerCount() <= 1 ) {
1491 return [ 0 => 0 ]; // no replication = no lag
1492 }
1493
1494 # Send the request to the load monitor
1495 return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $domain );
1496 }
1497
1498 public function safeGetLag( IDatabase $conn ) {
1499 if ( $this->getServerCount() == 1 ) {
1500 return 0;
1501 } else {
1502 return $conn->getLag();
1503 }
1504 }
1505
1506 /**
1507 * Wait for a replica DB to reach a specified master position
1508 *
1509 * This will connect to the master to get an accurate position if $pos is not given
1510 *
1511 * @param IDatabase $conn Replica DB
1512 * @param DBMasterPos|bool $pos Master position; default: current position
1513 * @param integer $timeout Timeout in seconds
1514 * @return bool Success
1515 * @since 1.27
1516 */
1517 public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) {
1518 if ( $this->getServerCount() == 1 || !$conn->getLBInfo( 'replica' ) ) {
1519 return true; // server is not a replica DB
1520 }
1521
1522 $pos = $pos ?: $this->getConnection( DB_MASTER )->getMasterPos();
1523 if ( !( $pos instanceof DBMasterPos ) ) {
1524 return false; // something is misconfigured
1525 }
1526
1527 $result = $conn->masterPosWait( $pos, $timeout );
1528 if ( $result == -1 || is_null( $result ) ) {
1529 $msg = __METHOD__ . ": Timed out waiting on {$conn->getServer()} pos {$pos}";
1530 $this->replLogger->warning( "$msg" );
1531 $ok = false;
1532 } else {
1533 $this->replLogger->info( __METHOD__ . ": Done" );
1534 $ok = true;
1535 }
1536
1537 return $ok;
1538 }
1539
1540 /**
1541 * Clear the cache for slag lag delay times
1542 *
1543 * This is only used for testing
1544 * @since 1.26
1545 */
1546 public function clearLagTimeCache() {
1547 $this->getLoadMonitor()->clearCaches();
1548 }
1549
1550 /**
1551 * Set a callback via DatabaseBase::setTransactionListener() on
1552 * all current and future master connections of this load balancer
1553 *
1554 * @param string $name Callback name
1555 * @param callable|null $callback
1556 * @since 1.28
1557 */
1558 public function setTransactionListener( $name, callable $callback = null ) {
1559 if ( $callback ) {
1560 $this->trxRecurringCallbacks[$name] = $callback;
1561 } else {
1562 unset( $this->trxRecurringCallbacks[$name] );
1563 }
1564 $this->forEachOpenMasterConnection(
1565 function ( DatabaseBase $conn ) use ( $name, $callback ) {
1566 $conn->setTransactionListener( $name, $callback );
1567 }
1568 );
1569 }
1570
1571 /**
1572 * Set a new table prefix for the existing local domain ID for testing
1573 *
1574 * @param string $prefix
1575 * @since 1.28
1576 */
1577 public function setDomainPrefix( $prefix ) {
1578 list( $dbName, ) = explode( '-', $this->localDomain, 2 );
1579
1580 $this->localDomain = "{$dbName}-{$prefix}";
1581 }
1582 }