MediaWiki  1.28.0
ILBFactory Interface Reference

An interface for generating database load balancers. More...

Inheritance diagram for ILBFactory:

Public Member Functions

 __construct (array $conf)
 Construct a manager of ILoadBalancer objects. More...
 
 appendPreShutdownTimeAsQuery ($url, $time)
 Append ?cpPosTime parameter to a URL for ChronologyProtector purposes if needed. More...
 
 beginMasterChanges ($fname=__METHOD__)
 Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set) More...
 
 closeAll ()
 Close all open database connections on all open load balancers. More...
 
 commitAll ($fname=__METHOD__, array $options=[])
 Commit open transactions on all connections. More...
 
 commitAndWaitForReplication ($fname, $ticket, array $opts=[])
 Convenience method for safely running commitMasterChanges()/waitForReplication() More...
 
 commitMasterChanges ($fname=__METHOD__, array $options=[])
 Commit changes on all master connections. More...
 
 destroy ()
 Disables all load balancers. More...
 
 disableChronologyProtection ()
 Disable the ChronologyProtector for all load balancers. More...
 
 flushReplicaSnapshots ($fname=__METHOD__)
 Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot. More...
 
 forEachLB ($callback, array $params=[])
 Execute a function for each tracked load balancer The callback is called with the load balancer as the first parameter, and $params passed as the subsequent parameters. More...
 
 getChronologyProtectorTouched ($dbName)
 
 getEmptyTransactionTicket ($fname)
 Get a token asserting that no transaction writes are active. More...
 
 getExternalLB ($cluster)
 Get a cached (tracked) load balancer for external storage. More...
 
 getMainLB ($domain=false)
 Get a cached (tracked) load balancer object. More...
 
 hasMasterChanges ()
 Determine if any master connection has pending changes. More...
 
 hasOrMadeRecentMasterChanges ($age=null)
 Determine if any master connection has pending/written changes from this request. More...
 
 laggedReplicaUsed ()
 Detemine if any lagged replica DB connection was used. More...
 
 newExternalLB ($cluster)
 Create a new load balancer for external storage. More...
 
 newMainLB ($domain=false)
 Create a new load balancer object. More...
 
 rollbackMasterChanges ($fname=__METHOD__)
 Rollback changes on all master connections. More...
 
 setAgentName ($agent)
 
 setDomainPrefix ($prefix)
 Set a new table prefix for the existing local domain ID for testing. More...
 
 setRequestInfo (array $info)
 
 setWaitForReplicationListener ($name, callable $callback=null)
 Add a callback to be run in every call to waitForReplication() before waiting. More...
 
 shutdown ($mode=self::SHUTDOWN_CHRONPROT_SYNC, callable $workCallback=null)
 Prepare all tracked load balancers for shutdown. More...
 
 waitForReplication (array $opts=[])
 Waits for the replica DBs to catch up to the current master position. More...
 

Public Attributes

const SHUTDOWN_CHRONPROT_ASYNC = 1
 
const SHUTDOWN_CHRONPROT_SYNC = 2
 
const SHUTDOWN_NO_CHRONPROT = 0
 

Detailed Description

An interface for generating database load balancers.

Since
1.28

Definition at line 29 of file ILBFactory.php.

Constructor & Destructor Documentation

ILBFactory::__construct ( array  $conf)

Construct a manager of ILoadBalancer objects.

Sub-classes will extend the required keys in $conf with additional parameters

Parameters
$conf$params Array with keys:
  • localDomain: A DatabaseDomain or domain ID string.
  • readOnlyReason : Reason the master DB is read-only if so [optional]
  • srvCache : BagOStuff object for server cache [optional]
  • memCache : BagOStuff object for cluster memory cache [optional]
  • wanCache : WANObjectCache object [optional]
  • hostname : The name of the current server [optional]
  • cliMode: Whether the execution context is a CLI script. [optional]
  • profiler : Class name or instance with profileIn()/profileOut() methods. [optional]
  • trxProfiler: TransactionProfiler instance. [optional]
  • replLogger: PSR-3 logger instance. [optional]
  • connLogger: PSR-3 logger instance. [optional]
  • queryLogger: PSR-3 logger instance. [optional]
  • perfLogger: PSR-3 logger instance. [optional]
  • errorLogger : Callback that takes an Exception and logs it. [optional]
Exceptions
InvalidArgumentException

Implemented in LBFactoryMulti, LBFactory, LBFactorySimple, and LBFactorySingle.

Member Function Documentation

ILBFactory::appendPreShutdownTimeAsQuery (   $url,
  $time 
)

