mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-66935 core_lock: Fix resource key clashes in db and postgres locks
This commit is contained in:
parent
9f997f9bd7
commit
7e086935e3
5 changed files with 86 additions and 32 deletions
|
@ -120,15 +120,17 @@ class db_record_lock_factory implements lock_factory {
|
|||
$giveuptime = $now + $timeout;
|
||||
$expires = $now + $maxlifetime;
|
||||
|
||||
if (!$this->db->record_exists('lock_db', array('resourcekey' => $resource))) {
|
||||
$resourcekey = $this->type . '_' . $resource;
|
||||
|
||||
if (!$this->db->record_exists('lock_db', array('resourcekey' => $resourcekey))) {
|
||||
$record = new \stdClass();
|
||||
$record->resourcekey = $resource;
|
||||
$record->resourcekey = $resourcekey;
|
||||
$result = $this->db->insert_record('lock_db', $record);
|
||||
}
|
||||
|
||||
$params = array('expires' => $expires,
|
||||
'token' => $token,
|
||||
'resourcekey' => $resource,
|
||||
'resourcekey' => $resourcekey,
|
||||
'now' => $now);
|
||||
$sql = 'UPDATE {lock_db}
|
||||
SET
|
||||
|
@ -143,7 +145,7 @@ class db_record_lock_factory implements lock_factory {
|
|||
$params['now'] = $now;
|
||||
$this->db->execute($sql, $params);
|
||||
|
||||
$countparams = array('owner' => $token, 'resourcekey' => $resource);
|
||||
$countparams = array('owner' => $token, 'resourcekey' => $resourcekey);
|
||||
$result = $this->db->count_records('lock_db', $countparams);
|
||||
$locked = $result === 1;
|
||||
if (!$locked && $timeout > 0) {
|
||||
|
|
|
@ -38,18 +38,17 @@ defined('MOODLE_INTERNAL') || die();
|
|||
class lock_config {
|
||||
|
||||
/**
|
||||
* Get an instance of the currently configured locking subclass.
|
||||
* Get the currently configured locking subclass.
|
||||
*
|
||||
* @param string $type - Unique namespace for the locks generated by this factory. e.g. core_cron
|
||||
* @return \core\lock\lock_factory
|
||||
* @return string class name
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_lock_factory($type) {
|
||||
public static function get_lock_factory_class(): string {
|
||||
|
||||
global $CFG, $DB;
|
||||
$lockfactory = null;
|
||||
|
||||
if (during_initial_install()) {
|
||||
$lockfactory = new \core\lock\installation_lock_factory($type);
|
||||
$lockfactoryclass = '\core\lock\installation_lock_factory';
|
||||
} else if (isset($CFG->lock_factory) && $CFG->lock_factory != 'auto') {
|
||||
if (!class_exists($CFG->lock_factory)) {
|
||||
// In this case I guess it is not safe to continue. Different cluster nodes could end up using different locking
|
||||
|
@ -57,7 +56,6 @@ class lock_config {
|
|||
throw new \coding_exception('Lock factory set in $CFG does not exist: ' . $CFG->lock_factory);
|
||||
}
|
||||
$lockfactoryclass = $CFG->lock_factory;
|
||||
$lockfactory = new $lockfactoryclass($type);
|
||||
} else {
|
||||
$dbtype = clean_param($DB->get_dbfamily(), PARAM_ALPHA);
|
||||
|
||||
|
@ -66,14 +64,31 @@ class lock_config {
|
|||
if (!class_exists($lockfactoryclass)) {
|
||||
$lockfactoryclass = '\core\lock\file_lock_factory';
|
||||
}
|
||||
/* @var lock_factory $lockfactory */
|
||||
$lockfactory = new $lockfactoryclass($type);
|
||||
|
||||
// Test if the auto chosen lock factory is available.
|
||||
$lockfactory = new $lockfactoryclass('test');
|
||||
if (!$lockfactory->is_available()) {
|
||||
// Final fallback - DB row locking.
|
||||
$lockfactory = new \core\lock\db_record_lock_factory($type);
|
||||
$lockfactoryclass = '\core\lock\db_record_lock_factory';
|
||||
}
|
||||
}
|
||||
|
||||
return $lockfactoryclass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the currently configured locking subclass.
|
||||
*
|
||||
* @param string $type - Unique namespace for the locks generated by this factory. e.g. core_cron
|
||||
* @return \core\lock\lock_factory
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_lock_factory(string $type): \core\lock\lock_factory {
|
||||
$lockfactoryclass = self::get_lock_factory_class();
|
||||
$lockfactory = new $lockfactoryclass($type);
|
||||
if (!$lockfactory->is_available()) {
|
||||
throw new \coding_exception("Lock factory class $lockfactoryclass is not available.");
|
||||
}
|
||||
return $lockfactory;
|
||||
}
|
||||
|
||||
|
|
|
@ -178,7 +178,7 @@ class postgres_lock_factory implements lock_factory {
|
|||
public function get_lock($resource, $timeout, $maxlifetime = 86400) {
|
||||
$giveuptime = time() + $timeout;
|
||||
|
||||
$token = $this->get_index_from_key($resource);
|
||||
$token = $this->get_index_from_key($this->type . '_' . $resource);
|
||||
|
||||
$params = array('locktype' => $this->dblockid,
|
||||
'token' => $token);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue