Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20namespace Wikimedia\Rdbms;
21
22/**
23 * This class is a delegate to ILBFactory for a given database cluster
24 *
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)
35 *
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
39 *
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
46 *
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.
49 *
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
59 *
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.
65 *
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.
69 *
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.
84 *
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).
104 *
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.
109 *
110 * @since 1.28
111 * @ingroup Database
112 */
113interface ILoadBalancer {
114    /**
115     * Request a replica DB connection. Can't be used as a binary flag with bitwise operators!
116     */
117    public const DB_REPLICA = -1;
118    /**
119     * Request a primary, write-enabled DB connection. Can't be used as a binary flag with bitwise
120     * operators!
121     * @since 1.36
122     */
123    public const DB_PRIMARY = -2;
124
125    // phpcs:disable MediaWiki.Usage.DeprecatedConstantUsage.DB_MASTER
126    /**
127     * Request a primary, write-enabled DB connection
128     * @deprecated since 1.36, Use DB_PRIMARY instead
129     */
130    public const DB_MASTER = self::DB_PRIMARY;
131    // phpcs:enable MediaWiki.Usage.DeprecatedConstantUsage.DB_MASTER
132
133    /** Domain specifier when no specific database needs to be selected */
134    public const DOMAIN_ANY = '';
135    /** The generic query group */
136    public const GROUP_GENERIC = '';
137
138    /** Yield an untracked, low-timeout, autocommit-mode handle (to gauge server health) */
139    public const CONN_UNTRACKED_GAUGE = 1;
140    /** Yield a tracked autocommit-mode handle (reuse existing ones) */
141    public const CONN_TRX_AUTOCOMMIT = 2;
142    /** Yield null on connection failure instead of throwing an exception */
143    public const CONN_SILENCE_ERRORS = 4;
144    /** Caller is requesting the primary DB server for possibly writes */
145    public const CONN_INTENT_WRITABLE = 8;
146
147    /**
148     * Get the name of the overall cluster of database servers managing the dataset
149     *
150     * Note that the cluster might contain servers in multiple datacenters.
151     * The load balancer instance only needs to be aware of the local replica servers,
152     * along with either the sole-primary server or the local co-primary server.
153     *
154     * This is useful for identifying a cluster or replicated dataset, even when:
155     *  - The primary server is sometimes swapped with another one
156     *  - The cluster/dataset is replicated among multiple datacenters, with one "primary"
157     *    datacenter having the writable primary server and the other datacenters having a
158     *    read-only replica in the "primary" server slot
159     *  - The dataset is replicated among multiple datacenters, via circular replication,
160     *    with each datacenter having its own "co-primary" server
161     *
162     * @return string
163     * @since 1.36
164     */
165    public function getClusterName(): string;
166
167    /**
168     * Get the local (and default) database domain ID of connection handles
169     *
170     * @see DatabaseDomain
171     * @return string Database domain ID; this specifies DB name, schema, and table prefix
172     * @since 1.31
173     */
174    public function getLocalDomainID(): string;
175
176    /**
177     * @param DatabaseDomain|string|false $domain Database domain
178     * @return string Value of $domain if it is foreign or the local domain otherwise
179     * @since 1.32
180     */
181    public function resolveDomainID( $domain ): string;
182
183    /**
184     * Indicate whether the tables on this domain are only temporary tables for testing
185     *
186     * In "temporary tables mode", the CONN_TRX_AUTOCOMMIT flag is ignored
187     *
188     * @param bool $value
189     * @param string $domain
190     * @return bool Whether "temporary tables mode" was active
191     * @since 1.34
192     */
193    public function setTempTablesOnlyMode( $value, $domain );
194
195    /**
196     * Get the specific server index of the reader connection for a given group
197     *
198     * This takes into account load ratios and lag times. It should return a consistent
199     * index during the life time of the load balancer. This initially checks replica DBs
200     * for connectivity to avoid returning an unusable server. This means that connections
201     * might be attempted by calling this method (usually one at the most but possibly more).
202     * Subsequent calls with the same $group will not need to make new connection attempts
203     * since the acquired connection for each group is preserved.
204     *
205     * @param string|false $group Query group or false for the generic group
206     * @return int|false Specific server index, or false if no DB handle can be obtained
207     */
208    public function getReaderIndex( $group = false );
209
210    /**
211     * Get a lazy-connecting database handle for a specific or virtual (DB_PRIMARY/DB_REPLICA) server index
212     *
213     * The server index, $i, can be one of the following:
214     *   - DB_REPLICA: a server index will be selected by the load balancer based on read
215     *      weight, connectivity, and replication lag. Note that the primary server might be
216     *      configured with read weight. If $groups is empty then it means "the generic group",
217     *      in which case all servers defined with read weight will be considered. Additional
218     *      query groups can be configured, having their own list of server indexes and read
219     *      weights. If a query group list is provided in $groups, then each recognized group
220     *      will be tried, left-to-right, until server index selection succeeds or all groups
221     *      have been tried, in which case the generic group will be tried.
222     *   - DB_PRIMARY: the primary server index will be used; the same as getWriterIndex().
223     *      The value of $groups should be [] when using this server index.
224     *   - Specific server index: a positive integer can be provided to use the server with
225     *      that index. An error will be thrown in no such server index is recognized. This
226     *      server selection method is usually only useful for internal load balancing logic.
227     *      The value of $groups should be [] when using a specific server index.
228     *
229     * Handle sharing is very useful when callers get DB_PRIMARY handles that are transaction
230     * round aware (the default). All such callers will operate within a single transaction as
231     * a consequence. The same applies to DB_REPLICA that are samely query grouped (the default)
232     * and  transaction round aware (the default).
233     *
234     * Use CONN_TRX_AUTOCOMMIT to use a separate pool of only autocommit handles. This flag is
235     * ignored for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in order to avoid deadlocks.
236     * getServerAttributes() can be used to check this attribute beforehand. Avoid using begin()
237     * and commit() on such handles. If handle methods like startAtomic() and endAtomic() must be
238     * used on the handles, callers should at least make sure that the atomic sections are closed
239     * on failure via try/catch and cancelAtomic().
240     *
241     * Use CONN_UNTRACKED_GAUGE to get a new, untracked, handle, that uses a low connection timeout, a low
242     * read timeout, and autocommit mode. This flag is intended for use only be internal callers.
243     *
244     * CONN_UNTRACKED_GAUGE and CONN_TRX_AUTOCOMMIT are incompatible.
245     *
246     * @see ILoadBalancer::getServerAttributes()
247     *
248     * @param int $i Specific (overrides $groups) or virtual (DB_PRIMARY/DB_REPLICA) server index
249     * @param string[]|string $groups Query group(s) in preference order; [] for the default group
250     * @param string|false $domain DB domain ID or false for the local domain
251     * @param int $flags Bitfield of CONN_* class constants
252     * @return IDatabase|false This returns false on failure if CONN_SILENCE_ERRORS is set
253     */
254    public function getConnection( $i, $groups = [], $domain = false, $flags = 0 );
255
256    /**
257     * Get a DB handle for a specific server index
258     *
259     * This is an internal utility method for methods like LoadBalancer::getConnectionInternal()
260     * and DBConnRef to create the underlying connection to a concrete server.
261     *
262     * The following is the responsibility of the caller:
263     *
264     * - translate any virtual server indexes (DB_PRIMARY/DB_REPLICA) to a real server index.
265     * - enforce read-only mode on primary DB handle if there is high replication lag.
266     *
267     * @see ILoadBalancer::getConnection()
268     *
269     * @internal Only for use within ILoadBalancer/ILoadMonitor
270     * @param int $i Specific server index
271     * @param string $domain Resolved DB domain
272     * @param int $flags Bitfield of class CONN_* constants
273     * @return IDatabase|false This returns false on failure if CONN_SILENCE_ERRORS is set
274     * @throws DBError If no DB handle could be obtained and CONN_SILENCE_ERRORS is not set
275     */
276    public function getServerConnection( $i, $domain, $flags = 0 );
277
278    /**
279     * @deprecated since 1.39 noop
280     * @param IDatabase $conn
281     */
282    public function reuseConnection( IDatabase $conn );
283
284    /**
285     * @deprecated since 1.39, use ILoadBalancer::getConnection() instead.
286     * @param int $i Specific 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 DBConnRef
291     */
292    public function getConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ): DBConnRef;
293
294    /**
295     * @internal Only to be used by DBConnRef
296     * @param int $i Specific (overrides $groups) or virtual (DB_PRIMARY/DB_REPLICA) server index
297     * @param string[]|string $groups Query group(s) in preference order; [] for the default group
298     * @param string|false $domain DB domain ID or false for the local domain
299     * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
300     * @return IDatabase
301     */
302    public function getConnectionInternal( $i, $groups = [], $domain = false, $flags = 0 ): IDatabase;
303
304    /**
305     * Get a DB handle, suitable for migrations and schema changes, for a server index
306     *
307     * The DBConnRef methods simply proxy an underlying IDatabase object which
308     * takes care of the actual connection and query logic.
309     *
310     * The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING
311     * (e.g. sqlite) in order to avoid deadlocks. getServerAttributes()
312     * can be used to check such flags beforehand. Avoid the use of begin() or startAtomic()
313     * on any CONN_TRX_AUTOCOMMIT connections.
314     *
315     * @see ILoadBalancer::getConnection() for parameter information
316     * @param int $i Specific or virtual (DB_PRIMARY/DB_REPLICA) server index
317     * @param string[]|string $groups Query group(s) in preference order; [] for the default group
318     * @param string|false $domain DB domain ID or false for the local domain
319     * @param int $flags Bitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
320     * @return DBConnRef
321     */
322    public function getMaintenanceConnectionRef( $i, $groups = [], $domain = false, $flags = 0 ): DBConnRef;
323
324    /**
325     * Get the specific server index of the "writer server"
326     *
327     * The "writer server" is the server that should be used to source writes and critical reads
328     * originating from the local datacenter. The "writer server" will be one of the following:
329     *   - The primary, for single-primary setups (even if it resides in a remote datacenter)
330     *   - The "preferred" co-primary relative to the local datacenter, for multi-primary setups
331     *   - The "preferred" static clone, for static clone server setups (e.g. no replication)
332     *
333     * @return int Specific server index
334     */
335    public function getWriterIndex();
336
337    /**
338     * Get the number of servers defined in configuration
339     *
340     * @return int
341     */
342    public function getServerCount();
343
344    /**
345     * Whether there are any replica servers configured
346     *
347     * This scans the list of servers defined in configuration, checking for:
348     *  - Servers that are listed after the primary and not flagged with "is static";
349     *    such servers are assumed to be typical streaming replicas
350     *  - Servers that are listed after the primary and flagged with "is static";
351     *    such servers are assumed to have a clone of the static dataset (matching the primary)
352     *
353     * @return bool
354     * @since 1.34
355     */
356    public function hasReplicaServers();
357
358    /**
359     * Whether any replica servers use streaming replication from the primary server
360     *
361     * This scans the list of servers defined in configuration, checking for:
362     *  - Servers that are listed after the primary and not flagged with "is static";
363     *    such servers are assumed to be typical streaming replicas
364     *
365     * It is possible for some replicas to be configured with "is static" but not
366     * others, though it generally should either be set for all or none of the replicas.
367     *
368     * If this returns false, this means that there is generally no reason to execute
369     * replication wait logic for session consistency and lag reduction.
370     *
371     * @return bool
372     * @since 1.34
373     */
374    public function hasStreamingReplicaServers();
375
376    /**
377     * Get the readable name of the server with the specified index
378     *
379     * @param int $i Specific server index
380     * @return string Readable server name, falling back to the hostname or IP address
381     */
382    public function getServerName( $i ): string;
383
384    /**
385     * Return the server configuration map for the server with the specified index
386     *
387     * @param int $i Specific server index
388     * @return array|false Server configuration map; false if the index is invalid
389     * @since 1.31
390     */
391    public function getServerInfo( $i );
392
393    /**
394     * Get the RDBMS type of the server with the specified index (e.g. "mysql", "sqlite")
395     *
396     * @param int $i Specific server index
397     * @return string One of (mysql,postgres,sqlite,...) or "unknown" for bad indexes
398     * @since 1.30
399     */
400    public function getServerType( $i );
401
402    /**
403     * Get basic attributes of the server with the specified index without connecting
404     *
405     * @param int $i Specific server index
406     * @return array (Database::ATTRIBUTE_* constant => value) for all such constants
407     * @since 1.31
408     */
409    public function getServerAttributes( $i );
410
411    /**
412     * Get the current primary replication position
413     *
414     * @return DBPrimaryPos|false Returns false if not applicable
415     * @throws DBError
416     * @since 1.37
417     */
418    public function getPrimaryPos();
419
420    /**
421     * Whether there are pending changes or callbacks in a transaction by this thread
422     * @return bool
423     * @since 1.37
424     */
425    public function hasPrimaryChanges();
426
427    /**
428     * Determine whether an explicit transaction is active on any open primary
429     * connection.
430     * @return bool
431     * @since 1.39
432     */
433    public function explicitTrxActive();
434
435    /**
436     * Check if this load balancer object had any recent or still
437     * pending writes issued against it by this PHP thread
438     *
439     * @param float|null $age How many seconds ago is "recent" [defaults to mWaitTimeout]
440     * @return bool
441     * @since 1.37
442     */
443    public function hasOrMadeRecentPrimaryChanges( $age = null );
444
445    /**
446     * @note This method may trigger a DB connection if not yet done
447     * @param string|false $domain DB domain ID or false (unused and deprecated since 1.40)
448     * @return string|false Reason the primary is read-only or false if it is not
449     */
450    public function getReadOnlyReason( $domain = false );
451
452    /**
453     * @return bool
454     */
455    public function pingAll();
456
457    /**
458     * Get the name and lag time of the most-lagged replica server
459     *
460     * This is useful for maintenance scripts that need to throttle their updates.
461     * May attempt to open connections to replica DBs on the default DB. If there is
462     * no lag, the maximum lag will be reported as -1.
463     *
464     * @return array{0:string,1:float|int|false,2:int} (host, max lag, index of max lagged host)
465     */
466    public function getMaxLag();
467
468    /**
469     * Get an estimate of replication lag (in seconds) for each server
470     *
471     * Results are cached for a short time in memcached/process cache
472     *
473     * Values may be "false" if replication is too broken to estimate
474     *
475     * @return float[]|int[]|false[] Map of (server index => lag) in order of server index
476     */
477    public function getLagTimes();
478
479    /**
480     * Wait for a replica DB to reach a specified primary position
481     *
482     * If $conn is not a replica server connection, then this will return true.
483     * Otherwise, if $pos is not provided, this will connect to the primary server
484     * to get an accurate position.
485     *
486     * @param IDatabase $conn Replica DB
487     * @return bool Success
488     * @since 1.37
489     */
490    public function waitForPrimaryPos( IDatabase $conn );
491
492    /**
493     * Set a callback via IDatabase::setTransactionListener() on
494     * all current and future primary connections of this load balancer
495     *
496     * @param string $name Callback name
497     * @param callable|null $callback
498     */
499    public function setTransactionListener( $name, callable $callback = null );
500
501    /**
502     * Make certain table names use their own database, schema, and table prefix
503     * when passed into SQL queries pre-escaped and without a qualified database name
504     *
505     * For example, "user" can be converted to "myschema.mydbname.user" for convenience.
506     * Appearances like `user`, somedb.user, somedb.someschema.user will used literally.
507     *
508     * Calling this twice will completely clear any old table aliases. Also, note that
509     * callers are responsible for making sure the schemas and databases actually exist.
510     *
511     * @param array[] $aliases Map of (table => (dbname, schema, prefix) map)
512     */
513    public function setTableAliases( array $aliases );
514
515    /**
516     * Convert certain database domains to alternative ones.
517     *
518     * This can be used for backwards compatibility logic.
519     *
520     * @param DatabaseDomain[]|string[] $aliases Map of (domain alias => domain)
521     * @since 1.35
522     */
523    public function setDomainAliases( array $aliases );
524}