Append ?cpPosTime parameter to a URL for ChronologyProtector purposes if needed.

Note that unlike cookies, this works accross domains

Parameters
string$url
float$timeUNIX timestamp just before shutdown() was called
Returns
string

Implemented in LBFactory.

ILBFactory::beginMasterChanges (   $fname = __METHOD__)

Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)

The DBO_TRX setting will be reverted to the default in each of these methods:

This allows for custom transaction rounds from any outer transaction scope.

Parameters
string$fname
Exceptions
DBTransactionError

Implemented in LBFactory.

ILBFactory::closeAll ( )

Close all open database connections on all open load balancers.

Implemented in LBFactory.

ILBFactory::commitAll (   $fname = __METHOD__,
array  $options = [] 
)

Commit open transactions on all connections.

This is useful for two main cases:

  • a) To commit changes to the masters.
  • b) To release the snapshot on all connections, master and replica DBs.
    Parameters
    string$fnameCaller name
    array$optionsOptions map:
  • maxWriteDuration: abort if more than this much time was spent in write queries

Implemented in LBFactory.

ILBFactory::commitAndWaitForReplication (   $fname,
  $ticket,
array  $opts = [] 
)

Convenience method for safely running commitMasterChanges()/waitForReplication()

This will commit and wait unless $ticket indicates it is unsafe to do so

Parameters
string$fnameCaller name (e.g. METHOD)
mixed$ticketResult of getEmptyTransactionTicket()
array$optsOptions to waitForReplication()
Exceptions
DBReplicationWaitError

Implemented in LBFactory.

ILBFactory::commitMasterChanges (   $fname = __METHOD__,
array  $options = [] 
)

Commit changes on all master connections.

Parameters
string$fnameCaller name
array$optionsOptions map:
  • maxWriteDuration: abort if more than this much time was spent in write queries
Exceptions
Exception

Implemented in LBFactory.

ILBFactory::destroy ( )

Disables all load balancers.

All connections are closed, and any attempt to open a new connection will result in a DBAccessError.

See also
ILoadBalancer::disable()

Implemented in LBFactory.

ILBFactory::disableChronologyProtection ( )

Disable the ChronologyProtector for all load balancers.

This can be called at the start of special API entry points

Implemented in LBFactory.

ILBFactory::flushReplicaSnapshots (   $fname = __METHOD__)

Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot.

Parameters
string$fnameCaller name

Implemented in LBFactory.

ILBFactory::forEachLB (   $callback,
array  $params = [] 
)

Execute a function for each tracked load balancer The callback is called with the load balancer as the first parameter, and $params passed as the subsequent parameters.

Parameters
callable$callback
array$params

Implemented in LBFactoryMulti, LBFactorySimple, and LBFactorySingle.

Referenced by LBFactory\appendPreShutdownTimeAsQuery(), LBFactory\commitMasterChanges(), LBFactory\forEachLBCallMethod(), LBFactory\hasMasterChanges(), LBFactory\hasOrMadeRecentMasterChanges(), LBFactory\laggedReplicaUsed(), LBFactory\logIfMultiDbTransaction(), LBFactory\rollbackMasterChanges(), LBFactory\setDomainPrefix(), LBFactory\shutdownChronologyProtector(), and LBFactory\waitForReplication().

ILBFactory::getChronologyProtectorTouched (   $dbName)
Parameters
string$dbNameDB master name (e.g. "db1052")
Returns
float|bool UNIX timestamp when client last touched the DB or false if not recent

Implemented in LBFactory.

ILBFactory::getEmptyTransactionTicket (   $fname)

Get a token asserting that no transaction writes are active.

Parameters
string$fnameCaller name (e.g. METHOD)
Returns
mixed A value to pass to commitAndWaitForReplication()

Implemented in LBFactory.

ILBFactory::getExternalLB (   $cluster)

Get a cached (tracked) load balancer for external storage.

Parameters
string$clusterExternal storage cluster name
Returns
ILoadBalancer

Implemented in LBFactoryMulti, LBFactory, LBFactorySimple, and LBFactorySingle.

ILBFactory::getMainLB (   $domain = false)

Get a cached (tracked) load balancer object.

Parameters
bool | string$domainDomain ID, or false for the current domain
Returns
ILoadBalancer

Implemented in LBFactoryMulti, LBFactory, LBFactorySimple, and LBFactorySingle.

ILBFactory::hasMasterChanges ( )

Determine if any master connection has pending changes.

Returns
bool

Implemented in LBFactory.

ILBFactory::hasOrMadeRecentMasterChanges (   $age = null)

Determine if any master connection has pending/written changes from this request.

