Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
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.
8
//
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.
13
//
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/>.
16
 
17
/**
18
 * Trait that adds read-only replica connection capability.
19
 *
20
 * Trait to wrap connect() method of database driver classes that gives
21
 * ability to use read only replica instances for SELECT queries. For the
22
 * databases that support replication and read only connections to the replica.
23
 * If the replica connection is configured there will be two database handles
24
 * created, one for the primary and another one for the replica. If there's no
25
 * replica specified everything uses primary handle.
26
 *
27
 * Classes that use this trait need to rename existing connect() method to
28
 * raw_connect(). In addition, they need to provide get_db_handle() and
29
 * set_db_handle() methods, due to dbhandle attributes not being named
30
 * consistently across the database driver classes.
31
 *
32
 * Read only replica connection is configured in the $CFG->dboptions['readonly']
33
 * array.
34
 * - It supports multiple 'instance' entries, in case one is not accessible,
35
 *   but only one (first connectable) instance is used.
36
 * - 'latency' option: primary -> replica sync latency in seconds (will probably
37
 *   be a fraction of a second). A table being written to is deemed fully synced
38
 *   after that period and suitable for replica read. Defaults to 1 sec.
39
 * - 'exclude_tables' option: a list of tables that never go to the replica for
40
 *   querying. The feature is meant to be used in emergency only, so the
41
 *   readonly feature can still be used in case there is a rogue query that
42
 *   does not go through the standard dml interface or some other unaccounted
43
 *   situation. It should not be used under normal circumstances, and its use
44
 *   indicates a problem in the system that needs addressig.
45
 *
46
 * Choice of the database handle is based on following:
47
 * - SQL_QUERY_INSERT, UPDATE and STRUCTURE record table from the query
48
 *   in the $written array and microtime() the event. For those queries primary
49
 *   write handle is used.
50
 * - SQL_QUERY_AUX queries will always use the primary write handle because they
51
 *   are used for transaction start/end, locking etc. In that respect, query_start() and
52
 *   query_end() *must not* be used during the connection phase.
53
 * - SQL_QUERY_AUX_READONLY queries will use the primary write handle if in a transaction.
54
 * - SELECT queries will use the primary write handle if:
55
 *   -- any of the tables involved is a temp table
56
 *   -- any of the tables involved is listed in the 'exclude_tables' option
57
 *   -- any of the tables involved is in the $written array:
58
 *      * current microtime() is compared to the write microrime, and if more than
59
 *        latency time has passed the replica handle is used
60
 *      * otherwise (not enough time passed) we choose the primary write handle
61
 *   If none of the above conditions are met the replica instance is used.
62
 *
63
 * A 'latency' example:
64
 *  - we have set $CFG->dboptions['readonly']['latency'] to 0.2.
65
 *  - a SQL_QUERY_UPDATE to table tbl_x happens, and it is recorded in
66
 *    the $written array
67
 *  - 0.15 seconds later SQL_QUERY_SELECT with tbl_x is requested - the primary
68
 *    connection is used
69
 *  - 0.10 seconds later (0.25 seconds after SQL_QUERY_UPDATE) another
70
 *    SQL_QUERY_SELECT with tbl_x is requested - this time more than 0.2 secs
71
 *    has gone and primary -> replica sync is assumed, so the replica connection is
72
 *    used again.
73
 *
74
 * @package    core
75
 * @category   dml
76
 * @copyright  2024 David Woloszyn <david.woloszyn@moodle.com>
77
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
78
 */
