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   * Postgres advisory locking factory.
  19   *
  20   * @package    core
  21   * @category   lock
  22   * @copyright  Damyon Wiese 2013
  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   * Postgres advisory locking factory.
  32   *
  33   * Postgres locking implementation using advisory locks. Some important points. Postgres has
  34   * 2 different forms of lock functions, some accepting a single int, and some accepting 2 ints. This implementation
  35   * uses the 2 int version so that it uses a separate namespace from the session locking. The second note,
  36   * is because postgres uses integer keys for locks, we first need to map strings to a unique integer. This is done
  37   * using a prefix of a sha1 hash converted to an integer.
  38   *
  39   * @package   core
  40   * @category  lock
  41   * @copyright Damyon Wiese 2013
  42   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class postgres_lock_factory implements lock_factory {
  45  
  46      /** @var int $dblockid - used as a namespace for these types of locks (separate from session locks) */
  47      protected $dblockid = -1;
  48  
  49      /** @var array $lockidcache - static cache for string -> int conversions required for pg advisory locks. */
  50      protected static $lockidcache = array();
  51  
  52      /** @var \moodle_database $db Hold a reference to the global $DB */
  53      protected $db;
  54  
  55      /** @var string $type Used to prefix lock keys */
  56      protected $type;
  57  
  58      /** @var array $openlocks - List of held locks - used by auto-release */
  59      protected $openlocks = array();
  60  
  61      /**
  62       * Calculate a unique instance id based on the database name and prefix.
  63       * @return int.
  64       */
  65      protected function get_unique_db_instance_id() {
  66          global $CFG;
  67  
  68          $strkey = $CFG->dbname . ':' . $CFG->prefix;
  69          $intkey = crc32($strkey);
  70          // Normalize between 64 bit unsigned int and 32 bit signed ints. Php could return either from crc32.
  71          if (PHP_INT_SIZE == 8) {
  72              if ($intkey > 0x7FFFFFFF) {
  73                  $intkey -= 0x100000000;
  74              }
  75          }
  76  
  77          return $intkey;
  78      }
  79  
  80      /**
  81       * Almighty constructor.
  82       * @param string $type - Used to prefix lock keys.
  83       */
  84      public function __construct($type) {
  85          global $DB;
  86  
  87          $this->type = $type;
  88          $this->dblockid = $this->get_unique_db_instance_id();
  89          // Save a reference to the global $DB so it will not be released while we still have open locks.
  90          $this->db = $DB;
  91  
  92          \core_shutdown_manager::register_function(array($this, 'auto_release'));
  93      }
  94  
  95      /**
  96       * Is available.
  97       * @return boolean - True if this lock type is available in this environment.
  98       */
  99      public function is_available() {
 100          return $this->db->get_dbfamily() === 'postgres';
 101      }
 102  
 103      /**
 104       * Return information about the blocking behaviour of the lock type on this platform.
 105       * @return boolean - Defer to the DB driver.
 106       */
 107      public function supports_timeout() {
 108          return true;
 109      }
 110  
 111      /**
 112       * Will this lock type will be automatically released when a process ends.
 113       *
 114       * @return boolean - Via shutdown handler.
 115       */
 116      public function supports_auto_release() {
 117          return true;
 118      }
 119  
 120      /**
 121       * Multiple locks for the same resource can be held by a single process.
 122       * @return boolean - Defer to the DB driver.
 123       */
 124      public function supports_recursion() {
 125          return true;
 126      }
 127  
 128      /**
 129       * This function generates the unique index for a specific lock key using
 130       * a sha1 prefix converted to decimal.
 131       *
 132       * @param string $key
 133       * @return int
 134       * @throws \moodle_exception
 135       */
 136      protected function get_index_from_key($key) {
 137  
 138          // A prefix of 7 hex chars is chosen as fffffff is the largest hex code
 139          // which when converted to decimal (268435455) fits inside a 4 byte int
 140          // which is the second param to pg_try_advisory_lock().
 141          $hash = substr(sha1($key), 0, 7);
 142          $index = hexdec($hash);
 143          return $index;
 144      }
 145  
 146      /**
 147       * Create and get a lock
 148       * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
 149       * @param int $timeout - The number of seconds to wait for a lock before giving up.
 150       * @param int $maxlifetime - Unused by this lock type.
 151       * @return boolean - true if a lock was obtained.
 152       */
 153      public function get_lock($resource, $timeout, $maxlifetime = 86400) {
 154          $giveuptime = time() + $timeout;
 155  
 156          $token = $this->get_index_from_key($this->type . '_' . $resource);
 157  
 158          $params = array('locktype' => $this->dblockid,
 159                          'token' => $token);
 160  
 161          $locked = false;
 162  
 163          do {
 164              $result = $this->db->get_record_sql('SELECT pg_try_advisory_lock(:locktype, :token) AS locked', $params);
 165              $locked = $result->locked === 't';
 166              if (!$locked && $timeout > 0) {
 167                  usleep(rand(10000, 250000)); // Sleep between 10 and 250 milliseconds.
 168              }
 169              // Try until the giveup time.
 170          } while (!$locked && time() < $giveuptime);
 171  
 172          if ($locked) {
 173              $this->openlocks[$token] = 1;
 174              return new lock($token, $this);
 175          }
 176          return false;
 177      }
 178  
 179      /**
 180       * Release a lock that was previously obtained with @lock.
 181       * @param lock $lock - a lock obtained from this factory.
 182       * @return boolean - true if the lock is no longer held (including if it was never held).
 183       */
 184      public function release_lock(lock $lock) {
 185          $params = array('locktype' => $this->dblockid,
 186                          'token' => $lock->get_key());
 187          $result = $this->db->get_record_sql('SELECT pg_advisory_unlock(:locktype, :token) AS unlocked', $params);
 188          $result = $result->unlocked === 't';
 189          if ($result) {
 190              unset($this->openlocks[$lock->get_key()]);
 191          }
 192          return $result;
 193      }
 194  
 195      /**
 196       * Extend a lock that was previously obtained with @lock.
 197       * @param lock $lock - a lock obtained from this factory.
 198       * @param int $maxlifetime - the new lifetime for the lock (in seconds).
 199       * @return boolean - true if the lock was extended.
 200       */
 201      public function extend_lock(lock $lock, $maxlifetime = 86400) {
 202          // Not supported by this factory.
 203          return false;
 204      }
 205  
 206      /**
 207       * Auto release any open locks on shutdown.
 208       * This is required, because we may be using persistent DB connections.
 209       */
 210      public function auto_release() {
 211          // Called from the shutdown handler. Must release all open locks.
 212          foreach ($this->openlocks as $key => $unused) {
 213              $lock = new lock($key, $this);
 214              $lock->release();
 215          }
 216      }
 217  
 218  }