Parameters
float$ageHow many seconds ago is "recent" [defaults to LB lag wait timeout]
Returns
bool

Implemented in LBFactory.

ILBFactory::laggedReplicaUsed ( )

Detemine if any lagged replica DB connection was used.

Returns
bool

Implemented in LBFactory.

ILBFactory::newExternalLB (   $cluster)

Create a new load balancer for external storage.

The resulting object will be untracked, not chronology-protected, and the caller is responsible for cleaning it up.

This method is for only advanced usage and callers should almost always use getExternalLB() instead. This method can be useful when a table is used as a key/value store. In that cases, one might want to query it in autocommit mode (DBO_TRX off) but still use DBO_TRX transaction rounds on other tables.

Parameters
string$clusterExternal storage cluster name
Returns
ILoadBalancer

Implemented in LBFactoryMulti, LBFactory, LBFactorySimple, and LBFactorySingle.

ILBFactory::newMainLB (   $domain = false)

Create a new load balancer object.

The resulting object will be untracked, not chronology-protected, and the caller is responsible for cleaning it up.

This method is for only advanced usage and callers should almost always use getMainLB() instead. This method can be useful when a table is used as a key/value store. In that cases, one might want to query it in autocommit mode (DBO_TRX off) but still use DBO_TRX transaction rounds on other tables.

Parameters
bool | string$domainDomain ID, or false for the current domain
Returns
ILoadBalancer

Implemented in LBFactoryMulti, LBFactory, LBFactorySimple, and LBFactorySingle.

ILBFactory::rollbackMasterChanges (   $fname = __METHOD__)

Rollback changes on all master connections.

Parameters
string$fnameCaller name

Implemented in LBFactory.

ILBFactory::setAgentName (   $agent)
Parameters
string$agentAgent name for query profiling

Implemented in LBFactory.

ILBFactory::setDomainPrefix (   $prefix)

Set a new table prefix for the existing local domain ID for testing.

Parameters
string$prefix

Implemented in LBFactory.

ILBFactory::setRequestInfo ( array  $info)
Parameters
array$infoMap of fields, including:
  • IPAddress : IP address
  • UserAgent : User-Agent HTTP header
  • ChronologyProtection : cookie/header value specifying ChronologyProtector usage

Implemented in LBFactory.

ILBFactory::setWaitForReplicationListener (   $name,
callable  $callback = null 
)

Add a callback to be run in every call to waitForReplication() before waiting.

Callbacks must clear any transactions that they start

Parameters
string$nameCallback name
callable | null$callbackUse null to unset a callback

Implemented in LBFactory.

ILBFactory::shutdown (   $mode = self::SHUTDOWN_CHRONPROT_SYNC,
callable  $workCallback = null 
)

Prepare all tracked load balancers for shutdown.

Parameters
integer$modeOne of the class SHUTDOWN_* constants
callable | null$workCallbackWork to mask ChronologyProtector writes

Implemented in LBFactory.

ILBFactory::waitForReplication ( array  $opts = [])

Waits for the replica DBs to catch up to the current master position.

Use this when updating very large numbers of rows, as in maintenance scripts, to avoid causing too much lag. Of course, this is a no-op if there are no replica DBs.

By default this waits on all DB clusters actually used in this request. This makes sense when lag being waiting on is caused by the code that does this check. In that case, setting "ifWritesSince" can avoid the overhead of waiting for clusters that were not changed since the last wait check. To forcefully wait on a specific cluster for a given domain, use the 'domain' parameter. To forcefully wait on an "external" cluster, use the "cluster" parameter.

Never call this function after a large DB write that is still in a transaction. It only makes sense to call this after the possible lag inducing changes were committed.

Parameters
array$optsOptional fields that include:
  • domain : wait on the load balancer DBs that handles the given domain ID
  • cluster : wait on the given external load balancer DBs
  • timeout : Max wait time. Default: ~60 seconds
  • ifWritesSince: Only wait if writes were done since this UNIX timestamp
Exceptions
DBReplicationWaitErrorIf a timeout or error occured waiting on a DB cluster

Implemented in LBFactory.

Member Data Documentation

const ILBFactory::SHUTDOWN_CHRONPROT_ASYNC = 1

Definition at line 31 of file ILBFactory.php.

const ILBFactory::SHUTDOWN_CHRONPROT_SYNC = 2

Definition at line 32 of file ILBFactory.php.

const ILBFactory::SHUTDOWN_NO_CHRONPROT = 0

Definition at line 30 of file ILBFactory.php.

Referenced by MediaWiki\restInPeace().


The documentation for this interface was generated from the following file: