Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core\lock;
18
 
19
use coding_exception;
20
 
21
/**
22
 * This is a db record locking factory.
23
 *
24
 * This lock factory uses record locks relying on sql of the form "SET XXX where YYY" and checking if the
25
 * value was set. It supports timeouts, autorelease and can work on any DB. The downside - is this
26
 * will always be slower than some shared memory type locking function.
27
 *
28
 * @package   core
29
 * @category  lock
30
 * @copyright Damyon Wiese 2013
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class db_record_lock_factory implements lock_factory {
34
 
35
    /** @var \moodle_database $db Hold a reference to the global $DB */
36
    protected $db;
37
 
38
    /** @var string $type Used to prefix lock keys */
39
    protected $type;
40
 
41
    /** @var array $openlocks - List of held locks - used by auto-release */
42
    protected $openlocks = array();
43
 
44
    /**
45
     * Is available.
46
     * @return boolean - True if this lock type is available in this environment.
47
     */
48
    public function is_available() {
49
        return true;
50
    }
51
 
52
    /**
53
     * Almighty constructor.
54
     * @param string $type - Used to prefix lock keys.
55
     */
56
    public function __construct($type) {
57
        global $DB;
58
 
59
        $this->type = $type;
60
        // Save a reference to the global $DB so it will not be released while we still have open locks.
61
        $this->db = $DB;
62
 
63
        \core_shutdown_manager::register_function(array($this, 'auto_release'));
64
    }
65
 
66
    /**
67
     * Return information about the blocking behaviour of the lock type on this platform.
68
     * @return boolean - True
69
     */
70
    public function supports_timeout() {
71
        return true;
72
    }
73
 
74
    /**
75
     * Will this lock type will be automatically released when a process ends.
76
     *
77
     * @return boolean - True (shutdown handler)
78
     */
79
    public function supports_auto_release() {
80
        return true;
81
    }
82
 
83
    /**
84
     * This function generates a unique token for the lock to use.
85
     * It is important that this token is not solely based on time as this could lead
86
     * to duplicates in a clustered environment (especially on VMs due to poor time precision).
87
     */
88
    protected function generate_unique_token() {
89
        return \core\uuid::generate();
90
    }
91
 
92
    /**
93
     * Create and get a lock
94
     * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
95
     * @param int $timeout - The number of seconds to wait for a lock before giving up.
96
     * @param int $maxlifetime - Unused by this lock type.
97
     * @return boolean - true if a lock was obtained.
98
     */
99
    public function get_lock($resource, $timeout, $maxlifetime = 86400) {
100
 
101
        $token = $this->generate_unique_token();
102
        $now = time();
103
        $giveuptime = $now + $timeout;
104
        $expires = $now + $maxlifetime;
105
 
106
        $resourcekey = $this->type . '_' . $resource;
107
 
108
        if (!$this->db->record_exists('lock_db', array('resourcekey' => $resourcekey))) {
109
            $record = new \stdClass();
110
            $record->resourcekey = $resourcekey;
111
            $result = $this->db->insert_record('lock_db', $record);
112
        }
113
 
114
        $params = array('expires' => $expires,
115
                        'token' => $token,
116
                        'resourcekey' => $resourcekey,
117
                        'now' => $now);
118
        $sql = 'UPDATE {lock_db}
119
                   SET
120
                       expires = :expires,
121
                       owner = :token
122
                 WHERE
123
                       resourcekey = :resourcekey AND
124
                       (owner IS NULL OR expires < :now)';
125
 
126
        do {
127
            $now = time();
128
            $params['now'] = $now;
129
            $this->db->execute($sql, $params);
130
 
131
            $countparams = array('owner' => $token, 'resourcekey' => $resourcekey);
132
            $result = $this->db->count_records('lock_db', $countparams);
133
            $locked = $result === 1;
134
            if (!$locked && $timeout > 0) {
135
                usleep(rand(10000, 250000)); // Sleep between 10 and 250 milliseconds.
136
            }
137
            // Try until the giveup time.
138
        } while (!$locked && $now < $giveuptime);
139
 
140
        if ($locked) {
141
            $this->openlocks[$token] = 1;
142
            return new lock($token, $this);
143
        }
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
        $params = array('noexpires' => null,
155
                        'token' => $lock->get_key(),
156
                        'noowner' => null);
157
 
158
        $sql = 'UPDATE {lock_db}
159
                    SET
160
                        expires = :noexpires,
161
                        owner = :noowner
162
                    WHERE
163
                        owner = :token';
164
        $result = $this->db->execute($sql, $params);
165
        if ($result) {
166
            unset($this->openlocks[$lock->get_key()]);
167
        }
168
        return $result;
169
    }
170
 
171
    /**
172
     * Auto release any open locks on shutdown.
173
     * This is required, because we may be using persistent DB connections.
174
     */
175
    public function auto_release() {
176
        // Called from the shutdown handler. Must release all open locks.
177
        foreach ($this->openlocks as $key => $unused) {
178
            $lock = new lock($key, $this);
179
            $lock->release();
180
        }
181
    }
182
}