2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Trait that adds read-only slave connection capability
22 * @copyright 2018 Srdjan Janković, Catalyst IT
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Trait to wrap connect() method of database driver classes that gives
30 * ability to use read only slave instances for SELECT queries. For the
31 * databases that support replication and read only connections to the slave.
32 * If the slave connection is configured there will be two database handles
33 * created, one for the master and another one for the slave. If there's no
34 * slave specified everything uses master handle.
36 * Classes that use this trait need to rename existing connect() method to
37 * raw_connect(). In addition, they need to provide get_db_handle() and
38 * set_db_handle() methods, due to dbhandle attributes not being named
39 * consistently across the database driver classes.
41 * Read only slave connection is configured in the $CFG->dboptions['readonly']
43 * - It supports multiple 'instance' entries, in case one is not accessible,
44 * but only one (first connectable) instance is used.
45 * - 'latency' option: master -> slave sync latency in seconds (will probably
46 * be a fraction of a second). A table being written to is deemed fully synced
47 * after that period and suitable for slave read. Defaults to 1 sec.
48 * - 'exclude_tables' option: a list of tables that never go to the slave for
49 * querying. The feature is meant to be used in emergency only, so the
50 * readonly feature can still be used in case there is a rogue query that
51 * does not go through the standard dml interface or some other unaccounted
52 * situation. It should not be used under normal circumstances, and its use
53 * indicates a problem in the system that needs addressig.
55 * Choice of the database handle is based on following:
56 * - SQL_QUERY_INSERT, UPDATE and STRUCTURE record table from the query
57 * in the $written array and microtime() the event. For those queries master
58 * write handle is used.
59 * - SQL_QUERY_AUX queries will always use the master write handle because they
60 * are used for transaction start/end, locking etc. In that respect, query_start() and
61 * query_end() *must not* be used during the connection phase.
62 * - SQL_QUERY_AUX_READONLY queries will use the master write handle if in a transaction.
63 * - SELECT queries will use the master write handle if:
64 * -- any of the tables involved is a temp table
65 * -- any of the tables involved is listed in the 'exclude_tables' option
66 * -- any of the tables involved is in the $written array:
67 * * current microtime() is compared to the write microrime, and if more than
68 * latency time has passed the slave handle is used
69 * * otherwise (not enough time passed) we choose the master write handle
70 * If none of the above conditions are met the slave instance is used.
72 * A 'latency' example:
73 * - we have set $CFG->dboptions['readonly']['latency'] to 0.2.
74 * - a SQL_QUERY_UPDATE to table tbl_x happens, and it is recorded in
76 * - 0.15 seconds later SQL_QUERY_SELECT with tbl_x is requested - the master
78 * - 0.10 seconds later (0.25 seconds after SQL_QUERY_UPDATE) another
79 * SQL_QUERY_SELECT with tbl_x is requested - this time more than 0.2 secs
80 * has gone and master -> slave sync is assumed, so the slave connection is
84 trait moodle_read_slave_trait
{
86 /** @var resource master write database handle */
89 /** @var resource slave read only database handle */
90 protected $dbhreadonly;
92 private $wantreadslave = false;
93 private $readsslave = 0;
94 private $slavelatency = 1;
95 private $structurechange = false;
97 private $written = []; // Track tables being written to.
98 private $readexclude = []; // Tables to exclude from using dbhreadonly.
100 // Store original params.
109 * Gets db handle currently used with queries
112 abstract protected function get_db_handle();
115 * Sets db handle to be used with subsequent queries
116 * @param resource $dbh
119 abstract protected function set_db_handle($dbh): void
;
123 * The real connection establisment, called from connect() and set_dbhwrite()
124 * @param string $dbhost The database host.
125 * @param string $dbuser The database username.
126 * @param string $dbpass The database username's password.
127 * @param string $dbname The name of the database being connected to.
128 * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
129 * @param array $dboptions driver specific options
131 * @throws dml_connection_exception if error
133 abstract protected function raw_connect(string $dbhost, string $dbuser, string $dbpass, string $dbname, $prefix, array $dboptions = null): bool;
137 * The connection parameters processor that sets up stage for master write and slave readonly handles.
138 * Must be called before other methods.
139 * @param string $dbhost The database host.
140 * @param string $dbuser The database username.
141 * @param string $dbpass The database username's password.
142 * @param string $dbname The name of the database being connected to.
143 * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
144 * @param array $dboptions driver specific options
146 * @throws dml_connection_exception if error
148 public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions = null) {
149 $this->pdbhost
= $dbhost;
150 $this->pdbuser
= $dbuser;
151 $this->pdbpass
= $dbpass;
152 $this->pdbname
= $dbname;
153 $this->pprefix
= $prefix;
154 $this->pdboptions
= $dboptions;
157 if (isset($dboptions['readonly'])) {
158 $this->wantreadslave
= true;
159 $dboptionsro = $dboptions['readonly'];
161 if (isset($dboptionsro['connecttimeout'])) {
162 $dboptions['connecttimeout'] = $dboptionsro['connecttimeout'];
163 } else if (!isset($dboptions['connecttimeout'])) {
164 $dboptions['connecttimeout'] = 2; // Default readonly connection timeout.
166 if (isset($dboptionsro['latency'])) {
167 $this->slavelatency
= $dboptionsro['latency'];
169 if (isset($dboptionsro['exclude_tables'])) {
170 $this->readexclude
= $dboptionsro['exclude_tables'];
171 if (!is_array($this->readexclude
)) {
172 throw new configuration_exception('exclude_tables must be an array');
175 $dbport = isset($dboptions['dbport']) ?
$dboptions['dbport'] : null;
177 $slaves = $dboptionsro['instance'];
178 if (!is_array($slaves) ||
!isset($slaves[0])) {
182 if (count($slaves) > 1) {
183 // Randomise things a bit.
187 // Find first connectable readonly slave.
189 foreach ($slaves as $slave) {
190 if (!is_array($slave)) {
191 $slave = ['dbhost' => $slave];
193 foreach (['dbhost', 'dbuser', 'dbpass'] as $dbparam) {
194 $rodb[$dbparam] = isset($slave[$dbparam]) ?
$slave[$dbparam] : $
$dbparam;
196 $dboptions['dbport'] = isset($slave['dbport']) ?
$slave['dbport'] : $dbport;
199 $this->raw_connect($rodb['dbhost'], $rodb['dbuser'], $rodb['dbpass'], $dbname, $prefix, $dboptions);
200 $this->dbhreadonly
= $this->get_db_handle();
202 } catch (dml_connection_exception
$e) { // phpcs:ignore
203 // If readonly slave is not connectable we'll have to do without it.
206 // ... lock_db queries always go to master.
207 // Since it is a lock and as such marshalls concurrent connections,
208 // it is best to leave it out and avoid master/slave latency.
209 $this->readexclude
[] = 'lock_db';
211 $this->readexclude
[] = 'sessions';
214 if (!$this->dbhreadonly
) {
215 $this->set_dbhwrite();
222 * Set database handle to readwrite master
223 * Will connect if required. Calls set_db_handle()
226 private function set_dbhwrite(): void
{
227 // Lazy connect to read/write master.
228 if (!$this->dbhwrite
) {
229 $temptables = $this->temptables
;
230 $this->raw_connect($this->pdbhost
, $this->pdbuser
, $this->pdbpass
, $this->pdbname
, $this->pprefix
, $this->pdboptions
);
232 $this->temptables
= $temptables; // Restore temptables, so we don't get separate sets for rw and ro.
234 $this->dbhwrite
= $this->get_db_handle();
236 $this->set_db_handle($this->dbhwrite
);
240 * Returns whether we want to connect to slave database for read queries.
241 * @return bool Want read only connection
243 public function want_read_slave(): bool {
244 return $this->wantreadslave
;
248 * Returns the number of reads done by the read only database.
249 * @return int Number of reads.
251 public function perf_get_reads_slave(): int {
252 return $this->readsslave
;
256 * On DBs that support it, switch to transaction mode and begin a transaction
257 * @return moodle_transaction
259 public function start_delegated_transaction() {
260 $this->set_dbhwrite();
261 return parent
::start_delegated_transaction();
265 * Called before each db query.
267 * @param array|null $params An array of parameters.
268 * @param int $type type of query
269 * @param mixed $extrainfo driver specific extra information
272 protected function query_start($sql, ?
array $params, $type, $extrainfo = null) {
273 parent
::query_start($sql, $params, $type, $extrainfo);
274 $this->select_db_handle($type, $sql);
278 * This should be called immediately after each db query. It does a clean up of resources.
280 * @param mixed $result The db specific result obtained from running a query.
283 protected function query_end($result) {
284 if ($this->written
) {
285 // Adjust the written time.
286 array_walk($this->written
, function (&$val) {
288 $val = microtime(true);
293 parent
::query_end($result);
297 * Select appropriate db handle - readwrite or readonly
298 * @param int $type type of query
302 protected function select_db_handle(int $type, string $sql): void
{
303 if ($this->dbhreadonly
&& $this->can_use_readonly($type, $sql)) {
305 $this->set_db_handle($this->dbhreadonly
);
308 $this->set_dbhwrite();
312 * Check if The query qualifies for readonly connection execution
313 * Logging queries are exempt, those are write operations that circumvent
314 * standard query_start/query_end paths.
315 * @param int $type type of query
319 protected function can_use_readonly(int $type, string $sql): bool {
320 if ($this->loggingquery
) {
324 if (during_initial_install()) {
328 // Transactions are done as AUX, we cannot play with that.
330 case SQL_QUERY_AUX_READONLY
:
331 // SQL_QUERY_AUX_READONLY may read the structure data.
332 // We don't have a way to reliably determine whether it is safe to go to readonly if the structure has changed.
333 return !$this->structurechange
;
334 case SQL_QUERY_SELECT
:
335 if ($this->transactions
) {
340 foreach ($this->table_names($sql) as $tablename) {
341 if (in_array($tablename, $this->readexclude
)) {
345 if ($this->temptables
&& $this->temptables
->is_temptable($tablename)) {
349 if (isset($this->written
[$tablename])) {
350 $now = $now ?
: microtime(true);
352 if ($now - $this->written
[$tablename] < $this->slavelatency
) {
355 unset($this->written
[$tablename]);
360 case SQL_QUERY_INSERT
:
361 case SQL_QUERY_UPDATE
:
362 foreach ($this->table_names($sql) as $tablename) {
363 $this->written
[$tablename] = true;
366 case SQL_QUERY_STRUCTURE
:
367 $this->structurechange
= true;
368 foreach ($this->table_names($sql) as $tablename) {
369 if (!in_array($tablename, $this->readexclude
)) {
370 $this->readexclude
[] = $tablename;
379 * Indicates delegated transaction finished successfully.
380 * Set written times after outermost transaction finished
381 * @param moodle_transaction $transaction The transaction to commit
383 * @throws dml_transaction_exception Creates and throws transaction related exceptions.
385 public function commit_delegated_transaction(moodle_transaction
$transaction) {
386 if ($this->written
) {
387 // Adjust the written time.
388 $now = microtime(true);
389 foreach ($this->written
as $tablename => $when) {
390 $this->written
[$tablename] = $now;
394 parent
::commit_delegated_transaction($transaction);
398 * Parse table names from query
402 protected function table_names(string $sql): array {
403 preg_match_all('/\b'.$this->prefix
.'([a-z][A-Za-z0-9_]*)/', $sql, $match);