79
trait moodle_read_replica_trait {
80
 
81
    /** @var resource Primary write database handle. */
82
    protected $dbhwrite;
83
 
84
    /** @var resource Replica read only database handle. */
85
    protected $dbhreadonly;
86
 
87
    /** @var bool Connect to replica database for read queries. */
88
    private $wantreadreplica = false;
89
 
90
    /** @var int The number of reads done by the read only database. */
91
    private $readsreplica = 0;
92
 
93
    /** @var int Replica letency in seconds. */
94
    private $replicalatency = 1;
95
 
96
    /** @var bool Structure changed status. */
97
    private $structurechange = false;
98
 
99
    /** @var array Track tables being written to. */
100
    private $written = [];
101
 
102
    /** @var array Tables to exclude from using dbhreadonly. */
103
    private $readexclude = [];
104
 
105
    /** @var string The database host. */
106
    private $pdbhost;
107
 
108
    /** @var string The database username. */
109
    private $pdbuser;
110
 
111
    /** @var string The database username's password. */
112
    private $pdbpass;
113
 
114
    /** @var string The name of the database being connected to. */
115
    private $pdbname;
116
 
117
    /** @var mixed String means moodle db prefix, false used for external databases where prefix not used. */
118
    private $pprefix;
119
 
120
    /** @var array|null Driver specific options. */
121
    private $pdboptions;
122
 
123
    /**
124
     * Gets db handle currently used with queries.
125
     *
126
     * @return resource
127
     */
128
    abstract protected function get_db_handle();
129
 
130
    /**
131
     * Sets db handle to be used with subsequent queries.
132
     *
133
     * @param resource $dbh
134
     */
135
    abstract protected function set_db_handle($dbh): void;
136
 
137
    /**
138
     * Connect to db.
139
     *
140
     * The real connection establisment, called from connect() and set_dbhwrite().
141
     *
142
     * @param string $dbhost The database host.
143
     * @param string $dbuser The database username.
144
     * @param string $dbpass The database username's password.
145
     * @param string $dbname The name of the database being connected to.
146
     * @param mixed $prefix String means moodle db prefix, false used for external databases where prefix not used.
147
     * @param array|null $dboptions Driver specific options.
148
     * @return bool
149
     * @throws dml_connection_exception
150
     */
151
    abstract protected function raw_connect(
152
        string $dbhost,
153
        string $dbuser,
154
        string $dbpass,
155
        string $dbname,
156
        $prefix,
157
        ?array $dboptions = null
158
    ): bool;
159
 
160
    /**
161
     * Connect to db.
162
     *
163
     * The connection parameters processor that sets up stage for primary write and replica readonly handles.
164
     * Must be called before other methods.
165
     *
166
     * @param string $dbhost The database host.
167
     * @param string $dbuser The database username.
168
     * @param string $dbpass The database username's password.
169
     * @param string $dbname The name of the database being connected to.
170
     * @param mixed $prefix String means moodle db prefix, false used for external databases where prefix not used.
171
     * @param array|null $dboptions Driver specific options.
172
     * @return bool
173
     * @throws dml_connection_exception
174
     */
175
    public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, ?array $dboptions = null) {
176
        $this->pdbhost = $dbhost;
177
        $this->pdbuser = $dbuser;
178
        $this->pdbpass = $dbpass;
179
        $this->pdbname = $dbname;
180
        $this->pprefix = $prefix;
181
        $this->pdboptions = $dboptions;
182
 
183
        $logconnection = false;
184
        if ($dboptions) {
185
            if (isset($dboptions['readonly'])) {
186
                $this->wantreadreplica = true;
187
                $dboptionsro = $dboptions['readonly'];
188
 
189
                if (isset($dboptionsro['connecttimeout'])) {
190
                    $dboptions['connecttimeout'] = $dboptionsro['connecttimeout'];
191
                } else if (!isset($dboptions['connecttimeout'])) {
192
                    $dboptions['connecttimeout'] = 2; // Default readonly connection timeout.
193
                }
194
                if (isset($dboptionsro['latency'])) {
195
                    $this->replicalatency = $dboptionsro['latency'];
196
                }
197
                if (isset($dboptionsro['exclude_tables'])) {
198
                    $this->readexclude = $dboptionsro['exclude_tables'];
199
                    if (!is_array($this->readexclude)) {
200
                        throw new configuration_exception('exclude_tables must be an array');
201
                    }
202
                }
203
                $dbport = isset($dboptions['dbport']) ? $dboptions['dbport'] : null;
204
 
205
                $replicas = $dboptionsro['instance'];
206
                if (!is_array($replicas) || !isset($replicas[0])) {
207
                    $replicas = [$replicas];
208
                }
209
 
210
                if (count($replicas) > 1) {
211
                    // Don't shuffle for unit tests as order is important for them to pass.
212
                    if (!PHPUNIT_TEST) {
213
                        // Randomise things a bit.
214
                        shuffle($replicas);
215
                    }
216
                }
217
 
218
                // Find first connectable readonly replica.
219
                $rodb = [];
220
                foreach ($replicas as $replica) {
221
                    if (!is_array($replica)) {
222
                        $replica = ['dbhost' => $replica];
223
                    }
224
                    foreach (['dbhost', 'dbuser', 'dbpass'] as $dbparam) {
225
                        $rodb[$dbparam] = isset($replica[$dbparam]) ? $replica[$dbparam] : $$dbparam;
226
                    }
227
                    $dboptions['dbport'] = isset($replica['dbport']) ? $replica['dbport'] : $dbport;
228
 
229
                    try {
230
                        $this->raw_connect($rodb['dbhost'], $rodb['dbuser'], $rodb['dbpass'], $dbname, $prefix, $dboptions);
231
                        $this->dbhreadonly = $this->get_db_handle();
232
                        if ($logconnection) {
233
                            debugging(
234
                                "Readonly db connection succeeded for host {$rodb['dbhost']}"
235
                            );
236
                        }
237
                        break;
238
                    } catch (dml_connection_exception $e) {
239
                        debugging(
240
                            "Readonly db connection failed for host {$rodb['dbhost']}: {$e->debuginfo}"
241
                        );
242
                        $logconnection = true;
243
                    }
244
                }
245
                // ... lock_db queries always go to primary.
246
                // Since it is a lock and as such marshalls concurrent connections,
247
                // it is best to leave it out and avoid primary/replica latency.
248
                $this->readexclude[] = 'lock_db';
249
                // ... and sessions.
250
                $this->readexclude[] = 'sessions';
251
            }
252
        }
253
        if (!$this->dbhreadonly) {
254
            try {
255
                $this->set_dbhwrite();
256
            } catch (dml_connection_exception $e) {
257
                debugging(
258
                    "Readwrite db connection failed for host {$this->pdbhost}: {$e->debuginfo}"
259
                );
260
                throw $e;
261
            }
262
            if ($logconnection) {
263
                debugging(
264
                    "Readwrite db connection succeeded for host {$this->pdbhost}"
265
                );
266
            }
267
        }
268
 
269
        return true;
270
    }
