PrefixSearch: Implement searching in multiple namespaces
[lhc/web/wiklou.git] / includes / libs / rdbms / 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 => IDatabase 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|IDatabase 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 IDatabase 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 IDatabase 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 IDatabase
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 IDatabase
778 * @throws DBAccessError
779 * @throws InvalidArgumentException
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(
788 'You must update your load-balancing configuration. ' .
789 'See DefaultSettings.php entry for $wgDBservers.' );
790 }
791
792 if ( $dbNameOverride !== false ) {
793 $server['dbname'] = $dbNameOverride;
794 }
795
796 // Let the handle know what the cluster master is (e.g. "db1052")
797 $masterName = $this->getServerName( $this->getWriterIndex() );
798 $server['clusterMasterHost'] = $masterName;
799
800 // Log when many connection are made on requests
801 if ( ++$this->connsOpened >= self::CONN_HELD_WARN_THRESHOLD ) {
802 $this->perfLogger->warning( __METHOD__ . ": " .
803 "{$this->connsOpened}+ connections made (master=$masterName)" );
804 }
805
806 // Set loggers
807 $server['connLogger'] = $this->connLogger;
808 $server['queryLogger'] = $this->queryLogger;
809 $server['trxProfiler'] = $this->trxProfiler;
810
811 // Create a live connection object
812 try {
813 $db = DatabaseBase::factory( $server['type'], $server );
814 } catch ( DBConnectionError $e ) {
815 // FIXME: This is probably the ugliest thing I have ever done to
816 // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
817 $db = $e->db;
818 }
819
820 $db->setLBInfo( $server );
821 $db->setLazyMasterHandle(
822 $this->getLazyConnectionRef( DB_MASTER, [], $db->getWikiID() )
823 );
824
825 if ( $server['serverIndex'] === $this->getWriterIndex() ) {
826 if ( $this->trxRoundId !== false ) {
827 $this->applyTransactionRoundFlags( $db );
828 }
829 foreach ( $this->trxRecurringCallbacks as $name => $callback ) {
830 $db->setTransactionListener( $name, $callback );
831 }
832 }
833
834 return $db;
835 }
836
837 /**
838 * @throws DBConnectionError
839 * @return bool
840 */
841 private function reportConnectionError() {
842 $conn = $this->mErrorConnection; // The connection which caused the error
843 $context = [
844 'method' => __METHOD__,
845 'last_error' => $this->mLastError,
846 ];
847
848 if ( !is_object( $conn ) ) {
849 // No last connection, probably due to all servers being too busy
850 $this->connLogger->error(
851 "LB failure with no last connection. Connection error: {last_error}",
852 $context
853 );
854
855 // If all servers were busy, mLastError will contain something sensible
856 throw new DBConnectionError( null, $this->mLastError );
857 } else {
858 $context['db_server'] = $conn->getProperty( 'mServer' );
859 $this->connLogger->warning(
860 "Connection error: {last_error} ({db_server})",
861 $context
862 );
863
864 // throws DBConnectionError
865 $conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" );
866 }
867
868 return false; /* not reached */
869 }
870
871 public function getWriterIndex() {
872 return 0;
873 }
874
875 public function haveIndex( $i ) {
876 return array_key_exists( $i, $this->mServers );
877 }
878
879 public function isNonZeroLoad( $i ) {
880 return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
881 }
882
883 public function getServerCount() {
884 return count( $this->mServers );
885 }
886
887 public function getServerName( $i ) {
888 if ( isset( $this->mServers[$i]['hostName'] ) ) {
889 $name = $this->mServers[$i]['hostName'];
890 } elseif ( isset( $this->mServers[$i]['host'] ) ) {
891 $name = $this->mServers[$i]['host'];
892 } else {
893 $name = '';
894 }
895
896 return ( $name != '' ) ? $name : 'localhost';
897 }
898
899 public function getServerInfo( $i ) {
900 if ( isset( $this->mServers[$i] ) ) {
901 return $this->mServers[$i];
902 } else {
903 return false;
904 }
905 }
906
907 public function setServerInfo( $i, array $serverInfo ) {
908 $this->mServers[$i] = $serverInfo;
909 }
910
911 public function getMasterPos() {
912 # If this entire request was served from a replica DB without opening a connection to the
913 # master (however unlikely that may be), then we can fetch the position from the replica DB.
914 $masterConn = $this->getAnyOpenConnection( $this->getWriterIndex() );
915 if ( !$masterConn ) {
916 $serverCount = count( $this->mServers );
917 for ( $i = 1; $i < $serverCount; $i++ ) {
918 $conn = $this->getAnyOpenConnection( $i );
919 if ( $conn ) {
920 return $conn->getSlavePos();
921 }
922 }
923 } else {
924 return $masterConn->getMasterPos();
925 }
926
927 return false;
928 }
929
930 /**
931 * Disable this load balancer. All connections are closed, and any attempt to
932 * open a new connection will result in a DBAccessError.
933 *
934 * @since 1.27
935 */
936 public function disable() {
937 $this->closeAll();
938 $this->disabled = true;
939 }
940
941 public function closeAll() {
942 $this->forEachOpenConnection( function ( IDatabase $conn ) {
943 $conn->close();
944 } );
945
946 $this->mConns = [
947 'local' => [],
948 'foreignFree' => [],
949 'foreignUsed' => [],
950 ];
951 $this->connsOpened = 0;
952 }
953
954 public function closeConnection( IDatabase $conn ) {
955 $serverIndex = $conn->getLBInfo( 'serverIndex' ); // second index level of mConns
956 foreach ( $this->mConns as $type => $connsByServer ) {
957 if ( !isset( $connsByServer[$serverIndex] ) ) {
958 continue;
959 }
960
961 foreach ( $connsByServer[$serverIndex] as $i => $trackedConn ) {
962 if ( $conn === $trackedConn ) {
963 unset( $this->mConns[$type][$serverIndex][$i] );
964 --$this->connsOpened;
965 break 2;
966 }
967 }
968 }
969
970 $conn->close();
971 }
972
973 public function commitAll( $fname = __METHOD__ ) {
974 $failures = [];
975
976 $restore = ( $this->trxRoundId !== false );
977 $this->trxRoundId = false;
978 $this->forEachOpenConnection(
979 function ( IDatabase $conn ) use ( $fname, $restore, &$failures ) {
980 try {
981 $conn->commit( $fname, $conn::FLUSHING_ALL_PEERS );
982 } catch ( DBError $e ) {
983 call_user_func( $this->errorLogger, $e );
984 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
985 }
986 if ( $restore && $conn->getLBInfo( 'master' ) ) {
987 $this->undoTransactionRoundFlags( $conn );
988 }
989 }
990 );
991
992 if ( $failures ) {
993 throw new DBExpectedError(
994 null,
995 "Commit failed on server(s) " . implode( "\n", array_unique( $failures ) )
996 );
997 }
998 }
999
1000 /**
1001 * Perform all pre-commit callbacks that remain part of the atomic transactions
1002 * and disable any post-commit callbacks until runMasterPostTrxCallbacks()
1003 * @since 1.28
1004 */
1005 public function finalizeMasterChanges() {
1006 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) {
1007 // Any error should cause all DB transactions to be rolled back together
1008 $conn->setTrxEndCallbackSuppression( false );
1009 $conn->runOnTransactionPreCommitCallbacks();
1010 // Defer post-commit callbacks until COMMIT finishes for all DBs
1011 $conn->setTrxEndCallbackSuppression( true );
1012 } );
1013 }
1014
1015 /**
1016 * Perform all pre-commit checks for things like replication safety
1017 * @param array $options Includes:
1018 * - maxWriteDuration : max write query duration time in seconds
1019 * @throws DBTransactionError
1020 * @since 1.28
1021 */
1022 public function approveMasterChanges( array $options ) {
1023 $limit = isset( $options['maxWriteDuration'] ) ? $options['maxWriteDuration'] : 0;
1024 $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( $limit ) {
1025 // If atomic sections or explicit transactions are still open, some caller must have
1026 // caught an exception but failed to properly rollback any changes. Detect that and
1027 // throw and error (causing rollback).
1028 if ( $conn->explicitTrxActive() ) {
1029 throw new DBTransactionError(
1030 $conn,
1031 "Explicit transaction still active. A caller may have caught an error."
1032 );
1033 }
1034 // Assert that the time to replicate the transaction will be sane.
1035 // If this fails, then all DB transactions will be rollback back together.
1036 $time = $conn->pendingWriteQueryDuration( $conn::ESTIMATE_DB_APPLY );
1037 if ( $limit > 0 && $time > $limit ) {
1038 throw new DBTransactionSizeError(
1039 $conn,
1040 "Transaction spent $time second(s) in writes, exceeding the $limit limit.",
1041 [ $time, $limit ]
1042 );
1043 }
1044 // If a connection sits idle while slow queries execute on another, that connection
1045 // may end up dropped before the commit round is reached. Ping servers to detect this.
1046 if ( $conn->writesOrCallbacksPending() && !$conn->ping() ) {
1047 throw new DBTransactionError(
1048 $conn,
1049 "A connection to the {$conn->getDBname()} database was lost before commit."
1050 );
1051 }
1052 } );
1053 }
1054
1055 /**
1056 * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)
1057 *
1058 * The DBO_TRX setting will be reverted to the default in each of these methods:
1059 * - commitMasterChanges()
1060 * - rollbackMasterChanges()
1061 * - commitAll()
1062 * This allows for custom transaction rounds from any outer transaction scope.
1063 *
1064 * @param string $fname
1065 * @throws DBExpectedError
1066 * @since 1.28
1067 */
1068 public function beginMasterChanges( $fname = __METHOD__ ) {
1069 if ( $this->trxRoundId !== false ) {
1070 throw new DBTransactionError(
1071 null,
1072 "$fname: Transaction round '{$this->trxRoundId}' already started."
1073 );
1074 }
1075 $this->trxRoundId = $fname;
1076
1077 $failures = [];
1078 $this->forEachOpenMasterConnection(
1079 function ( DatabaseBase $conn ) use ( $fname, &$failures ) {
1080 $conn->setTrxEndCallbackSuppression( true );
1081 try {
1082 $conn->flushSnapshot( $fname );
1083 } catch ( DBError $e ) {
1084 call_user_func( $this->errorLogger, $e );
1085 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
1086 }
1087 $conn->setTrxEndCallbackSuppression( false );
1088 $this->applyTransactionRoundFlags( $conn );
1089 }
1090 );
1091
1092 if ( $failures ) {
1093 throw new DBExpectedError(
1094 null,
1095 "$fname: Flush failed on server(s) " . implode( "\n", array_unique( $failures ) )
1096 );
1097 }
1098 }
1099
1100 public function commitMasterChanges( $fname = __METHOD__ ) {
1101 $failures = [];
1102
1103 $restore = ( $this->trxRoundId !== false );
1104 $this->trxRoundId = false;
1105 $this->forEachOpenMasterConnection(
1106 function ( IDatabase $conn ) use ( $fname, $restore, &$failures ) {
1107 try {
1108 if ( $conn->writesOrCallbacksPending() ) {
1109 $conn->commit( $fname, $conn::FLUSHING_ALL_PEERS );
1110 } elseif ( $restore ) {
1111 $conn->flushSnapshot( $fname );
1112 }
1113 } catch ( DBError $e ) {
1114 call_user_func( $this->errorLogger, $e );
1115 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
1116 }
1117 if ( $restore ) {
1118 $this->undoTransactionRoundFlags( $conn );
1119 }
1120 }
1121 );
1122
1123 if ( $failures ) {
1124 throw new DBExpectedError(
1125 null,
1126 "$fname: Commit failed on server(s) " . implode( "\n", array_unique( $failures ) )
1127 );
1128 }
1129 }
1130
1131 /**
1132 * Issue all pending post-COMMIT/ROLLBACK callbacks
1133 * @param integer $type IDatabase::TRIGGER_* constant
1134 * @return Exception|null The first exception or null if there were none
1135 * @since 1.28
1136 */
1137 public function runMasterPostTrxCallbacks( $type ) {
1138 $e = null; // first exception
1139 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) use ( $type, &$e ) {
1140 $conn->setTrxEndCallbackSuppression( false );
1141 if ( $conn->writesOrCallbacksPending() ) {
1142 // This happens if onTransactionIdle() callbacks leave callbacks on *another* DB
1143 // (which finished its callbacks already). Warn and recover in this case. Let the
1144 // callbacks run in the final commitMasterChanges() in LBFactory::shutdown().
1145 $this->queryLogger->error( __METHOD__ . ": found writes/callbacks pending." );
1146 return;
1147 } elseif ( $conn->trxLevel() ) {
1148 // This happens for single-DB setups where DB_REPLICA uses the master DB,
1149 // thus leaving an implicit read-only transaction open at this point. It
1150 // also happens if onTransactionIdle() callbacks leave implicit transactions
1151 // open on *other* DBs (which is slightly improper). Let these COMMIT on the
1152 // next call to commitMasterChanges(), possibly in LBFactory::shutdown().
1153 return;
1154 }
1155 try {
1156 $conn->runOnTransactionIdleCallbacks( $type );
1157 } catch ( Exception $ex ) {
1158 $e = $e ?: $ex;
1159 }
1160 try {
1161 $conn->runTransactionListenerCallbacks( $type );
1162 } catch ( Exception $ex ) {
1163 $e = $e ?: $ex;
1164 }
1165 } );
1166
1167 return $e;
1168 }
1169
1170 /**
1171 * Issue ROLLBACK only on master, only if queries were done on connection
1172 * @param string $fname Caller name
1173 * @throws DBExpectedError
1174 * @since 1.23
1175 */
1176 public function rollbackMasterChanges( $fname = __METHOD__ ) {
1177 $restore = ( $this->trxRoundId !== false );
1178 $this->trxRoundId = false;
1179 $this->forEachOpenMasterConnection(
1180 function ( IDatabase $conn ) use ( $fname, $restore ) {
1181 if ( $conn->writesOrCallbacksPending() ) {
1182 $conn->rollback( $fname, $conn::FLUSHING_ALL_PEERS );
1183 }
1184 if ( $restore ) {
1185 $this->undoTransactionRoundFlags( $conn );
1186 }
1187 }
1188 );
1189 }
1190
1191 /**
1192 * Suppress all pending post-COMMIT/ROLLBACK callbacks
1193 * @return Exception|null The first exception or null if there were none
1194 * @since 1.28
1195 */
1196 public function suppressTransactionEndCallbacks() {
1197 $this->forEachOpenMasterConnection( function ( DatabaseBase $conn ) {
1198 $conn->setTrxEndCallbackSuppression( true );
1199 } );
1200 }
1201
1202 /**
1203 * @param IDatabase $conn
1204 */
1205 private function applyTransactionRoundFlags( IDatabase $conn ) {
1206 if ( $conn->getFlag( DBO_DEFAULT ) ) {
1207 // DBO_TRX is controlled entirely by CLI mode presence with DBO_DEFAULT.
1208 // Force DBO_TRX even in CLI mode since a commit round is expected soon.
1209 $conn->setFlag( DBO_TRX, $conn::REMEMBER_PRIOR );
1210 // If config has explicitly requested DBO_TRX be either on or off by not
1211 // setting DBO_DEFAULT, then respect that. Forcing no transactions is useful
1212 // for things like blob stores (ExternalStore) which want auto-commit mode.
1213 }
1214 }
1215
1216 /**
1217 * @param IDatabase $conn
1218 */
1219 private function undoTransactionRoundFlags( IDatabase $conn ) {
1220 if ( $conn->getFlag( DBO_DEFAULT ) ) {
1221 $conn->restoreFlags( $conn::RESTORE_PRIOR );
1222 }
1223 }
1224
1225 /**
1226 * Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot
1227 *
1228 * @param string $fname Caller name
1229 * @since 1.28
1230 */
1231 public function flushReplicaSnapshots( $fname = __METHOD__ ) {
1232 $this->forEachOpenReplicaConnection( function ( IDatabase $conn ) {
1233 $conn->flushSnapshot( __METHOD__ );
1234 } );
1235 }
1236
1237 /**
1238 * @return bool Whether a master connection is already open
1239 * @since 1.24
1240 */
1241 public function hasMasterConnection() {
1242 return $this->isOpen( $this->getWriterIndex() );
1243 }
1244
1245 /**
1246 * Determine if there are pending changes in a transaction by this thread
1247 * @since 1.23
1248 * @return bool
1249 */
1250 public function hasMasterChanges() {
1251 $pending = 0;
1252 $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$pending ) {
1253 $pending |= $conn->writesOrCallbacksPending();
1254 } );
1255
1256 return (bool)$pending;
1257 }
1258
1259 /**
1260 * Get the timestamp of the latest write query done by this thread
1261 * @since 1.25
1262 * @return float|bool UNIX timestamp or false
1263 */
1264 public function lastMasterChangeTimestamp() {
1265 $lastTime = false;
1266 $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$lastTime ) {
1267 $lastTime = max( $lastTime, $conn->lastDoneWrites() );
1268 } );
1269
1270 return $lastTime;
1271 }
1272
1273 /**
1274 * Check if this load balancer object had any recent or still
1275 * pending writes issued against it by this PHP thread
1276 *
1277 * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
1278 * @return bool
1279 * @since 1.25
1280 */
1281 public function hasOrMadeRecentMasterChanges( $age = null ) {
1282 $age = ( $age === null ) ? $this->mWaitTimeout : $age;
1283
1284 return ( $this->hasMasterChanges()
1285 || $this->lastMasterChangeTimestamp() > microtime( true ) - $age );
1286 }
1287
1288 /**
1289 * Get the list of callers that have pending master changes
1290 *
1291 * @return string[] List of method names
1292 * @since 1.27
1293 */
1294 public function pendingMasterChangeCallers() {
1295 $fnames = [];
1296 $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$fnames ) {
1297 $fnames = array_merge( $fnames, $conn->pendingWriteCallers() );
1298 } );
1299
1300 return $fnames;
1301 }
1302
1303 public function getLaggedReplicaMode( $domain = false ) {
1304 // No-op if there is only one DB (also avoids recursion)
1305 if ( !$this->laggedReplicaMode && $this->getServerCount() > 1 ) {
1306 try {
1307 // See if laggedReplicaMode gets set
1308 $conn = $this->getConnection( DB_REPLICA, false, $domain );
1309 $this->reuseConnection( $conn );
1310 } catch ( DBConnectionError $e ) {
1311 // Avoid expensive re-connect attempts and failures
1312 $this->allReplicasDownMode = true;
1313 $this->laggedReplicaMode = true;
1314 }
1315 }
1316
1317 return $this->laggedReplicaMode;
1318 }
1319
1320 /**
1321 * @param bool $domain
1322 * @return bool
1323 * @deprecated 1.28; use getLaggedReplicaMode()
1324 */
1325 public function getLaggedSlaveMode( $domain = false ) {
1326 return $this->getLaggedReplicaMode( $domain );
1327 }
1328
1329 /**
1330 * @note This method will never cause a new DB connection
1331 * @return bool Whether any generic connection used for reads was highly "lagged"
1332 * @since 1.28
1333 */
1334 public function laggedReplicaUsed() {
1335 return $this->laggedReplicaMode;
1336 }
1337
1338 /**
1339 * @return bool
1340 * @since 1.27
1341 * @deprecated Since 1.28; use laggedReplicaUsed()
1342 */
1343 public function laggedSlaveUsed() {
1344 return $this->laggedReplicaUsed();
1345 }
1346
1347 /**
1348 * @note This method may trigger a DB connection if not yet done
1349 * @param string|bool $domain Domain ID, or false for the current domain
1350 * @param IDatabase|null DB master connection; used to avoid loops [optional]
1351 * @return string|bool Reason the master is read-only or false if it is not
1352 * @since 1.27
1353 */
1354 public function getReadOnlyReason( $domain = false, IDatabase $conn = null ) {
1355 if ( $this->readOnlyReason !== false ) {
1356 return $this->readOnlyReason;
1357 } elseif ( $this->getLaggedReplicaMode( $domain ) ) {
1358 if ( $this->allReplicasDownMode ) {
1359 return 'The database has been automatically locked ' .
1360 'until the replica database servers become available';
1361 } else {
1362 return 'The database has been automatically locked ' .
1363 'while the replica database servers catch up to the master.';
1364 }
1365 } elseif ( $this->masterRunningReadOnly( $domain, $conn ) ) {
1366 return 'The database master is running in read-only mode.';
1367 }
1368
1369 return false;
1370 }
1371
1372 /**
1373 * @param string $domain Domain ID, or false for the current domain
1374 * @param IDatabase|null DB master connectionl used to avoid loops [optional]
1375 * @return bool
1376 */
1377 private function masterRunningReadOnly( $domain, IDatabase $conn = null ) {
1378 $cache = $this->wanCache;
1379 $masterServer = $this->getServerName( $this->getWriterIndex() );
1380
1381 return (bool)$cache->getWithSetCallback(
1382 $cache->makeGlobalKey( __CLASS__, 'server-read-only', $masterServer ),
1383 self::TTL_CACHE_READONLY,
1384 function () use ( $domain, $conn ) {
1385 $this->trxProfiler->setSilenced( true );
1386 try {
1387 $dbw = $conn ?: $this->getConnection( DB_MASTER, [], $domain );
1388 $readOnly = (int)$dbw->serverIsReadOnly();
1389 } catch ( DBError $e ) {
1390 $readOnly = 0;
1391 }
1392 $this->trxProfiler->setSilenced( false );
1393 return $readOnly;
1394 },
1395 [ 'pcTTL' => $cache::TTL_PROC_LONG, 'busyValue' => 0 ]
1396 );
1397 }
1398
1399 public function allowLagged( $mode = null ) {
1400 if ( $mode === null ) {
1401 return $this->mAllowLagged;
1402 }
1403 $this->mAllowLagged = $mode;
1404
1405 return $this->mAllowLagged;
1406 }
1407
1408 public function pingAll() {
1409 $success = true;
1410 $this->forEachOpenConnection( function ( IDatabase $conn ) use ( &$success ) {
1411 if ( !$conn->ping() ) {
1412 $success = false;
1413 }
1414 } );
1415
1416 return $success;
1417 }
1418
1419 public function forEachOpenConnection( $callback, array $params = [] ) {
1420 foreach ( $this->mConns as $connsByServer ) {
1421 foreach ( $connsByServer as $serverConns ) {
1422 foreach ( $serverConns as $conn ) {
1423 $mergedParams = array_merge( [ $conn ], $params );
1424 call_user_func_array( $callback, $mergedParams );
1425 }
1426 }
1427 }
1428 }
1429
1430 /**
1431 * Call a function with each open connection object to a master
1432 * @param callable $callback
1433 * @param array $params
1434 * @since 1.28
1435 */
1436 public function forEachOpenMasterConnection( $callback, array $params = [] ) {
1437 $masterIndex = $this->getWriterIndex();
1438 foreach ( $this->mConns as $connsByServer ) {
1439 if ( isset( $connsByServer[$masterIndex] ) ) {
1440 /** @var IDatabase $conn */
1441 foreach ( $connsByServer[$masterIndex] as $conn ) {
1442 $mergedParams = array_merge( [ $conn ], $params );
1443 call_user_func_array( $callback, $mergedParams );
1444 }
1445 }
1446 }
1447 }
1448
1449 /**
1450 * Call a function with each open replica DB connection object
1451 * @param callable $callback
1452 * @param array $params
1453 * @since 1.28
1454 */
1455 public function forEachOpenReplicaConnection( $callback, array $params = [] ) {
1456 foreach ( $this->mConns as $connsByServer ) {
1457 foreach ( $connsByServer as $i => $serverConns ) {
1458 if ( $i === $this->getWriterIndex() ) {
1459 continue; // skip master
1460 }
1461 foreach ( $serverConns as $conn ) {
1462 $mergedParams = array_merge( [ $conn ], $params );
1463 call_user_func_array( $callback, $mergedParams );
1464 }
1465 }
1466 }
1467 }
1468
1469 public function getMaxLag( $domain = false ) {
1470 $maxLag = -1;
1471 $host = '';
1472 $maxIndex = 0;
1473
1474 if ( $this->getServerCount() <= 1 ) {
1475 return [ $host, $maxLag, $maxIndex ]; // no replication = no lag
1476 }
1477
1478 $lagTimes = $this->getLagTimes( $domain );
1479 foreach ( $lagTimes as $i => $lag ) {
1480 if ( $this->mLoads[$i] > 0 && $lag > $maxLag ) {
1481 $maxLag = $lag;
1482 $host = $this->mServers[$i]['host'];
1483 $maxIndex = $i;
1484 }
1485 }
1486
1487 return [ $host, $maxLag, $maxIndex ];
1488 }
1489
1490 public function getLagTimes( $domain = false ) {
1491 if ( $this->getServerCount() <= 1 ) {
1492 return [ 0 => 0 ]; // no replication = no lag
1493 }
1494
1495 # Send the request to the load monitor
1496 return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $domain );
1497 }
1498
1499 public function safeGetLag( IDatabase $conn ) {
1500 if ( $this->getServerCount() == 1 ) {
1501 return 0;
1502 } else {
1503 return $conn->getLag();
1504 }
1505 }
1506
1507 /**
1508 * Wait for a replica DB to reach a specified master position
1509 *
1510 * This will connect to the master to get an accurate position if $pos is not given
1511 *
1512 * @param IDatabase $conn Replica DB
1513 * @param DBMasterPos|bool $pos Master position; default: current position
1514 * @param integer $timeout Timeout in seconds
1515 * @return bool Success
1516 * @since 1.27
1517 */
1518 public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) {
1519 if ( $this->getServerCount() == 1 || !$conn->getLBInfo( 'replica' ) ) {
1520 return true; // server is not a replica DB
1521 }
1522
1523 $pos = $pos ?: $this->getConnection( DB_MASTER )->getMasterPos();
1524 if ( !( $pos instanceof DBMasterPos ) ) {
1525 return false; // something is misconfigured
1526 }
1527
1528 $result = $conn->masterPosWait( $pos, $timeout );
1529 if ( $result == -1 || is_null( $result ) ) {
1530 $msg = __METHOD__ . ": Timed out waiting on {$conn->getServer()} pos {$pos}";
1531 $this->replLogger->warning( "$msg" );
1532 $ok = false;
1533 } else {
1534 $this->replLogger->info( __METHOD__ . ": Done" );
1535 $ok = true;
1536 }
1537
1538 return $ok;
1539 }
1540
1541 /**
1542 * Clear the cache for slag lag delay times
1543 *
1544 * This is only used for testing
1545 * @since 1.26
1546 */
1547 public function clearLagTimeCache() {
1548 $this->getLoadMonitor()->clearCaches();
1549 }
1550
1551 /**
1552 * Set a callback via IDatabase::setTransactionListener() on
1553 * all current and future master connections of this load balancer
1554 *
1555 * @param string $name Callback name
1556 * @param callable|null $callback
1557 * @since 1.28
1558 */
1559 public function setTransactionListener( $name, callable $callback = null ) {
1560 if ( $callback ) {
1561 $this->trxRecurringCallbacks[$name] = $callback;
1562 } else {
1563 unset( $this->trxRecurringCallbacks[$name] );
1564 }
1565 $this->forEachOpenMasterConnection(
1566 function ( IDatabase $conn ) use ( $name, $callback ) {
1567 $conn->setTransactionListener( $name, $callback );
1568 }
1569 );
1570 }
1571
1572 /**
1573 * Set a new table prefix for the existing local domain ID for testing
1574 *
1575 * @param string $prefix
1576 * @since 1.28
1577 */
1578 public function setDomainPrefix( $prefix ) {
1579 list( $dbName, ) = explode( '-', $this->localDomain, 2 );
1580
1581 $this->localDomain = "{$dbName}-{$prefix}";
1582 }
1583 }