Repository navigation
Expand file tree
/
Copy pathQueryCache.php
More file actions
338 lines (274 loc) · 10.3 KB
/
Copy pathQueryCache.php
File metadata and controls
338 lines (274 loc) · 10.3 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace Utopia\Database\Cache;
use RuntimeException;
use Utopia\Cache\Cache;
use Utopia\Database\Document;
class QueryCache
{
private const string ACTIVE_PREFIX = 'active:';
private const string INITIAL_EPOCH = self::ACTIVE_PREFIX.'initial';
private const string BLOCKED_PREFIX = 'blocked:';
private const string SEPARATOR = '@';
private const string NEVER_STARTED = '0';
private const int PERMANENT = \PHP_INT_MAX;
private const int VERSION = 1;
/** @var array<string, Region> */
private array $regions = [];
public function __construct(
private readonly Cache $cache,
private readonly string $cacheName = 'default',
) {
}
public function setRegion(string $collection, Region $region): void
{
$this->regions[$collection] = $region;
}
public function getRegion(string $collection): Region
{
return $this->regions[$collection] ?? new Region();
}
public function getCollectionKey(Scope $scope, string $collection): string
{
$scopeHash = \md5(\serialize([
'hostname' => $scope->hostname,
'database' => $scope->database,
'namespace' => $scope->namespace,
'tenant' => $scope->tenant,
'collection' => $collection,
]));
return "{$this->cacheName}:qcache:{$collection}:{$scopeHash}";
}
/**
* Resolve a query's entry in the collection's current epoch; null while the
* collection's region is disabled or a write to it is in progress.
*
* @param array<mixed> $queries
*
* @phpstan-impure
*/
public function getEntry(Scope $scope, string $collection, array $queries, string $context = ''): ?Entry
{
if (! $this->getRegion($collection)->enabled) {
return null;
}
$key = $this->getCollectionKey($scope, $collection);
$epoch = $this->getEpoch($key, $collection);
if ($epoch === null) {
return null;
}
$hash = \md5(\serialize([
'queries' => $queries,
'context' => $context,
]));
return new Entry($key.'#'.$epoch.':'.$hash, $collection);
}
/**
* @return array<Document>|null
*
* @phpstan-impure
*/
public function get(Entry $entry): ?array
{
/** @var mixed $data */
$data = $this->cache->load($entry->key, $this->getRegion($entry->collection)->ttl);
if ($data === false || $data === null) {
return null;
}
if (
! \is_array($data)
|| ($data['version'] ?? null) !== self::VERSION
|| ! \is_array($data['documents'] ?? null)
) {
$this->purgeLoadedKey($entry->key);
return null;
}
$documents = [];
foreach ($data['documents'] as $item) {
if ($item instanceof Document) {
$documents[] = $item;
continue;
}
if (! \is_array($item)) {
$this->purgeLoadedKey($entry->key);
return null;
}
$typed = [];
foreach ($item as $key => $value) {
if (\is_string($key)) {
$typed[$key] = $value;
}
}
$documents[] = new Document($typed);
}
return $documents;
}
public function getGeneration(Entry $entry): string
{
return $this->cache->getGeneration($entry->key);
}
/**
* @param array<mixed> $results
*/
public function set(Entry $entry, array $results, string $generation): bool
{
$data = [];
foreach ($results as $result) {
if (! $result instanceof Document) {
return false;
}
$data[] = $result->getArrayCopy();
}
return $this->cache->saveWithLease($entry->key, [
'version' => self::VERSION,
'documents' => $data,
], '', $generation) !== false;
}
public function invalidateCollection(Scope $scope, string $collection): void
{
$key = $this->getCollectionKey($scope, $collection);
$token = \bin2hex(\random_bytes(16));
$this->blockCollection($key, $token);
$this->activateCollection($key, $token);
}
/**
* Publish a shared tombstone before a mutation starts.
*/
public function blockCollection(string $key, string $token): void
{
if (! (new Owners($this->cache))->register($key, $token)) {
throw new RuntimeException("Failed to register query cache owner for '{$key}'");
}
if ($this->cache->save($this->getEpochKey($key), self::BLOCKED_PREFIX.$token.self::SEPARATOR.\time()) === false) {
throw new RuntimeException("Failed to block query cache epoch for '{$key}'");
}
$this->cache->purge($this->getStartedKey($key));
}
/**
* Replace this mutation's shared tombstone with a fresh usable epoch once no
* other mutation of the collection is in progress.
*/
public function activateCollection(string $key, string $token): void
{
$registration = (new Owners($this->cache))->find($key, $token);
$owner = $this->cache->load($registration->key, self::PERMANENT, $registration->field);
if ($owner !== false && $owner !== null && $owner !== $token) {
throw new RuntimeException("Invalid query cache owner for '{$key}'");
}
$owned = $owner === $token;
if ($owned && ! $this->cache->purge($registration->key, $registration->field)) {
$owner = $this->cache->load($registration->key, self::PERMANENT, $registration->field);
if ($owner !== false && $owner !== null) {
throw new RuntimeException("Failed to release query cache owner for '{$key}'");
}
$owned = false;
}
$epochKey = $this->getEpochKey($key);
$startedKey = $this->getStartedKey($key);
$finishedKey = $this->getFinishedKey($key);
$started = $this->cache->getGeneration($startedKey);
$finished = $this->cache->getGeneration($finishedKey);
$current = $this->cache->load($epochKey, self::PERMANENT);
$ours = $this->isTombstoneOf($current, $token);
if ($started === $finished) {
if (! $owned && ($ours || ! $this->isTombstone($current))) {
$this->publish($key, $finished);
}
return;
}
if (! $owned && ! $ours) {
return;
}
$this->cache->purge($finishedKey);
$nextStarted = $this->cache->getGeneration($startedKey);
$nextFinished = $this->cache->getGeneration($finishedKey);
if ($nextStarted === $nextFinished) {
$this->publish($key, $nextFinished);
return;
}
if ($nextFinished === $finished && $this->isTombstoneOf($this->cache->load($epochKey, self::PERMANENT), $token)) {
throw new RuntimeException("Failed to finish query cache invalidation for '{$key}'");
}
}
public function flush(): void
{
if (! $this->cache->flush()) {
throw new RuntimeException('Failed to flush query cache');
}
}
/**
* Epochs never expire in the cache, so one cannot vanish under a transaction that
* outlives the region TTL. An active epoch carries the finished generation it was
* published at: while the started generation still equals it, no mutation has
* begun since, so a reader needs one generation read. A tombstone carries its
* write time and lapses with the region, but only once no mutation is in flight.
*/
private function getEpoch(string $key, string $collection): ?string
{
$value = $this->cache->load($this->getEpochKey($key), self::PERMANENT);
if ($value === false || $value === null) {
return $this->cache->getGeneration($this->getStartedKey($key)) === self::NEVER_STARTED
? self::INITIAL_EPOCH
: null;
}
if (! \is_string($value) || $value === '') {
throw new RuntimeException("Invalid query cache epoch for '{$key}'");
}
$separator = \strrpos($value, self::SEPARATOR);
if ($separator === false) {
return null;
}
$marker = \substr($value, 0, $separator);
$stamp = \substr($value, $separator + 1);
if (\str_starts_with($value, self::BLOCKED_PREFIX)) {
if ((int) $stamp + $this->getRegion($collection)->ttl > \time() || ! $this->isQuiescent($key)) {
return null;
}
return self::INITIAL_EPOCH;
}
if (! \str_starts_with($value, self::ACTIVE_PREFIX)) {
return null;
}
$started = $this->cache->getGeneration($this->getStartedKey($key));
if ($started !== $stamp && $started !== $this->cache->getGeneration($this->getFinishedKey($key))) {
return null;
}
return $marker;
}
private function isQuiescent(string $key): bool
{
return $this->cache->getGeneration($this->getStartedKey($key)) === $this->cache->getGeneration($this->getFinishedKey($key));
}
private function isTombstone(mixed $value): bool
{
return \is_string($value) && \str_starts_with($value, self::BLOCKED_PREFIX);
}
private function isTombstoneOf(mixed $value, string $token): bool
{
return \is_string($value) && \str_starts_with($value, self::BLOCKED_PREFIX.$token.self::SEPARATOR);
}
private function publish(string $key, string $finished): void
{
$epoch = self::ACTIVE_PREFIX.\bin2hex(\random_bytes(16)).self::SEPARATOR.$finished;
if ($this->cache->save($this->getEpochKey($key), $epoch) === false) {
throw new RuntimeException("Failed to activate query cache for '{$key}'");
}
}
private function getEpochKey(string $key): string
{
return $key.'#epoch';
}
private function getFinishedKey(string $key): string
{
return $key.'#finished';
}
private function getStartedKey(string $key): string
{
return $key.'#started';
}
private function purgeLoadedKey(string $key): void
{
if (! $this->cache->purge($key)) {
throw new RuntimeException("Failed to purge invalid query cache entry '{$key}'");
}
}
}