271
 
272
    /**
273
     * Set database handle to readwrite primary.
274
     *
275
     * Will connect if required. Calls set_db_handle().
276
     */
277
    private function set_dbhwrite(): void {
278
        // Lazy connect to read/write primary.
279
        if (!$this->dbhwrite) {
280
            $temptables = $this->temptables;
281
            $this->raw_connect($this->pdbhost, $this->pdbuser, $this->pdbpass, $this->pdbname, $this->pprefix, $this->pdboptions);
282
            if ($temptables) {
283
                $this->temptables = $temptables; // Restore temptables, so we don't get separate sets for rw and ro.
284
            }
285
            $this->dbhwrite = $this->get_db_handle();
286
        }
287
        $this->set_db_handle($this->dbhwrite);
288
    }
289
 
290
    /**
291
     * Returns whether we want to connect to replica database for read queries.
292
     *
293
     * @return bool Want read only connection.
294
     */
295
    public function want_read_replica(): bool {
296
        return $this->wantreadreplica;
297
    }
298
 
299
    /**
300
     * Returns the number of reads done by the read only database.
301
     *
302
     * @return int Number of reads.
303
     */
304
    public function perf_get_reads_replica(): int {
305
        return $this->readsreplica;
306
    }
307
 
308
    /**
309
     * On DBs that support it, switch to transaction mode and begin a transaction.
310
     *
311
     * @return moodle_transaction
312
     */
313
    public function start_delegated_transaction() {
314
        $this->set_dbhwrite();
315
        return parent::start_delegated_transaction();
316
    }
317
 
318
    /**
319
     * Called before each db query.
320
     *
321
     * @param string $sql
322
     * @param array|null $params An array of parameters.
323
     * @param int $type type of query
324
     * @param mixed $extrainfo driver specific extra information
325
     */
326
    protected function query_start($sql, ?array $params, $type, $extrainfo = null) {
327
        parent::query_start($sql, $params, $type, $extrainfo);
328
        $this->select_db_handle($type, $sql);
329
    }
330
 
