MediaWiki  master
LoadBalancer.php
Go to the documentation of this file.
1 <?php
22 namespace Wikimedia\Rdbms;
23 
24 use ArrayUtils;
25 use BagOStuff;
26 use EmptyBagOStuff;
27 use InvalidArgumentException;
28 use LogicException;
29 use Psr\Log\LoggerInterface;
30 use Psr\Log\NullLogger;
31 use RuntimeException;
32 use Throwable;
33 use UnexpectedValueException;
34 use WANObjectCache;
35 use Wikimedia\ScopedCallback;
36 
42 class LoadBalancer implements ILoadBalancer {
44  private $loadMonitor;
48  private $srvCache;
50  private $wanCache;
52  private $profiler;
54  private $trxProfiler;
56  private $connLogger;
58  private $queryLogger;
60  private $replLogger;
62  private $perfLogger;
64  private $errorLogger;
67 
69  private $localDomain;
70 
74  private $conns;
75 
77  private $servers;
79  private $groupLoads;
81  private $maxLagByIndex;
83  private $waitTimeout;
87  private $maxLag;
89  private $defaultGroup;
90 
92  private $hostname;
94  private $cliMode;
96  private $agent;
97 
99  private $tableAliases = [];
101  private $indexAliases = [];
103  private $domainAliases = [];
107  private $tempTablesOnlyMode = [];
108 
110  private $trxRoundId = false;
112  private $trxRoundStage = self::ROUND_CURSORY;
116  private $readIndexByGroup = [];
118  private $waitForPos;
120  private $allowLagged = false;
122  private $laggedReplicaMode = false;
124  private $lastError = 'Unknown error';
126  private $readOnlyReason = false;
128  private $connectionCounter = 0;
130  private $disabled = false;
132  private $connectionAttempted = false;
133 
135  private $id;
137  private $ownerId;
138 
140  private $nonLocalDomainCache = [];
141 
142  private static $INFO_SERVER_INDEX = 'serverIndex';
143  private static $INFO_AUTOCOMMIT_ONLY = 'autoCommitOnly';
144  private static $INFO_FORIEGN = 'foreign';
145  private static $INFO_FOREIGN_REF_COUNT = 'foreignPoolRefCount';
146 
148  private const CONN_HELD_WARN_THRESHOLD = 10;
149 
151  private const MAX_LAG_DEFAULT = 6;
153  private const MAX_WAIT_DEFAULT = 10;
155  private const TTL_CACHE_READONLY = 5;
156 
157  private const KEY_LOCAL = 'local';
158  private const KEY_FOREIGN_FREE = 'foreignFree';
159  private const KEY_FOREIGN_INUSE = 'foreignInUse';
160 
161  private const KEY_LOCAL_NOROUND = 'localAutoCommit';
162  private const KEY_FOREIGN_FREE_NOROUND = 'foreignFreeAutoCommit';
163  private const KEY_FOREIGN_INUSE_NOROUND = 'foreignInUseAutoCommit';
164 
166  private const ROUND_CURSORY = 'cursory';
168  private const ROUND_FINALIZED = 'finalized';
170  private const ROUND_APPROVED = 'approved';
172  private const ROUND_COMMIT_CALLBACKS = 'commit-callbacks';
174  private const ROUND_ROLLBACK_CALLBACKS = 'rollback-callbacks';
176  private const ROUND_ERROR = 'error';
177 
178  public function __construct( array $params ) {
179  if ( !isset( $params['servers'] ) || !count( $params['servers'] ) ) {
180  throw new InvalidArgumentException( 'Missing or empty "servers" parameter' );
181  }
182 
183  $localDomain = isset( $params['localDomain'] )
184  ? DatabaseDomain::newFromId( $params['localDomain'] )
186  $this->setLocalDomain( $localDomain );
187 
188  $this->maxLag = $params['maxLag'] ?? self::MAX_LAG_DEFAULT;
189 
190  $listKey = -1;
191  $this->servers = [];
192  $this->groupLoads = [ self::GROUP_GENERIC => [] ];
193  foreach ( $params['servers'] as $i => $server ) {
194  if ( ++$listKey !== $i ) {
195  throw new UnexpectedValueException( 'List expected for "servers" parameter' );
196  }
197  $this->servers[$i] = $server;
198  foreach ( ( $server['groupLoads'] ?? [] ) as $group => $ratio ) {
199  $this->groupLoads[$group][$i] = $ratio;
200  }
201  $this->groupLoads[self::GROUP_GENERIC][$i] = $server['load'];
202  $this->maxLagByIndex[$i] = $server['max lag'] ?? $this->maxLag;
203  }
204 
205  $this->waitTimeout = $params['waitTimeout'] ?? self::MAX_WAIT_DEFAULT;
206 
207  $this->conns = self::newTrackedConnectionsArray();
208 
209  if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) {
210  $this->readOnlyReason = $params['readOnlyReason'];
211  }
212 
213  $this->loadMonitorConfig = $params['loadMonitor'] ?? [ 'class' => 'LoadMonitorNull' ];
214  $this->loadMonitorConfig += [ 'lagWarnThreshold' => $this->maxLag ];
215 
216  $this->srvCache = $params['srvCache'] ?? new EmptyBagOStuff();
217  $this->wanCache = $params['wanCache'] ?? WANObjectCache::newEmpty();
218  $this->profiler = $params['profiler'] ?? null;
219  $this->trxProfiler = $params['trxProfiler'] ?? new TransactionProfiler();
220 
221  $this->errorLogger = $params['errorLogger'] ?? function ( Throwable $e ) {
222  trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
223  };
224  $this->deprecationLogger = $params['deprecationLogger'] ?? function ( $msg ) {
225  trigger_error( $msg, E_USER_DEPRECATED );
226  };
227  foreach ( [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ] as $key ) {
228  $this->$key = $params[$key] ?? new NullLogger();
229  }
230 
231  $this->hostname = $params['hostname'] ?? ( gethostname() ?: 'unknown' );
232  $this->cliMode = $params['cliMode'] ?? ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
233  $this->agent = $params['agent'] ?? '';
234 
235  if ( isset( $params['chronologyCallback'] ) ) {
236  $this->chronologyCallback = $params['chronologyCallback'];
237  }
238 
239  if ( isset( $params['roundStage'] ) ) {
240  if ( $params['roundStage'] === self::STAGE_POSTCOMMIT_CALLBACKS ) {
241  $this->trxRoundStage = self::ROUND_COMMIT_CALLBACKS;
242  } elseif ( $params['roundStage'] === self::STAGE_POSTROLLBACK_CALLBACKS ) {
243  $this->trxRoundStage = self::ROUND_ROLLBACK_CALLBACKS;
244  }
245  }
246 
247  $group = $params['defaultGroup'] ?? self::GROUP_GENERIC;
248  $this->defaultGroup = isset( $this->groupLoads[$group] ) ? $group : self::GROUP_GENERIC;
249 
250  static $nextId;
251  $this->id = $nextId = ( is_int( $nextId ) ? $nextId++ : mt_rand() );
252  $this->ownerId = $params['ownerId'] ?? null;
253  }
254 
255  private static function newTrackedConnectionsArray() {
256  return [
257  // Connection were transaction rounds may be applied
258  self::KEY_LOCAL => [],
259  self::KEY_FOREIGN_INUSE => [],
260  self::KEY_FOREIGN_FREE => [],
261  // Auto-committing counterpart connections that ignore transaction rounds
262  self::KEY_LOCAL_NOROUND => [],
263  self::KEY_FOREIGN_INUSE_NOROUND => [],
264  self::KEY_FOREIGN_FREE_NOROUND => []
265  ];
266  }
267 
268  public function getLocalDomainID() {
269  return $this->localDomain->getId();
270  }
271 
272  public function resolveDomainID( $domain ) {
273  return $this->resolveDomainInstance( $domain )->getId();
274  }
275 
280  final protected function resolveDomainInstance( $domain ) {
281  if ( $domain instanceof DatabaseDomain ) {
282  return $domain; // already a domain instance
283  } elseif ( $domain === false || $domain === $this->localDomain->getId() ) {
284  return $this->localDomain;
285  } elseif ( isset( $this->domainAliases[$domain] ) ) {
286  $this->domainAliases[$domain] =
287  DatabaseDomain::newFromId( $this->domainAliases[$domain] );
288 
289  return $this->domainAliases[$domain];
290  }
291 
292  $cachedDomain = $this->nonLocalDomainCache[$domain] ?? null;
293  if ( $cachedDomain === null ) {
294  $cachedDomain = DatabaseDomain::newFromId( $domain );
295  $this->nonLocalDomainCache = [ $domain => $cachedDomain ];
296  }
297 
298  return $cachedDomain;
299  }
300 
308  private function resolveGroups( $groups, $i ) {
309  // If a specific replica server was specified, then $groups makes no sense
310  if ( $i > 0 && $groups !== [] && $groups !== false ) {
311  $list = implode( ', ', (array)$groups );
312  throw new LogicException( "Query group(s) ($list) given with server index (#$i)" );
313  }
314 
315  if ( $groups === [] || $groups === false || $groups === $this->defaultGroup ) {
316  $resolvedGroups = [ $this->defaultGroup ]; // common case
317  } elseif ( is_string( $groups ) && isset( $this->groupLoads[$groups] ) ) {
318  $resolvedGroups = [ $groups, $this->defaultGroup ];
319  } elseif ( is_array( $groups ) ) {
320  $resolvedGroups = array_keys( array_flip( $groups ) + [ self::GROUP_GENERIC => 1 ] );
321  } else {
322  $resolvedGroups = [ $this->defaultGroup ];
323  }
324 
325  return $resolvedGroups;
326  }
327 
334  private function sanitizeConnectionFlags( $flags, $i, $domain ) {
335  // Whether an outside caller is explicitly requesting the master database server
336  if ( $i === self::DB_MASTER || $i === $this->getWriterIndex() ) {
337  $flags |= self::CONN_INTENT_WRITABLE;
338  }
339 
340  if ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT ) {
341  // Callers use CONN_TRX_AUTOCOMMIT to bypass REPEATABLE-READ staleness without
342  // resorting to row locks (e.g. FOR UPDATE) or to make small out-of-band commits
343  // during larger transactions. This is useful for avoiding lock contention.
344 
345  // Master DB server attributes (should match those of the replica DB servers)
346  $attributes = $this->getServerAttributes( $this->getWriterIndex() );
347  if ( $attributes[Database::ATTR_DB_LEVEL_LOCKING] ) {
348  // The RDBMS does not support concurrent writes (e.g. SQLite), so attempts
349  // to use separate connections would just cause self-deadlocks. Note that
350  // REPEATABLE-READ staleness is not an issue since DB-level locking means
351  // that transactions are Strict Serializable anyway.
352  $flags &= ~self::CONN_TRX_AUTOCOMMIT;
353  $type = $this->getServerType( $this->getWriterIndex() );
354  $this->connLogger->info( __METHOD__ . ": CONN_TRX_AUTOCOMMIT disallowed ($type)" );
355  } elseif ( isset( $this->tempTablesOnlyMode[$domain] ) ) {
356  // T202116: integration tests are active and queries should be all be using
357  // temporary clone tables (via prefix). Such tables are not visible accross
358  // different connections nor can there be REPEATABLE-READ snapshot staleness,
359  // so use the same connection for everything.
360  $flags &= ~self::CONN_TRX_AUTOCOMMIT;
361  }
362  }
363 
364  return $flags;
365  }
366 
372  private function enforceConnectionFlags( IDatabase $conn, $flags ) {
373  if ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT ) {
374  if ( $conn->trxLevel() ) { // sanity
375  throw new DBUnexpectedError(
376  $conn,
377  'Handle requested with CONN_TRX_AUTOCOMMIT yet it has a transaction'
378  );
379  }
380 
381  $conn->clearFlag( $conn::DBO_TRX ); // auto-commit mode
382  }
383  }
384 
390  private function getLoadMonitor() {
391  if ( !isset( $this->loadMonitor ) ) {
392  $compat = [
393  'LoadMonitor' => LoadMonitor::class,
394  'LoadMonitorNull' => LoadMonitorNull::class,
395  'LoadMonitorMySQL' => LoadMonitorMySQL::class,
396  ];
397 
398  $class = $this->loadMonitorConfig['class'];
399  if ( isset( $compat[$class] ) ) {
400  $class = $compat[$class];
401  }
402 
403  $this->loadMonitor = new $class(
404  $this, $this->srvCache, $this->wanCache, $this->loadMonitorConfig );
405  $this->loadMonitor->setLogger( $this->replLogger );
406  }
407 
408  return $this->loadMonitor;
409  }
410 
417  private function getRandomNonLagged( array $loads, $domain = false, $maxLag = INF ) {
418  $lags = $this->getLagTimes( $domain );
419 
420  # Unset excessively lagged servers
421  foreach ( $lags as $i => $lag ) {
422  if ( $i !== $this->getWriterIndex() ) {
423  # How much lag this server nominally is allowed to have
424  $maxServerLag = $this->servers[$i]['max lag'] ?? $this->maxLag; // default
425  # Constrain that futher by $maxLag argument
426  $maxServerLag = min( $maxServerLag, $maxLag );
427 
428  $host = $this->getServerName( $i );
429  if ( $lag === false && !is_infinite( $maxServerLag ) ) {
430  $this->replLogger->debug(
431  __METHOD__ .
432  ": server {dbserver} is not replicating?", [ 'dbserver' => $host ] );
433  unset( $loads[$i] );
434  } elseif ( $lag > $maxServerLag ) {
435  $this->replLogger->debug(
436  __METHOD__ .
437  ": server {dbserver} has {lag} seconds of lag (>= {maxlag})",
438  [ 'dbserver' => $host, 'lag' => $lag, 'maxlag' => $maxServerLag ]
439  );
440  unset( $loads[$i] );
441  }
442  }
443  }
444 
445  # Find out if all the replica DBs with non-zero load are lagged
446  $sum = 0;
447  foreach ( $loads as $load ) {
448  $sum += $load;
449  }
450  if ( $sum == 0 ) {
451  # No appropriate DB servers except maybe the master and some replica DBs with zero load
452  # Do NOT use the master
453  # Instead, this function will return false, triggering read-only mode,
454  # and a lagged replica DB will be used instead.
455  return false;
456  }
457 
458  if ( count( $loads ) == 0 ) {
459  return false;
460  }
461 
462  # Return a random representative of the remainder
463  return ArrayUtils::pickRandom( $loads );
464  }
465 
474  private function getConnectionIndex( $i, array $groups, $domain ) {
475  if ( $i === self::DB_MASTER ) {
476  $i = $this->getWriterIndex();
477  } elseif ( $i === self::DB_REPLICA ) {
478  foreach ( $groups as $group ) {
479  $groupIndex = $this->getReaderIndex( $group, $domain );
480  if ( $groupIndex !== false ) {
481  $i = $groupIndex; // group connection succeeded
482  break;
483  }
484  }
485  } elseif ( !isset( $this->servers[$i] ) ) {
486  throw new UnexpectedValueException( "Invalid server index index #$i" );
487  }
488 
489  if ( $i === self::DB_REPLICA ) {
490  $this->lastError = 'Unknown error'; // set here in case of worse failure
491  $this->lastError = 'No working replica DB server: ' . $this->lastError;
492  $this->reportConnectionError();
493  return null; // unreachable due to exception
494  }
495 
496  return $i;
497  }
498 
499  public function getReaderIndex( $group = false, $domain = false ) {
500  if ( $this->getServerCount() == 1 ) {
501  // Skip the load balancing if there's only one server
502  return $this->getWriterIndex();
503  }
504 
505  $group = is_string( $group ) ? $group : self::GROUP_GENERIC;
506 
507  $index = $this->getExistingReaderIndex( $group );
508  if ( $index >= 0 ) {
509  // A reader index was already selected and "waitForPos" was handled
510  return $index;
511  }
512 
513  // Use the server weight array for this load group
514  if ( isset( $this->groupLoads[$group] ) ) {
515  $loads = $this->groupLoads[$group];
516  } else {
517  $this->connLogger->info( __METHOD__ . ": no loads for group $group" );
518 
519  return false;
520  }
521 
522  // Scale the configured load ratios according to each server's load and state
523  $this->getLoadMonitor()->scaleLoads( $loads, $domain );
524 
525  // Pick a server to use, accounting for weights, load, lag, and "waitForPos"
526  $this->lazyLoadReplicationPositions(); // optimizes server candidate selection
527  list( $i, $laggedReplicaMode ) = $this->pickReaderIndex( $loads, $domain );
528  if ( $i === false ) {
529  // DB connection unsuccessful
530  return false;
531  }
532 
533  // If data seen by queries is expected to reflect the transactions committed as of
534  // or after a given replication position then wait for the DB to apply those changes
535  if ( $this->waitForPos && $i !== $this->getWriterIndex() && !$this->doWait( $i ) ) {
536  // Data will be outdated compared to what was expected
537  $laggedReplicaMode = true;
538  }
539 
540  // Cache the reader index for future DB_REPLICA handles
541  $this->setExistingReaderIndex( $group, $i );
542  // Record whether the generic reader index is in "lagged replica DB" mode
543  if ( $group === self::GROUP_GENERIC && $laggedReplicaMode ) {
544  $this->laggedReplicaMode = true;
545  }
546 
547  $serverName = $this->getServerName( $i );
548  $this->connLogger->debug( __METHOD__ . ": using server $serverName for group '$group'" );
549 
550  return $i;
551  }
552 
559  protected function getExistingReaderIndex( $group ) {
560  return $this->readIndexByGroup[$group] ?? -1;
561  }
562 
569  private function setExistingReaderIndex( $group, $index ) {
570  if ( $index < 0 ) {
571  throw new UnexpectedValueException( "Cannot set a negative read server index" );
572  }
573  $this->readIndexByGroup[$group] = $index;
574  }
575 
581  private function pickReaderIndex( array $loads, $domain = false ) {
582  if ( $loads === [] ) {
583  throw new InvalidArgumentException( "Server configuration array is empty" );
584  }
585 
587  $i = false;
589  $laggedReplicaMode = false;
590 
591  // Quickly look through the available servers for a server that meets criteria...
592  $currentLoads = $loads;
593  while ( count( $currentLoads ) ) {
594  if ( $this->allowLagged || $laggedReplicaMode ) {
595  $i = ArrayUtils::pickRandom( $currentLoads );
596  } else {
597  $i = false;
598  if ( $this->waitForPos && $this->waitForPos->asOfTime() ) {
599  $this->replLogger->debug( __METHOD__ . ": replication positions detected" );
600  // "chronologyCallback" sets "waitForPos" for session consistency.
601  // This triggers doWait() after connect, so it's especially good to
602  // avoid lagged servers so as to avoid excessive delay in that method.
603  $ago = microtime( true ) - $this->waitForPos->asOfTime();
604  // Aim for <= 1 second of waiting (being too picky can backfire)
605  $i = $this->getRandomNonLagged( $currentLoads, $domain, $ago + 1 );
606  }
607  if ( $i === false ) {
608  // Any server with less lag than it's 'max lag' param is preferable
609  $i = $this->getRandomNonLagged( $currentLoads, $domain );
610  }
611  if ( $i === false && count( $currentLoads ) ) {
612  // All replica DBs lagged. Switch to read-only mode
613  $this->replLogger->error(
614  __METHOD__ . ": all replica DBs lagged. Switch to read-only mode" );
615  $i = ArrayUtils::pickRandom( $currentLoads );
616  $laggedReplicaMode = true;
617  }
618  }
619 
620  if ( $i === false ) {
621  // pickRandom() returned false.
622  // This is permanent and means the configuration or the load monitor
623  // wants us to return false.
624  $this->connLogger->debug( __METHOD__ . ": pickRandom() returned false" );
625 
626  return [ false, false ];
627  }
628 
629  $serverName = $this->getServerName( $i );
630  $this->connLogger->debug( __METHOD__ . ": Using reader #$i: $serverName..." );
631 
632  // Get a connection to this server without triggering other server connections
633  $conn = $this->getServerConnection( $i, $domain, self::CONN_SILENCE_ERRORS );
634  if ( !$conn ) {
635  $this->connLogger->warning( __METHOD__ . ": Failed connecting to $i/$domain" );
636  unset( $currentLoads[$i] ); // avoid this server next iteration
637  $i = false;
638  continue;
639  }
640 
641  // Decrement reference counter, we are finished with this connection.
642  // It will be incremented for the caller later.
643  if ( $domain !== false ) {
644  $this->reuseConnection( $conn );
645  }
646 
647  // Return this server
648  break;
649  }
650 
651  // If all servers were down, quit now
652  if ( $currentLoads === [] ) {
653  $this->connLogger->error( __METHOD__ . ": all servers down" );
654  }
655 
656  return [ $i, $laggedReplicaMode ];
657  }
658 
659  public function waitFor( $pos ) {
660  $oldPos = $this->waitForPos;
661  try {
662  $this->waitForPos = $pos;
663  // If a generic reader connection was already established, then wait now
664  $i = $this->getExistingReaderIndex( self::GROUP_GENERIC );
665  if ( $i > 0 && !$this->doWait( $i ) ) {
666  $this->laggedReplicaMode = true;
667  }
668  // Otherwise, wait until a connection is established in getReaderIndex()
669  } finally {
670  // Restore the older position if it was higher since this is used for lag-protection
671  $this->setWaitForPositionIfHigher( $oldPos );
672  }
673  }
674 
675  public function waitForOne( $pos, $timeout = null ) {
676  $oldPos = $this->waitForPos;
677  try {
678  $this->waitForPos = $pos;
679 
680  $i = $this->getExistingReaderIndex( self::GROUP_GENERIC );
681  if ( $i <= 0 ) {
682  // Pick a generic replica DB if there isn't one yet
683  $readLoads = $this->groupLoads[self::GROUP_GENERIC];
684  unset( $readLoads[$this->getWriterIndex()] ); // replica DBs only
685  $readLoads = array_filter( $readLoads ); // with non-zero load
686  $i = ArrayUtils::pickRandom( $readLoads );
687  }
688 
689  if ( $i > 0 ) {
690  $ok = $this->doWait( $i, true, $timeout );
691  } else {
692  $ok = true; // no applicable loads
693  }
694  return $ok;
695  } finally {
696  // Restore the old position; this is used for throttling, not lag-protection
697  $this->waitForPos = $oldPos;
698  }
699  }
700 
701  public function waitForAll( $pos, $timeout = null ) {
702  $timeout = $timeout ?: $this->waitTimeout;
703 
704  $oldPos = $this->waitForPos;
705  try {
706  $this->waitForPos = $pos;
707  $serverCount = $this->getServerCount();
708 
709  $ok = true;
710  for ( $i = 1; $i < $serverCount; $i++ ) {
711  if ( $this->serverHasLoadInAnyGroup( $i ) ) {
712  $start = microtime( true );
713  $ok = $this->doWait( $i, true, $timeout ) && $ok;
714  $timeout -= intval( microtime( true ) - $start );
715  if ( $timeout <= 0 ) {
716  break; // timeout reached
717  }
718  }
719  }
720  return $ok;
721  } finally {
722  // Restore the old position; this is used for throttling, not lag-protection
723  $this->waitForPos = $oldPos;
724  }
725  }
726 
731  private function serverHasLoadInAnyGroup( $i ) {
732  foreach ( $this->groupLoads as $loadsByIndex ) {
733  if ( ( $loadsByIndex[$i] ?? 0 ) > 0 ) {
734  return true;
735  }
736  }
737 
738  return false;
739  }
740 
744  private function setWaitForPositionIfHigher( $pos ) {
745  if ( !$pos ) {
746  return;
747  }
748 
749  if ( !$this->waitForPos || $pos->hasReached( $this->waitForPos ) ) {
750  $this->waitForPos = $pos;
751  }
752  }
753 
754  public function getAnyOpenConnection( $i, $flags = 0 ) {
755  $i = ( $i === self::DB_MASTER ) ? $this->getWriterIndex() : $i;
756  // Connection handles required to be in auto-commit mode use a separate connection
757  // pool since the main pool is effected by implicit and explicit transaction rounds
758  $autocommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
759 
760  $conn = false;
761  foreach ( $this->conns as $connsByServer ) {
762  // Get the connection array server indexes to inspect
763  if ( $i === self::DB_REPLICA ) {
764  $indexes = array_keys( $connsByServer );
765  } else {
766  $indexes = isset( $connsByServer[$i] ) ? [ $i ] : [];
767  }
768 
769  foreach ( $indexes as $index ) {
770  $conn = $this->pickAnyOpenConnection( $connsByServer[$index], $autocommit );
771  if ( $conn ) {
772  break;
773  }
774  }
775  }
776 
777  if ( $conn ) {
778  $this->enforceConnectionFlags( $conn, $flags );
779  }
780 
781  return $conn;
782  }
783 
789  private function pickAnyOpenConnection( $candidateConns, $autocommit ) {
790  $conn = false;
791 
792  foreach ( $candidateConns as $candidateConn ) {
793  if ( !$candidateConn->isOpen() ) {
794  continue; // some sort of error occurred?
795  } elseif (
796  $autocommit &&
797  (
798  // Connection is transaction round aware
799  !$candidateConn->getLBInfo( self::$INFO_AUTOCOMMIT_ONLY ) ||
800  // Some sort of error left a transaction open?
801  $candidateConn->trxLevel()
802  )
803  ) {
804  continue; // some sort of error left a transaction open?
805  }
806 
807  $conn = $candidateConn;
808  }
809 
810  return $conn;
811  }
812 
820  protected function doWait( $index, $open = false, $timeout = null ) {
821  $timeout = max( 1, intval( $timeout ?: $this->waitTimeout ) );
822 
823  // Check if we already know that the DB has reached this point
824  $server = $this->getServerName( $index );
825  $key = $this->srvCache->makeGlobalKey( __CLASS__, 'last-known-pos', $server, 'v1' );
827  $knownReachedPos = $this->srvCache->get( $key );
828  if (
829  $knownReachedPos instanceof DBMasterPos &&
830  $knownReachedPos->hasReached( $this->waitForPos )
831  ) {
832  $this->replLogger->debug(
833  __METHOD__ .
834  ": replica DB {dbserver} known to be caught up (pos >= $knownReachedPos).",
835  [ 'dbserver' => $server ]
836  );
837  return true;
838  }
839 
840  // Find a connection to wait on, creating one if needed and allowed
841  $close = false; // close the connection afterwards
842  $flags = self::CONN_SILENCE_ERRORS;
843  $conn = $this->getAnyOpenConnection( $index, $flags );
844  if ( !$conn ) {
845  if ( !$open ) {
846  $this->replLogger->debug(
847  __METHOD__ . ': no connection open for {dbserver}',
848  [ 'dbserver' => $server ]
849  );
850 
851  return false;
852  }
853  // Get a connection to this server without triggering other server connections
854  $conn = $this->getServerConnection( $index, self::DOMAIN_ANY, $flags );
855  if ( !$conn ) {
856  $this->replLogger->warning(
857  __METHOD__ . ': failed to connect to {dbserver}',
858  [ 'dbserver' => $server ]
859  );
860 
861  return false;
862  }
863  // Avoid connection spam in waitForAll() when connections
864  // are made just for the sake of doing this lag check.
865  $close = true;
866  }
867 
868  $this->replLogger->info(
869  __METHOD__ .
870  ': waiting for replica DB {dbserver} to catch up...',
871  [ 'dbserver' => $server ]
872  );
873 
874  $result = $conn->masterPosWait( $this->waitForPos, $timeout );
875 
876  $ok = ( $result !== null && $result != -1 );
877  if ( $ok ) {
878  // Remember that the DB reached this point
879  $this->srvCache->set( $key, $this->waitForPos, BagOStuff::TTL_DAY );
880  }
881 
882  if ( $close ) {
883  $this->closeConnection( $conn );
884  }
885 
886  return $ok;
887  }
888 
889  public function getConnection( $i, $groups = [], $domain = false, $flags = 0 ) {
890  $domain = $this->resolveDomainID( $domain );
891  $groups = $this->resolveGroups( $groups, $i );
892  $flags = $this->sanitizeConnectionFlags( $flags, $i, $domain );
893  // If given DB_MASTER/DB_REPLICA, resolve it to a specific server index. Resolving
894  // DB_REPLICA might trigger getServerConnection() calls due to the getReaderIndex()
895  // connectivity checks or LoadMonitor::scaleLoads() server state cache regeneration.
896  // The use of getServerConnection() instead of getConnection() avoids infinite loops.
897  $serverIndex = $this->getConnectionIndex( $i, $groups, $domain );
898  // Get an open connection to that server (might trigger a new connection)
899  $conn = $this->getServerConnection( $serverIndex, $domain, $flags );
900  // Set master DB handles as read-only if there is high replication lag
901  if (
902  $serverIndex === $this->getWriterIndex() &&
903  $this->getLaggedReplicaMode( $domain ) &&
904  !is_string( $conn->getLBInfo( $conn::LB_READ_ONLY_REASON ) )
905  ) {
906  $reason = ( $this->getExistingReaderIndex( self::GROUP_GENERIC ) >= 0 )
907  ? 'The database is read-only until replication lag decreases.'
908  : 'The database is read-only until replica database servers becomes reachable.';
909  $conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $reason );
910  }
911 
912  return $conn;
913  }
914 
922  public function getServerConnection( $i, $domain, $flags = 0 ) {
923  // Number of connections made before getting the server index and handle
924  $priorConnectionsMade = $this->connectionCounter;
925  // Get an open connection to this server (might trigger a new connection)
926  $conn = $this->localDomain->equals( $domain )
927  ? $this->getLocalConnection( $i, $flags )
928  : $this->getForeignConnection( $i, $domain, $flags );
929  // Throw an error or otherwise bail out if the connection attempt failed
930  if ( !( $conn instanceof IDatabase ) ) {
931  if ( ( $flags & self::CONN_SILENCE_ERRORS ) != self::CONN_SILENCE_ERRORS ) {
932  $this->reportConnectionError();
933  }
934 
935  return false;
936  }
937 
938  // Profile any new connections caused by this method
939  if ( $this->connectionCounter > $priorConnectionsMade ) {
940  $this->trxProfiler->recordConnection(
941  $conn->getServer(),
942  $conn->getDBname(),
943  ( ( $flags & self::CONN_INTENT_WRITABLE ) == self::CONN_INTENT_WRITABLE )
944  );
945  }
946 
947  if ( !$conn->isOpen() ) {
948  $this->errorConnection = $conn;
949  // Connection was made but later unrecoverably lost for some reason.
950  // Do not return a handle that will just throw exceptions on use, but
951  // let the calling code, e.g. getReaderIndex(), try another server.
952  return false;
953  }
954 
955  // Make sure that flags like CONN_TRX_AUTOCOMMIT are respected by this handle
956  $this->enforceConnectionFlags( $conn, $flags );
957  // Set master DB handles as read-only if the load balancer is configured as read-only
958  // or the master database server is running in server-side read-only mode. Note that
959  // replica DB handles are always read-only via Database::assertIsWritableMaster().
960  // Read-only mode due to replication lag is *avoided* here to avoid recursion.
961  if ( $i === $this->getWriterIndex() ) {
962  if ( $this->readOnlyReason !== false ) {
964  } elseif ( $this->isMasterConnectionReadOnly( $conn, $flags ) ) {
965  $readOnlyReason = 'The master database server is running in read-only mode.';
966  } else {
967  $readOnlyReason = false;
968  }
969  $conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $readOnlyReason );
970  }
971 
972  return $conn;
973  }
974 
975  public function reuseConnection( IDatabase $conn ) {
976  $serverIndex = $conn->getLBInfo( self::$INFO_SERVER_INDEX );
977  $refCount = $conn->getLBInfo( self::$INFO_FOREIGN_REF_COUNT );
978  if ( $serverIndex === null || $refCount === null ) {
979  return; // non-foreign connection; no domain-use tracking to update
980  } elseif ( $conn instanceof DBConnRef ) {
981  // DBConnRef already handles calling reuseConnection() and only passes the live
982  // Database instance to this method. Any caller passing in a DBConnRef is broken.
983  $this->connLogger->error(
984  __METHOD__ . ": got DBConnRef instance",
985  [ 'exception' => new RuntimeException() ]
986  );
987 
988  return;
989  }
990 
991  if ( $this->disabled ) {
992  return; // DBConnRef handle probably survived longer than the LoadBalancer
993  }
994 
995  if ( $conn->getLBInfo( self::$INFO_AUTOCOMMIT_ONLY ) ) {
996  $connFreeKey = self::KEY_FOREIGN_FREE_NOROUND;
997  $connInUseKey = self::KEY_FOREIGN_INUSE_NOROUND;
998  } else {
999  $connFreeKey = self::KEY_FOREIGN_FREE;
1000  $connInUseKey = self::KEY_FOREIGN_INUSE;
1001  }
1002 
1003  $domain = $conn->getDomainID();
1004  if ( !isset( $this->conns[$connInUseKey][$serverIndex][$domain] ) ) {
1005  throw new InvalidArgumentException(
1006  "Connection $serverIndex/$domain not found; it may have already been freed" );
1007  } elseif ( $this->conns[$connInUseKey][$serverIndex][$domain] !== $conn ) {
1008  throw new InvalidArgumentException(
1009  "Connection $serverIndex/$domain mismatched; it may have already been freed" );
1010  }
1011 
1012  $conn->setLBInfo( self::$INFO_FOREIGN_REF_COUNT, --$refCount );
1013  if ( $refCount <= 0 ) {
1014  $this->conns[$connFreeKey][$serverIndex][$domain] = $conn;
1015  unset( $this->conns[$connInUseKey][$serverIndex][$domain] );
1016  if ( !$this->conns[$connInUseKey][$serverIndex] ) {
1017  unset( $this->conns[$connInUseKey][$serverIndex] ); // clean up
1018  }
1019  $this->connLogger->debug( __METHOD__ . ": freed connection $serverIndex/$domain" );
1020  } else {
1021  $this->connLogger->debug( __METHOD__ .
1022  ": reference count for $serverIndex/$domain reduced to $refCount" );
1023  }
1024  }
1025 
1026  public function getConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ) {
1027  $domain = $this->resolveDomainID( $domain );
1028  $role = $this->getRoleFromIndex( $i );
1029 
1030  return new DBConnRef( $this, $this->getConnection( $i, $groups, $domain, $flags ), $role );
1031  }
1032 
1033  public function getLazyConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ) {
1034  $domain = $this->resolveDomainID( $domain );
1035  $role = $this->getRoleFromIndex( $i );
1036 
1037  return new DBConnRef( $this, [ $i, $groups, $domain, $flags ], $role );
1038  }
1039 
1040  public function getMaintenanceConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ) {
1041  $domain = $this->resolveDomainID( $domain );
1042  $role = $this->getRoleFromIndex( $i );
1043 
1044  return new MaintainableDBConnRef(
1045  $this, $this->getConnection( $i, $groups, $domain, $flags ), $role );
1046  }
1047 
1052  private function getRoleFromIndex( $i ) {
1053  return ( $i === self::DB_MASTER || $i === $this->getWriterIndex() )
1054  ? self::DB_MASTER
1055  : self::DB_REPLICA;
1056  }
1057 
1065  public function openConnection( $i, $domain = false, $flags = 0 ) {
1066  return $this->getConnection( $i, [], $domain, $flags | self::CONN_SILENCE_ERRORS );
1067  }
1068 
1083  private function getLocalConnection( $i, $flags = 0 ) {
1084  $autoCommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
1085  // Connection handles required to be in auto-commit mode use a separate connection
1086  // pool since the main pool is effected by implicit and explicit transaction rounds
1087  $connKey = $autoCommit ? self::KEY_LOCAL_NOROUND : self::KEY_LOCAL;
1088 
1089  if ( isset( $this->conns[$connKey][$i][0] ) ) {
1090  $conn = $this->conns[$connKey][$i][0];
1091  } else {
1092  $conn = $this->reallyOpenConnection(
1093  $i,
1094  $this->localDomain,
1095  [ self::$INFO_AUTOCOMMIT_ONLY => $autoCommit ]
1096  );
1097  if ( $conn->isOpen() ) {
1098  $this->connLogger->debug( __METHOD__ . ": opened new connection for $i" );
1099  $this->conns[$connKey][$i][0] = $conn;
1100  } else {
1101  $this->connLogger->warning( __METHOD__ . ": connection error for $i" );
1102  $this->errorConnection = $conn;
1103  $conn = false;
1104  }
1105  }
1106 
1107  // Sanity check to make sure that the right domain is selected
1108  if (
1109  $conn instanceof IDatabase &&
1110  !$this->localDomain->isCompatible( $conn->getDomainID() )
1111  ) {
1112  throw new UnexpectedValueException(
1113  "Got connection to '{$conn->getDomainID()}', " .
1114  "but expected local domain ('{$this->localDomain}')"
1115  );
1116  }
1117 
1118  return $conn;
1119  }
1120 
1145  private function getForeignConnection( $i, $domain, $flags = 0 ) {
1146  $domainInstance = DatabaseDomain::newFromId( $domain );
1147  $autoCommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
1148  // Connection handles required to be in auto-commit mode use a separate connection
1149  // pool since the main pool is effected by implicit and explicit transaction rounds
1150  if ( $autoCommit ) {
1151  $connFreeKey = self::KEY_FOREIGN_FREE_NOROUND;
1152  $connInUseKey = self::KEY_FOREIGN_INUSE_NOROUND;
1153  } else {
1154  $connFreeKey = self::KEY_FOREIGN_FREE;
1155  $connInUseKey = self::KEY_FOREIGN_INUSE;
1156  }
1157 
1159  $conn = null;
1160 
1161  if ( isset( $this->conns[$connInUseKey][$i][$domain] ) ) {
1162  // Reuse an in-use connection for the same domain
1163  $conn = $this->conns[$connInUseKey][$i][$domain];
1164  $this->connLogger->debug( __METHOD__ . ": reusing connection $i/$domain" );
1165  } elseif ( isset( $this->conns[$connFreeKey][$i][$domain] ) ) {
1166  // Reuse a free connection for the same domain
1167  $conn = $this->conns[$connFreeKey][$i][$domain];
1168  unset( $this->conns[$connFreeKey][$i][$domain] );
1169  $this->conns[$connInUseKey][$i][$domain] = $conn;
1170  $this->connLogger->debug( __METHOD__ . ": reusing free connection $i/$domain" );
1171  } elseif ( !empty( $this->conns[$connFreeKey][$i] ) ) {
1172  // Reuse a free connection from another domain if possible
1173  foreach ( $this->conns[$connFreeKey][$i] as $oldDomain => $oldConn ) {
1174  if ( $domainInstance->getDatabase() !== null ) {
1175  // Check if changing the database will require a new connection.
1176  // In that case, leave the connection handle alone and keep looking.
1177  // This prevents connections from being closed mid-transaction and can
1178  // also avoid overhead if the same database will later be requested.
1179  if (
1180  $oldConn->databasesAreIndependent() &&
1181  $oldConn->getDBname() !== $domainInstance->getDatabase()
1182  ) {
1183  continue;
1184  }
1185  // Select the new database, schema, and prefix
1186  $conn = $oldConn;
1187  $conn->selectDomain( $domainInstance );
1188  } else {
1189  // Stay on the current database, but update the schema/prefix
1190  $conn = $oldConn;
1191  $conn->dbSchema( $domainInstance->getSchema() );
1192  $conn->tablePrefix( $domainInstance->getTablePrefix() );
1193  }
1194  unset( $this->conns[$connFreeKey][$i][$oldDomain] );
1195  // Note that if $domain is an empty string, getDomainID() might not match it
1196  $this->conns[$connInUseKey][$i][$conn->getDomainID()] = $conn;
1197  $this->connLogger->debug( __METHOD__ .
1198  ": reusing free connection from $oldDomain for $domain" );
1199  break;
1200  }
1201  }
1202 
1203  if ( !$conn ) {
1204  $conn = $this->reallyOpenConnection(
1205  $i,
1206  $domainInstance,
1207  [
1208  self::$INFO_AUTOCOMMIT_ONLY => $autoCommit,
1209  self::$INFO_FORIEGN => true,
1210  self::$INFO_FOREIGN_REF_COUNT => 0
1211  ]
1212  );
1213  if ( $conn->isOpen() ) {
1214  // Note that if $domain is an empty string, getDomainID() might not match it
1215  $this->conns[$connInUseKey][$i][$conn->getDomainID()] = $conn;
1216  $this->connLogger->debug( __METHOD__ . ": opened new connection for $i/$domain" );
1217  } else {
1218  $this->connLogger->warning( __METHOD__ . ": connection error for $i/$domain" );
1219  $this->errorConnection = $conn;
1220  $conn = false;
1221  }
1222  }
1223 
1224  if ( $conn instanceof IDatabase ) {
1225  // Sanity check to make sure that the right domain is selected
1226  if ( !$domainInstance->isCompatible( $conn->getDomainID() ) ) {
1227  throw new UnexpectedValueException(
1228  "Got connection to '{$conn->getDomainID()}', but expected '$domain'" );
1229  }
1230  // Increment reference count
1231  $refCount = $conn->getLBInfo( self::$INFO_FOREIGN_REF_COUNT );
1232  $conn->setLBInfo( self::$INFO_FOREIGN_REF_COUNT, $refCount + 1 );
1233  }
1234 
1235  return $conn;
1236  }
1237 
1238  public function getServerAttributes( $i ) {
1240  $this->getServerType( $i ),
1241  $this->servers[$i]['driver'] ?? null
1242  );
1243  }
1244 
1251  private function isOpen( $index ) {
1252  return (bool)$this->getAnyOpenConnection( $index );
1253  }
1254 
1267  protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo ) {
1268  if ( $this->disabled ) {
1269  throw new DBAccessError();
1270  }
1271 
1272  $server = $this->getServerInfoStrict( $i );
1273 
1274  $conn = Database::factory(
1275  $server['type'],
1276  array_merge( $server, [
1277  // Basic replication role information
1278  'topologyRole' => $this->getTopologyRole( $i, $server ),
1279  'topologicalMaster' => $this->getMasterServerName(),
1280  // Use the database specified in $domain (null means "none or entrypoint DB");
1281  // fallback to the $server default if the RDBMs is an embedded library using a
1282  // file on disk since there would be nothing to access to without a DB/file name.
1283  'dbname' => $this->getServerAttributes( $i )[Database::ATTR_DB_IS_FILE]
1284  ? ( $domain->getDatabase() ?? $server['dbname'] ?? null )
1285  : $domain->getDatabase(),
1286  // Override the $server default schema with that of $domain if specified
1287  'schema' => $domain->getSchema() ?? $server['schema'] ?? null,
1288  // Use the table prefix specified in $domain
1289  'tablePrefix' => $domain->getTablePrefix(),
1290  // Participate in transaction rounds if $server does not specify otherwise
1291  'flags' => $this->initConnFlags( $server['flags'] ?? IDatabase::DBO_DEFAULT ),
1292  // Inject the PHP execution mode and the agent string
1293  'cliMode' => $this->cliMode,
1294  'agent' => $this->agent,
1295  'ownerId' => $this->id,
1296  // Inject object and callback dependencies
1297  'lazyMasterHandle' => $this->getLazyConnectionRef(
1299  [],
1300  $domain->getId()
1301  ),
1302  'srvCache' => $this->srvCache,
1303  'connLogger' => $this->connLogger,
1304  'queryLogger' => $this->queryLogger,
1305  'replLogger' => $this->replLogger,
1306  'errorLogger' => $this->errorLogger,
1307  'deprecationLogger' => $this->deprecationLogger,
1308  'profiler' => $this->profiler,
1309  'trxProfiler' => $this->trxProfiler
1310  ] ),
1311  Database::NEW_UNCONNECTED
1312  );
1313  // Attach load balancer information to the handle
1314  $conn->setLBInfo( [ self::$INFO_SERVER_INDEX => $i ] + $lbInfo );
1315  // Set alternative table/index names before any queries can be issued
1316  $conn->setTableAliases( $this->tableAliases );
1317  $conn->setIndexAliases( $this->indexAliases );
1318  // Account for any active transaction round and listeners
1319  if ( $i === $this->getWriterIndex() ) {
1320  if ( $this->trxRoundId !== false ) {
1321  $this->applyTransactionRoundFlags( $conn );
1322  }
1323  foreach ( $this->trxRecurringCallbacks as $name => $callback ) {
1324  $conn->setTransactionListener( $name, $callback );
1325  }
1326  }
1327 
1328  // Make the connection handle live
1329  try {
1330  $conn->initConnection();
1332  } catch ( DBConnectionError $e ) {
1333  // ignore; let the DB handle the logging
1334  }
1335 
1336  // Try to maintain session consistency for clients that trigger write transactions
1337  // in a request or script and then return soon after in another request or script.
1338  // This requires cooperation with ChronologyProtector and the application wiring.
1339  if ( $conn->isOpen() ) {
1341  }
1342 
1343  // Log when many connection are made during a single request/script
1344  $count = $this->getCurrentConnectionCount();
1345  if ( $count >= self::CONN_HELD_WARN_THRESHOLD ) {
1346  $this->perfLogger->warning(
1347  __METHOD__ . ": {connections}+ connections made (master={masterdb})",
1348  [
1349  'connections' => $count,
1350  'dbserver' => $conn->getServer(),
1351  'masterdb' => $this->getMasterServerName()
1352  ]
1353  );
1354  }
1355 
1356  return $conn;
1357  }
1358 
1364  private function getTopologyRole( $i, array $server ) {
1365  if ( !empty( $server['is static'] ) ) {
1366  return IDatabase::ROLE_STATIC_CLONE;
1367  }
1368 
1369  return ( $i === $this->getWriterIndex() )
1370  ? IDatabase::ROLE_STREAMING_MASTER
1371  : IDatabase::ROLE_STREAMING_REPLICA;
1372  }
1373 
1379  private function initConnFlags( $flags ) {
1380  if ( ( $flags & IDatabase::DBO_DEFAULT ) === IDatabase::DBO_DEFAULT ) {
1381  if ( $this->cliMode ) {
1382  $flags &= ~IDatabase::DBO_TRX;
1383  } else {
1384  $flags |= IDatabase::DBO_TRX;
1385  }
1386  }
1387 
1388  return $flags;
1389  }
1390 
1394  private function lazyLoadReplicationPositions() {
1395  if ( !$this->connectionAttempted && $this->chronologyCallback ) {
1396  $this->connectionAttempted = true;
1397  ( $this->chronologyCallback )( $this ); // generally calls waitFor()
1398  $this->connLogger->debug( __METHOD__ . ': executed chronology callback.' );
1399  }
1400  }
1401 
1405  private function reportConnectionError() {
1406  $conn = $this->errorConnection; // the connection which caused the error
1407  $context = [
1408  'method' => __METHOD__,
1409  'last_error' => $this->lastError,
1410  ];
1411 
1412  if ( $conn instanceof IDatabase ) {
1413  $context['db_server'] = $conn->getServer();
1414  $this->connLogger->warning(
1415  __METHOD__ . ": connection error: {last_error} ({db_server})",
1416  $context
1417  );
1418 
1419  throw new DBConnectionError( $conn, "{$this->lastError} ({$context['db_server']})" );
1420  } else {
1421  // No last connection, probably due to all servers being too busy
1422  $this->connLogger->error(
1423  __METHOD__ .
1424  ": LB failure with no last connection. Connection error: {last_error}",
1425  $context
1426  );
1427 
1428  // If all servers were busy, "lastError" will contain something sensible
1429  throw new DBConnectionError( null, $this->lastError );
1430  }
1431  }
1432 
1433  public function getWriterIndex() {
1434  return 0;
1435  }
1436 
1444  public function haveIndex( $i ) {
1445  return array_key_exists( $i, $this->servers );
1446  }
1447 
1455  public function isNonZeroLoad( $i ) {
1456  return ( isset( $this->servers[$i] ) && $this->groupLoads[self::GROUP_GENERIC][$i] > 0 );
1457  }
1458 
1459  public function getServerCount() {
1460  return count( $this->servers );
1461  }
1462 
1463  public function hasReplicaServers() {
1464  return ( $this->getServerCount() > 1 );
1465  }
1466 
1467  public function hasStreamingReplicaServers() {
1468  foreach ( $this->servers as $i => $server ) {
1469  if ( $i !== $this->getWriterIndex() && empty( $server['is static'] ) ) {
1470  return true;
1471  }
1472  }
1473 
1474  return false;
1475  }
1476 
1477  public function getServerName( $i ) {
1478  $name = $this->servers[$i]['hostName'] ?? ( $this->servers[$i]['host'] ?? '' );
1479 
1480  return ( $name != '' ) ? $name : 'localhost';
1481  }
1482 
1483  public function getServerInfo( $i ) {
1484  return $this->servers[$i] ?? false;
1485  }
1486 
1487  public function getServerType( $i ) {
1488  return $this->servers[$i]['type'] ?? 'unknown';
1489  }
1490 
1491  public function getMasterPos() {
1492  $index = $this->getWriterIndex();
1493 
1494  $conn = $this->getAnyOpenConnection( $index );
1495  if ( $conn ) {
1496  return $conn->getMasterPos();
1497  }
1498 
1499  $conn = $this->getConnection( $index, self::CONN_SILENCE_ERRORS );
1500  if ( !$conn ) {
1501  $this->reportConnectionError();
1502  return null; // unreachable due to exception
1503  }
1504 
1505  try {
1506  return $conn->getMasterPos();
1507  } finally {
1508  $this->closeConnection( $conn );
1509  }
1510  }
1511 
1512  public function getReplicaResumePos() {
1513  // Get the position of any existing master server connection
1514  $masterConn = $this->getAnyOpenConnection( $this->getWriterIndex() );
1515  if ( $masterConn ) {
1516  return $masterConn->getMasterPos();
1517  }
1518 
1519  // Get the highest position of any existing replica server connection
1520  $highestPos = false;
1521  $serverCount = $this->getServerCount();
1522  for ( $i = 1; $i < $serverCount; $i++ ) {
1523  if ( !empty( $this->servers[$i]['is static'] ) ) {
1524  continue; // server does not use replication
1525  }
1526 
1527  $conn = $this->getAnyOpenConnection( $i );
1528  $pos = $conn ? $conn->getReplicaPos() : false;
1529  if ( !$pos ) {
1530  continue; // no open connection or could not get position
1531  }
1532 
1533  $highestPos = $highestPos ?: $pos;
1534  if ( $pos->hasReached( $highestPos ) ) {
1535  $highestPos = $pos;
1536  }
1537  }
1538 
1539  return $highestPos;
1540  }
1541 
1542  public function disable( $fname = __METHOD__, $owner = null ) {
1543  $this->assertOwnership( $fname, $owner );
1544  $this->closeAll( $fname, $owner );
1545  $this->disabled = true;
1546  }
1547 
1548  public function closeAll( $fname = __METHOD__, $owner = null ) {
1549  $this->assertOwnership( $fname, $owner );
1550  if ( $this->ownerId === null ) {
1552  $scope = ScopedCallback::newScopedIgnoreUserAbort();
1553  }
1554  $this->forEachOpenConnection( function ( IDatabase $conn ) use ( $fname ) {
1555  $host = $conn->getServer();
1556  $this->connLogger->debug( "$fname: closing connection to database '$host'." );
1557  $conn->close( $fname, $this->id );
1558  } );
1559 
1560  $this->conns = self::newTrackedConnectionsArray();
1561  }
1562 
1563  public function closeConnection( IDatabase $conn ) {
1564  if ( $conn instanceof DBConnRef ) {
1565  // Avoid calling close() but still leaving the handle in the pool
1566  throw new RuntimeException( 'Cannot close DBConnRef instance; it must be shareable' );
1567  }
1568 
1569  $serverIndex = $conn->getLBInfo( self::$INFO_SERVER_INDEX );
1570  foreach ( $this->conns as $type => $connsByServer ) {
1571  if ( !isset( $connsByServer[$serverIndex] ) ) {
1572  continue;
1573  }
1574 
1575  foreach ( $connsByServer[$serverIndex] as $i => $trackedConn ) {
1576  if ( $conn === $trackedConn ) {
1577  $host = $this->getServerName( $i );
1578  $this->connLogger->debug(
1579  __METHOD__ . ": closing connection to database $i at '$host'." );
1580  unset( $this->conns[$type][$serverIndex][$i] );
1581  break 2;
1582  }
1583  }
1584  }
1585 
1586  $conn->close( __METHOD__ );
1587  }
1588 
1589  public function commitAll( $fname = __METHOD__, $owner = null ) {
1590  $this->commitMasterChanges( $fname, $owner );
1591  $this->flushMasterSnapshots( $fname, $owner );
1592  $this->flushReplicaSnapshots( $fname, $owner );
1593  }
1594 
1595  public function finalizeMasterChanges( $fname = __METHOD__, $owner = null ) {
1596  $this->assertOwnership( $fname, $owner );
1597  $this->assertTransactionRoundStage( [ self::ROUND_CURSORY, self::ROUND_FINALIZED ] );
1598  if ( $this->ownerId === null ) {
1600  $scope = ScopedCallback::newScopedIgnoreUserAbort();
1601  }
1602 
1603  $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
1604  // Loop until callbacks stop adding callbacks on other connections
1605  $total = 0;
1606  do {
1607  $count = 0; // callbacks execution attempts
1608  $this->forEachOpenMasterConnection( function ( Database $conn ) use ( &$count ) {
1609  // Run any pre-commit callbacks while leaving the post-commit ones suppressed.
1610  // Any error should cause all (peer) transactions to be rolled back together.
1611  $count += $conn->runOnTransactionPreCommitCallbacks();
1612  } );
1613  $total += $count;
1614  } while ( $count > 0 );
1615  // Defer post-commit callbacks until after COMMIT/ROLLBACK happens on all handles
1616  $this->forEachOpenMasterConnection( function ( Database $conn ) {
1617  $conn->setTrxEndCallbackSuppression( true );
1618  } );
1619  $this->trxRoundStage = self::ROUND_FINALIZED;
1620 
1621  return $total;
1622  }
1623 
1624  public function approveMasterChanges( array $options, $fname = __METHOD__, $owner = null ) {
1625  $this->assertOwnership( $fname, $owner );
1626  $this->assertTransactionRoundStage( self::ROUND_FINALIZED );
1627  if ( $this->ownerId === null ) {
1629  $scope = ScopedCallback::newScopedIgnoreUserAbort();
1630  }
1631 
1632  $limit = $options['maxWriteDuration'] ?? 0;
1633 
1634  $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
1635  $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( $limit ) {
1636  // If atomic sections or explicit transactions are still open, some caller must have
1637  // caught an exception but failed to properly rollback any changes. Detect that and
1638  // throw an error (causing rollback).
1639  $conn->assertNoOpenTransactions();
1640  // Assert that the time to replicate the transaction will be sane.
1641  // If this fails, then all DB transactions will be rollback back together.
1642  $time = $conn->pendingWriteQueryDuration( $conn::ESTIMATE_DB_APPLY );
1643  if ( $limit > 0 && $time > $limit ) {
1644  throw new DBTransactionSizeError(
1645  $conn,
1646  "Transaction spent $time second(s) in writes, exceeding the limit of $limit",
1647  [ $time, $limit ]
1648  );
1649  }
1650  // If a connection sits idle while slow queries execute on another, that connection
1651  // may end up dropped before the commit round is reached. Ping servers to detect this.
1652  if ( $conn->writesOrCallbacksPending() && !$conn->ping() ) {
1653  throw new DBTransactionError(
1654  $conn,
1655  "A connection to the {$conn->getDBname()} database was lost before commit"
1656  );
1657  }
1658  } );
1659  $this->trxRoundStage = self::ROUND_APPROVED;
1660  }
1661 
1662  public function beginMasterChanges( $fname = __METHOD__, $owner = null ) {
1663  $this->assertOwnership( $fname, $owner );
1664  if ( $this->trxRoundId !== false ) {
1665  throw new DBTransactionError(
1666  null,
1667  "$fname: Transaction round '{$this->trxRoundId}' already started"
1668  );
1669  }
1670  $this->assertTransactionRoundStage( self::ROUND_CURSORY );
1671  if ( $this->ownerId === null ) {
1673  $scope = ScopedCallback::newScopedIgnoreUserAbort();
1674  }
1675 
1676  // Clear any empty transactions (no writes/callbacks) from the implicit round
1677  $this->flushMasterSnapshots( $fname, $owner );
1678 
1679  $this->trxRoundId = $fname;
1680  $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
1681  // Mark applicable handles as participating in this explicit transaction round.
1682  // For each of these handles, any writes and callbacks will be tied to a single
1683  // transaction. The (peer) handles will reject begin()/commit() calls unless they
1684  // are part of an en masse commit or an en masse rollback.
1685  $this->forEachOpenMasterConnection( function ( Database $conn ) {
1686  $this->applyTransactionRoundFlags( $conn );
1687  } );
1688  $this->trxRoundStage = self::ROUND_CURSORY;
1689  }
1690 
1691  public function commitMasterChanges( $fname = __METHOD__, $owner = null ) {
1692  $this->assertOwnership( $fname, $owner );
1693  $this->assertTransactionRoundStage( self::ROUND_APPROVED );
1694  if ( $this->ownerId === null ) {
1696  $scope = ScopedCallback::newScopedIgnoreUserAbort();
1697  }
1698 
1699  $failures = [];
1700 
1701  $restore = ( $this->trxRoundId !== false );
1702  $this->trxRoundId = false;
1703  $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
1704  // Commit any writes and clear any snapshots as well (callbacks require AUTOCOMMIT).
1705  // Note that callbacks should already be suppressed due to finalizeMasterChanges().
1707  function ( IDatabase $conn ) use ( $fname, &$failures ) {
1708  try {
1709  $conn->commit( $fname, $conn::FLUSHING_ALL_PEERS );
1710  } catch ( DBError $e ) {
1711  ( $this->errorLogger )( $e );
1712  $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
1713  }
1714  }
1715  );
1716  if ( $failures ) {
1717  throw new DBTransactionError(
1718  null,
1719  "$fname: Commit failed on server(s) " . implode( "\n", array_unique( $failures ) )
1720  );
1721  }
1722  if ( $restore ) {
1723  // Unmark handles as participating in this explicit transaction round
1724  $this->forEachOpenMasterConnection( function ( Database $conn ) {
1725  $this->undoTransactionRoundFlags( $conn );
1726  } );
1727  }
1728  $this->trxRoundStage = self::ROUND_COMMIT_CALLBACKS;
1729  }
1730 
1731  public function runMasterTransactionIdleCallbacks( $fname = __METHOD__, $owner = null ) {
1732  $this->assertOwnership( $fname, $owner );
1733  if ( $this->trxRoundStage === self::ROUND_COMMIT_CALLBACKS ) {
1734  $type = IDatabase::TRIGGER_COMMIT;
1735  } elseif ( $this->trxRoundStage === self::ROUND_ROLLBACK_CALLBACKS ) {
1736  $type = IDatabase::TRIGGER_ROLLBACK;
1737  } else {
1738  throw new DBTransactionError(
1739  null,
1740  "Transaction should be in the callback stage (not '{$this->trxRoundStage}')"
1741  );
1742  }
1743  if ( $this->ownerId === null ) {
1745  $scope = ScopedCallback::newScopedIgnoreUserAbort();
1746  }
1747 
1748  $oldStage = $this->trxRoundStage;
1749  $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
1750 
1751  // Now that the COMMIT/ROLLBACK step is over, enable post-commit callback runs
1752  $this->forEachOpenMasterConnection( function ( Database $conn ) {
1753  $conn->setTrxEndCallbackSuppression( false );
1754  } );
1755 
1756  $e = null; // first exception
1757  $fname = __METHOD__;
1758  // Loop until callbacks stop adding callbacks on other connections
1759  do {
1760  // Run any pending callbacks for each connection...
1761  $count = 0; // callback execution attempts
1763  function ( Database $conn ) use ( $type, &$e, &$count ) {
1764  if ( $conn->trxLevel() ) {
1765  return; // retry in the next iteration, after commit() is called
1766  }
1767  try {
1768  $count += $conn->runOnTransactionIdleCallbacks( $type );
1769  } catch ( Throwable $ex ) {
1770  $e = $e ?: $ex;
1771  }
1772  }
1773  );
1774  // Clear out any active transactions left over from callbacks...
1775  $this->forEachOpenMasterConnection( function ( Database $conn ) use ( &$e, $fname ) {
1776  if ( $conn->writesPending() ) {
1777  // A callback from another handle wrote to this one and DBO_TRX is set
1778  $this->queryLogger->warning( $fname . ": found writes pending." );
1779  $fnames = implode( ', ', $conn->pendingWriteAndCallbackCallers() );
1780  $this->queryLogger->warning(
1781  "$fname: found writes pending ($fnames).",
1782  [
1783  'db_server' => $conn->getServer(),
1784  'db_name' => $conn->getDBname(),
1785  'exception' => new RuntimeException()
1786  ]
1787  );
1788  } elseif ( $conn->trxLevel() ) {
1789  // A callback from another handle read from this one and DBO_TRX is set,
1790  // which can easily happen if there is only one DB (no replicas)
1791  $this->queryLogger->debug( "$fname: found empty transaction." );
1792  }
1793  try {
1794  $conn->commit( $fname, $conn::FLUSHING_ALL_PEERS );
1795  } catch ( Throwable $ex ) {
1796  $e = $e ?: $ex;
1797  }
1798  } );
1799  } while ( $count > 0 );
1800 
1801  $this->trxRoundStage = $oldStage;
1802 
1803  return $e;
1804  }
1805 
1806  public function runMasterTransactionListenerCallbacks( $fname = __METHOD__, $owner = null ) {
1807  $this->assertOwnership( $fname, $owner );
1808  if ( $this->trxRoundStage === self::ROUND_COMMIT_CALLBACKS ) {
1809  $type = IDatabase::TRIGGER_COMMIT;
1810  } elseif ( $this->trxRoundStage === self::ROUND_ROLLBACK_CALLBACKS ) {
1811  $type = IDatabase::TRIGGER_ROLLBACK;
1812  } else {
1813  throw new DBTransactionError(
1814  null,
1815  "Transaction should be in the callback stage (not '{$this->trxRoundStage}')"
1816  );
1817  }
1818  if ( $this->ownerId === null ) {
1820  $scope = ScopedCallback::newScopedIgnoreUserAbort();
1821  }
1822 
1823  $e = null;
1824 
1825  $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
1826  $this->forEachOpenMasterConnection( function ( Database $conn ) use ( $type, &$e ) {
1827  try {
1829  } catch ( Throwable $ex ) {
1830  $e = $e ?: $ex;
1831  }
1832  } );
1833  $this->trxRoundStage = self::ROUND_CURSORY;
1834 
1835  return $e;
1836  }
1837 
1838  public function rollbackMasterChanges( $fname = __METHOD__, $owner = null ) {
1839  $this->assertOwnership( $fname, $owner );
1840  if ( $this->ownerId === null ) {
1842  $scope = ScopedCallback::newScopedIgnoreUserAbort();
1843  }
1844 
1845  $restore = ( $this->trxRoundId !== false );
1846  $this->trxRoundId = false;
1847  $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
1848  $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( $fname ) {
1849  $conn->rollback( $fname, $conn::FLUSHING_ALL_PEERS );
1850  } );
1851  if ( $restore ) {
1852  // Unmark handles as participating in this explicit transaction round
1853  $this->forEachOpenMasterConnection( function ( Database $conn ) {
1854  $this->undoTransactionRoundFlags( $conn );
1855  } );
1856  }
1857  $this->trxRoundStage = self::ROUND_ROLLBACK_CALLBACKS;
1858  }
1859 
1864  private function assertTransactionRoundStage( $stage ) {
1865  $stages = (array)$stage;
1866 
1867  if ( !in_array( $this->trxRoundStage, $stages, true ) ) {
1868  $stageList = implode(
1869  '/',
1870  array_map( function ( $v ) {
1871  return "'$v'";
1872  }, $stages )
1873  );
1874  throw new DBTransactionError(
1875  null,
1876  "Transaction round stage must be $stageList (not '{$this->trxRoundStage}')"
1877  );
1878  }
1879  }
1880 
1893  private function assertOwnership( $fname, $owner ) {
1894  if ( $this->ownerId !== null && $owner !== $this->ownerId && $owner !== $this->id ) {
1895  throw new DBTransactionError(
1896  null,
1897  "$fname: LoadBalancer is owned by ID '{$this->ownerId}' (got '$owner')."
1898  );
1899  }
1900  }
1901 
1911  private function applyTransactionRoundFlags( Database $conn ) {
1912  if ( $conn->getLBInfo( self::$INFO_AUTOCOMMIT_ONLY ) ) {
1913  return; // transaction rounds do not apply to these connections
1914  }
1915 
1916  if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) {
1917  // DBO_TRX is controlled entirely by CLI mode presence with DBO_DEFAULT.
1918  // Force DBO_TRX even in CLI mode since a commit round is expected soon.
1919  $conn->setFlag( $conn::DBO_TRX, $conn::REMEMBER_PRIOR );
1920  }
1921 
1922  if ( $conn->getFlag( $conn::DBO_TRX ) ) {
1923  $conn->setLBInfo( $conn::LB_TRX_ROUND_ID, $this->trxRoundId );
1924  }
1925  }
1926 
1930  private function undoTransactionRoundFlags( Database $conn ) {
1931  if ( $conn->getLBInfo( self::$INFO_AUTOCOMMIT_ONLY ) ) {
1932  return; // transaction rounds do not apply to these connections
1933  }
1934 
1935  if ( $conn->getFlag( $conn::DBO_TRX ) ) {
1936  $conn->setLBInfo( $conn::LB_TRX_ROUND_ID, null ); // remove the round ID
1937  }
1938 
1939  if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) {
1940  $conn->restoreFlags( $conn::RESTORE_PRIOR );
1941  }
1942  }
1943 
1944  public function flushReplicaSnapshots( $fname = __METHOD__, $owner = null ) {
1945  $this->assertOwnership( $fname, $owner );
1946  $this->forEachOpenReplicaConnection( function ( IDatabase $conn ) use ( $fname ) {
1947  $conn->flushSnapshot( $fname );
1948  } );
1949  }
1950 
1951  public function flushMasterSnapshots( $fname = __METHOD__, $owner = null ) {
1952  $this->assertOwnership( $fname, $owner );
1953  $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( $fname ) {
1954  $conn->flushSnapshot( $fname );
1955  } );
1956  }
1957 
1962  public function getTransactionRoundStage() {
1963  return $this->trxRoundStage;
1964  }
1965 
1966  public function hasMasterConnection() {
1967  return $this->isOpen( $this->getWriterIndex() );
1968  }
1969 
1970  public function hasMasterChanges() {
1971  $pending = false;
1972  $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$pending ) {
1973  $pending = $pending || $conn->writesOrCallbacksPending();
1974  } );
1975 
1976  return $pending;
1977  }
1978 
1979  public function lastMasterChangeTimestamp() {
1980  $lastTime = false;
1981  $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$lastTime ) {
1982  $lastTime = max( $lastTime, $conn->lastDoneWrites() );
1983  } );
1984 
1985  return $lastTime;
1986  }
1987 
1988  public function hasOrMadeRecentMasterChanges( $age = null ) {
1989  $age = ( $age === null ) ? $this->waitTimeout : $age;
1990 
1991  return ( $this->hasMasterChanges()
1992  || $this->lastMasterChangeTimestamp() > microtime( true ) - $age );
1993  }
1994 
1995  public function pendingMasterChangeCallers() {
1996  $fnames = [];
1997  $this->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$fnames ) {
1998  $fnames = array_merge( $fnames, $conn->pendingWriteCallers() );
1999  } );
2000 
2001  return $fnames;
2002  }
2003 
2004  public function getLaggedReplicaMode( $domain = false ) {
2005  if ( $this->laggedReplicaMode ) {
2006  return true; // stay in lagged replica mode
2007  }
2008 
2009  if ( $this->hasStreamingReplicaServers() ) {
2010  // This will set "laggedReplicaMode" as needed
2011  $this->getReaderIndex( self::GROUP_GENERIC, $domain );
2012  }
2013 
2014  return $this->laggedReplicaMode;
2015  }
2016 
2017  public function laggedReplicaUsed() {
2018  return $this->laggedReplicaMode;
2019  }
2020 
2021  public function getReadOnlyReason( $domain = false ) {
2022  $domainInstance = DatabaseDomain::newFromId( $this->resolveDomainID( $domain ) );
2023 
2024  if ( $this->readOnlyReason !== false ) {
2025  return $this->readOnlyReason;
2026  } elseif ( $this->isMasterRunningReadOnly( $domainInstance ) ) {
2027  return 'The master database server is running in read-only mode.';
2028  } elseif ( $this->getLaggedReplicaMode( $domain ) ) {
2029  return ( $this->getExistingReaderIndex( self::GROUP_GENERIC ) >= 0 )
2030  ? 'The database is read-only until replication lag decreases.'
2031  : 'The database is read-only until a replica database server becomes reachable.';
2032  }
2033 
2034  return false;
2035  }
2036 
2042  private function isMasterConnectionReadOnly( IDatabase $conn, $flags = 0 ) {
2043  // Note that table prefixes are not related to server-side read-only mode
2044  $key = $this->srvCache->makeGlobalKey(
2045  'rdbms-server-readonly',
2046  $conn->getServer(),
2047  $conn->getDBname(),
2048  $conn->dbSchema()
2049  );
2050 
2051  if ( ( $flags & self::CONN_REFRESH_READ_ONLY ) == self::CONN_REFRESH_READ_ONLY ) {
2052  try {
2053  $readOnly = (int)$conn->serverIsReadOnly();
2054  } catch ( DBError $e ) {
2055  $readOnly = 0;
2056  }
2057  $this->srvCache->set( $key, $readOnly, BagOStuff::TTL_PROC_SHORT );
2058  } else {
2059  $readOnly = $this->srvCache->getWithSetCallback(
2060  $key,
2061  BagOStuff::TTL_PROC_SHORT,
2062  function () use ( $conn ) {
2063  try {
2064  return (int)$conn->serverIsReadOnly();
2065  } catch ( DBError $e ) {
2066  return 0;
2067  }
2068  }
2069  );
2070  }
2071 
2072  return (bool)$readOnly;
2073  }
2074 
2079  private function isMasterRunningReadOnly( DatabaseDomain $domain ) {
2080  // Context will often be HTTP GET/HEAD; heavily cache the results
2081  return (bool)$this->wanCache->getWithSetCallback(
2082  // Note that table prefixes are not related to server-side read-only mode
2083  $this->wanCache->makeGlobalKey(
2084  'rdbms-server-readonly',
2085  $this->getMasterServerName(),
2086  $domain->getDatabase(),
2087  $domain->getSchema()
2088  ),
2089  self::TTL_CACHE_READONLY,
2090  function () use ( $domain ) {
2091  $old = $this->trxProfiler->setSilenced( true );
2092  try {
2093  $index = $this->getWriterIndex();
2094  // Reset the cache for isMasterConnectionReadOnly()
2095  $flags = self::CONN_REFRESH_READ_ONLY;
2096  $conn = $this->getServerConnection( $index, $domain->getId(), $flags );
2097  // Reuse the process cache set above
2098  $readOnly = (int)$this->isMasterConnectionReadOnly( $conn );
2099  $this->reuseConnection( $conn );
2100  } catch ( DBError $e ) {
2101  $readOnly = 0;
2102  }
2103  $this->trxProfiler->setSilenced( $old );
2104 
2105  return $readOnly;
2106  },
2107  [ 'pcTTL' => WANObjectCache::TTL_PROC_LONG, 'lockTSE' => 10, 'busyValue' => 0 ]
2108  );
2109  }
2110 
2111  public function allowLagged( $mode = null ) {
2112  if ( $mode === null ) {
2113  return $this->allowLagged;
2114  }
2115  $this->allowLagged = $mode;
2116 
2117  return $this->allowLagged;
2118  }
2119 
2120  public function pingAll() {
2121  $success = true;
2122  $this->forEachOpenConnection( function ( IDatabase $conn ) use ( &$success ) {
2123  if ( !$conn->ping() ) {
2124  $success = false;
2125  }
2126  } );
2127 
2128  return $success;
2129  }
2130 
2131  public function forEachOpenConnection( $callback, array $params = [] ) {
2132  foreach ( $this->conns as $connsByServer ) {
2133  foreach ( $connsByServer as $serverConns ) {
2134  foreach ( $serverConns as $conn ) {
2135  $callback( $conn, ...$params );
2136  }
2137  }
2138  }
2139  }
2140 
2141  public function forEachOpenMasterConnection( $callback, array $params = [] ) {
2142  $masterIndex = $this->getWriterIndex();
2143  foreach ( $this->conns as $connsByServer ) {
2144  if ( isset( $connsByServer[$masterIndex] ) ) {
2146  foreach ( $connsByServer[$masterIndex] as $conn ) {
2147  $callback( $conn, ...$params );
2148  }
2149  }
2150  }
2151  }
2152 
2153  public function forEachOpenReplicaConnection( $callback, array $params = [] ) {
2154  foreach ( $this->conns as $connsByServer ) {
2155  foreach ( $connsByServer as $i => $serverConns ) {
2156  if ( $i === $this->getWriterIndex() ) {
2157  continue; // skip master
2158  }
2159  foreach ( $serverConns as $conn ) {
2160  $callback( $conn, ...$params );
2161  }
2162  }
2163  }
2164  }
2165 
2169  private function getCurrentConnectionCount() {
2170  $count = 0;
2171  foreach ( $this->conns as $connsByServer ) {
2172  foreach ( $connsByServer as $serverConns ) {
2173  $count += count( $serverConns );
2174  }
2175  }
2176 
2177  return $count;
2178  }
2179 
2180  public function getMaxLag( $domain = false ) {
2181  $host = '';
2182  $maxLag = -1;
2183  $maxIndex = 0;
2184 
2185  if ( $this->hasReplicaServers() ) {
2186  $lagTimes = $this->getLagTimes( $domain );
2187  foreach ( $lagTimes as $i => $lag ) {
2188  if ( $this->groupLoads[self::GROUP_GENERIC][$i] > 0 && $lag > $maxLag ) {
2189  $maxLag = $lag;
2190  $host = $this->getServerInfoStrict( $i, 'host' );
2191  $maxIndex = $i;
2192  }
2193  }
2194  }
2195 
2196  return [ $host, $maxLag, $maxIndex ];
2197  }
2198 
2199  public function getLagTimes( $domain = false ) {
2200  if ( !$this->hasReplicaServers() ) {
2201  return [ $this->getWriterIndex() => 0 ]; // no replication = no lag
2202  }
2203 
2204  $knownLagTimes = []; // map of (server index => 0 seconds)
2205  $indexesWithLag = [];
2206  foreach ( $this->servers as $i => $server ) {
2207  if ( empty( $server['is static'] ) ) {
2208  $indexesWithLag[] = $i; // DB server might have replication lag
2209  } else {
2210  $knownLagTimes[$i] = 0; // DB server is a non-replicating and read-only archive
2211  }
2212  }
2213 
2214  return $this->getLoadMonitor()->getLagTimes( $indexesWithLag, $domain ) + $knownLagTimes;
2215  }
2216 
2232  public function safeGetLag( IDatabase $conn ) {
2233  return $conn->getLag();
2234  }
2235 
2236  public function waitForMasterPos( IDatabase $conn, $pos = false, $timeout = null ) {
2237  $timeout = max( 1, $timeout ?: $this->waitTimeout );
2238 
2239  if ( $conn->getLBInfo( self::$INFO_SERVER_INDEX ) === $this->getWriterIndex() ) {
2240  return true; // not a replica DB server
2241  }
2242 
2243  if ( !$pos ) {
2244  // Get the current master position, opening a connection if needed
2245  $index = $this->getWriterIndex();
2246  $flags = self::CONN_SILENCE_ERRORS;
2247  $masterConn = $this->getAnyOpenConnection( $index, $flags );
2248  if ( $masterConn ) {
2249  $pos = $masterConn->getMasterPos();
2250  } else {
2251  $masterConn = $this->getServerConnection( $index, self::DOMAIN_ANY, $flags );
2252  if ( !$masterConn ) {
2253  throw new DBReplicationWaitError(
2254  null,
2255  "Could not obtain a master database connection to get the position"
2256  );
2257  }
2258  $pos = $masterConn->getMasterPos();
2259  $this->closeConnection( $masterConn );
2260  }
2261  }
2262 
2263  if ( $pos instanceof DBMasterPos ) {
2264  $start = microtime( true );
2265  $result = $conn->masterPosWait( $pos, $timeout );
2266  $seconds = max( microtime( true ) - $start, 0 );
2267 
2268  $ok = ( $result !== null && $result != -1 );
2269  if ( $ok ) {
2270  $this->replLogger->warning(
2271  __METHOD__ . ': timed out waiting on {dbserver} pos {pos} [{seconds}s]',
2272  [
2273  'dbserver' => $conn->getServer(),
2274  'pos' => $pos,
2275  'seconds' => round( $seconds, 6 ),
2276  'exception' => new RuntimeException(),
2277  ]
2278  );
2279  } else {
2280  $this->replLogger->debug( __METHOD__ . ': done waiting' );
2281  }
2282  } else {
2283  $ok = false; // something is misconfigured
2284  $this->replLogger->error(
2285  __METHOD__ . ': could not get master pos for {dbserver}',
2286  [
2287  'dbserver' => $conn->getServer(),
2288  'exception' => new RuntimeException(),
2289  ]
2290  );
2291  }
2292 
2293  return $ok;
2294  }
2295 
2308  public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = null ) {
2309  return $this->waitForMasterPos( $conn, $pos, $timeout );
2310  }
2311 
2312  public function setTransactionListener( $name, callable $callback = null ) {
2313  if ( $callback ) {
2314  $this->trxRecurringCallbacks[$name] = $callback;
2315  } else {
2316  unset( $this->trxRecurringCallbacks[$name] );
2317  }
2319  function ( IDatabase $conn ) use ( $name, $callback ) {
2320  $conn->setTransactionListener( $name, $callback );
2321  }
2322  );
2323  }
2324 
2325  public function setTableAliases( array $aliases ) {
2326  $this->tableAliases = $aliases;
2327  }
2328 
2329  public function setIndexAliases( array $aliases ) {
2330  $this->indexAliases = $aliases;
2331  }
2332 
2333  public function setDomainAliases( array $aliases ) {
2334  $this->domainAliases = $aliases;
2335  }
2336 
2337  public function setLocalDomainPrefix( $prefix ) {
2338  // Find connections to explicit foreign domains still marked as in-use...
2339  $domainsInUse = [];
2340  $this->forEachOpenConnection( function ( IDatabase $conn ) use ( &$domainsInUse ) {
2341  // Once reuseConnection() is called on a handle, its reference count goes from 1 to 0.
2342  // Until then, it is still in use by the caller (explicitly or via DBConnRef scope).
2343  if ( $conn->getLBInfo( self::$INFO_FOREIGN_REF_COUNT ) > 0 ) {
2344  $domainsInUse[] = $conn->getDomainID();
2345  }
2346  } );
2347 
2348  // Do not switch connections to explicit foreign domains unless marked as safe
2349  if ( $domainsInUse ) {
2350  $domains = implode( ', ', $domainsInUse );
2351  throw new DBUnexpectedError( null,
2352  "Foreign domain connections are still in use ($domains)" );
2353  }
2354 
2355  $this->setLocalDomain( new DatabaseDomain(
2356  $this->localDomain->getDatabase(),
2357  $this->localDomain->getSchema(),
2358  $prefix
2359  ) );
2360 
2361  // Update the prefix for all local connections...
2362  $this->forEachOpenConnection( function ( IDatabase $conn ) use ( $prefix ) {
2363  if ( !$conn->getLBInfo( self::$INFO_FORIEGN ) ) {
2364  $conn->tablePrefix( $prefix );
2365  }
2366  } );
2367  }
2368 
2369  public function redefineLocalDomain( $domain ) {
2370  $this->closeAll( __METHOD__, $this->id );
2371 
2372  $this->setLocalDomain( DatabaseDomain::newFromId( $domain ) );
2373  }
2374 
2375  public function setTempTablesOnlyMode( $value, $domain ) {
2376  $old = $this->tempTablesOnlyMode[$domain] ?? false;
2377  if ( $value ) {
2378  $this->tempTablesOnlyMode[$domain] = true;
2379  } else {
2380  unset( $this->tempTablesOnlyMode[$domain] );
2381  }
2382 
2383  return $old;
2384  }
2385 
2389  private function setLocalDomain( DatabaseDomain $domain ) {
2390  $this->localDomain = $domain;
2391  }
2392 
2399  private function getServerInfoStrict( $i, $field = null ) {
2400  if ( !isset( $this->servers[$i] ) || !is_array( $this->servers[$i] ) ) {
2401  throw new InvalidArgumentException( "No server with index '$i'" );
2402  }
2403 
2404  if ( $field !== null ) {
2405  if ( !array_key_exists( $field, $this->servers[$i] ) ) {
2406  throw new InvalidArgumentException( "No field '$field' in server index '$i'" );
2407  }
2408 
2409  return $this->servers[$i][$field];
2410  }
2411 
2412  return $this->servers[$i];
2413  }
2414 
2418  private function getMasterServerName() {
2419  return $this->getServerName( $this->getWriterIndex() );
2420  }
2421 
2422  public function __destruct() {
2423  // Avoid connection leaks for sanity
2424  $this->disable( __METHOD__, $this->ownerId );
2425  }
2426 }
2427 
2431 class_alias( LoadBalancer::class, 'LoadBalancer' );
Wikimedia\Rdbms\LoadBalancer\$allowLagged
bool $allowLagged
Whether to disregard replica DB lag as a factor in replica DB selection.
Definition: LoadBalancer.php:120
Wikimedia\Rdbms\LoadBalancer\getServerType
getServerType( $i)
Get DB type of the server with the specified index.
Definition: LoadBalancer.php:1487
Wikimedia\Rdbms\LoadBalancer\getExistingReaderIndex
getExistingReaderIndex( $group)
Get the server index chosen by the load balancer for use with the given query group.
Definition: LoadBalancer.php:559
Wikimedia\Rdbms\LoadBalancer\setWaitForPositionIfHigher
setWaitForPositionIfHigher( $pos)
Definition: LoadBalancer.php:744
Wikimedia\Rdbms\LoadBalancer\waitForMasterPos
waitForMasterPos(IDatabase $conn, $pos=false, $timeout=null)
Wait for a replica DB to reach a specified master position.
Definition: LoadBalancer.php:2236
Wikimedia\Rdbms\DBTransactionSizeError
@newable
Definition: DBTransactionSizeError.php:28
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:50
Wikimedia\Rdbms\LoadBalancer\getServerInfo
getServerInfo( $i)
Return the server info structure for a given index or false if the index is invalid.
Definition: LoadBalancer.php:1483
Wikimedia\Rdbms\LoadBalancer\newTrackedConnectionsArray
static newTrackedConnectionsArray()
Definition: LoadBalancer.php:255
Wikimedia\Rdbms\LoadBalancer\$trxProfiler
TransactionProfiler $trxProfiler
Definition: LoadBalancer.php:54
Wikimedia\Rdbms\LoadBalancer\isOpen
isOpen( $index)
Test if the specified index represents an open connection.
Definition: LoadBalancer.php:1251
Wikimedia\Rdbms\LoadBalancer\$trxRoundId
string bool $trxRoundId
Explicit DBO_TRX transaction round active or false if none.
Definition: LoadBalancer.php:110
Wikimedia\Rdbms\IDatabase\getServer
getServer()
Get the server hostname or IP address.
Wikimedia\Rdbms\LoadBalancer\hasOrMadeRecentMasterChanges
hasOrMadeRecentMasterChanges( $age=null)
Check if this load balancer object had any recent or still pending writes issued against it by this P...
Definition: LoadBalancer.php:1988
Wikimedia\Rdbms\LoadBalancer\assertOwnership
assertOwnership( $fname, $owner)
Assure that if this instance is owned, the caller is either the owner or is internal.
Definition: LoadBalancer.php:1893
Wikimedia\Rdbms\DatabaseDomain\getId
getId()
Definition: DatabaseDomain.php:196
Wikimedia\Rdbms\IDatabase\pendingWriteQueryDuration
pendingWriteQueryDuration( $type=self::ESTIMATE_TOTAL)
Get the time spend running write queries for this transaction.
Wikimedia\Rdbms\DatabaseDomain\newFromId
static newFromId( $domain)
Definition: DatabaseDomain.php:77
Wikimedia\Rdbms\LoadBalancer\getWriterIndex
getWriterIndex()
Get the server index of the master server.
Definition: LoadBalancer.php:1433
EmptyBagOStuff
A BagOStuff object with no objects in it.
Definition: EmptyBagOStuff.php:29
Wikimedia\Rdbms\LoadBalancer\serverHasLoadInAnyGroup
serverHasLoadInAnyGroup( $i)
Definition: LoadBalancer.php:731
Wikimedia\Rdbms\IDatabase\rollback
rollback( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Rollback a transaction previously started using begin()
Wikimedia\Rdbms\DBAccessError
Exception class for attempted DB access @newable.
Definition: DBAccessError.php:29
Wikimedia\Rdbms\LoadBalancer\lazyLoadReplicationPositions
lazyLoadReplicationPositions()
Make sure that any "waitForPos" positions are loaded and available to doWait()
Definition: LoadBalancer.php:1394
Wikimedia\Rdbms\IDatabase\setLBInfo
setLBInfo( $nameOrArray, $value=null)
Set the entire array or a particular key of the managing load balancer info array.
Wikimedia\Rdbms\LoadBalancer\$waitTimeout
int $waitTimeout
Seconds to spend waiting on replica DB lag to resolve.
Definition: LoadBalancer.php:83
Wikimedia\Rdbms\LoadBalancer\getConnectionIndex
getConnectionIndex( $i, array $groups, $domain)
Get the server index to use for a specified server index and query group list.
Definition: LoadBalancer.php:474
Wikimedia\Rdbms\ILoadMonitor
An interface for database load monitoring.
Definition: ILoadMonitor.php:35
Wikimedia\Rdbms\LoadBalancer\getCurrentConnectionCount
getCurrentConnectionCount()
Definition: LoadBalancer.php:2169
Wikimedia\Rdbms\LoadBalancer\getMasterPos
getMasterPos()
Get the current master replication position.
Definition: LoadBalancer.php:1491
Wikimedia\Rdbms\IDatabase\ping
ping(&$rtt=null)
Ping the server and try to reconnect if it there is no connection.
Wikimedia\Rdbms\Database\factory
static factory( $type, $params=[], $connect=self::NEW_CONNECTED)
Construct a Database subclass instance given a database type and parameters.
Definition: Database.php:396
Wikimedia\Rdbms\LoadBalancer\safeGetLag
safeGetLag(IDatabase $conn)
Get the lag in seconds for a given connection, or zero if this load balancer does not have replicatio...
Definition: LoadBalancer.php:2232
Wikimedia\Rdbms\DBReplicationWaitError
Exception class for replica DB wait errors @newable.
Definition: DBReplicationWaitError.php:29
Wikimedia\Rdbms\LoadBalancer\getServerInfoStrict
getServerInfoStrict( $i, $field=null)
Definition: LoadBalancer.php:2399
Wikimedia\Rdbms\DatabaseDomain\getTablePrefix
getTablePrefix()
Definition: DatabaseDomain.php:189
Wikimedia\Rdbms\LoadBalancer\approveMasterChanges
approveMasterChanges(array $options, $fname=__METHOD__, $owner=null)
Perform all pre-commit checks for things like replication safety.
Definition: LoadBalancer.php:1624
Wikimedia\Rdbms\LoadBalancer\$domainAliases
DatabaseDomain[] string[] $domainAliases
Map of (domain alias => DB domain)
Definition: LoadBalancer.php:103
Wikimedia\Rdbms\IDatabase\trxLevel
trxLevel()
Gets the current transaction level.
Wikimedia\Rdbms\LoadBalancer\setLocalDomainPrefix
setLocalDomainPrefix( $prefix)
Set a new table prefix for the existing local domain ID for testing.
Definition: LoadBalancer.php:2337
Wikimedia\Rdbms\LoadBalancer\$nonLocalDomainCache
DatabaseDomain[] $nonLocalDomainCache
Map of (domain ID => domain instance)
Definition: LoadBalancer.php:140
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\LoadBalancer\$loadMonitorConfig
array $loadMonitorConfig
The LoadMonitor configuration.
Definition: LoadBalancer.php:85
Wikimedia\Rdbms\LoadBalancer\getServerName
getServerName( $i)
Get the host name or IP address of the server with the specified index.
Definition: LoadBalancer.php:1477
Wikimedia\Rdbms\DBMasterPos
An object representing a master or replica DB position in a replicated setup.
Definition: DBMasterPos.php:14
Wikimedia\Rdbms\LoadBalancer\initConnFlags
initConnFlags( $flags)
Definition: LoadBalancer.php:1379
Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection
forEachOpenMasterConnection( $callback, array $params=[])
Call a function with each open connection object to a master.
Definition: LoadBalancer.php:2141
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:71
Wikimedia\Rdbms\LoadBalancer\getConnectionRef
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle reference for a real or virtual (DB_MASTER/DB_REPLICA) server index.
Definition: LoadBalancer.php:1026
Wikimedia\Rdbms\LoadBalancer\$maxLag
int $maxLag
Definition: LoadBalancer.php:87
Wikimedia\Rdbms\LoadBalancer\$conns
IDatabase[][][] Database[][][] $conns
Map of (connection category => server index => IDatabase[])
Definition: LoadBalancer.php:74
Wikimedia\Rdbms\LoadBalancer\sanitizeConnectionFlags
sanitizeConnectionFlags( $flags, $i, $domain)
Definition: LoadBalancer.php:334
Wikimedia\Rdbms\LoadBalancer\getConnection
getConnection( $i, $groups=[], $domain=false, $flags=0)
Get a live handle for a real or virtual (DB_MASTER/DB_REPLICA) server index.
Definition: LoadBalancer.php:889
Wikimedia\Rdbms\DatabaseDomain\getDatabase
getDatabase()
Definition: DatabaseDomain.php:175
Wikimedia\Rdbms\LoadBalancer\enforceConnectionFlags
enforceConnectionFlags(IDatabase $conn, $flags)
Definition: LoadBalancer.php:372
Wikimedia\Rdbms\LoadBalancer\setExistingReaderIndex
setExistingReaderIndex( $group, $index)
Set the server index chosen by the load balancer for use with the given query group.
Definition: LoadBalancer.php:569
Wikimedia\Rdbms\LoadBalancer\getLagTimes
getLagTimes( $domain=false)
Get an estimate of replication lag (in seconds) for each server.
Definition: LoadBalancer.php:2199
$success
$success
Definition: NoLocalSettings.php:42
Wikimedia\Rdbms\IDatabase\getLag
getLag()
Get the amount of replication lag for this database server.
Wikimedia\Rdbms\LoadBalancer\$perfLogger
LoggerInterface $perfLogger
Definition: LoadBalancer.php:62
Wikimedia\Rdbms\LoadBalancer\$ownerId
int null $ownerId
Integer ID of the managing LBFactory instance or null if none.
Definition: LoadBalancer.php:137
Wikimedia\Rdbms\LoadBalancer\KEY_FOREIGN_INUSE
const KEY_FOREIGN_INUSE
Definition: LoadBalancer.php:159
Wikimedia\Rdbms\LoadBalancer\finalizeMasterChanges
finalizeMasterChanges( $fname=__METHOD__, $owner=null)
Run pre-commit callbacks and defer execution of post-commit callbacks.
Definition: LoadBalancer.php:1595
Wikimedia\Rdbms\LoadBalancer\$replLogger
LoggerInterface $replLogger
Definition: LoadBalancer.php:60
Wikimedia\Rdbms\LoadBalancer\doWait
doWait( $index, $open=false, $timeout=null)
Wait for a given replica DB to catch up to the master pos stored in "waitForPos".
Definition: LoadBalancer.php:820
Wikimedia\Rdbms\Database\getFlag
getFlag( $flag)
Returns a boolean whether the flag $flag is set for this connection.
Definition: Database.php:805
Wikimedia\Rdbms\LoadBalancer\flushMasterSnapshots
flushMasterSnapshots( $fname=__METHOD__, $owner=null)
Commit all master DB transactions so as to flush any REPEATABLE-READ or SSI snapshots.
Definition: LoadBalancer.php:1951
Wikimedia\Rdbms\DBError
Database error base class @newable Stable to extend.
Definition: DBError.php:32
Wikimedia\Rdbms\LoadBalancer\hasMasterChanges
hasMasterChanges()
Whether there are pending changes or callbacks in a transaction by this thread.
Definition: LoadBalancer.php:1970
Wikimedia\Rdbms\LoadBalancer\getServerAttributes
getServerAttributes( $i)
Definition: LoadBalancer.php:1238
Wikimedia\Rdbms\LoadBalancer\resolveDomainID
resolveDomainID( $domain)
Definition: LoadBalancer.php:272
DBO_TRX
const DBO_TRX
Definition: defines.php:12
Wikimedia\Rdbms\LoadBalancer\KEY_FOREIGN_INUSE_NOROUND
const KEY_FOREIGN_INUSE_NOROUND
Definition: LoadBalancer.php:163
Wikimedia\Rdbms\IDatabase\dbSchema
dbSchema( $schema=null)
Get/set the db schema.
Wikimedia\Rdbms\LoadBalancer\commitMasterChanges
commitMasterChanges( $fname=__METHOD__, $owner=null)
Issue COMMIT on all open master connections to flush changes and view snapshots.
Definition: LoadBalancer.php:1691
Wikimedia\Rdbms\LoadBalancer\getMaxLag
getMaxLag( $domain=false)
Get the hostname and lag time of the most-lagged replica server.
Definition: LoadBalancer.php:2180
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Wikimedia\Rdbms\IDatabase\lastDoneWrites
lastDoneWrites()
Get the last time the connection may have been used for a write query.
Wikimedia\Rdbms\LoadBalancer\$INFO_SERVER_INDEX
static $INFO_SERVER_INDEX
Definition: LoadBalancer.php:142
Wikimedia\Rdbms\LoadBalancer\$deprecationLogger
callable $deprecationLogger
Deprecation logger.
Definition: LoadBalancer.php:66
Wikimedia\Rdbms\DatabaseDomain\newUnspecified
static newUnspecified()
Definition: DatabaseDomain.php:114
Wikimedia\Rdbms\LoadBalancer\$connectionAttempted
bool $connectionAttempted
Whether any connection has been attempted yet.
Definition: LoadBalancer.php:132
Wikimedia\Rdbms\LoadBalancer\allowLagged
allowLagged( $mode=null)
Disables/enables lag checks.
Definition: LoadBalancer.php:2111
WANObjectCache\newEmpty
static newEmpty()
Get an instance that wraps EmptyBagOStuff.
Definition: WANObjectCache.php:348
Wikimedia\Rdbms\MaintainableDBConnRef
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition: MaintainableDBConnRef.php:13
Wikimedia\Rdbms\LoadBalancer\setLocalDomain
setLocalDomain(DatabaseDomain $domain)
Definition: LoadBalancer.php:2389
Wikimedia\Rdbms\LoadBalancer\getReaderIndex
getReaderIndex( $group=false, $domain=false)
Get the server index of the reader connection for a given group.
Definition: LoadBalancer.php:499
Wikimedia\Rdbms\LoadBalancer\KEY_LOCAL_NOROUND
const KEY_LOCAL_NOROUND
Definition: LoadBalancer.php:161
Wikimedia\Rdbms\Database\runOnTransactionPreCommitCallbacks
runOnTransactionPreCommitCallbacks()
Actually consume and run any "on transaction pre-commit" callbacks.
Definition: Database.php:4142
Wikimedia\Rdbms\LoadBalancer\KEY_LOCAL
const KEY_LOCAL
Definition: LoadBalancer.php:157
Wikimedia\Rdbms\LoadBalancer\$tempTablesOnlyMode
bool[] $tempTablesOnlyMode
Map of (domain => whether to use "temp tables only" mode)
Definition: LoadBalancer.php:107
Wikimedia\Rdbms\LoadBalancer\$groupLoads
array[] $groupLoads
Map of (group => server index => weight)
Definition: LoadBalancer.php:79
Wikimedia\Rdbms\LoadBalancer\KEY_FOREIGN_FREE
const KEY_FOREIGN_FREE
Definition: LoadBalancer.php:158
Wikimedia\Rdbms\LoadBalancer\$id
$id
var int An identifier for this class instance
Definition: LoadBalancer.php:135
Wikimedia\Rdbms\LoadBalancer\getRandomNonLagged
getRandomNonLagged(array $loads, $domain=false, $maxLag=INF)
Definition: LoadBalancer.php:417
Wikimedia\Rdbms\LoadBalancer\$maxLagByIndex
int[] $maxLagByIndex
Map of (server index => seconds of lag considered "high")
Definition: LoadBalancer.php:81
Wikimedia\Rdbms\LoadBalancer\pickReaderIndex
pickReaderIndex(array $loads, $domain=false)
Definition: LoadBalancer.php:581
Wikimedia\Rdbms\LoadBalancer\pendingMasterChangeCallers
pendingMasterChangeCallers()
Get the list of callers that have pending master changes.
Definition: LoadBalancer.php:1995
Wikimedia\Rdbms\LoadBalancer\flushReplicaSnapshots
flushReplicaSnapshots( $fname=__METHOD__, $owner=null)
Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshots.
Definition: LoadBalancer.php:1944
Wikimedia\Rdbms\LoadBalancer\closeAll
closeAll( $fname=__METHOD__, $owner=null)
Close all open connections.
Definition: LoadBalancer.php:1548
Wikimedia\Rdbms\Database\trxLevel
trxLevel()
Gets the current transaction level.
Definition: Database.php:548
Wikimedia\Rdbms\LoadBalancer\getMasterServerName
getMasterServerName()
Definition: LoadBalancer.php:2418
Wikimedia\Rdbms\Database\setFlag
setFlag( $flag, $remember=self::REMEMBER_NOTHING)
Set a flag for this connection.
Definition: Database.php:762
Wikimedia\Rdbms\LoadBalancer\setTempTablesOnlyMode
setTempTablesOnlyMode( $value, $domain)
Indicate whether the tables on this domain are only temporary tables for testing.
Definition: LoadBalancer.php:2375
Wikimedia\Rdbms\LoadBalancer\__destruct
__destruct()
Definition: LoadBalancer.php:2422
Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection
reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo)
Open a new network connection to a server (uncached)
Definition: LoadBalancer.php:1267
Wikimedia\Rdbms\LoadBalancer\hasMasterConnection
hasMasterConnection()
Definition: LoadBalancer.php:1966
Wikimedia\Rdbms\LoadBalancer\disable
disable( $fname=__METHOD__, $owner=null)
Close all connections and disable this load balancer.
Definition: LoadBalancer.php:1542
Wikimedia\Rdbms\LoadBalancer\applyTransactionRoundFlags
applyTransactionRoundFlags(Database $conn)
Make all DB servers with DBO_DEFAULT/DBO_TRX set join the transaction round.
Definition: LoadBalancer.php:1911
Wikimedia\Rdbms\LoadBalancer\forEachOpenConnection
forEachOpenConnection( $callback, array $params=[])
Call a function with each open connection object.
Definition: LoadBalancer.php:2131
Wikimedia\Rdbms\LoadBalancer\$srvCache
BagOStuff $srvCache
Definition: LoadBalancer.php:48
Wikimedia\Rdbms\LoadBalancer\isNonZeroLoad
isNonZeroLoad( $i)
Returns true if the specified index is valid and has non-zero load.
Definition: LoadBalancer.php:1455
Wikimedia\Rdbms\LoadBalancer\__construct
__construct(array $params)
Construct a manager of IDatabase connection objects.
Definition: LoadBalancer.php:178
Wikimedia\Rdbms\IDatabase\close
close( $fname=__METHOD__, $owner=null)
Close the database connection.
Wikimedia\Rdbms\LoadBalancer\waitForOne
waitForOne( $pos, $timeout=null)
Set the master wait position and wait for a generic replica DB to catch up to it.
Definition: LoadBalancer.php:675
Wikimedia\Rdbms\LoadBalancer\$hostname
string $hostname
Current server name.
Definition: LoadBalancer.php:92
Wikimedia\Rdbms\LoadBalancer\$errorLogger
callable $errorLogger
Exception logger.
Definition: LoadBalancer.php:64
Wikimedia\Rdbms\LoadBalancer\pingAll
pingAll()
Definition: LoadBalancer.php:2120
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Wikimedia\Rdbms\LoadBalancer\$lastError
string $lastError
The last DB selection or connection error.
Definition: LoadBalancer.php:124
Wikimedia\Rdbms\LoadBalancer\forEachOpenReplicaConnection
forEachOpenReplicaConnection( $callback, array $params=[])
Call a function with each open replica DB connection object.
Definition: LoadBalancer.php:2153
Wikimedia\Rdbms\Database\restoreFlags
restoreFlags( $state=self::RESTORE_PRIOR)
Restore the flags to their prior state before the last setFlag/clearFlag call.
Definition: Database.php:792
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Wikimedia\Rdbms\Database\setLBInfo
setLBInfo( $nameOrArray, $value=null)
Set the entire array or a particular key of the managing load balancer info array.
Definition: Database.php:615
Wikimedia\Rdbms\Database\getLBInfo
getLBInfo( $name=null)
Get properties passed down from the server info array of the load balancer.
Definition: Database.php:603
Wikimedia\Rdbms\IDatabase\assertNoOpenTransactions
assertNoOpenTransactions()
Assert that all explicit transactions or atomic sections have been closed.
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:42
Wikimedia\Rdbms\LoadBalancer\resolveGroups
resolveGroups( $groups, $i)
Resolve $groups into a list of query groups defining as having database servers.
Definition: LoadBalancer.php:308
Wikimedia\Rdbms\LoadBalancer\getMaintenanceConnectionRef
getMaintenanceConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle for a real or virtual (DB_MASTER/DB_REPLICA) server index that can be used...
Definition: LoadBalancer.php:1040
Wikimedia\Rdbms\LoadBalancer\redefineLocalDomain
redefineLocalDomain( $domain)
Close all connection and redefine the local domain for testing or schema creation.
Definition: LoadBalancer.php:2369
Wikimedia\Rdbms\LoadBalancer\runMasterTransactionIdleCallbacks
runMasterTransactionIdleCallbacks( $fname=__METHOD__, $owner=null)
Consume and run all pending post-COMMIT/ROLLBACK callbacks and commit dangling transactions.
Definition: LoadBalancer.php:1731
Wikimedia\Rdbms\LoadBalancer\$chronologyCallback
callable null $chronologyCallback
Callback to run before the first connection attempt.
Definition: LoadBalancer.php:46
Wikimedia\Rdbms\LoadBalancer\getLoadMonitor
getLoadMonitor()
Get a LoadMonitor instance.
Definition: LoadBalancer.php:390
Wikimedia\Rdbms\LoadBalancer\$servers
array[] $servers
Map of (server index => server config array)
Definition: LoadBalancer.php:77
Wikimedia\Rdbms\LoadBalancer\setTransactionListener
setTransactionListener( $name, callable $callback=null)
Set a callback via IDatabase::setTransactionListener() on all current and future master connections o...
Definition: LoadBalancer.php:2312
Wikimedia\Rdbms\IDatabase\getDomainID
getDomainID()
Return the currently selected domain ID.
Wikimedia\Rdbms\IDatabase\setTransactionListener
setTransactionListener( $name, callable $callback=null)
Run a callback after each time any transaction commits or rolls back.
Wikimedia\Rdbms\IDatabase\getDBname
getDBname()
Get the current DB name.
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:125
Wikimedia\Rdbms\LoadBalancer\getLocalDomainID
getLocalDomainID()
Get the local (and default) database domain ID of connection handles.
Definition: LoadBalancer.php:268
Wikimedia\Rdbms\Database\setTrxEndCallbackSuppression
setTrxEndCallbackSuppression( $suppress)
Whether to disable running of post-COMMIT/ROLLBACK callbacks.
Definition: Database.php:4059
Wikimedia\Rdbms\LoadBalancer\getLocalConnection
getLocalConnection( $i, $flags=0)
Open a connection to a local DB, or return one if it is already open.
Definition: LoadBalancer.php:1083
Wikimedia\Rdbms\IDatabase\pendingWriteCallers
pendingWriteCallers()
Get the list of method names that did write queries for this transaction.
Wikimedia\Rdbms\LoadBalancer\setDomainAliases
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
Definition: LoadBalancer.php:2333
Wikimedia\Rdbms\LoadBalancer\$connectionCounter
int $connectionCounter
Total number of new connections ever made with this instance.
Definition: LoadBalancer.php:128
Wikimedia\Rdbms\LoadBalancer\safeWaitForMasterPos
safeWaitForMasterPos(IDatabase $conn, $pos=false, $timeout=null)
Wait for a replica DB to reach a specified master position.
Definition: LoadBalancer.php:2308
Wikimedia\Rdbms\LoadBalancer\rollbackMasterChanges
rollbackMasterChanges( $fname=__METHOD__, $owner=null)
Issue ROLLBACK only on master, only if queries were done on connection.
Definition: LoadBalancer.php:1838
Wikimedia\Rdbms\LoadBalancer\getServerConnection
getServerConnection( $i, $domain, $flags=0)
Definition: LoadBalancer.php:922
Wikimedia\Rdbms\Database\getServer
getServer()
Get the server hostname or IP address.
Definition: Database.php:2794
Wikimedia\Rdbms\DBUnexpectedError
@newable Stable to extend
Definition: DBUnexpectedError.php:29
Wikimedia\Rdbms\LoadBalancer\getLazyConnectionRef
getLazyConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a database handle reference for a real or virtual (DB_MASTER/DB_REPLICA) server index.
Definition: LoadBalancer.php:1033
Wikimedia\Rdbms\IDatabase\masterPosWait
masterPosWait(DBMasterPos $pos, $timeout)
Wait for the replica DB to catch up to a given master position.
Wikimedia\Rdbms\LoadBalancer\$trxRoundStage
string $trxRoundStage
Stage of the current transaction round in the transaction round life-cycle.
Definition: LoadBalancer.php:112
Wikimedia\Rdbms\LoadBalancer\$indexAliases
string[] $indexAliases
Map of (index alias => index)
Definition: LoadBalancer.php:101
Wikimedia\Rdbms\LoadBalancer\beginMasterChanges
beginMasterChanges( $fname=__METHOD__, $owner=null)
Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)
Definition: LoadBalancer.php:1662
Wikimedia\Rdbms\LoadBalancer\$readOnlyReason
string bool $readOnlyReason
Reason this instance is read-only or false if not.
Definition: LoadBalancer.php:126
Wikimedia\Rdbms\LoadBalancer\getTransactionRoundStage
getTransactionRoundStage()
Definition: LoadBalancer.php:1962
Wikimedia\Rdbms\LoadBalancer\hasStreamingReplicaServers
hasStreamingReplicaServers()
Whether any replica servers use streaming replication from the master server.
Definition: LoadBalancer.php:1467
Wikimedia\Rdbms\LoadBalancer\getForeignConnection
getForeignConnection( $i, $domain, $flags=0)
Open a connection to a foreign DB, or return one if it is already open.
Definition: LoadBalancer.php:1145
Wikimedia\Rdbms\LoadBalancer\$defaultGroup
string null $defaultGroup
Default query group to use with getConnection()
Definition: LoadBalancer.php:89
Wikimedia\Rdbms\DBConnRef
Helper class used for automatically marking an IDatabase connection as reusable (once it no longer ma...
Definition: DBConnRef.php:29
Wikimedia\Rdbms\LoadBalancer\setTableAliases
setTableAliases(array $aliases)
Make certain table names use their own database, schema, and table prefix when passed into SQL querie...
Definition: LoadBalancer.php:2325
Wikimedia\Rdbms\DBTransactionError
@newable Stable to extend
Definition: DBTransactionError.php:29
Wikimedia\Rdbms\LoadBalancer\assertTransactionRoundStage
assertTransactionRoundStage( $stage)
Definition: LoadBalancer.php:1864
Wikimedia\Rdbms\LoadBalancer\isMasterRunningReadOnly
isMasterRunningReadOnly(DatabaseDomain $domain)
Definition: LoadBalancer.php:2079
Wikimedia\Rdbms\LoadBalancer\$localDomain
DatabaseDomain $localDomain
Local DB domain ID and default for selectDB() calls.
Definition: LoadBalancer.php:69
Wikimedia\Rdbms\LoadBalancer\pickAnyOpenConnection
pickAnyOpenConnection( $candidateConns, $autocommit)
Definition: LoadBalancer.php:789
Wikimedia\Rdbms\LoadBalancer\$waitForPos
bool DBMasterPos $waitForPos
Replication sync position or false if not set.
Definition: LoadBalancer.php:118
ArrayUtils\pickRandom
static pickRandom( $weights)
Given an array of non-normalised probabilities, this function will select an element and return the a...
Definition: ArrayUtils.php:66
Wikimedia\Rdbms\LoadBalancer\getAnyOpenConnection
getAnyOpenConnection( $i, $flags=0)
Get any open connection to a given server index, local or foreign.
Definition: LoadBalancer.php:754
ArrayUtils
A collection of static methods to play with arrays.
Definition: ArrayUtils.php:28
Wikimedia\Rdbms\LoadBalancer\$disabled
bool $disabled
Definition: LoadBalancer.php:130
Wikimedia\Rdbms\LoadBalancer\getLaggedReplicaMode
getLaggedReplicaMode( $domain=false)
Definition: LoadBalancer.php:2004
Wikimedia\Rdbms\LoadBalancer\commitAll
commitAll( $fname=__METHOD__, $owner=null)
Commit transactions on all open connections.
Definition: LoadBalancer.php:1589
Wikimedia\Rdbms\LoadBalancer\$INFO_FORIEGN
static $INFO_FORIEGN
Definition: LoadBalancer.php:144
Wikimedia\Rdbms\Database\writesPending
writesPending()
Definition: Database.php:655
Wikimedia\Rdbms\LoadBalancer\hasReplicaServers
hasReplicaServers()
Whether there are any replica servers configured.
Definition: LoadBalancer.php:1463
Wikimedia\Rdbms\DatabaseDomain\getSchema
getSchema()
Definition: DatabaseDomain.php:182
Wikimedia\Rdbms\LoadBalancer\runMasterTransactionListenerCallbacks
runMasterTransactionListenerCallbacks( $fname=__METHOD__, $owner=null)
Run all recurring post-COMMIT/ROLLBACK listener callbacks.
Definition: LoadBalancer.php:1806
Wikimedia\Rdbms\Database\runTransactionListenerCallbacks
runTransactionListenerCallbacks( $trigger)
Actually run any "transaction listener" callbacks.
Definition: Database.php:4218
Wikimedia\Rdbms\LoadBalancer\$errorConnection
Database $errorConnection
Connection handle that caused a problem.
Definition: LoadBalancer.php:114
Wikimedia\Rdbms\DBConnectionError
@newable
Definition: DBConnectionError.php:27
Wikimedia\Rdbms\IDatabase\getLBInfo
getLBInfo( $name=null)
Get properties passed down from the server info array of the load balancer.
Wikimedia\Rdbms\LoadBalancer\$profiler
mixed $profiler
Class name or object With profileIn/profileOut methods.
Definition: LoadBalancer.php:52
Wikimedia\Rdbms\LoadBalancer\getTopologyRole
getTopologyRole( $i, array $server)
Definition: LoadBalancer.php:1364
Wikimedia\Rdbms\LoadBalancer\resolveDomainInstance
resolveDomainInstance( $domain)
Definition: LoadBalancer.php:280
Wikimedia\Rdbms\LoadBalancer\haveIndex
haveIndex( $i)
Returns true if the specified index is a valid server index.
Definition: LoadBalancer.php:1444
Wikimedia\Rdbms\LoadBalancer\getReplicaResumePos
getReplicaResumePos()
Get the highest DB replication position for chronology control purposes.
Definition: LoadBalancer.php:1512
Wikimedia\Rdbms\DBMasterPos\hasReached
hasReached(DBMasterPos $pos)
Wikimedia\Rdbms\LoadBalancer\$trxRecurringCallbacks
callable[] $trxRecurringCallbacks
Map of (name => callable)
Definition: LoadBalancer.php:105
Wikimedia\Rdbms\LoadBalancer\getReadOnlyReason
getReadOnlyReason( $domain=false)
Definition: LoadBalancer.php:2021
Wikimedia\Rdbms\Database\attributesFromType
static attributesFromType( $dbType, $driver=null)
Definition: Database.php:451
Wikimedia\Rdbms\LoadBalancer\$INFO_AUTOCOMMIT_ONLY
static $INFO_AUTOCOMMIT_ONLY
Definition: LoadBalancer.php:143
Wikimedia\Rdbms\IDatabase\serverIsReadOnly
serverIsReadOnly()
Wikimedia\Rdbms\LoadBalancer\reuseConnection
reuseConnection(IDatabase $conn)
Mark a live handle as being available for reuse under a different database domain.
Definition: LoadBalancer.php:975
Wikimedia\Rdbms\LoadBalancer\$readIndexByGroup
int[] $readIndexByGroup
The group replica server indexes keyed by group.
Definition: LoadBalancer.php:116
Wikimedia\Rdbms\LoadBalancer\setIndexAliases
setIndexAliases(array $aliases)
Convert certain index names to alternative names before querying the DB.
Definition: LoadBalancer.php:2329
Wikimedia\Rdbms\LoadBalancer\laggedReplicaUsed
laggedReplicaUsed()
Checks whether the database for generic connections this request was both:
Definition: LoadBalancer.php:2017
Wikimedia\Rdbms\LoadBalancer\$wanCache
WANObjectCache $wanCache
Definition: LoadBalancer.php:50
Wikimedia\Rdbms\LoadBalancer\undoTransactionRoundFlags
undoTransactionRoundFlags(Database $conn)
Definition: LoadBalancer.php:1930
Wikimedia\Rdbms\LoadBalancer\$agent
string $agent
Agent name for query profiling.
Definition: LoadBalancer.php:96
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/schema/prefix specifications for IDatabase.
Definition: DatabaseDomain.php:40
Wikimedia\Rdbms\TransactionProfiler
Detect high-contention DB queries via profiling calls.
Definition: TransactionProfiler.php:38
Wikimedia\Rdbms\LoadBalancer\$laggedReplicaMode
bool $laggedReplicaMode
Whether the generic reader fell back to a lagged replica DB.
Definition: LoadBalancer.php:122
Wikimedia\Rdbms\LoadBalancer\$queryLogger
LoggerInterface $queryLogger
Definition: LoadBalancer.php:58
Wikimedia\Rdbms\LoadBalancer\$loadMonitor
ILoadMonitor $loadMonitor
Definition: LoadBalancer.php:44
Wikimedia\Rdbms\LoadBalancer\openConnection
openConnection( $i, $domain=false, $flags=0)
Definition: LoadBalancer.php:1065
DBO_DEFAULT
const DBO_DEFAULT
Definition: defines.php:13
Wikimedia\Rdbms\LoadBalancer\waitForAll
waitForAll( $pos, $timeout=null)
Set the master wait position and wait for ALL replica DBs to catch up to it.
Definition: LoadBalancer.php:701
Wikimedia\Rdbms\LoadBalancer\isMasterConnectionReadOnly
isMasterConnectionReadOnly(IDatabase $conn, $flags=0)
Definition: LoadBalancer.php:2042
Wikimedia\Rdbms\LoadBalancer\$connLogger
LoggerInterface $connLogger
Definition: LoadBalancer.php:56
Wikimedia\Rdbms\LoadBalancer\waitFor
waitFor( $pos)
Set the master position to reach before the next generic group DB handle query.
Definition: LoadBalancer.php:659
Wikimedia\Rdbms\LoadBalancer\lastMasterChangeTimestamp
lastMasterChangeTimestamp()
Get the timestamp of the latest write query done by this thread.
Definition: LoadBalancer.php:1979
Wikimedia\Rdbms\LoadBalancer\$cliMode
bool $cliMode
Whether this PHP instance is for a CLI script.
Definition: LoadBalancer.php:94
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
Wikimedia\Rdbms\LoadBalancer\getServerCount
getServerCount()
Get the number of servers defined in configuration.
Definition: LoadBalancer.php:1459
Wikimedia\Rdbms\IDatabase\commit
commit( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Commits a transaction previously started using begin()
Wikimedia\Rdbms\LoadBalancer\closeConnection
closeConnection(IDatabase $conn)
Close a connection.
Definition: LoadBalancer.php:1563
Wikimedia\Rdbms\LoadBalancer\KEY_FOREIGN_FREE_NOROUND
const KEY_FOREIGN_FREE_NOROUND
Definition: LoadBalancer.php:162
Wikimedia\Rdbms\IDatabase\flushSnapshot
flushSnapshot( $fname=__METHOD__, $flush=self::FLUSHING_ONE)
Commit any transaction but error out if writes or callbacks are pending.
Wikimedia\Rdbms\LoadBalancer\reportConnectionError
reportConnectionError()
Definition: LoadBalancer.php:1405
Wikimedia\Rdbms\LoadBalancer\$INFO_FOREIGN_REF_COUNT
static $INFO_FOREIGN_REF_COUNT
Definition: LoadBalancer.php:145
Wikimedia\Rdbms\IDatabase\clearFlag
clearFlag( $flag, $remember=self::REMEMBER_NOTHING)
Clear a flag for this connection.
Wikimedia\Rdbms\LoadBalancer\$tableAliases
array[] $tableAliases
$aliases Map of (table => (dbname, schema, prefix) map)
Definition: LoadBalancer.php:99
$type
$type
Definition: testCompression.php:52
Wikimedia\Rdbms\LoadBalancer\getRoleFromIndex
getRoleFromIndex( $i)
Definition: LoadBalancer.php:1052
Wikimedia\Rdbms\IDatabase\writesOrCallbacksPending
writesOrCallbacksPending()
Whether there is a transaction open with either possible write queries or unresolved pre-commit/commi...