1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
<?php
namespace Drupal\Core\Lock;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\Database\IntegrityConstraintViolationException;
/**
* Defines the database lock backend. This is the default backend in Drupal.
*
* @ingroup lock
*/
class DatabaseLockBackend extends LockBackendAbstract {
/**
* The database table name.
*/
const TABLE_NAME = 'semaphore';
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a new DatabaseLockBackend.
*
* @param \Drupal\Core\Database\Connection $database
* The database connection.
*/
public function __construct(Connection $database) {
// __destruct() is causing problems with garbage collections, register a
// shutdown function instead.
drupal_register_shutdown_function([$this, 'releaseAll']);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public function acquire($name, $timeout = 30.0) {
$name = $this->normalizeName($name);
// Insure that the timeout is at least 1 ms.
$timeout = max($timeout, 0.001);
$expire = microtime(TRUE) + $timeout;
if (isset($this->locks[$name])) {
// Try to extend the expiration of a lock we already acquired.
$success = (bool) $this->database->update('semaphore')
->fields(['expire' => $expire])
->condition('name', $name)
->condition('value', $this->getLockId())
->execute();
if (!$success) {
// The lock was broken.
unset($this->locks[$name]);
}
return $success;
}
else {
// Optimistically try to acquire the lock, then retry once if it fails.
// The first time through the loop cannot be a retry.
$retry = FALSE;
// We always want to do this code at least once.
do {
try {
$this->database->insert('semaphore')
->fields([
'name' => $name,
'value' => $this->getLockId(),
'expire' => $expire,
])
->execute();
// We track all acquired locks in the global variable.
$this->locks[$name] = TRUE;
// We never need to try again.
$retry = FALSE;
}
catch (IntegrityConstraintViolationException) {
// Suppress the error. If this is our first pass through the loop,
// then $retry is FALSE. In this case, the insert failed because some
// other request acquired the lock but did not release it. We decide
// whether to retry by checking lockMayBeAvailable(). This will clear
// the offending row from the database table in case it has expired.
$retry = $retry ? FALSE : $this->lockMayBeAvailable($name);
}
catch (\Exception $e) {
// Create the semaphore table if it does not exist and retry.
if ($this->ensureTableExists()) {
// Retry only once.
$retry = !$retry;
}
else {
throw $e;
}
}
// We only retry in case the first attempt failed, but we then broke
// an expired lock.
} while ($retry);
}
return isset($this->locks[$name]);
}
/**
* {@inheritdoc}
*/
public function lockMayBeAvailable($name) {
$name = $this->normalizeName($name);
try {
$lock = $this->database->query('SELECT [expire], [value] FROM {semaphore} WHERE [name] = :name', [':name' => $name])->fetchAssoc();
}
catch (\Exception $e) {
$this->catchException($e);
// If the table does not exist yet then the lock may be available.
$lock = FALSE;
}
if (!$lock) {
return TRUE;
}
$expire = (float) $lock['expire'];
$now = microtime(TRUE);
if ($now > $expire) {
// We check two conditions to prevent a race condition where another
// request acquired the lock and set a new expire time. We add a small
// number to $expire to avoid errors with float to string conversion.
return (bool) $this->database->delete('semaphore')
->condition('name', $name)
->condition('value', $lock['value'])
->condition('expire', 0.0001 + $expire, '<=')
->execute();
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function release($name) {
$name = $this->normalizeName($name);
unset($this->locks[$name]);
try {
$this->database->delete('semaphore')
->condition('name', $name)
->condition('value', $this->getLockId())
->execute();
}
catch (\Exception $e) {
$this->catchException($e);
}
}
/**
* {@inheritdoc}
*/
public function releaseAll($lock_id = NULL) {
// Only attempt to release locks if any were acquired.
if (!empty($this->locks)) {
$this->locks = [];
if (empty($lock_id)) {
$lock_id = $this->getLockId();
}
$this->database->delete('semaphore')
->condition('value', $lock_id)
->execute();
}
}
/**
* Check if the semaphore table exists and create it if not.
*/
protected function ensureTableExists() {
try {
$database_schema = $this->database->schema();
$schema_definition = $this->schemaDefinition();
$database_schema->createTable(static::TABLE_NAME, $schema_definition);
}
// If another process has already created the semaphore table, attempting to
// recreate it will throw an exception. In this case just catch the
// exception and do nothing.
catch (DatabaseException) {
}
catch (\Exception) {
return FALSE;
}
return TRUE;
}
/**
* Act on an exception when semaphore might be stale.
*
* If the table does not yet exist, that's fine, but if the table exists and
* yet the query failed, then the semaphore is stale and the exception needs
* to propagate.
*
* @param \Exception $e
* The exception.
*
* @throws \Exception
*/
protected function catchException(\Exception $e) {
if ($this->database->schema()->tableExists(static::TABLE_NAME)) {
throw $e;
}
}
/**
* Normalizes a lock name in order to comply with database limitations.
*
* @param string $name
* The passed in lock name.
*
* @return string
* An ASCII-encoded lock name that is at most 255 characters long.
*/
protected function normalizeName($name) {
// Nothing to do if the name is a US ASCII string of 255 characters or less.
$name_is_ascii = mb_check_encoding($name, 'ASCII');
if (strlen($name) <= 255 && $name_is_ascii) {
return $name;
}
// Return a string that uses as much as possible of the original name with
// the hash appended.
$hash = Crypt::hashBase64($name);
if (!$name_is_ascii) {
return $hash;
}
return substr($name, 0, 255 - strlen($hash)) . $hash;
}
/**
* Defines the schema for the semaphore table.
*
* @internal
*/
public function schemaDefinition() {
return [
'description' => 'Table for holding semaphores, locks, flags, etc. that cannot be stored as state since they must not be cached.',
'fields' => [
'name' => [
'description' => 'Primary Key: Unique name.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'value' => [
'description' => 'A value for the semaphore.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'expire' => [
'description' => 'A Unix timestamp with microseconds indicating when the semaphore should expire.',
'type' => 'float',
'size' => 'big',
'not null' => TRUE,
],
],
'indexes' => [
'value' => ['value'],
'expire' => ['expire'],
],
'primary key' => ['name'],
];
}
}
|