331
    /**
332
     * This should be called immediately after each db query. It does a clean up of resources.
333
     *
334
     * @param mixed $result The db specific result obtained from running a query.
335
     */
336
    protected function query_end($result) {
337
        if ($this->written) {
338
            // Adjust the written time.
339
            array_walk($this->written, function (&$val) {
340
                if ($val === true) {
341
                    $val = microtime(true);
342
                }
343
            });
344
        }
345
 
346
        parent::query_end($result);
347
    }
348
 
349
    /**
350
     * Select appropriate db handle - readwrite or readonly.
351
     *
352
     * @param int $type Type of query.
353
     * @param string $sql The sql to use.
354
     */
355
    protected function select_db_handle(int $type, string $sql): void {
356
        if ($this->dbhreadonly && $this->can_use_readonly($type, $sql)) {
357
            $this->readsreplica++;
358
            $this->set_db_handle($this->dbhreadonly);
359
            return;
360
        }
361
        $this->set_dbhwrite();
362
    }
363
 
364
    /**
365
     * Check if The query qualifies for readonly connection execution.
366
     *
367
     * Logging queries are exempt, those are write operations that circumvent standard query_start/query_end paths.
368
     *
369
     * @param int $type Type of query.
370
     * @param string $sql The sql to use.
371
     * @return bool
372
     */
373
    protected function can_use_readonly(int $type, string $sql): bool {
374
        if ($this->loggingquery) {
375
            return false;
376
        }
377
 
378
        if (during_initial_install()) {
379
            return false;
380
        }
381
 
382
        // Transactions are done as AUX, we cannot play with that.
383
        switch ($type) {
384
            case SQL_QUERY_AUX_READONLY:
385
                // SQL_QUERY_AUX_READONLY may read the structure data.
386
                // We don't have a way to reliably determine whether it is safe to go to readonly if the structure has changed.
387
                return !$this->structurechange;
388
            case SQL_QUERY_SELECT:
389
                if ($this->transactions) {
390
                    return false;
391
                }
392
 
393
                $now = null;
394
                foreach ($this->table_names($sql) as $tablename) {
395
                    if (in_array($tablename, $this->readexclude)) {
396
                        return false;
397
                    }
398
 
399
                    if ($this->temptables && $this->temptables->is_temptable($tablename)) {
400
                        return false;
401
                    }
402
 
403
                    if (isset($this->written[$tablename])) {
404
                        $now = $now ?: microtime(true);
405
 
406
                        if ($now - $this->written[$tablename] < $this->replicalatency) {
407
                            return false;
408
                        }
409
                        unset($this->written[$tablename]);
410
                    }
411
                }
412
 
413
                return true;
414
            case SQL_QUERY_INSERT:
415
            case SQL_QUERY_UPDATE:
416
                foreach ($this->table_names($sql) as $tablename) {
417
                    $this->written[$tablename] = true;
418
                }
419
                return false;
420
            case SQL_QUERY_STRUCTURE:
421
                $this->structurechange = true;
422
                foreach ($this->table_names($sql) as $tablename) {
423
                    if (!in_array($tablename, $this->readexclude)) {
424
                        $this->readexclude[] = $tablename;
425
                    }
426
                }
427
                return false;
428
        }
429
        return false;
430
    }
431
 
432
    /**
433
     * Indicates delegated transaction finished successfully.
434
     *
435
     * Set written times after outermost transaction finished.
436
     *
437
     * @param moodle_transaction $transaction The transaction to commit.
438
     * @throws dml_transaction_exception Creates and throws transaction related exceptions.
439
     */
440
    public function commit_delegated_transaction(moodle_transaction $transaction) {
441
        if ($this->written) {
442
            // Adjust the written time.
443
            $now = microtime(true);
444
            foreach ($this->written as $tablename => $when) {
445
                $this->written[$tablename] = $now;
446
            }
447
        }
448
 
449
        parent::commit_delegated_transaction($transaction);
450
    }
451
 
452
    /**
453
     * Parse table names from query.
454
     *
455
     * @param string $sql The sql to use.
456
     * @return array
457
     */
458
    protected function table_names(string $sql): array {
459
        preg_match_all('/\b'.$this->prefix.'([a-z][A-Za-z0-9_]*)/', $sql, $match);
460
        return $match[1];
461
    }
462
}