Skip to content

Commit

Permalink
MDL-19711 dml: Enable use of readonly slave database handles
Browse files Browse the repository at this point in the history
Implemented with moodle_read_slave_trait

Functionality is triggered by supplying config dboption['readonly'].
See config-dist.php for more info on supported dboptions.

pgsql and mysqli drivers are using this feature. Also added support for
connection timeout for these two drivers.
  • Loading branch information
srdjan-catalyst committed May 19, 2020
1 parent d851183 commit 46cfde3
Show file tree
Hide file tree
Showing 19 changed files with 2,422 additions and 28 deletions.
39 changes: 39 additions & 0 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,45 @@
// set to zero if you are using pg_bouncer in
// 'transaction' mode (it is fine in 'session'
// mode).
/*
'connecttimeout' => null, // Set connect timeout in seconds. Not all drivers support it.
'readonly' => [ // Set to read-only slave details, to get safe reads
// from there instead of the master node. Optional.
// Currently supported by pgsql and mysqli variety classes.
// If not supported silently ignored.
'instance' => [ // Readonly slave connection parameters
[
'dbhost' => 'slave.dbhost',
'dbport' => '', // Defaults to master port
'dbuser' => '', // Defaults to master user
'dbpass' => '', // Defaults to master password
],
[...],
],
Instance(s) can alternatively be specified as:
'instance' => 'slave.dbhost',
'instance' => ['slave.dbhost1', 'slave.dbhost2'],
'instance' => ['dbhost' => 'slave.dbhost', 'dbport' => '', 'dbuser' => '', 'dbpass' => ''],
'connecttimeout' => 2, // Set read-only slave connect timeout in seconds. See above.
'latency' => 0.5, // Set read-only slave sync latency in seconds.
// When 'latency' seconds have lapsed after an update to a table
// it is deemed safe to use readonly slave for reading from the table.
// It is optional. If omitted once written to a table it will always
// use master handle for reading.
// Lower values increase the performance, but setting it too low means
// missing the master-slave sync.
'exclude_tables' => [ // Tables to exclude from read-only slave feature.
'table1', // Should not be used, unless in rare cases when some area of the system
'table2', // is malfunctioning and you still want to use readonly feature.
], // Then one can exclude offending tables while investigating.
More info available in lib/dml/moodle_read_slave_trait.php where the feature is implemented.
]
*/
// For all database config settings see https://docs.moodle.org/en/Database_settings
);


Expand Down
20 changes: 18 additions & 2 deletions lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ abstract class moodle_database {
/** @var float Last time in seconds with millisecond precision. */
protected $last_time;
/** @var bool Flag indicating logging of query in progress. This helps prevent infinite loops. */
private $loggingquery = false;
protected $loggingquery = false;

/** @var bool True if the db is used for db sessions. */
protected $used_for_db_sessions = false;

/** @var array Array containing open transactions. */
private $transactions = array();
protected $transactions = array();
/** @var bool Flag used to force rollback of all current transactions. */
private $force_rollback = false;

Expand Down Expand Up @@ -2717,6 +2717,22 @@ public function perf_get_reads() {
return $this->reads;
}

/**
* Returns whether we want to connect to slave database for read queries.
* @return bool Want read only connection
*/
public function want_read_slave(): bool {
return false;
}

/**
* Returns the number of reads before first write done by this database.
* @return int Number of reads.
*/
public function perf_get_reads_slave(): int {
return 0;
}

/**
* Returns the number of writes done by this database.
* @return int Number of writes.
Expand Down
Loading

0 comments on commit 46cfde3

Please sign in to comment.