-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathfilesystem_store_test.rs
912 lines (795 loc) · 34.2 KB
/
filesystem_store_test.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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
// Copyright 2023 The Native Link Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::cell::RefCell;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::ops::DerefMut;
use std::path::Path;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
use async_lock::RwLock;
use async_trait::async_trait;
use error::{Code, Error, ResultExt};
use filetime::{set_file_atime, FileTime};
use futures::executor::block_on;
use futures::task::Poll;
use futures::{poll, Future};
use native_link_store::filesystem_store::{
digest_from_filename, EncodedFilePath, FileEntry, FileEntryImpl, FilesystemStore,
};
use native_link_util::buf_channel::{make_buf_channel_pair, DropCloserReadHalf};
use native_link_util::common::{fs, DigestInfo, JoinHandleDropGuard};
use native_link_util::evicting_map::LenEntry;
use native_link_util::store_trait::{Store, UploadSizeInfo};
use once_cell::sync::Lazy;
use rand::{thread_rng, Rng};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::Barrier;
use tokio::time::sleep;
use tokio_stream::wrappers::ReadDirStream;
use tokio_stream::StreamExt;
trait FileEntryHooks {
fn on_unref<Fe: FileEntry>(_entry: &Fe) {}
fn on_drop<Fe: FileEntry>(_entry: &Fe) {}
}
struct TestFileEntry<Hooks: FileEntryHooks + 'static + Sync + Send> {
inner: Option<FileEntryImpl>,
_phantom: PhantomData<Hooks>,
}
impl<Hooks: FileEntryHooks + 'static + Sync + Send> Debug for TestFileEntry<Hooks> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_struct("TestFileEntry").field("inner", &self.inner).finish()
}
}
#[async_trait]
impl<Hooks: FileEntryHooks + 'static + Sync + Send> FileEntry for TestFileEntry<Hooks> {
fn create(file_size: u64, encoded_file_path: RwLock<EncodedFilePath>) -> Self {
Self {
inner: Some(FileEntryImpl::create(file_size, encoded_file_path)),
_phantom: PhantomData,
}
}
async fn make_and_open_file(
encoded_file_path: EncodedFilePath,
) -> Result<(Self, fs::ResumeableFileSlot<'static>, OsString), Error> {
let (inner, file_slot, path) = FileEntryImpl::make_and_open_file(encoded_file_path).await?;
Ok((
Self {
inner: Some(inner),
_phantom: PhantomData,
},
file_slot,
path,
))
}
fn get_file_size(&mut self) -> &mut u64 {
self.inner.as_mut().unwrap().get_file_size()
}
fn get_encoded_file_path(&self) -> &RwLock<EncodedFilePath> {
self.inner.as_ref().unwrap().get_encoded_file_path()
}
async fn read_file_part<'a>(&'a self, offset: u64, length: u64) -> Result<fs::ResumeableFileSlot<'a>, Error> {
self.inner.as_ref().unwrap().read_file_part(offset, length).await
}
async fn get_file_path_locked<
T,
Fut: Future<Output = Result<T, Error>> + Send,
F: FnOnce(OsString) -> Fut + Send,
>(
&self,
handler: F,
) -> Result<T, Error> {
self.inner.as_ref().unwrap().get_file_path_locked(handler).await
}
}
#[async_trait]
impl<Hooks: FileEntryHooks + 'static + Sync + Send> LenEntry for TestFileEntry<Hooks> {
fn len(&self) -> usize {
self.inner.as_ref().unwrap().len()
}
fn is_empty(&self) -> bool {
self.inner.as_ref().unwrap().is_empty()
}
async fn touch(&self) {
self.inner.as_ref().unwrap().touch().await
}
async fn unref(&self) {
Hooks::on_unref(self);
self.inner.as_ref().unwrap().unref().await
}
}
impl<Hooks: FileEntryHooks + 'static + Sync + Send> Drop for TestFileEntry<Hooks> {
fn drop(&mut self) {
let mut inner = self.inner.take().unwrap();
let shared_context = inner.get_shared_context_for_test();
// We do this complicated bit here because tokio does not give a way to run a
// command that will wait for all tasks and sub spawns to complete.
// Sadly we need to rely on `active_drop_spawns` to hit zero to ensure that
// all tasks have completed.
let thread_handle = std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
rt.block_on(async move {
// Drop the FileEntryImpl in a controlled setting then wait for the
// `active_drop_spawns` to hit zero.
drop(inner);
while shared_context.active_drop_spawns.load(Ordering::Relaxed) > 0 {
tokio::task::yield_now().await;
}
});
});
thread_handle.join().unwrap();
// At this point we can guarantee our file drop spawn has completed.
Hooks::on_drop(self);
}
}
/// Get temporary path from either `TEST_TMPDIR` or best effort temp directory if
/// not set.
fn make_temp_path(data: &str) -> String {
format!(
"{}/{}/{}",
env::var("TEST_TMPDIR").unwrap_or(env::temp_dir().to_str().unwrap().to_string()),
thread_rng().gen::<u64>(),
data
)
}
async fn read_file_contents(file_name: &OsStr) -> Result<Vec<u8>, Error> {
let mut file = fs::open_file(file_name, u64::MAX)
.await
.err_tip(|| format!("Failed to open file: {file_name:?}"))?;
let mut data = vec![];
file.as_reader()
.await?
.read_to_end(&mut data)
.await
.err_tip(|| "Error reading file to end")?;
Ok(data)
}
async fn write_file(file_name: &OsStr, data: &[u8]) -> Result<(), Error> {
let mut file = fs::create_file(file_name)
.await
.err_tip(|| format!("Failed to create file: {file_name:?}"))?;
file.as_writer().await?.write_all(data).await?;
file.as_writer()
.await?
.as_mut()
.sync_all()
.await
.err_tip(|| "Could not sync file")
}
#[cfg(test)]
mod filesystem_store_tests {
use pretty_assertions::assert_eq;
use super::*; // Must be declared in every module.
const HASH1: &str = "0123456789abcdef000000000000000000010000000000000123456789abcdef";
const HASH2: &str = "0123456789abcdef000000000000000000020000000000000123456789abcdef";
const VALUE1: &str = "123";
const VALUE2: &str = "321";
#[tokio::test]
async fn valid_results_after_shutdown_test() -> Result<(), Error> {
let digest = DigestInfo::try_new(HASH1, VALUE1.len())?;
let content_path = make_temp_path("content_path");
let temp_path = make_temp_path("temp_path");
{
let store = Box::pin(
FilesystemStore::<FileEntryImpl>::new(&native_link_config::stores::FilesystemStore {
content_path: content_path.clone(),
temp_path: temp_path.clone(),
eviction_policy: None,
..Default::default()
})
.await?,
);
// Insert dummy value into store.
store.as_ref().update_oneshot(digest, VALUE1.into()).await?;
assert_eq!(
store.as_ref().has(digest).await,
Ok(Some(VALUE1.len())),
"Expected filesystem store to have hash: {}",
HASH1
);
}
{
// With a new store ensure content is still readable (ie: restores from shutdown).
let store = Box::pin(
FilesystemStore::<FileEntryImpl>::new(&native_link_config::stores::FilesystemStore {
content_path,
temp_path,
eviction_policy: None,
..Default::default()
})
.await?,
);
let content = store.as_ref().get_part_unchunked(digest, 0, None, None).await?;
assert_eq!(content, VALUE1.as_bytes());
}
Ok(())
}
#[tokio::test]
async fn temp_files_get_deleted_on_replace_test() -> Result<(), Error> {
let digest1 = DigestInfo::try_new(HASH1, VALUE1.len())?;
let content_path = make_temp_path("content_path");
let temp_path = make_temp_path("temp_path");
static DELETES_FINISHED: AtomicU32 = AtomicU32::new(0);
struct LocalHooks {}
impl FileEntryHooks for LocalHooks {
fn on_drop<Fe: FileEntry>(_file_entry: &Fe) {
DELETES_FINISHED.fetch_add(1, Ordering::Relaxed);
}
}
let store = Box::pin(
FilesystemStore::<TestFileEntry<LocalHooks>>::new(&native_link_config::stores::FilesystemStore {
content_path: content_path.clone(),
temp_path: temp_path.clone(),
eviction_policy: Some(native_link_config::stores::EvictionPolicy {
max_count: 3,
..Default::default()
}),
..Default::default()
})
.await?,
);
store.as_ref().update_oneshot(digest1, VALUE1.into()).await?;
let expected_file_name = OsString::from(format!(
"{}/{}-{}",
content_path,
digest1.hash_str(),
digest1.size_bytes
));
{
// Check to ensure our file exists where it should and content matches.
let data = read_file_contents(&expected_file_name).await?;
assert_eq!(&data[..], VALUE1.as_bytes(), "Expected file content to match");
}
// Replace content.
store.as_ref().update_oneshot(digest1, VALUE2.into()).await?;
{
// Check to ensure our file now has new content.
let data = read_file_contents(&expected_file_name).await?;
assert_eq!(&data[..], VALUE2.as_bytes(), "Expected file content to match");
}
loop {
if DELETES_FINISHED.load(Ordering::Relaxed) == 1 {
break;
}
tokio::task::yield_now().await;
}
let (_permit, temp_dir_handle) = fs::read_dir(temp_path.clone())
.await
.err_tip(|| "Failed opening temp directory")?
.into_inner();
let mut read_dir_stream = ReadDirStream::new(temp_dir_handle);
if let Some(temp_dir_entry) = read_dir_stream.next().await {
let path = temp_dir_entry?.path();
panic!("No files should exist in temp directory, found: {path:?}");
}
Ok(())
}
// This test ensures that if a file is overridden and an open stream to the file already
// exists, the open stream will continue to work properly and when the stream is done the
// temporary file (of the object that was deleted) is cleaned up.
#[tokio::test]
async fn file_continues_to_stream_on_content_replace_test() -> Result<(), Error> {
let digest1 = DigestInfo::try_new(HASH1, VALUE1.len())?;
let content_path = make_temp_path("content_path");
let temp_path = make_temp_path("temp_path");
static DELETES_FINISHED: AtomicU32 = AtomicU32::new(0);
struct LocalHooks {}
impl FileEntryHooks for LocalHooks {
fn on_drop<Fe: FileEntry>(_file_entry: &Fe) {
DELETES_FINISHED.fetch_add(1, Ordering::Relaxed);
}
}
let store = Arc::new(
FilesystemStore::<TestFileEntry<LocalHooks>>::new(&native_link_config::stores::FilesystemStore {
content_path: content_path.clone(),
temp_path: temp_path.clone(),
eviction_policy: Some(native_link_config::stores::EvictionPolicy {
max_count: 3,
..Default::default()
}),
read_buffer_size: 1,
})
.await?,
);
let store_pin = Pin::new(store.as_ref());
// Insert data into store.
store_pin.as_ref().update_oneshot(digest1, VALUE1.into()).await?;
let (writer, mut reader) = make_buf_channel_pair();
let store_clone = store.clone();
let digest1_clone = digest1;
tokio::spawn(async move { Pin::new(store_clone.as_ref()).get(digest1_clone, writer).await });
{
// Check to ensure our first byte has been received. The future should be stalled here.
let first_byte = DropCloserReadHalf::take(&mut reader, 1)
.await
.err_tip(|| "Error reading first byte")?;
assert_eq!(first_byte[0], VALUE1.as_bytes()[0], "Expected first byte to match");
}
// Replace content.
store_pin.as_ref().update_oneshot(digest1, VALUE2.into()).await?;
// Ensure we let any background tasks finish.
tokio::task::yield_now().await;
{
// Now ensure we only have 1 file in our temp path.
let (_permit, temp_dir_handle) = fs::read_dir(temp_path.clone())
.await
.err_tip(|| "Failed opening temp directory")?
.into_inner();
let mut read_dir_stream = ReadDirStream::new(temp_dir_handle);
let mut num_files = 0;
while let Some(temp_dir_entry) = read_dir_stream.next().await {
num_files += 1;
let path = temp_dir_entry?.path();
let data = read_file_contents(path.as_os_str()).await?;
assert_eq!(&data[..], VALUE1.as_bytes(), "Expected file content to match");
}
assert_eq!(num_files, 1, "There should only be one file in the temp directory");
}
let remaining_file_data = DropCloserReadHalf::take(&mut reader, 1024)
.await
.err_tip(|| "Error reading remaining bytes")?;
assert_eq!(
&remaining_file_data,
VALUE1[1..].as_bytes(),
"Expected file content to match"
);
loop {
if DELETES_FINISHED.load(Ordering::Relaxed) == 1 {
break;
}
tokio::task::yield_now().await;
}
{
// Now ensure our temp file was cleaned up.
let (_permit, temp_dir_handle) = fs::read_dir(temp_path.clone())
.await
.err_tip(|| "Failed opening temp directory")?
.into_inner();
let mut read_dir_stream = ReadDirStream::new(temp_dir_handle);
if let Some(temp_dir_entry) = read_dir_stream.next().await {
let path = temp_dir_entry?.path();
panic!("No files should exist in temp directory, found: {path:?}");
}
}
Ok(())
}
// Eviction has a different code path than a file replacement, so we check that if a
// file is evicted and has an open stream on it, it will stay alive and eventually
// get deleted.
#[tokio::test]
async fn file_gets_cleans_up_on_cache_eviction() -> Result<(), Error> {
let digest1 = DigestInfo::try_new(HASH1, VALUE1.len())?;
let digest2 = DigestInfo::try_new(HASH2, VALUE2.len())?;
let content_path = make_temp_path("content_path");
let temp_path = make_temp_path("temp_path");
static DELETES_FINISHED: AtomicU32 = AtomicU32::new(0);
struct LocalHooks {}
impl FileEntryHooks for LocalHooks {
fn on_drop<Fe: FileEntry>(_file_entry: &Fe) {
DELETES_FINISHED.fetch_add(1, Ordering::Relaxed);
}
}
let store = Arc::new(
FilesystemStore::<TestFileEntry<LocalHooks>>::new(&native_link_config::stores::FilesystemStore {
content_path: content_path.clone(),
temp_path: temp_path.clone(),
eviction_policy: Some(native_link_config::stores::EvictionPolicy {
max_count: 1,
..Default::default()
}),
read_buffer_size: 1,
})
.await?,
);
let store_pin = Pin::new(store.as_ref());
// Insert data into store.
store_pin.as_ref().update_oneshot(digest1, VALUE1.into()).await?;
let mut reader = {
let (writer, reader) = make_buf_channel_pair();
let store_clone = store.clone();
tokio::spawn(async move { Pin::new(store_clone.as_ref()).get(digest1, writer).await });
reader
};
// Ensure we have received 1 byte in our buffer. This will ensure we have a reference to
// our file open.
assert!(reader.peek().await.is_ok(), "Could not peek into reader");
// Insert new content. This will evict the old item.
store_pin.as_ref().update_oneshot(digest2, VALUE2.into()).await?;
// Ensure we let any background tasks finish.
tokio::task::yield_now().await;
{
// Now ensure we only have 1 file in our temp path.
let (_permit, temp_dir_handle) = fs::read_dir(temp_path.clone())
.await
.err_tip(|| "Failed opening temp directory")?
.into_inner();
let mut read_dir_stream = ReadDirStream::new(temp_dir_handle);
let mut num_files = 0;
while let Some(temp_dir_entry) = read_dir_stream.next().await {
num_files += 1;
let path = temp_dir_entry?.path();
let data = read_file_contents(path.as_os_str()).await?;
assert_eq!(&data[..], VALUE1.as_bytes(), "Expected file content to match");
}
assert_eq!(num_files, 1, "There should only be one file in the temp directory");
}
let reader_data = DropCloserReadHalf::take(&mut reader, 1024)
.await
.err_tip(|| "Error reading remaining bytes")?;
assert_eq!(&reader_data, VALUE1, "Expected file content to match");
loop {
if DELETES_FINISHED.load(Ordering::Relaxed) == 1 {
break;
}
tokio::task::yield_now().await;
}
{
// Now ensure our temp file was cleaned up.
let (_permit, temp_dir_handle) = fs::read_dir(temp_path.clone())
.await
.err_tip(|| "Failed opening temp directory")?
.into_inner();
let mut read_dir_stream = ReadDirStream::new(temp_dir_handle);
if let Some(temp_dir_entry) = read_dir_stream.next().await {
let path = temp_dir_entry?.path();
panic!("No files should exist in temp directory, found: {:?}", path);
}
}
Ok(())
}
#[tokio::test]
async fn atime_updates_on_get_part_test() -> Result<(), Error> {
let digest1 = DigestInfo::try_new(HASH1, VALUE1.len())?;
let store = Box::pin(
FilesystemStore::<FileEntryImpl>::new(&native_link_config::stores::FilesystemStore {
content_path: make_temp_path("content_path"),
temp_path: make_temp_path("temp_path"),
eviction_policy: None,
..Default::default()
})
.await?,
);
// Insert data into store.
store.as_ref().update_oneshot(digest1, VALUE1.into()).await?;
let file_entry = store.get_file_entry_for_digest(&digest1).await?;
file_entry
.get_file_path_locked(move |path| async move {
// Set atime to along time ago.
set_file_atime(&path, FileTime::from_system_time(SystemTime::UNIX_EPOCH))?;
// Check to ensure it was set to zero from previous command.
assert_eq!(fs::metadata(&path).await?.accessed()?, SystemTime::UNIX_EPOCH);
Ok(())
})
.await?;
// Now touch digest1.
let data = store.as_ref().get_part_unchunked(digest1, 0, None, None).await?;
assert_eq!(data, VALUE1.as_bytes());
file_entry
.get_file_path_locked(move |path| async move {
// Ensure it was updated.
assert!(fs::metadata(&path).await?.accessed()? > SystemTime::UNIX_EPOCH);
Ok(())
})
.await?;
Ok(())
}
#[tokio::test]
async fn oldest_entry_evicted_with_access_times_loaded_from_disk() -> Result<(), Error> {
// Note these are swapped to ensure they aren't in numerical order.
let digest1 = DigestInfo::try_new(HASH2, VALUE2.len())?;
let digest2 = DigestInfo::try_new(HASH1, VALUE1.len())?;
let content_path = make_temp_path("content_path");
fs::create_dir_all(&content_path).await?;
// Make the two files on disk before loading the store.
let file1 = OsString::from(format!(
"{}/{}-{}",
content_path,
digest1.hash_str(),
digest1.size_bytes
));
let file2 = OsString::from(format!(
"{}/{}-{}",
content_path,
digest2.hash_str(),
digest2.size_bytes
));
write_file(&file1, VALUE1.as_bytes()).await?;
write_file(&file2, VALUE2.as_bytes()).await?;
set_file_atime(&file1, FileTime::from_unix_time(0, 0))?;
set_file_atime(&file2, FileTime::from_unix_time(1, 0))?;
// Load the existing store from disk.
let store = Box::pin(
FilesystemStore::<FileEntryImpl>::new(&native_link_config::stores::FilesystemStore {
content_path,
temp_path: make_temp_path("temp_path"),
eviction_policy: Some(native_link_config::stores::EvictionPolicy {
max_bytes: 0,
max_seconds: 0,
max_count: 1,
evict_bytes: 0,
}),
..Default::default()
})
.await?,
);
// This should exist and not have been evicted.
store.get_file_entry_for_digest(&digest2).await?;
// This should have been evicted.
match store.get_file_entry_for_digest(&digest1).await {
Ok(_) => panic!("Oldest file should have been evicted."),
Err(error) => assert_eq!(error.code, Code::NotFound),
}
Ok(())
}
#[tokio::test]
async fn eviction_drops_file_test() -> Result<(), Error> {
let digest1 = DigestInfo::try_new(HASH1, VALUE1.len())?;
let store = Box::pin(
FilesystemStore::<FileEntryImpl>::new(&native_link_config::stores::FilesystemStore {
content_path: make_temp_path("content_path"),
temp_path: make_temp_path("temp_path"),
eviction_policy: None,
..Default::default()
})
.await?,
);
// Insert data into store.
store.as_ref().update_oneshot(digest1, VALUE1.into()).await?;
let file_entry = store.get_file_entry_for_digest(&digest1).await?;
file_entry
.get_file_path_locked(move |path| async move {
// Set atime to along time ago.
set_file_atime(&path, FileTime::from_system_time(SystemTime::UNIX_EPOCH))?;
// Check to ensure it was set to zero from previous command.
assert_eq!(fs::metadata(&path).await?.accessed()?, SystemTime::UNIX_EPOCH);
Ok(())
})
.await?;
// Now touch digest1.
let data = store.as_ref().get_part_unchunked(digest1, 0, None, None).await?;
assert_eq!(data, VALUE1.as_bytes());
file_entry
.get_file_path_locked(move |path| async move {
// Ensure it was updated.
assert!(fs::metadata(&path).await?.accessed()? > SystemTime::UNIX_EPOCH);
Ok(())
})
.await?;
Ok(())
}
// Test to ensure that if we are holding a reference to `FileEntry` and the contents are
// replaced, the `FileEntry` continues to use the old data.
// `FileEntry` file contents should be immutable for the lifetime of the object.
#[tokio::test]
async fn digest_contents_replaced_continues_using_old_data() -> Result<(), Error> {
let digest = DigestInfo::try_new(HASH1, VALUE1.len())?;
let store = Box::pin(
FilesystemStore::<FileEntryImpl>::new(&native_link_config::stores::FilesystemStore {
content_path: make_temp_path("content_path"),
temp_path: make_temp_path("temp_path"),
eviction_policy: None,
..Default::default()
})
.await?,
);
// Insert data into store.
store.as_ref().update_oneshot(digest, VALUE1.into()).await?;
let file_entry = store.get_file_entry_for_digest(&digest).await?;
{
// The file contents should equal our initial data.
let mut reader = file_entry.read_file_part(0, u64::MAX).await?;
let mut file_contents = String::new();
reader.as_reader().await?.read_to_string(&mut file_contents).await?;
assert_eq!(file_contents, VALUE1);
}
// Now replace the data.
store.as_ref().update_oneshot(digest, VALUE2.into()).await?;
{
// The file contents still equal our old data.
let mut reader = file_entry.read_file_part(0, u64::MAX).await?;
let mut file_contents = String::new();
reader.as_reader().await?.read_to_string(&mut file_contents).await?;
assert_eq!(file_contents, VALUE1);
}
Ok(())
}
#[tokio::test]
async fn eviction_on_insert_calls_unref_once() -> Result<(), Error> {
const BIG_VALUE: &str = "0123";
let small_digest = DigestInfo::try_new(HASH1, VALUE1.len())?;
let big_digest = DigestInfo::try_new(HASH1, BIG_VALUE.len())?;
static UNREFED_DIGESTS: Lazy<Mutex<Vec<DigestInfo>>> = Lazy::new(|| Mutex::new(Vec::new()));
struct LocalHooks {}
impl FileEntryHooks for LocalHooks {
fn on_unref<Fe: FileEntry>(file_entry: &Fe) {
block_on(file_entry.get_file_path_locked(move |path_str| async move {
let path = Path::new(&path_str);
let digest = digest_from_filename(path.file_name().unwrap().to_str().unwrap()).unwrap();
UNREFED_DIGESTS.lock().unwrap().push(digest);
Ok(())
}))
.unwrap();
}
}
let store = Box::pin(
FilesystemStore::<TestFileEntry<LocalHooks>>::new(&native_link_config::stores::FilesystemStore {
content_path: make_temp_path("content_path"),
temp_path: make_temp_path("temp_path"),
eviction_policy: Some(native_link_config::stores::EvictionPolicy {
max_bytes: 5,
..Default::default()
}),
..Default::default()
})
.await?,
);
// Insert data into store.
store.as_ref().update_oneshot(small_digest, VALUE1.into()).await?;
store.as_ref().update_oneshot(big_digest, BIG_VALUE.into()).await?;
{
// Our first digest should have been unrefed exactly once.
let unrefed_digests = UNREFED_DIGESTS.lock().unwrap();
assert_eq!(unrefed_digests.len(), 1, "Expected exactly 1 unrefed digest");
assert_eq!(unrefed_digests[0], small_digest, "Expected digest to match");
}
Ok(())
}
#[allow(clippy::await_holding_refcell_ref)]
#[tokio::test]
async fn rename_on_insert_fails_due_to_filesystem_error_proper_cleanup_happens() -> Result<(), Error> {
let digest = DigestInfo::try_new(HASH1, VALUE1.len())?;
let content_path = make_temp_path("content_path");
let temp_path = make_temp_path("temp_path");
static FILE_DELETED_BARRIER: Lazy<Arc<Barrier>> = Lazy::new(|| Arc::new(Barrier::new(2)));
struct LocalHooks {}
impl FileEntryHooks for LocalHooks {
fn on_drop<Fe: FileEntry>(_file_entry: &Fe) {
tokio::spawn(FILE_DELETED_BARRIER.wait());
}
}
let store = Box::pin(
FilesystemStore::<TestFileEntry<LocalHooks>>::new(&native_link_config::stores::FilesystemStore {
content_path: content_path.clone(),
temp_path: temp_path.clone(),
eviction_policy: None,
..Default::default()
})
.await?,
);
let (mut tx, rx) = make_buf_channel_pair();
let update_fut = Rc::new(RefCell::new(store.as_ref().update(
digest,
rx,
UploadSizeInfo::MaxSize(100),
)));
// This will process as much of the future as it can before it needs to pause.
// Our temp file will be created and opened and ready to have contents streamed
// to it.
assert_eq!(poll!(update_fut.borrow_mut().deref_mut())?, Poll::Pending);
const INITIAL_CONTENT: &str = "hello";
tx.send(INITIAL_CONTENT.into()).await?;
// Now we extract that temp file that is generated.
async fn wait_for_temp_file<Fut: Future<Output = Result<(), Error>>, F: Fn() -> Fut>(
temp_path: &str,
yield_fn: F,
) -> Result<fs::DirEntry, Error> {
loop {
yield_fn().await?;
let (_permit, dir_handle) = fs::read_dir(&temp_path).await?.into_inner();
let mut read_dir_stream = ReadDirStream::new(dir_handle);
if let Some(dir_entry) = read_dir_stream.next().await {
assert!(
read_dir_stream.next().await.is_none(),
"There should only be one file in temp directory"
);
let dir_entry = dir_entry?;
{
// Some filesystems won't sync automatically, so force it.
let mut file_handle = fs::open_file(dir_entry.path().into_os_string(), u64::MAX)
.await
.err_tip(|| "Failed to open temp file")?;
// We don't care if it fails, this is only best attempt.
let _ = file_handle.as_reader().await?.get_ref().as_ref().sync_all().await;
}
// Ensure we have written to the file too. This ensures we have an open file handle.
// Failing to do this may result in the file existing, but the `update_fut` not actually
// sending data to it yet.
if dir_entry.metadata().await?.len() >= INITIAL_CONTENT.len() as u64 {
return Ok(dir_entry);
}
}
}
// Unreachable.
}
wait_for_temp_file(&temp_path, || {
let update_fut_clone = update_fut.clone();
async move {
// This will ensure we yield to our future and other potential spawns.
tokio::task::yield_now().await;
assert_eq!(poll!(update_fut_clone.borrow_mut().deref_mut())?, Poll::Pending);
Ok(())
}
})
.await?;
// Now make it impossible for the file to be moved into the final path.
// This will trigger an error on `rename()`.
fs::remove_dir_all(&content_path).await?;
// Because send_eof() waits for shutdown of the rx side, we cannot just await in this thread.
tokio::spawn(async move {
tx.send_eof().await.unwrap();
});
// Now finish waiting on update(). This should reuslt in an error because we deleted our dest
// folder.
let update_result = update_fut.borrow_mut().deref_mut().await;
assert!(
update_result.is_err(),
"Expected update to fail due to temp file being deleted before rename"
);
// Delete may happen on another thread, so wait for it.
FILE_DELETED_BARRIER.wait().await;
// Now it should have cleaned up it's temp files.
{
// Ensure `temp_path` is empty.
let (_permit, dir_handle) = fs::read_dir(&temp_path).await?.into_inner();
let mut read_dir_stream = ReadDirStream::new(dir_handle);
assert!(
read_dir_stream.next().await.is_none(),
"File found in temp_path after update() rename failure"
);
}
// Finally ensure that our entry is not in the store.
assert_eq!(store.as_ref().has(digest).await?, None, "Entry should not be in store");
Ok(())
}
#[tokio::test]
async fn get_part_timeout_test() -> Result<(), Error> {
let large_value = "x".repeat(1024);
let digest = DigestInfo::try_new(HASH1, large_value.len())?;
let content_path = make_temp_path("content_path");
let temp_path = make_temp_path("temp_path");
let store = Arc::new(
FilesystemStore::<FileEntryImpl>::new_with_timeout(
&native_link_config::stores::FilesystemStore {
content_path: content_path.clone(),
temp_path: temp_path.clone(),
read_buffer_size: 1,
..Default::default()
},
|_| sleep(Duration::ZERO),
)
.await?,
);
let store_pin = Pin::new(store.as_ref());
store_pin
.as_ref()
.update_oneshot(digest, large_value.clone().into())
.await?;
let (writer, mut reader) = make_buf_channel_pair();
let store_clone = store.clone();
let digest_clone = digest;
let _drop_guard = JoinHandleDropGuard::new(tokio::spawn(async move {
Pin::new(store_clone.as_ref()).get(digest_clone, writer).await
}));
let file_data = DropCloserReadHalf::take(&mut reader, 1024)
.await
.err_tip(|| "Error reading bytes")?;
assert_eq!(&file_data, large_value.as_bytes(), "Expected file content to match");
Ok(())
}
}