-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcache.rs
More file actions
464 lines (402 loc) · 13.4 KB
/
cache.rs
File metadata and controls
464 lines (402 loc) · 13.4 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use std::{sync::Arc, time::Duration};
#[cfg(test)]
use proptest_derive::Arbitrary;
use crate::{
body::Body,
component::fastly::api::types::Error as ComponentError,
wiggle_abi::types::{BodyHandle, CacheOverrideTag, FastlyStatus},
};
use http::{HeaderMap, HeaderValue};
mod store;
mod variance;
use store::{CacheData, CacheKeyObjects, ObjectMeta, Obligation};
pub use variance::VaryRule;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("invalid key")]
InvalidKey,
#[error("handle is not writeable")]
CannotWrite,
#[error("no entry for key in cache")]
Missing,
#[error("cache entry's body is currently being read by another body")]
HandleBodyUsed,
}
impl From<Error> for crate::Error {
fn from(value: Error) -> Self {
crate::Error::CacheError(value)
}
}
impl From<&Error> for FastlyStatus {
fn from(value: &Error) -> Self {
match value {
// TODO: cceckman-at-fastly: These may not correspond to the same errors as the compute
// platform uses. Check!
Error::InvalidKey => FastlyStatus::Inval,
Error::CannotWrite => FastlyStatus::Badf,
Error::Missing => FastlyStatus::None,
Error::HandleBodyUsed => FastlyStatus::Badf,
}
}
}
impl From<Error> for ComponentError {
fn from(value: Error) -> Self {
match value {
// TODO: cceckman-at-fastly: These may not correspond to the same errors as the compute
// platform uses. Check!
Error::InvalidKey => ComponentError::InvalidArgument,
Error::CannotWrite => ComponentError::BadHandle,
Error::Missing => ComponentError::OptionalNone,
Error::HandleBodyUsed => ComponentError::BadHandle,
}
}
}
/// Primary cache key: an up-to-4KiB buffer.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct CacheKey(
#[cfg_attr(test, proptest(filter = "|f| f.len() <= CacheKey::MAX_LENGTH"))] Vec<u8>,
);
impl CacheKey {
/// The maximum size of a cache key is 4KiB.
pub const MAX_LENGTH: usize = 4096;
}
impl TryFrom<&Vec<u8>> for CacheKey {
type Error = Error;
fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error> {
value.as_slice().try_into()
}
}
impl TryFrom<Vec<u8>> for CacheKey {
type Error = Error;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
if value.len() > Self::MAX_LENGTH {
Err(Error::InvalidKey)
} else {
Ok(CacheKey(value))
}
}
}
impl TryFrom<&[u8]> for CacheKey {
type Error = Error;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() > CacheKey::MAX_LENGTH {
Err(Error::InvalidKey)
} else {
Ok(CacheKey(value.to_owned()))
}
}
}
impl TryFrom<&str> for CacheKey {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.as_bytes().try_into()
}
}
/// The result of a lookup: the object (if found), and/or an obligation to fetch.
#[derive(Debug)]
pub struct CacheEntry {
key: CacheKey,
found: Option<Found>,
go_get: Option<Obligation>,
}
impl CacheEntry {
/// Return a stub entry to hold in CacheBusy.
pub fn stub(&self) -> CacheEntry {
Self {
key: self.key.clone(),
found: None,
go_get: None,
}
}
/// Returns the key used to generate this CacheEntry.
pub fn key(&self) -> &CacheKey {
&self.key
}
/// Returns the data found in the cache, if any was present.
pub fn found(&self) -> Option<&Found> {
self.found.as_ref()
}
/// Returns the data found in the cache, if any was present.
pub fn found_mut(&mut self) -> Option<&mut Found> {
self.found.as_mut()
}
/// Returns the obligation to fetch, if required
pub fn go_get(&self) -> Option<&Obligation> {
self.go_get.as_ref()
}
/// Extract the write obligation, if present.
pub fn take_go_get(&mut self) -> Option<Obligation> {
self.go_get.take()
}
}
/// A successful retrieval of an item from the cache.
#[derive(Debug)]
pub struct Found {
data: Arc<CacheData>,
/// The handle for the last body used to read from this Found.
///
/// Only one Body may be outstanding from a given Found at a time.
/// (This is an implementation restriction within the compute platform).
/// We mirror the BodyHandle here when we create it; we can later check whether the handle is
/// still valid, to find an outstanding read.
pub last_body_handle: Option<BodyHandle>,
}
impl Found {
/// Access the body of the cached object.
pub fn body(&self) -> Result<Body, crate::Error> {
self.data.as_ref().get_body()
}
/// Access the metadata of the cached object.
pub fn meta(&self) -> &ObjectMeta {
self.data.get_meta()
}
}
/// Cache for a service.
///
// TODO: cceckman-at-fastly:
// Explain some about how this works:
// - Request collapsing
// - Stale-while-revalidate
pub struct Cache {
inner: moka::future::Cache<CacheKey, Arc<CacheKeyObjects>>,
}
impl Default for Cache {
fn default() -> Self {
// TODO: cceckman-at-fastly:
// Weight by size, allow a cap on max size?
let inner = moka::future::Cache::builder()
.eviction_listener(|key, _value, cause| {
tracing::info!("cache eviction of {key:?}: {cause:?}")
})
.build();
Cache { inner }
}
}
impl Cache {
/// Perform a non-transactional lookup.
pub async fn lookup(&self, key: &CacheKey, headers: &HeaderMap) -> CacheEntry {
let found = self
.inner
.get_with_by_ref(&key, async { Default::default() })
.await
.get(headers)
.map(|data| Found {
data,
last_body_handle: None,
});
CacheEntry {
key: key.clone(),
found,
go_get: None,
}
}
/// Perform a transactional lookup.
pub async fn transaction_lookup(&self, key: &CacheKey, headers: &HeaderMap) -> CacheEntry {
let (found, obligation) = self
.inner
.get_with_by_ref(&key, async { Default::default() })
.await
.transaction_get(headers)
.await;
CacheEntry {
key: key.clone(),
found: found.map(|data| Found {
data,
last_body_handle: None,
}),
go_get: obligation,
}
}
/// Perform a non-transactional lookup for the given cache key.
/// Note: races with other insertions, including transactional insertions.
/// Last writer wins!
pub async fn insert(
&self,
key: &CacheKey,
request_headers: HeaderMap,
options: WriteOptions,
body: Body,
) {
self.inner
.get_with_by_ref(&key, async { Default::default() })
.await
.insert(request_headers, options, body, None);
}
}
/// Options that can be applied to a write, e.g. insert or transaction_insert.
#[derive(Default)]
pub struct WriteOptions {
pub max_age: Duration,
pub initial_age: Duration,
pub vary_rule: VaryRule,
}
impl WriteOptions {
pub fn new(max_age: Duration) -> Self {
WriteOptions {
max_age,
initial_age: Duration::ZERO,
vary_rule: VaryRule::default(),
}
}
}
/// Optional override for response caching behavior.
#[derive(Clone, Debug, Default)]
pub enum CacheOverride {
/// Do not override the behavior specified in the origin response's cache control headers.
#[default]
None,
/// Do not cache the response to this request, regardless of the origin response's headers.
Pass,
/// Override particular cache control settings.
///
/// The origin response's cache control headers will be used for ttl and stale_while_revalidate if `None`.
Override {
ttl: Option<u32>,
stale_while_revalidate: Option<u32>,
pci: bool,
surrogate_key: Option<HeaderValue>,
},
}
impl CacheOverride {
pub fn is_pass(&self) -> bool {
if let Self::Pass = self {
true
} else {
false
}
}
/// Convert from the representation suitable for passing across the ABI boundary.
///
/// Returns `None` if the tag is not recognized. Depending on the tag, some of the values may be
/// ignored.
pub fn from_abi(
tag: u32,
ttl: u32,
swr: u32,
surrogate_key: Option<HeaderValue>,
) -> Option<Self> {
CacheOverrideTag::from_bits(tag).map(|tag| {
if tag.contains(CacheOverrideTag::PASS) {
return CacheOverride::Pass;
}
if tag.is_empty() && surrogate_key.is_none() {
return CacheOverride::None;
}
let ttl = if tag.contains(CacheOverrideTag::TTL) {
Some(ttl)
} else {
None
};
let stale_while_revalidate = if tag.contains(CacheOverrideTag::STALE_WHILE_REVALIDATE) {
Some(swr)
} else {
None
};
let pci = tag.contains(CacheOverrideTag::PCI);
CacheOverride::Override {
ttl,
stale_while_revalidate,
pci,
surrogate_key,
}
})
}
}
#[cfg(test)]
mod tests {
use http::HeaderName;
use proptest::prelude::*;
use super::*;
proptest! {
#[test]
fn reject_cache_key_too_long(l in 4097usize..5000) {
let mut v : Vec<u8> = Vec::new();
v.resize(l, 0);
CacheKey::try_from(&v).unwrap_err();
}
}
proptest! {
#[test]
fn accept_valid_cache_key_len(l in 0usize..4096) {
let mut v : Vec<u8> = Vec::new();
v.resize(l, 0);
let _ = CacheKey::try_from(&v).unwrap();
}
}
proptest! {
#[test]
fn nontransactional_insert_lookup(
key in any::<CacheKey>(),
max_age in any::<u32>(),
initial_age in any::<u32>(),
value in any::<Vec<u8>>()) {
let cache = Cache::default();
// We can't use tokio::test and proptest! together; both alter the signature of the
// test function, and are not aware of each other enough for it to pass.
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
rt.block_on(async {
let empty = cache.lookup(&key, &HeaderMap::default()).await;
assert!(empty.found().is_none());
// TODO: cceckman-at-fastly -- check GoGet
let write_options = WriteOptions {
max_age: Duration::from_secs(max_age as u64),
initial_age: Duration::from_secs(initial_age as u64),
vary_rule: VaryRule::default(),
};
cache.insert(&key, HeaderMap::default(), write_options, value.clone().into()).await;
let nonempty = cache.lookup(&key, &HeaderMap::default()).await;
let found = nonempty.found().expect("should have found inserted key");
let got = found.body().unwrap().read_into_vec().await.unwrap();
assert_eq!(got, value);
});
}
}
#[tokio::test]
async fn insert_immediately_stale() {
let cache = Cache::default();
let key = ([1u8].as_slice()).try_into().unwrap();
// Insert an already-stale entry:
let write_options = WriteOptions {
max_age: Duration::from_secs(1),
initial_age: Duration::from_secs(2),
vary_rule: VaryRule::default(),
};
let mut body = Body::empty();
body.push_back([1u8].as_slice());
cache
.insert(&key, HeaderMap::default(), write_options, body)
.await;
let nonempty = cache.lookup(&key, &HeaderMap::default()).await;
let found = nonempty.found().expect("should have found inserted key");
assert!(!found.meta().is_fresh());
}
#[tokio::test]
async fn test_vary() {
let cache = Cache::default();
let key = ([1u8].as_slice()).try_into().unwrap();
let header_name = HeaderName::from_static("x-viceroy-test");
let request_headers: HeaderMap = [(header_name.clone(), HeaderValue::from_static("test"))]
.into_iter()
.collect();
let write_options = WriteOptions {
max_age: Duration::from_secs(100),
initial_age: Duration::from_secs(2),
vary_rule: VaryRule::new([&header_name].into_iter()),
};
let body = Body::empty();
cache
.insert(&key, request_headers.clone(), write_options, body)
.await;
let empty_headers = cache.lookup(&key, &HeaderMap::default()).await;
assert!(empty_headers.found().is_none());
let matched_headers = cache.lookup(&key, &request_headers).await;
assert!(matched_headers.found.is_some());
let r2_headers: HeaderMap = [(header_name.clone(), HeaderValue::from_static("assert"))]
.into_iter()
.collect();
let mismatched_headers = cache.lookup(&key, &r2_headers).await;
assert!(mismatched_headers.found.is_none());
}
}