Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * MySQL / MariaDB locking factory.
  19   *
  20   * @package    core
  21   * @category   lock
  22   * @copyright  Brendan Heywood <brendan@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\lock;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * MySQL / MariaDB locking factory.
  32   *
  33   * @package   core
  34   * @category  lock
  35   * @copyright Brendan Heywood <brendan@catalyst-au.net>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class mysql_lock_factory implements lock_factory {
  39  
  40      /** @var string $dbprefix - used as a namespace for these types of locks */
  41      protected $dbprefix = '';
  42  
  43      /** @var \moodle_database $db Hold a reference to the global $DB */
  44      protected $db;
  45  
  46      /** @var array $openlocks - List of held locks - used by auto-release */
  47      protected $openlocks = [];
  48  
  49      /**
  50       * Return a unique prefix based on the database name and prefix.
  51       * @param string $type - Used to prefix lock keys.
  52       * @return string.
  53       */
  54      protected function get_unique_db_prefix($type) {
  55          global $CFG;
  56          $prefix = $CFG->dbname . ':';
  57          if (isset($CFG->prefix)) {
  58              $prefix .= $CFG->prefix;
  59          }
  60          $prefix .= '_' . $type . '_';
  61          return $prefix;
  62      }
  63  
  64      /**
  65       * Lock constructor.
  66       * @param string $type - Used to prefix lock keys.
  67       */
  68      public function __construct($type) {
  69          global $DB;
  70  
  71          $this->dbprefix = $this->get_unique_db_prefix($type);
  72          // Save a reference to the global $DB so it will not be released while we still have open locks.
  73          $this->db = $DB;
  74  
  75          \core_shutdown_manager::register_function([$this, 'auto_release']);
  76      }
  77  
  78      /**
  79       * Is available.
  80       * @return boolean - True if this lock type is available in this environment.
  81       */
  82      public function is_available() {
  83          return $this->db->get_dbfamily() === 'mysql';
  84      }
  85  
  86      /**
  87       * Return information about the blocking behaviour of the lock type on this platform.
  88       * @return boolean - Defer to the DB driver.
  89       */
  90      public function supports_timeout() {
  91          return true;
  92      }
  93  
  94      /**
  95       * Will this lock type will be automatically released when a process ends.
  96       *
  97       * @return boolean - Via shutdown handler.
  98       */
  99      public function supports_auto_release() {
 100          return true;
 101      }
 102  
 103      /**
 104       * Multiple locks for the same resource can NOT be held by a single process.
 105       *
 106       * Hard coded to false and workaround inconsistent support in different
 107       * versions of MySQL / MariaDB.
 108       *
 109       * @return boolean - false
 110       */
 111      public function supports_recursion() {
 112          return false;
 113      }
 114  
 115      /**
 116       * Create and get a lock
 117       * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
 118       * @param int $timeout - The number of seconds to wait for a lock before giving up.
 119       * @param int $maxlifetime - Unused by this lock type.
 120       * @return boolean - true if a lock was obtained.
 121       */
 122      public function get_lock($resource, $timeout, $maxlifetime = 86400) {
 123  
 124          // We sha1 to avoid long key names hitting the mysql str limit.
 125          $resourcekey = sha1($this->dbprefix . $resource);
 126  
 127          // Even though some versions of MySQL and MariaDB can support stacked locks
 128          // just never stack them and always fail fast.
 129          if (isset($this->openlocks[$resourcekey])) {
 130              return false;
 131          }
 132  
 133          $params = [
 134              'resourcekey' => $resourcekey,
 135              'timeout' => $timeout
 136          ];
 137  
 138          $result = $this->db->get_record_sql('SELECT GET_LOCK(:resourcekey, :timeout) AS locked', $params);
 139          $locked = $result->locked == 1;
 140  
 141          if ($locked) {
 142              $this->openlocks[$resourcekey] = 1;
 143              return new lock($resourcekey, $this);
 144          }
 145          return false;
 146      }
 147  
 148      /**
 149       * Release a lock that was previously obtained with @lock.
 150       * @param lock $lock - a lock obtained from this factory.
 151       * @return boolean - true if the lock is no longer held (including if it was never held).
 152       */
 153      public function release_lock(lock $lock) {
 154  
 155          $params = [
 156              'resourcekey' => $lock->get_key()
 157          ];
 158          $result = $this->db->get_record_sql('SELECT RELEASE_LOCK(:resourcekey) AS unlocked', $params);
 159          $result = $result->unlocked == 1;
 160          if ($result) {
 161              unset($this->openlocks[$lock->get_key()]);
 162          }
 163          return $result;
 164      }
 165  
 166      /**
 167       * Extend a lock that was previously obtained with @lock.
 168       * @param lock $lock - a lock obtained from this factory.
 169       * @param int $maxlifetime - the new lifetime for the lock (in seconds).
 170       * @return boolean - true if the lock was extended.
 171       */
 172      public function extend_lock(lock $lock, $maxlifetime = 86400) {
 173          // Not supported by this factory.
 174          return false;
 175      }
 176  
 177      /**
 178       * Auto release any open locks on shutdown.
 179       * This is required, because we may be using persistent DB connections.
 180       */
 181      public function auto_release() {
 182          // Called from the shutdown handler. Must release all open locks.
 183          foreach ($this->openlocks as $key => $unused) {
 184              $lock = new lock($key, $this);
 185              $lock->release();
 186          }
 187      }
 188  
 189  }