Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 19... Línea 19...
19
 *
19
 *
20
 * @package    core
20
 * @package    core
21
 * @category   dml
21
 * @category   dml
22
 * @copyright  2018 Srdjan Janković, Catalyst IT
22
 * @copyright  2018 Srdjan Janković, Catalyst IT
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
24
 * @deprecated Since Moodle 5.0. See MDL-71257.
-
 
25
 * @todo       Final deprecation in Moodle 6.0. See MDL-83171.
24
 */
26
 */
-
 
27
#[\core\attribute\deprecated(
-
 
28
    replacement: moodle_read_replica_trait::class,
-
 
29
    since: '5.0',
-
 
30
    mdl: 'MDL-71257',
-
 
31
    reason: 'Renamed'
25
 
32
)]
26
defined('MOODLE_INTERNAL') || die();
-
 
Línea 27... Línea 33...
27
 
33
 
28
/**
34
/**
29
 * Trait to wrap connect() method of database driver classes that gives
35
 * Trait to wrap connect() method of database driver classes that gives
30
 * ability to use read only slave instances for SELECT queries. For the
36
 * ability to use read only slave instances for SELECT queries. For the
Línea 128... Línea 134...
128
     * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
134
     * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
129
     * @param array $dboptions driver specific options
135
     * @param array $dboptions driver specific options
130
     * @return bool true
136
     * @return bool true
131
     * @throws dml_connection_exception if error
137
     * @throws dml_connection_exception if error
132
     */
138
     */
133
    abstract protected function raw_connect(string $dbhost, string $dbuser, string $dbpass, string $dbname, $prefix, array $dboptions = null): bool;
139
    abstract protected function raw_connect(string $dbhost, string $dbuser, string $dbpass, string $dbname, $prefix, ?array $dboptions = null): bool;
Línea 134... Línea 140...
134
 
140
 
135
    /**
141
    /**
136
     * Connect to db
142
     * Connect to db
137
     * The connection parameters processor that sets up stage for master write and slave readonly handles.
143
     * The connection parameters processor that sets up stage for master write and slave readonly handles.
Línea 142... Línea 148...
142
     * @param string $dbname The name of the database being connected to.
148
     * @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
149
     * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
144
     * @param array $dboptions driver specific options
150
     * @param array $dboptions driver specific options
145
     * @return bool true
151
     * @return bool true
146
     * @throws dml_connection_exception if error
152
     * @throws dml_connection_exception if error
-
 
153
     * @deprecated Since Moodle 5.0. See MDL-71257.
-
 
154
     * @todo Final deprecation in Moodle 6.0. See MDL-83171.
147
     */
155
     */
-
 
156
    #[\core\attribute\deprecated(
-
 
157
        replacement: 'moodle_read_replica_trait::connect',
-
 
158
        since: '5.0',
-
 
159
        mdl: 'MDL-71257',
-
 
160
        reason: 'Renamed trait'
-
 
161
    )]
148
    public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions = null) {
162
    public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, ?array $dboptions = null) {
-
 
163
        \core\deprecation::emit_deprecation(__FUNCTION__);
149
        $this->pdbhost = $dbhost;
164
        $this->pdbhost = $dbhost;
150
        $this->pdbuser = $dbuser;
165
        $this->pdbuser = $dbuser;
151
        $this->pdbpass = $dbpass;
166
        $this->pdbpass = $dbpass;
152
        $this->pdbname = $dbname;
167
        $this->pdbname = $dbname;
153
        $this->pprefix = $prefix;
168
        $this->pprefix = $prefix;
154
        $this->pdboptions = $dboptions;
169
        $this->pdboptions = $dboptions;
Línea -... Línea 170...
-
 
170
 
155
 
171
        $logconnection = false;
156
        if ($dboptions) {
172
        if ($dboptions) {
157
            if (isset($dboptions['readonly'])) {
173
            if (isset($dboptions['readonly'])) {
158
                $this->wantreadslave = true;
174
                $this->wantreadslave = true;
Línea 178... Línea 194...
178
                if (!is_array($slaves) || !isset($slaves[0])) {
194
                if (!is_array($slaves) || !isset($slaves[0])) {
179
                    $slaves = [$slaves];
195
                    $slaves = [$slaves];
180
                }
196
                }
Línea 181... Línea 197...
181
 
197
 
-
 
198
                if (count($slaves) > 1) {
-
 
199
                    // Don't shuffle for unit tests as order is important for them to pass.
182
                if (count($slaves) > 1) {
200
                    if (!PHPUNIT_TEST) {
183
                    // Randomise things a bit.
201
                        // Randomise things a bit.
-
 
202
                        shuffle($slaves);
184
                    shuffle($slaves);
203
                    }
Línea 185... Línea 204...
185
                }
204
                }
186
 
205
 
187
                // Find first connectable readonly slave.
206
                // Find first connectable readonly slave.
Línea 196... Línea 215...
196
                    $dboptions['dbport'] = isset($slave['dbport']) ? $slave['dbport'] : $dbport;
215
                    $dboptions['dbport'] = isset($slave['dbport']) ? $slave['dbport'] : $dbport;
Línea 197... Línea 216...
197
 
216
 
198
                    try {
217
                    try {
199
                        $this->raw_connect($rodb['dbhost'], $rodb['dbuser'], $rodb['dbpass'], $dbname, $prefix, $dboptions);
218
                        $this->raw_connect($rodb['dbhost'], $rodb['dbuser'], $rodb['dbpass'], $dbname, $prefix, $dboptions);
-
 
219
                        $this->dbhreadonly = $this->get_db_handle();
-
 
220
                        if ($logconnection) {
-
 
221
                            debugging(
-
 
222
                                "Readonly db connection succeeded for host {$rodb['dbhost']}"
-
 
223
                            );
200
                        $this->dbhreadonly = $this->get_db_handle();
224
                        }
201
                        break;
225
                        break;
-
 
226
                    } catch (dml_connection_exception $e) {
202
                    } catch (dml_connection_exception $e) { // phpcs:ignore
227
                        debugging(
-
 
228
                            "Readonly db connection failed for host {$rodb['dbhost']}: {$e->debuginfo}"
-
 
229
                        );
203
                        // If readonly slave is not connectable we'll have to do without it.
230
                        $logconnection = true;
204
                    }
231
                    }
205
                }
232
                }
206
                // ... lock_db queries always go to master.
233
                // ... lock_db queries always go to master.
207
                // Since it is a lock and as such marshalls concurrent connections,
234
                // Since it is a lock and as such marshalls concurrent connections,
Línea 210... Línea 237...
210
                // ... and sessions.
237
                // ... and sessions.
211
                $this->readexclude[] = 'sessions';
238
                $this->readexclude[] = 'sessions';
212
            }
239
            }
213
        }
240
        }
214
        if (!$this->dbhreadonly) {
241
        if (!$this->dbhreadonly) {
-
 
242
            try {
215
            $this->set_dbhwrite();
243
                $this->set_dbhwrite();
-
 
244
            } catch (dml_connection_exception $e) {
-
 
245
                debugging(
-
 
246
                    "Readwrite db connection failed for host {$this->pdbhost}: {$e->debuginfo}"
-
 
247
                );
-
 
248
                throw $e;
-
 
249
            }
-
 
250
            if ($logconnection) {
-
 
251
                debugging(
-
 
252
                    "Readwrite db connection succeeded for host {$this->pdbhost}"
-
 
253
                );
-
 
254
            }
216
        }
255
        }
Línea 217... Línea 256...
217
 
256
 
218
        return true;
257
        return true;
Línea 237... Línea 276...
237
    }
276
    }
Línea 238... Línea 277...
238
 
277
 
239
    /**
278
    /**
240
     * Returns whether we want to connect to slave database for read queries.
279
     * Returns whether we want to connect to slave database for read queries.
-
 
280
     * @return bool Want read only connection
-
 
281
     * @deprecated Since Moodle 5.0. See MDL-71257.
241
     * @return bool Want read only connection
282
     * @todo Final deprecation in Moodle 6.0. See MDL-83171.
-
 
283
     */
-
 
284
    #[\core\attribute\deprecated(
-
 
285
        replacement: 'moodle_read_replica_trait::want_read_replica',
-
 
286
        since: '5.0',
-
 
287
        mdl: 'MDL-71257',
