-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_cache.rs
More file actions
341 lines (323 loc) · 10.8 KB
/
Copy pathquery_cache.rs
File metadata and controls
341 lines (323 loc) · 10.8 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
339
340
341
//! Memoize deterministic queries across revisions using observed dependencies.
//! Every lookup still follows the query's consistency policy and HTTP registry
//! resolution. Independent queries and committed snapshots execute in parallel.
use crate::{consensus::Records, evaluator::DependencyCertificate};
use serde_json::Value;
use std::collections::{BTreeMap, VecDeque};
use std::sync::{Arc, Mutex, Weak};
use tokio::sync::Mutex as AsyncMutex;
/// Match the canonical object-key order without cloning caller arguments or
/// principals into temporary JSON trees.
pub(super) fn key(name: &str, args: &Value, principal: &Value) -> String {
#[derive(serde::Serialize)]
struct Invocation<'a> {
args: &'a Value,
name: &'a str,
}
#[derive(serde::Serialize)]
struct Key<'a> {
invocation: Invocation<'a>,
principal: &'a Value,
}
serde_json::to_string(&Key {
invocation: Invocation { args, name },
principal,
})
.expect("query cache key is JSON")
}
pub(super) struct QueryCache {
entries: Mutex<Entries>,
flights: Mutex<Flights>,
max_bytes: usize,
max_flight_bytes: usize,
}
impl Default for QueryCache {
fn default() -> Self {
let settings = super::tuning::settings().expect("validated query cache budgets");
Self::new(settings.query_cache_bytes, settings.query_flight_bytes)
}
}
#[derive(Default)]
struct Flights {
pending: BTreeMap<(u64, String), Weak<AsyncMutex<bool>>>,
bytes: usize,
}
#[derive(Default)]
struct Entries {
values: BTreeMap<String, Entry>,
order: VecDeque<String>,
bytes: usize,
}
struct Entry {
revision: u64,
value: Arc<str>,
certificate: Arc<DependencyCertificate>,
bytes: usize,
}
impl QueryCache {
fn new(max_bytes: usize, max_flight_bytes: usize) -> Self {
Self {
entries: Mutex::default(),
flights: Mutex::default(),
max_bytes,
max_flight_bytes,
}
}
/// A weak, bounded registry only retains keys while evaluations are active.
/// The boolean allows non-cacheable queries to release all queued callers
/// after their first result rather than serializing clock-sensitive work.
pub(super) fn flight(&self, revision: u64, key: &str) -> Option<Arc<AsyncMutex<bool>>> {
let cost = key.len().saturating_add(256);
if cost > self.max_flight_bytes {
return None;
}
let mut flights = self
.flights
.lock()
.unwrap_or_else(|error| error.into_inner());
let key = (revision, key.to_owned());
if let Some(active) = flights.pending.get(&key).and_then(Weak::upgrade) {
return Some(active);
}
let mut removed = 0;
flights.pending.retain(|(_, key), value| {
if value.strong_count() == 0 {
removed += key.len().saturating_add(256);
false
} else {
true
}
});
flights.bytes -= removed;
if flights.bytes.saturating_add(cost) > self.max_flight_bytes {
return None;
}
let active = Arc::new(AsyncMutex::new(true));
flights.bytes += cost;
flights.pending.insert(key, Arc::downgrade(&active));
Some(active)
}
#[cfg(test)]
pub(super) fn get(&self, _revision: u64, key: &str, snapshot: &Records) -> Option<Value> {
self.get_certified(key, snapshot).map(|(value, _)| value)
}
/// A reused value, with the certificate that still holds for it.
pub(super) fn get_certified(
&self,
key: &str,
snapshot: &Records,
) -> Option<(Value, Arc<DependencyCertificate>)> {
let (encoded, certificate) = self.get_valid(key, snapshot)?;
Some((serde_json::from_str(&encoded).ok()?, certificate))
}
/// HTTP callers can reuse validated JSON without decoding it only to encode
/// it again. The caller must account for retention after cache eviction.
pub(super) fn get_encoded(&self, key: &str, snapshot: &Records) -> Option<Arc<str>> {
self.get_valid(key, snapshot).map(|(encoded, _)| encoded)
}
fn get_valid(
&self,
key: &str,
snapshot: &Records,
) -> Option<(Arc<str>, Arc<DependencyCertificate>)> {
let entries = self
.entries
.lock()
.unwrap_or_else(|error| error.into_inner());
let entry = entries
.values
.get(key)
.map(|entry| (entry.value.clone(), entry.certificate.clone()));
drop(entries);
// Dependency checks happen outside the global mutex.
let (encoded, certificate) = entry?;
certificate
.valid(snapshot)
.then_some((encoded, certificate))
}
pub(super) fn insert(
&self,
revision: u64,
key: String,
value: Value,
certificate: Arc<DependencyCertificate>,
) {
if self.max_bytes == 0 {
return;
}
let Ok(encoded) = serde_json::to_string(&value) else {
return;
};
let size = 2usize
.saturating_mul(key.len())
.saturating_add(encoded.len())
.saturating_add(certificate.allocation_cost())
.saturating_add(256);
if size > self.max_bytes {
return;
}
let mut entries = self
.entries
.lock()
.unwrap_or_else(|error| error.into_inner());
// Delayed work must not evict a newer certificate for the same query.
if entries
.values
.get(&key)
.is_some_and(|entry| entry.revision >= revision)
{
return;
}
if let Some(previous) = entries.values.remove(&key) {
entries.bytes -= previous.bytes;
entries.order.retain(|existing| existing != &key);
}
while entries.bytes.saturating_add(size) > self.max_bytes {
let Some(oldest) = entries.order.pop_front() else {
break;
};
if let Some(removed) = entries.values.remove(&oldest) {
entries.bytes -= removed.bytes;
}
}
entries.bytes += size;
entries.order.push_back(key.clone());
entries.values.insert(
key,
Entry {
revision,
value: encoded.into(),
certificate,
bytes: size,
},
);
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn borrowed_cache_keys_preserve_canonical_arguments_and_principal_scopes() {
let args = json!({"escaped":"\u{0000}\n🌸", "nested":[null,{"x":1}]});
for principal in [
Value::Null,
json!({"subject":"alice","claims":{"role":"reader"}}),
] {
assert_eq!(
key("quoted\"method", &args, &principal),
serde_json::to_string(&json!({
"invocation":{"name":"quoted\"method","args":args},
"principal":principal,
}))
.unwrap()
);
}
}
#[tokio::test]
async fn flights_join_only_identical_snapshots_and_cancel_without_poisoning() {
let cache = QueryCache::default();
let first = cache.flight(1, "read").unwrap();
let second = cache.flight(1, "read").unwrap();
assert!(Arc::ptr_eq(&first, &second));
let held = first.lock().await;
assert!(second.try_lock().is_err());
assert!(cache.flight(2, "read").unwrap().try_lock().is_ok());
assert!(cache.flight(1, "other").unwrap().try_lock().is_ok());
drop(held);
assert!(*second.lock().await);
*second.lock().await = false;
assert!(!*first.lock().await);
drop((first, second));
assert!(*cache.flight(1, "read").unwrap().lock().await);
}
#[test]
fn flight_registry_is_byte_bounded_and_reclaims_finished_entries() {
let cache = QueryCache::new(4096, 1024);
let active: Vec<_> = (0..3)
.map(|index| cache.flight(1, &index.to_string()).unwrap())
.collect();
assert!(cache.flight(1, "full").is_none());
drop(active);
let large = cache.flight(1, &"x".repeat(768)).unwrap();
assert!(cache.flight(1, "full").is_none());
drop(large);
assert!(cache.flight(1, "new").is_some());
assert!(cache.flight(1, &"x".repeat(769)).is_none());
assert!(QueryCache::new(0, 0).flight(0, "disabled").is_none());
}
#[test]
fn unrelated_revisions_reuse_and_late_evaluations_cannot_replace_newer_values() {
let cache = QueryCache::default();
let snapshot = Records::default();
cache.insert(
1,
"constant".into(),
json!(1),
Arc::<DependencyCertificate>::default(),
);
assert_eq!(cache.get(2, "constant", &snapshot), Some(json!(1)));
cache.insert(
2,
"constant".into(),
json!(2),
Arc::<DependencyCertificate>::default(),
);
cache.insert(
1,
"constant".into(),
json!(0),
Arc::<DependencyCertificate>::default(),
);
assert_eq!(cache.get(3, "constant", &snapshot), Some(json!(2)));
}
#[test]
fn byte_budget_replaces_count_cap_and_can_disable_reuse() {
let snapshot = Records::default();
let cache = QueryCache::new(512 * 1024, 0);
for index in 0..1000 {
cache.insert(
1,
index.to_string(),
json!(index),
Arc::<DependencyCertificate>::default(),
);
}
assert_eq!(
cache.get(1, "0", &snapshot),
Some(json!(0)),
"no fixed 256-entry cap"
);
let small = QueryCache::new(1024, 0);
let value = json!("x".repeat(512));
small.insert(
1,
"a".into(),
value.clone(),
Arc::<DependencyCertificate>::default(),
);
small.insert(
1,
"b".into(),
value.clone(),
Arc::<DependencyCertificate>::default(),
);
assert!(small.get(1, "a", &snapshot).is_none());
assert_eq!(small.get(1, "b", &snapshot), Some(value));
small.insert(
1,
"oversized".into(),
json!("x".repeat(1024)),
Arc::<DependencyCertificate>::default(),
);
assert!(small.get(1, "oversized", &snapshot).is_none());
let disabled = QueryCache::new(0, 0);
disabled.insert(
1,
"x".into(),
Value::Null,
Arc::<DependencyCertificate>::default(),
);
assert!(disabled.get(1, "x", &snapshot).is_none());
}
}