Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / rdbms / loadbalancer / ILoadBalancer.php
blob1725d9fbd9390595b04f2bec2f040a41eb6ddcdd
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
20 namespace Wikimedia\Rdbms;
22 /**
23 * This class is a delegate to ILBFactory for a given database cluster
25 * ILoadBalancer tracks the database connections and transactions for a given database cluster.
26 * A "cluster" is considered to be the set of database servers that manage a given dataset.
27 * Within a given cluster, each database server can have one of the following roles:
28 * - sole-primary: the server used by this datacenter for writes from the application
29 * - co-primary: one of several servers used by this datacenter for writes from the application,
30 * relying on asynchronous replication to synchronize their copies of the dataset
31 * - replica: a server used only for reads from the application, relying on asynchronous
32 * replication to apply writes from the primary server or co-primary servers
33 * - static clone: a server that only accepts reads from the application, does not replicate,
34 * and has a copy of the final dataset, which must be static (all the servers reject writes)
36 * Single-datacenter database clusters consist of either:
37 * - A sole-primary server and zero or more replica servers
38 * - A set of static clone servers
40 * Multi-datacenter database clusters either consist of either:
41 * - A sole-primary server and zero or more replica servers in the "primary datacenter"
42 * (the one datacenter meant to handle requests/jobs that mutate the database), and zero or
43 * more replica servers in each "secondary datacenter" (all the other datacenters)
44 * - A co-primary server and zero or more replica servers within each datacenter
45 * - A set of static clone servers in each datacenter
47 * The term "primary" refers to the server used by this datacenter for handling writes,
48 * whether it is a sole-primary or co-primary.
50 * The "servers" configuration array contains the list of database servers to use for operations
51 * originating from the the local datacenter. The first entry must refer to the server to use for
52 * write and read-for-write operations (e.g. the "writer server"):
53 * - If there is a primary server, then the first entry must refer to it, even if the primary
54 * server resides in a remote datacenter
55 * - If there are co-primary servers, then the first entry must refer to the one in the local
56 * datacenter
57 * - If the servers are static clones, then the first entry can refer to any of them, since the
58 * concept of a "writer server" is merely nominal
60 * On an infrastructure level, circular replication setups can have more than one database server
61 * act as a replication "source" within the same datacenter, provided that no more than one of the
62 * servers are writable at any time, namely the "writer server". The other source servers will be
63 * treated as replicas by the load balancer, but can be quickly promoted to the "writer server" by
64 * the site admin as needed.
66 * Likewise, Galera Cluster setups still require the choice of a single "writer server" for each
67 * datacenter. Limiting the number of servers that initiate transactions helps reduce the rate of
68 * aborted transactions due to wsrep conflicts.
70 * By default, each DB server uses DBO_DEFAULT for its 'flags' setting, unless explicitly set
71 * otherwise in configuration. DBO_DEFAULT behavior depends on whether 'cliMode' is set:
72 * - In CLI mode, the flag has no effect with regards to LoadBalancer.
73 * - In non-CLI mode, the flag causes implicit transactions to be used; the first query on
74 * a database starts a transaction on that database. The transactions are meant to remain
75 * pending until either commitPrimaryChanges() or rollbackPrimaryChanges() is called. The
76 * application must have some point where it calls commitPrimaryChanges() near the end of
77 * the PHP request.
78 * Every iteration of beginPrimaryChanges()/commitPrimaryChanges() is called a "transaction round".
79 * Rounds are useful on the primary DB connections because they make single-DB (and by and large
80 * multi-DB) updates in web requests all-or-nothing. Also, transactions on replica DBs are useful
81 * when REPEATABLE-READ or SERIALIZABLE isolation is used because all foreign keys and constraints
82 * hold across separate queries in the DB transaction since the data appears within a consistent
83 * point-in-time snapshot.
85 * The typical caller will use LoadBalancer::getConnection( DB_* ) to yield a database
86 * connection handle. The choice of which DB server to use is based on pre-defined loads for
87 * weighted random selection, adjustments thereof by LoadMonitor, and the amount of replication
88 * lag on each DB server. Lag checks might cause problems in certain setups, so they should be
89 * tuned in the server configuration maps as follows:
90 * - Sole-primary + N Replica(s): set 'max lag' to an appropriate threshold for avoiding any
91 * replica database lagged by this much or more. If all replicas are this lagged, then the
92 * load balancer considers the cluster to be read-only.
93 * - Per-datacenter co-primary + N Replica(s): set 'max lag' to an appropriate threshold for
94 * avoiding any replica database lagged by this much or more. If all replicas are this
95 * lagged, then the load balancer considers the cluster to be read-only.
96 * - Read-only archive clones: set 'is static' in the server configuration maps. This will
97 * treat all such DBs as having 0 lag.
98 * - Externally updated dataset clones: set 'is static' in the server configuration maps.
99 * This will treat all such DBs as having 0 lag.
100 * - SQL load balancing proxy: any proxy should handle lag checks on its own, so the 'max lag'
101 * parameter should probably be set to INF in the server configuration maps. This will make
102 * the load balancer ignore whatever it detects as the lag of the logical replica is (which
103 * would probably just randomly bounce around).
105 * If using a SQL proxy service, it would probably be best to have two proxy hosts for the load
106 * balancer to talk to. One would be the 'host' of the "writer server" entry and another for the
107 * (logical) replica server entry. The proxy could map the load balancer's "replica" DB to any
108 * number of physical replica DBs.
110 * @since 1.28
111 * @ingroup Database
113 interface ILoadBalancer {
115 * Request a replica DB connection. Can't be used as a binary flag with bitwise operators!
117 public const DB_REPLICA = -1;
119 * Request a primary, write-enabled DB connection. Can't be used as a binary flag with bitwise
120 * operators!
121 * @since 1.36
123 public const DB_PRIMARY = -2;
125 /** Domain specifier when no specific database needs to be selected */
126 public const DOMAIN_ANY = '';
127 /** The generic query group */
128 public const GROUP_GENERIC = '';
130 /** Yield a tracked autocommit-mode handle (reuse existing ones) */
131 public const CONN_TRX_AUTOCOMMIT = 1;
133 * Yield an untracked, low-timeout, autocommit-mode handle (to gauge server health)
134 * @internal
136 public const CONN_UNTRACKED_GAUGE = 2;
138 * Yield null on connection failure instead of throwing an exception
139 * @internal
141 public const CONN_SILENCE_ERRORS = 4;
144 * Get the name of the overall cluster of database servers managing the dataset
146 * Note that the cluster might contain servers in multiple datacenters.
147 * The load balancer instance only needs to be aware of the local replica servers,
148 * along with either the sole-primary server or the local co-primary server.
150 * This is useful for identifying a cluster or replicated dataset, even when:
151 * - The primary server is sometimes swapped with another one
152 * - The cluster/dataset is replicated among multiple datacenters, with one "primary"
153 * datacenter having the writable primary server and the other datacenters having a
154 * read-only replica in the "primary" server slot
155 * - The dataset is replicated among multiple datacenters, via circular replication,
156 * with each datacenter having its own "co-primary" server
158 * @return string
159 * @since 1.36
161 public function getClusterName(): string;
164 * Get the local (and default) database domain ID of connection handles
166 * @see DatabaseDomain
167 * @return string Database domain ID; this specifies DB name, schema, and table prefix
168 * @since 1.31
170 public function getLocalDomainID(): string;
173 * @param DatabaseDomain|string|false $domain Database domain
174 * @return string Value of $domain if it is foreign or the local domain otherwise
175 * @since 1.32
177 public function resolveDomainID( $domain ): string;
180 * Indicate whether the tables on this domain are only temporary tables for testing
182 * In "temporary tables mode", the CONN_TRX_AUTOCOMMIT flag is ignored
184 * @param bool $value
185 * @param string $domain
186 * @return bool Whether "temporary tables mode" was active
187 * @since 1.34
189 public function setTempTablesOnlyMode( $value, $domain );
192 * Get the specific server index of the reader connection for a given group
194 * This takes into account load ratios and lag times. It should return a consistent
195 * index during the life time of the load balancer. This initially checks replica DBs
196 * for connectivity to avoid returning an unusable server. This means that connections
197 * might be attempted by calling this method (usually one at the most but possibly more).
198 * Subsequent calls with the same $group will not need to make new connection attempts
199 * since the acquired connection for each group is preserved.
201 * @param string|false $group Query group or false for the generic group
202 * @return int|false Specific server index, or false if no DB handle can be obtained
204 public function getReaderIndex( $group = false );
207 * Get a lazy-connecting database handle for a specific or virtual (DB_PRIMARY/DB_REPLICA) server index
209 * The server index, $i, can be one of the following:
210 * - DB_REPLICA: a server index will be selected by the load balancer based on read
211 * weight, connectivity, and replication lag. Note that the primary server might be
212 * configured with read weight. If $groups is empty then it means "the generic group",
213 * in which case all servers defined with read weight will be considered. Additional
214 * query groups can be configured, having their own list of server indexes and read
215 * weights. If a query group list is provided in $groups, then each recognized group
216 * will be tried, left-to-right, until server index selection succeeds or all groups
217 * have been tried, in which case the generic group will be tried.
218 * - DB_PRIMARY: the primary server index will be used; the same as ServerInfo::WRITER_INDEX.
219 * The value of $groups should be [] when using this server index.
220 * - Specific server index: a positive integer can be provided to use the server with
221 * that index. An error will be thrown in no such server index is recognized. This
222 * server selection method is usually only useful for internal load balancing logic.
223 * The value of $groups should be [] when using a specific server index.
225 * Handle sharing is very useful when callers get DB_PRIMARY handles that are transaction
226 * round aware (the default). All such callers will operate within a single transaction as
227 * a consequence. The same applies to DB_REPLICA that are samely query grouped (the default)
228 * and transaction round aware (the default).
230 * Use CONN_TRX_AUTOCOMMIT to use a separate pool of only autocommit handles. This flag is
231 * ignored for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in order to avoid deadlocks.
232 * getServerAttributes() can be used to check this attribute beforehand. Avoid using begin()
233 * and commit() on such handles. If handle methods like startAtomic() and endAtomic() must be
234 * used on the handles, callers should at least make sure that the atomic sections are closed
235 * on failure via try/catch and cancelAtomic().
237 * Use CONN_UNTRACKED_GAUGE to get a new, untracked, handle, that uses a low connection timeout, a low
238 * read timeout, and autocommit mode. This flag is intended for use only be internal callers.
240 * CONN_UNTRACKED_GAUGE and CONN_TRX_AUTOCOMMIT are incompatible.
242 * @see ILoadBalancer::getServerAttributes()
244 * @param int $i Specific (overrides $groups) or virtual (DB_PRIMARY/DB_REPLICA) server index
245 * @param string[]|string $groups Query group(s) in preference order; [] for the default group
246 * @param string|false $domain DB domain ID or false for the local domain
247 * @param int $flags Bitfield of CONN_* class constants
248 * @return IDatabase|false This returns false on failure if CONN_SILENCE_ERRORS is set
250 public function getConnection( $i, $groups = [], $domain = false, $flags = 0 );
253 * Get a DB handle for a specific server index
255 * This is an internal utility method for methods like LoadBalancer::getConnectionInternal()
256 * and DBConnRef to create the underlying connection to a concrete server.
258 * The following is the responsibility of the caller:
260 * - translate any virtual server indexes (DB_PRIMARY/DB_REPLICA) to a real server index.
261 * - enforce read-only mode on primary DB handle if there is high replication lag.
263 * @see ILoadBalancer::getConnection()
265 * @internal Only for use within ILoadBalancer/ILoadMonitor
266 * @param int $i Specific server index
267 * @param string $domain Resolved DB domain
268 * @param int $flags Bitfield of class CONN_* constants
269 * @return IDatabaseForOwner|false This returns false on failure if CONN_SILENCE_ERRORS is set
270 * @throws DBError If no DB handle could be obtained and CONN_SILENCE_ERRORS is not set
272 public function getServerConnection( $i, $domain, $flags = 0 );
275 * @deprecated since 1.39, use ILoadBalancer::getConnection() instead.
276 * @param int $i Specific or virtual (DB_PRIMARY/DB_REPLICA) server index
277 * @param string[]|string $groups Query group(s) in preference order; [] for the default group
278 * @param string|false $domain DB domain ID or false for the local domain
279 * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
280 * @return DBConnRef
282 public function getConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ): DBConnRef;
285 * @internal Only to be used by DBConnRef
286 * @param int $i Specific (overrides $groups) or virtual (DB_PRIMARY/DB_REPLICA) server index
287 * @param string[]|string $groups Query group(s) in preference order; [] for the default group
288 * @param string|false $domain DB domain ID or false for the local domain
289 * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
290 * @return IDatabase
292 public function getConnectionInternal( $i, $groups = [], $domain = false, $flags = 0 ): IDatabase;
295 * Get a DB handle, suitable for migrations and schema changes, for a server index
297 * The DBConnRef methods simply proxy an underlying IDatabase object which
298 * takes care of the actual connection and query logic.
300 * The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
301 * (e.g. sqlite) in order to avoid deadlocks. getServerAttributes()
302 * can be used to check such flags beforehand. Avoid the use of begin() or startAtomic()
303 * on any CONN_TRX_AUTOCOMMIT connections.
305 * @see ILoadBalancer::getConnection() for parameter information
306 * @param int $i Specific or virtual (DB_PRIMARY/DB_REPLICA) server index
307 * @param string[]|string $groups Query group(s) in preference order; [] for the default group
308 * @param string|false $domain DB domain ID or false for the local domain
309 * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
310 * @return DBConnRef
312 public function getMaintenanceConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ): DBConnRef;
315 * Get the number of servers defined in configuration
317 * @return int
319 public function getServerCount();
322 * Whether there are any replica servers configured
324 * This scans the list of servers defined in configuration, checking for:
325 * - Servers that are listed after the primary and not flagged with "is static";
326 * such servers are assumed to be typical streaming replicas
327 * - Servers that are listed after the primary and flagged with "is static";
328 * such servers are assumed to have a clone of the static dataset (matching the primary)
330 * @return bool
331 * @since 1.34
333 public function hasReplicaServers();
336 * Whether any replica servers use streaming replication from the primary server
338 * This scans the list of servers defined in configuration, checking for:
339 * - Servers that are listed after the primary and not flagged with "is static";
340 * such servers are assumed to be typical streaming replicas
342 * It is possible for some replicas to be configured with "is static" but not
343 * others, though it generally should either be set for all or none of the replicas.
345 * If this returns false, this means that there is generally no reason to execute
346 * replication wait logic for session consistency and lag reduction.
348 * @return bool
349 * @since 1.34
351 public function hasStreamingReplicaServers();
354 * Get the readable name of the server with the specified index
356 * @param int $i Specific server index
357 * @return string Readable server name, falling back to the hostname or IP address
359 public function getServerName( $i ): string;
362 * Return the server configuration map for the server with the specified index
364 * @param int $i Specific server index
365 * @return array|false Server configuration map; false if the index is invalid
366 * @since 1.31
368 public function getServerInfo( $i );
371 * Get the RDBMS type of the server with the specified index (e.g. "mysql", "sqlite")
373 * @param int $i Specific server index
374 * @return string One of (mysql,postgres,sqlite,...) or "unknown" for bad indexes
375 * @since 1.30
377 public function getServerType( $i );
380 * Get basic attributes of the server with the specified index without connecting
382 * @param int $i Specific server index
383 * @return array (Database::ATTRIBUTE_* constant => value) for all such constants
384 * @since 1.31
386 public function getServerAttributes( $i );
389 * Get the current primary replication position
391 * @return DBPrimaryPos|false Returns false if not applicable
392 * @throws DBError
393 * @since 1.37
395 public function getPrimaryPos();
398 * Whether there are pending changes or callbacks in a transaction by this thread
399 * @return bool
400 * @since 1.37
402 public function hasPrimaryChanges();
405 * Determine whether an explicit transaction is active on any open primary
406 * connection.
407 * @return bool
408 * @since 1.39
410 public function explicitTrxActive();
413 * Check if this load balancer object had any recent or still
414 * pending writes issued against it by this PHP thread
416 * @param float|null $age How many seconds ago is "recent" [defaults to mWaitTimeout]
417 * @return bool
418 * @since 1.37
420 public function hasOrMadeRecentPrimaryChanges( $age = null );
423 * @note This method may trigger a DB connection if not yet done
424 * @return string|false Reason the primary is read-only or false if it is not
426 public function getReadOnlyReason();
429 * @return bool
431 public function pingAll();
434 * Get the name and lag time of the most-lagged replica server
436 * This is useful for maintenance scripts that need to throttle their updates.
437 * May attempt to open connections to replica DBs on the default DB. If there is
438 * no lag, the maximum lag will be reported as -1.
440 * @return array{0:string,1:float|int|false,2:int} (host, max lag, index of max lagged host)
442 public function getMaxLag();
445 * Get an estimate of replication lag (in seconds) for each server
447 * Results are cached for a short time in memcached/process cache
449 * Values may be "false" if replication is too broken to estimate
451 * @return float[]|int[]|false[] Map of (server index => lag) in order of server index
453 public function getLagTimes();
456 * Wait for a replica DB to reach a specified primary position
458 * If $conn is not a replica server connection, then this will return true.
459 * Otherwise, if $pos is not provided, this will connect to the primary server
460 * to get an accurate position.
462 * @param IDatabase $conn Replica DB
463 * @return bool Success
464 * @since 1.37
466 public function waitForPrimaryPos( IDatabase $conn );
469 * Set a callback via IDatabase::setTransactionListener() on
470 * all current and future primary connections of this load balancer
472 * @param string $name Callback name
473 * @param callable|null $callback
475 public function setTransactionListener( $name, ?callable $callback = null );
478 * Make certain table names use their own database, schema, and table prefix
479 * when passed into SQL queries pre-escaped and without a qualified database name
481 * For example, "user" can be converted to "myschema.mydbname.user" for convenience.
482 * Appearances like `user`, somedb.user, somedb.someschema.user will used literally.
484 * Calling this twice will completely clear any old table aliases. Also, note that
485 * callers are responsible for making sure the schemas and databases actually exist.
487 * @param array[] $aliases Map of (table => (dbname, schema, prefix) map)
489 public function setTableAliases( array $aliases );
492 * Convert certain database domains to alternative ones.
494 * This can be used for backwards compatibility logic.
496 * @param DatabaseDomain[]|string[] $aliases Map of (domain alias => domain)
497 * @since 1.35
499 public function setDomainAliases( array $aliases );