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