-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathdisk_cache.rs
867 lines (755 loc) · 27.9 KB
/
disk_cache.rs
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
//! An ObjectStore implementation with disk as cache.
//! The disk cache is a read-through caching, with page as its minimal cache
//! unit.
//!
//! Page is used for reasons below:
//! - reduce file size in case of there are too many request with small range.
use std::{collections::BTreeMap, fmt::Display, ops::Range, sync::Arc};
use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use common_util::time::current_as_rfc3339;
use crc::{Crc, CRC_32_ISCSI};
use futures::stream::BoxStream;
use log::{debug, error, info};
use lru::LruCache;
use prost::Message;
use serde::{Deserialize, Serialize};
use snafu::{ensure, Backtrace, ResultExt, Snafu};
use tokio::{
fs::{File, OpenOptions},
io::{AsyncReadExt, AsyncWrite, AsyncWriteExt},
sync::Mutex,
};
use upstream::{
path::Path, Error as ObjectStoreError, GetResult, ListResult, MultipartId, ObjectMeta,
ObjectStore, Result,
};
const MANIFEST_FILE: &str = "manifest.json";
const CURRENT_VERSION: usize = 1;
pub const CASTAGNOLI: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);
#[derive(Debug, Snafu)]
enum Error {
#[snafu(display(
"IO failed, file:{}, source:{}.\nbacktrace:\n{}",
file,
source,
backtrace
))]
Io {
file: String,
source: std::io::Error,
backtrace: Backtrace,
},
#[snafu(display(
"Failed to deserialize manifest, source:{}.\nbacktrace:\n{}",
source,
backtrace
))]
DeserializeManifest {
source: serde_json::Error,
backtrace: Backtrace,
},
#[snafu(display(
"Failed to serialize manifest, source:{}.\nbacktrace:\n{}",
source,
backtrace
))]
SerializeManifest {
source: serde_json::Error,
backtrace: Backtrace,
},
#[snafu(display("Invalid manifest page size, old:{}, new:{}.", old, new))]
InvalidManifest { old: usize, new: usize },
#[snafu(display(
"Failed to persist cache, file:{}, source:{}.\nbacktrace:\n{}",
file,
source,
backtrace
))]
PersistCache {
file: String,
source: tokio::io::Error,
backtrace: Backtrace,
},
#[snafu(display(
"Failed to decode cache pb value, file:{}, source:{}.\nbacktrace:\n{}",
file,
source,
backtrace
))]
DecodeCache {
file: String,
source: prost::DecodeError,
backtrace: Backtrace,
},
}
impl From<Error> for ObjectStoreError {
fn from(source: Error) -> Self {
Self::Generic {
store: "DiskCacheStore",
source: Box::new(source),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
struct Manifest {
create_at: String,
page_size: usize,
version: usize,
}
// TODO: support partition to reduce lock contention.
#[derive(Debug)]
struct DiskCache {
root_dir: String,
cap: usize,
// Cache key is used as filename on disk.
cache: Mutex<LruCache<String, ()>>,
}
impl DiskCache {
fn new(root_dir: String, cap: usize) -> Self {
Self {
root_dir,
cap,
cache: Mutex::new(LruCache::new(cap)),
}
}
/// Update the cache.
///
/// The returned value denotes whether succeed.
// TODO: We now hold lock when doing IO, possible to release it?
async fn update_cache(&self, key: String, value: Option<Bytes>) -> bool {
let mut cache = self.cache.lock().await;
debug!(
"Disk cache update, key:{}, len:{}, cap:{}.",
&key,
cache.len(),
self.cap
);
// TODO: remove a batch of files to avoid IO during the following update cache.
if cache.len() >= self.cap {
let (filename, _) = cache.pop_lru().unwrap();
let file_path = std::path::Path::new(&self.root_dir)
.join(filename)
.into_os_string()
.into_string()
.unwrap();
debug!("Remove disk cache, filename:{}.", &file_path);
if let Err(e) = tokio::fs::remove_file(&file_path).await {
error!("Remove disk cache failed, file:{}, err:{}.", file_path, e);
}
}
// Persist the value if needed
if let Some(value) = value {
if let Err(e) = self.persist_bytes(&key, value).await {
error!("Failed to persist cache, key:{}, err:{}.", key, e);
return false;
}
}
// Update the key
cache.push(key, ());
true
}
async fn insert(&self, key: String, value: Bytes) -> bool {
self.update_cache(key, Some(value)).await
}
async fn recover(&self, filename: String) -> bool {
self.update_cache(filename, None).await
}
async fn get(&self, key: &str) -> Option<Bytes> {
let mut cache = self.cache.lock().await;
if cache.get(key).is_some() {
// TODO: release lock when doing IO
match self.read_bytes(key).await {
Ok(v) => Some(v),
Err(e) => {
error!(
"Read disk cache failed but ignored, key:{}, err:{}.",
key, e
);
None
}
}
} else {
None
}
}
async fn persist_bytes(&self, filename: &str, value: Bytes) -> Result<()> {
let file_path = std::path::Path::new(&self.root_dir)
.join(filename)
.into_os_string()
.into_string()
.unwrap();
let mut file = File::create(&file_path).await.with_context(|| Io {
file: file_path.clone(),
})?;
let bytes = value.to_vec();
let pb_bytes = ceresdbproto::oss_cache::Bytes {
crc: CASTAGNOLI.checksum(&bytes),
value: bytes,
};
file.write_all(&pb_bytes.encode_to_vec())
.await
.with_context(|| PersistCache {
file: file_path.clone(),
})?;
Ok(())
}
async fn read_bytes(&self, filename: &str) -> Result<Bytes> {
let file_path = std::path::Path::new(&self.root_dir)
.join(filename)
.into_os_string()
.into_string()
.unwrap();
let mut f = File::open(&file_path).await.with_context(|| Io {
file: file_path.clone(),
})?;
let mut buf = Vec::new();
f.read_to_end(&mut buf).await.with_context(|| Io {
file: file_path.clone(),
})?;
let bytes = ceresdbproto::oss_cache::Bytes::decode(&*buf).with_context(|| DecodeCache {
file: file_path.clone(),
})?;
// TODO: CRC checking
Ok(bytes.value.into())
}
}
impl Display for DiskCache {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DiskCache")
.field("path", &self.root_dir)
.field("cache", &self.cache)
.finish()
}
}
/// There will be two kinds of file in this cache:
/// 1. manifest.json, which contains metadata, like
/// ```json
/// {
/// "create_at": "2022-12-01T08:51:15.167795+00:00",
/// "page_size": 1048576,
/// "version": 1
/// }
/// ```
/// 2. ${sst-path}-${range.start}-${range.end}, which contains bytes of given
/// range, start/end are aligned to page_size.
#[derive(Debug)]
pub struct DiskCacheStore {
cache: DiskCache,
// Max disk capacity cache use can
cap: usize,
// Size of each cached bytes
page_size: usize,
// location path size cache
size_cache: Arc<Mutex<LruCache<String, usize>>>,
underlying_store: Arc<dyn ObjectStore>,
}
impl DiskCacheStore {
pub async fn try_new(
cache_dir: String,
cap: usize,
page_size: usize,
underlying_store: Arc<dyn ObjectStore>,
) -> Result<Self> {
assert!(cap % page_size == 0);
let _ = Self::create_manifest_if_not_exists(&cache_dir, page_size).await?;
let cache = DiskCache::new(cache_dir.clone(), cap / page_size);
Self::recover_cache(&cache_dir, &cache).await?;
let size_cache = Arc::new(Mutex::new(LruCache::new(cap / page_size)));
Ok(Self {
cache,
size_cache,
cap,
page_size,
underlying_store,
})
}
async fn create_manifest_if_not_exists(cache_dir: &str, page_size: usize) -> Result<Manifest> {
let mut file = OpenOptions::new()
.write(true)
.create(true)
.read(true)
.truncate(false)
.open(std::path::Path::new(cache_dir).join(MANIFEST_FILE))
.await
.with_context(|| Io {
file: MANIFEST_FILE.to_string(),
})?;
let metadata = file.metadata().await.with_context(|| Io {
file: MANIFEST_FILE.to_string(),
})?;
// empty file, create a new one
if metadata.len() == 0 {
let manifest = Manifest {
page_size,
create_at: current_as_rfc3339(),
version: CURRENT_VERSION,
};
let buf = serde_json::to_vec_pretty(&manifest).context(SerializeManifest)?;
file.write_all(&buf).await.with_context(|| Io {
file: MANIFEST_FILE.to_string(),
})?;
return Ok(manifest);
}
let mut buf = Vec::new();
file.read_to_end(&mut buf).await.with_context(|| Io {
file: MANIFEST_FILE.to_string(),
})?;
let manifest: Manifest = serde_json::from_slice(&buf).context(DeserializeManifest)?;
ensure!(
manifest.page_size == page_size,
InvalidManifest {
old: manifest.page_size,
new: page_size
}
);
// TODO: check version
Ok(manifest)
}
async fn recover_cache(cache_dir: &str, cache: &DiskCache) -> Result<()> {
let mut cache_dir = tokio::fs::read_dir(cache_dir).await.with_context(|| Io {
file: cache_dir.to_string(),
})?;
// TODO: sort by access time
while let Some(entry) = cache_dir.next_entry().await.with_context(|| Io {
file: "entry when iter cache_dir".to_string(),
})? {
let filename = entry.file_name().into_string().unwrap();
info!("Disk cache recover_cache, filename:{}.", &filename);
if filename != MANIFEST_FILE {
cache.recover(filename).await;
}
}
Ok(())
}
fn normalize_range(&self, max_size: usize, range: &Range<usize>) -> Vec<Range<usize>> {
let start = range.start / self.page_size * self.page_size;
let end = (range.end + self.page_size - 1) / self.page_size * self.page_size;
(start..end.min(max_size))
.step_by(self.page_size)
.map(|start| start..(start + self.page_size).min(max_size))
.collect::<Vec<_>>()
}
fn cache_key(location: &Path, range: &Range<usize>) -> String {
format!(
"{}-{}-{}",
location.as_ref().replace('/', "-"),
range.start,
range.end
)
}
}
impl Display for DiskCacheStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DiskCacheStore")
.field("page_size", &self.page_size)
.field("cap", &self.cap)
.field("cache", &self.cache)
.finish()
}
}
#[async_trait]
impl ObjectStore for DiskCacheStore {
async fn put(&self, location: &Path, bytes: Bytes) -> Result<()> {
self.underlying_store.put(location, bytes).await
}
async fn put_multipart(
&self,
location: &Path,
) -> Result<(MultipartId, Box<dyn AsyncWrite + Unpin + Send>)> {
self.underlying_store.put_multipart(location).await
}
async fn abort_multipart(&self, location: &Path, multipart_id: &MultipartId) -> Result<()> {
self.underlying_store
.abort_multipart(location, multipart_id)
.await
}
// TODO: don't cache whole path for reasons below
// In sst module, we only use get_range, get is not used
async fn get(&self, location: &Path) -> Result<GetResult> {
self.underlying_store.get(location).await
}
async fn get_range(&self, location: &Path, range: Range<usize>) -> Result<Bytes> {
// TODO: aligned_range will larger than real file size, need to truncate
let file_size = {
let mut size_cache = self.size_cache.lock().await;
if let Some(size) = size_cache.get(location.as_ref()) {
*size
} else {
// release lock before doing IO
drop(size_cache);
// TODO: multiple threads may go here, how to fix?
let object_meta = self.head(location).await?;
{
let mut size_cache = self.size_cache.lock().await;
size_cache.put(location.to_string(), object_meta.size);
}
object_meta.size
}
};
let aligned_ranges = self.normalize_range(file_size, &range);
let mut ranged_bytes = BTreeMap::new();
let mut missing_ranges = Vec::new();
for range in aligned_ranges {
let cache_key = Self::cache_key(location, &range);
if let Some(bytes) = self.cache.get(&cache_key).await {
ranged_bytes.insert(range.start, bytes);
} else {
missing_ranges.push(range);
}
}
for range in missing_ranges {
let range_start = range.start;
let cache_key = Self::cache_key(location, &range);
// TODO: we should use get_ranges here.
let bytes = self.underlying_store.get_range(location, range).await?;
self.cache.insert(cache_key, bytes.clone()).await;
ranged_bytes.insert(range_start, bytes);
}
// we get all bytes for each aligned_range, organize real bytes
// fast path
if ranged_bytes.len() == 1 {
let (range_start, bytes) = ranged_bytes.pop_first().unwrap();
let result = bytes.slice((range.start - range_start)..(range.end - range_start));
return Ok(result);
}
// there are multiple aligned ranges for one request, such as
// range = [3, 33), page_size = 16, then aligned ranges will be
// [0, 16), [16, 32), [32, 48)
// we need to combine those ranged bytes to get final result bytes
let mut byte_buf = BytesMut::with_capacity(range.end - range.start);
let (range_start, bytes) = ranged_bytes.pop_first().unwrap();
byte_buf.extend(bytes.slice((range.start - range_start)..));
let (range_start, bytes) = ranged_bytes.pop_last().unwrap();
let last_part = bytes.slice(..(range.end - range_start));
for bytes in ranged_bytes.into_values() {
byte_buf.extend(bytes);
}
byte_buf.extend(last_part);
Ok(byte_buf.freeze())
}
async fn head(&self, location: &Path) -> Result<ObjectMeta> {
self.underlying_store.head(location).await
}
async fn delete(&self, location: &Path) -> Result<()> {
self.underlying_store.delete(location).await
}
async fn list(&self, prefix: Option<&Path>) -> Result<BoxStream<'_, Result<ObjectMeta>>> {
self.underlying_store.list(prefix).await
}
async fn list_with_delimiter(&self, prefix: Option<&Path>) -> Result<ListResult> {
self.underlying_store.list_with_delimiter(prefix).await
}
async fn copy(&self, from: &Path, to: &Path) -> Result<()> {
self.underlying_store.copy(from, to).await
}
async fn copy_if_not_exists(&self, from: &Path, to: &Path) -> Result<()> {
self.underlying_store.copy_if_not_exists(from, to).await
}
}
#[cfg(test)]
mod test {
use tempfile::{tempdir, TempDir};
use upstream::local::LocalFileSystem;
use super::*;
struct StoreWithCacheDir {
inner: DiskCacheStore,
cache_dir: TempDir,
}
async fn prepare_store(page_size: usize, cap: usize) -> StoreWithCacheDir {
let local_path = tempdir().unwrap();
let local_store = Arc::new(LocalFileSystem::new_with_prefix(local_path.path()).unwrap());
let cache_dir = tempdir().unwrap();
let store = DiskCacheStore::try_new(
cache_dir.as_ref().to_string_lossy().to_string(),
cap,
page_size,
local_store,
)
.await
.unwrap();
StoreWithCacheDir {
inner: store,
cache_dir,
}
}
#[tokio::test]
async fn test_normalize_range_less_than_file_size() {
let page_size = 16;
let testcases = vec![
(0..1, vec![0..16]),
(0..16, vec![0..16]),
(0..17, vec![0..16, 16..32]),
(16..32, vec![16..32]),
(
16..100,
vec![16..32, 32..48, 48..64, 64..80, 80..96, 96..112],
),
];
let store = prepare_store(page_size, 1024).await;
for (input, expected) in testcases {
assert_eq!(store.inner.normalize_range(1024, &input), expected);
}
}
#[tokio::test]
async fn test_normalize_range_great_than_file_size() {
let page_size = 16;
let testcases = vec![
(0..1, vec![0..16]),
(0..16, vec![0..16]),
(0..17, vec![0..16, 16..20]),
(16..32, vec![16..20]),
(32..100, vec![]),
];
let store = prepare_store(page_size, 1024).await;
for (input, expected) in testcases {
assert_eq!(store.inner.normalize_range(20, &input), expected);
}
}
fn test_file_exists(cache_dir: &TempDir, location: &Path, range: &Range<usize>) -> bool {
cache_dir
.path()
.join(DiskCacheStore::cache_key(location, range))
.exists()
}
#[tokio::test]
async fn test_disk_cache_store_get_range() {
let page_size = 16;
// 51 byte
let data = b"a b c d e f g h i j k l m n o p q r s t u v w x y z";
let location = Path::from("1.sst");
let store = prepare_store(page_size, 1024).await;
let mut buf = BytesMut::with_capacity(data.len() * 4);
// extend 4 times, then location will contain 200 bytes
for _ in 0..4 {
buf.extend_from_slice(data);
}
store.inner.put(&location, buf.freeze()).await.unwrap();
let testcases = vec![
(0..6, "a b c "),
(0..16, "a b c d e f g h "),
// len of aligned ranges will be 2
(0..17, "a b c d e f g h i"),
(16..17, "i"),
// len of aligned ranges will be 6
(16..100, "i j k l m n o p q r s t u v w x y za b c d e f g h i j k l m n o p q r s t u v w x y"),
];
for (input, expected) in testcases {
assert_eq!(
store.inner.get_range(&location, input).await.unwrap(),
Bytes::copy_from_slice(expected.as_bytes())
);
}
// remove cached values, then get again
{
let mut data_cache = store.inner.cache.cache.lock().await;
for range in vec![0..16, 16..32, 32..48, 48..64, 64..80, 80..96, 96..112] {
assert!(data_cache.contains(DiskCacheStore::cache_key(&location, &range).as_str()));
assert!(test_file_exists(&store.cache_dir, &location, &range));
}
for range in vec![16..32, 48..64, 80..96] {
assert!(data_cache
.pop(&DiskCacheStore::cache_key(&location, &range))
.is_some());
}
}
assert_eq!(
store.inner.get_range(&location, 16..100).await.unwrap(),
Bytes::copy_from_slice(
b"i j k l m n o p q r s t u v w x y za b c d e f g h i j k l m n o p q r s t u v w x y"
)
);
}
#[tokio::test]
async fn test_disk_cache_remove_cache_file() {
let page_size = 16;
// 51 byte
let data = b"a b c d e f g h i j k l m n o p q r s t u v w x y z";
let location = Path::from("remove_cache_file.sst");
let store = prepare_store(page_size, 32).await;
let mut buf = BytesMut::with_capacity(data.len() * 4);
// extend 4 times, then location will contain 200 bytes, but cache cap is 32
for _ in 0..4 {
buf.extend_from_slice(data);
}
store.inner.put(&location, buf.freeze()).await.unwrap();
let _ = store.inner.get_range(&location, 0..16).await.unwrap();
let _ = store.inner.get_range(&location, 16..32).await.unwrap();
// cache is full now
assert!(test_file_exists(&store.cache_dir, &location, &(0..16)));
assert!(test_file_exists(&store.cache_dir, &location, &(16..32)));
// insert new cache, evict oldest entry
let _ = store.inner.get_range(&location, 32..48).await.unwrap();
assert!(!test_file_exists(&store.cache_dir, &location, &(0..16)));
assert!(test_file_exists(&store.cache_dir, &location, &(32..48)));
// insert new cache, evict oldest entry
let _ = store.inner.get_range(&location, 48..64).await.unwrap();
assert!(!test_file_exists(&store.cache_dir, &location, &(16..32)));
assert!(test_file_exists(&store.cache_dir, &location, &(48..64)));
}
#[tokio::test]
async fn test_disk_cache_manifest() {
let cache_dir = tempdir().unwrap();
let cache_root_dir = cache_dir.as_ref().to_string_lossy().to_string();
let page_size = 8;
let first_create_time = {
let _store = {
let local_path = tempdir().unwrap();
let local_store =
Arc::new(LocalFileSystem::new_with_prefix(local_path.path()).unwrap());
DiskCacheStore::try_new(cache_root_dir.clone(), 160, 8, local_store)
.await
.unwrap()
};
let manifest =
DiskCacheStore::create_manifest_if_not_exists(&cache_root_dir, page_size)
.await
.unwrap();
assert_eq!(manifest.page_size, 8);
assert_eq!(manifest.version, 1);
manifest.create_at
};
// open again
{
let _store = {
let local_path = tempdir().unwrap();
let local_store =
Arc::new(LocalFileSystem::new_with_prefix(local_path.path()).unwrap());
DiskCacheStore::try_new(cache_root_dir.clone(), 160, 8, local_store)
.await
.unwrap()
};
let manifest =
DiskCacheStore::create_manifest_if_not_exists(&cache_root_dir, page_size)
.await
.unwrap();
assert_eq!(manifest.create_at, first_create_time);
assert_eq!(manifest.page_size, 8);
assert_eq!(manifest.version, 1);
}
// open again, but with different page_size
{
let local_path = tempdir().unwrap();
let local_store =
Arc::new(LocalFileSystem::new_with_prefix(local_path.path()).unwrap());
let store = DiskCacheStore::try_new(
cache_dir.as_ref().to_string_lossy().to_string(),
160,
page_size * 2,
local_store,
)
.await;
assert!(store.is_err())
}
}
#[tokio::test]
async fn test_disk_cache_recovery() {
let cache_dir = tempdir().unwrap();
let cache_root_dir = cache_dir.as_ref().to_string_lossy().to_string();
let page_size = 16;
let location = Path::from("recovery.sst");
{
let store = {
let local_path = tempdir().unwrap();
let local_store =
Arc::new(LocalFileSystem::new_with_prefix(local_path.path()).unwrap());
DiskCacheStore::try_new(cache_root_dir.clone(), 10240, page_size, local_store)
.await
.unwrap()
};
let data = b"abcd";
let mut buf = BytesMut::with_capacity(data.len() * 1024);
for _ in 0..1024 {
buf.extend_from_slice(data);
}
store.put(&location, buf.freeze()).await.unwrap();
assert!(!store
.get_range(&location, 16..100)
.await
.unwrap()
.is_empty());
};
// recover
{
let store = {
let local_path = tempdir().unwrap();
let local_store =
Arc::new(LocalFileSystem::new_with_prefix(local_path.path()).unwrap());
DiskCacheStore::try_new(cache_root_dir.clone(), 160, page_size, local_store)
.await
.unwrap()
};
let cache = store.cache.cache.lock().await;
for range in vec![16..32, 32..48, 48..64, 64..80, 80..96, 96..112] {
assert!(cache.contains(&DiskCacheStore::cache_key(&location, &range)));
assert!(test_file_exists(&cache_dir, &location, &range));
}
};
}
#[test]
fn test_disk_cache_bytes_crc() {
let testcases = vec![("abc", 910901175), ("hello ceresdb", 2026251212)];
for (input, expect) in testcases {
let actual = CASTAGNOLI.checksum(input.as_bytes());
assert_eq!(actual, expect);
}
}
#[tokio::test]
async fn corrupted_disk_cache() {
let StoreWithCacheDir {
inner: store,
cache_dir,
} = prepare_store(16, 1024).await;
let test_file_name = "corrupted_disk_cache_file";
let test_file_path = Path::from(test_file_name);
let test_file_bytes = Bytes::from("corrupted_disk_cache_file_data");
// Put data into store and get it to let the cache load the data.
store
.put(&test_file_path, test_file_bytes.clone())
.await
.unwrap();
// The data should be in the cache.
let got_bytes = store
.get_range(&test_file_path, 0..test_file_bytes.len())
.await
.unwrap();
assert_eq!(got_bytes, test_file_bytes);
// Corrupt files in the cache dir.
let mut cache_read_dir = tokio::fs::read_dir(cache_dir.as_ref()).await.unwrap();
while let Some(entry) = cache_read_dir.next_entry().await.unwrap() {
let path_buf = entry.path();
let path = path_buf.to_str().unwrap();
if path.contains(test_file_name) {
let mut file = tokio::fs::OpenOptions::new()
.write(true)
.open(path)
.await
.unwrap();
file.write_all(b"corrupted").await.unwrap();
}
}
// The data should be removed from the cache.
let got_bytes = store
.get_range(&test_file_path, 0..test_file_bytes.len())
.await
.unwrap();
assert_eq!(got_bytes, test_file_bytes);
// The cache should be updated.
let mut cache_read_dir = tokio::fs::read_dir(cache_dir.as_ref()).await.unwrap();
while let Some(entry) = cache_read_dir.next_entry().await.unwrap() {
let path_buf = entry.path();
let path = path_buf.to_str().unwrap();
if path.contains(test_file_name) {
let mut file = tokio::fs::OpenOptions::new()
.read(true)
.open(path)
.await
.unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).await.unwrap();
assert_ne!(buffer, b"corrupted");
}
}
}
}