-
 
288
        reason: 'Renamed trait'
242
     */
289
    )]
-
 
290
    public function want_read_slave(): bool {
243
    public function want_read_slave(): bool {
291
        \core\deprecation::emit_deprecation(__FUNCTION__);
244
        return $this->wantreadslave;
292
        return $this->wantreadslave;
Línea 245... Línea 293...
245
    }
293
    }
246
 
294
 
247
    /**
295
    /**
-
 
296
     * Returns the number of reads done by the read only database.
-
 
297
     * @return int Number of reads.
248
     * Returns the number of reads done by the read only database.
298
     * @deprecated Since Moodle 5.0. See MDL-71257.
-
 
299
     * @todo Final deprecation in Moodle 6.0. See MDL-83171.
-
 
300
     */
-
 
301
    #[\core\attribute\deprecated(
-
 
302
        replacement: 'moodle_read_replica_trait::perf_get_reads_replica',
-
 
303
        since: '5.0',
-
 
304
        mdl: 'MDL-71257',
249
     * @return int Number of reads.
305
        reason: 'Renamed trait'
-
 
306
    )]
250
     */
307
    public function perf_get_reads_slave(): int {
251
    public function perf_get_reads_slave(): int {
308
        \core\deprecation::emit_deprecation(__FUNCTION__);
Línea 252... Línea 309...
252
        return $this->readsslave;
309
        return $this->readsslave;
253
    }
310
    }
254
 
311
 
-
 
312
    /**
-
 
313
     * On DBs that support it, switch to transaction mode and begin a transaction
255
    /**
314
     * @return moodle_transaction
-
 
315
     * @deprecated Since Moodle 5.0. See MDL-71257.
-
 
316
     * @todo Final deprecation in Moodle 6.0. See MDL-83171.
-
 
317
     */
-
 
318
    #[\core\attribute\deprecated(
-
 
319
        replacement: 'moodle_read_replica_trait::start_delegated_transaction',
-
 
320
        since: '5.0',
256
     * On DBs that support it, switch to transaction mode and begin a transaction
321
        mdl: 'MDL-71257',
-
 
322
        reason: 'Renamed trait'
257
     * @return moodle_transaction
323
    )]
258
     */
324
    public function start_delegated_transaction() {
259
    public function start_delegated_transaction() {
325
        \core\deprecation::emit_deprecation(__FUNCTION__);
Línea 260... Línea 326...
260
        $this->set_dbhwrite();
326
        $this->set_dbhwrite();
Línea 379... Línea 445...
379
     * Indicates delegated transaction finished successfully.
445
     * Indicates delegated transaction finished successfully.
380
     * Set written times after outermost transaction finished
446
     * Set written times after outermost transaction finished
381
     * @param moodle_transaction $transaction The transaction to commit
447
     * @param moodle_transaction $transaction The transaction to commit
382
     * @return void
448
     * @return void
383
     * @throws dml_transaction_exception Creates and throws transaction related exceptions.
449
     * @throws dml_transaction_exception Creates and throws transaction related exceptions.
-
 
450
     * @deprecated Since Moodle 5.0. See MDL-71257.
-
 
451
     * @todo Final deprecation in Moodle 6.0. See MDL-83171.
384
     */
452
     */
-
 
453
    #[\core\attribute\deprecated(
-
 
454
        replacement: 'moodle_read_replica_trait::commit_delegated_transaction',
-
 
455
        since: '5.0',
-
 
456
        mdl: 'MDL-71257',
-
 
457
        reason: 'Renamed trait'
-
 
458
    )]
385
    public function commit_delegated_transaction(moodle_transaction $transaction) {
459
    public function commit_delegated_transaction(moodle_transaction $transaction) {
-
 
460
        \core\deprecation::emit_deprecation(__FUNCTION__);
386
        if ($this->written) {
461
        if ($this->written) {
387
            // Adjust the written time.
462
            // Adjust the written time.
388
            $now = microtime(true);
463
            $now = microtime(true);
389
            foreach ($this->written as $tablename => $when) {
464
            foreach ($this->written as $tablename => $when) {
390
                $this->written[$tablename] = $now;
465
                $this->written[$tablename] = $now;