-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an integration test for the shared cache #1071
Open
vladem
wants to merge
1
commit into
awslabs:main
Choose a base branch
from
vladem:express-cache-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use crate::common::fuse::{self, TestSessionConfig}; | ||
use crate::common::s3::{get_express_cache_bucket, get_test_bucket_and_prefix}; | ||
use mountpoint_s3::data_cache::{DataCache, DiskDataCache, DiskDataCacheConfig, ExpressDataCache}; | ||
use mountpoint_s3_client::S3CrtClient; | ||
use rand::{Rng, RngCore, SeedableRng}; | ||
use rand_chacha::ChaChaRng; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
use test_case::test_case; | ||
|
||
#[test_case("key", 100, 1024; "simple")] | ||
#[test_case("£", 100, 1024; "non-ascii key")] | ||
#[test_case("key", 1024, 1024; "long key")] | ||
#[test_case("key", 100, 1024 * 1024; "big file")] | ||
fn express_cache_write_read(key_suffix: &str, key_size: usize, object_size: usize) { | ||
cache_write_read_base( | ||
key_suffix, | ||
key_size, | ||
object_size, | ||
express_cache_factory, | ||
"express_cache_write_read", | ||
) | ||
} | ||
|
||
#[test_case("key", 100, 1024; "simple")] | ||
#[test_case("£", 100, 1024; "non-ascii key")] | ||
#[test_case("key", 1024, 1024; "long key")] | ||
#[test_case("key", 100, 1024 * 1024; "big file")] | ||
fn disk_cache_write_read(key_suffix: &str, key_size: usize, object_size: usize) { | ||
let cache_dir = tempfile::tempdir().unwrap(); | ||
cache_write_read_base( | ||
key_suffix, | ||
key_size, | ||
object_size, | ||
disk_cache_factory(cache_dir.path().to_owned()), | ||
"disk_cache_write_read", | ||
); | ||
} | ||
|
||
fn cache_write_read_base<Cache, CacheFactory>( | ||
key_suffix: &str, | ||
key_size: usize, | ||
object_size: usize, | ||
cache_factory: CacheFactory, | ||
test_name: &str, | ||
) where | ||
Cache: DataCache + Send + Sync + 'static, | ||
CacheFactory: FnOnce(S3CrtClient, u64) -> Cache, | ||
{ | ||
// mount a bucket | ||
const BLOCK_SIZE: u64 = 512 * 1024; | ||
let mut test_config: TestSessionConfig = TestSessionConfig::default(); | ||
test_config.filesystem_config.cache_config.serve_lookup_from_cache = true; | ||
test_config.filesystem_config.cache_config.dir_ttl = Duration::from_secs(3600); | ||
test_config.filesystem_config.cache_config.file_ttl = Duration::from_secs(3600); | ||
let (_, prefix) = get_test_bucket_and_prefix(test_name); | ||
let (mount_point, _session, mut client) = | ||
fuse::s3_session::new_with_cache_factory(prefix.clone(), cache_factory, test_config, BLOCK_SIZE); | ||
|
||
// write an object, no caching happens yet | ||
let key = get_object_key(&prefix, key_suffix, key_size); | ||
let path = mount_point.path().join(&key); | ||
let written = random_binary_data(object_size); | ||
fs::write(&path, &written).expect("write should succeed"); | ||
|
||
// first read should be from the source bucket and be cached | ||
let read = fs::read(&path).expect("read should succeed"); | ||
assert_eq!(read, written); | ||
|
||
// cache population is async, 1 second should be enough for it to finish | ||
sleep(Duration::from_secs(1)); | ||
|
||
// ensure data may not be served from the source bucket | ||
client.remove_object(&key).expect("remove must succeed"); | ||
assert!( | ||
!client.contains_key(&key).expect("head object must succeed"), | ||
"object should not exist in the source bucket" | ||
); | ||
|
||
// second read should be from the cache | ||
let read = fs::read(&path).expect("read from the cache should succeed"); | ||
assert_eq!(read, written); | ||
} | ||
|
||
fn express_cache_factory(client: S3CrtClient, block_size: u64) -> ExpressDataCache<S3CrtClient> { | ||
let express_bucket_name = get_express_cache_bucket(); | ||
ExpressDataCache::new(&express_bucket_name, client, &express_bucket_name, block_size) | ||
} | ||
|
||
fn disk_cache_factory(cache_dir: PathBuf) -> impl FnOnce(S3CrtClient, u64) -> DiskDataCache { | ||
move |_, block_size| { | ||
let cache_config = DiskDataCacheConfig { | ||
block_size, | ||
limit: Default::default(), | ||
}; | ||
DiskDataCache::new(cache_dir, cache_config) | ||
} | ||
} | ||
|
||
fn random_binary_data(size_in_bytes: usize) -> Vec<u8> { | ||
let seed = rand::thread_rng().gen(); | ||
let mut rng = ChaChaRng::seed_from_u64(seed); | ||
let mut data = vec![0; size_in_bytes]; | ||
rng.fill_bytes(&mut data); | ||
data | ||
} | ||
|
||
// Creates a random key which has a size of at least `min_size_in_bytes` | ||
fn get_object_key(key_prefix: &str, key_suffix: &str, min_size_in_bytes: usize) -> String { | ||
let random_suffix: u64 = rand::thread_rng().gen(); | ||
let last_key_part = format!("{key_suffix}{random_suffix}"); // part of the key after all the "/" | ||
let full_key = format!("{key_prefix}{last_key_part}"); | ||
let full_key_size = full_key.as_bytes().len(); | ||
let padding_size = min_size_in_bytes.saturating_sub(full_key_size); | ||
let padding = "0".repeat(padding_size); | ||
format!("{last_key_part}{padding}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way for us to tell if it's done?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can wait (with a timeout) for a cache block to appear in the express bucket, but it will require calculating the expected s3 key of the block in the test code. I can make block_key and cache prefix functions public. Next we may want to re-use SDKTestClient to access the cache bucket, which will require another exposure of the create_test_client method which is private now.
On the other hand,
block_key
function is really an impl detail of the express cache, which is why I decided not to implement the check in this way. I can not imagine a scenario where 1 sec is not enough to populate the cache in this test case, and this may be a good indicator of something going wrong.@muddyfish, any thoughts which may incline us towards polling s3 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that avoiding flakey tests is something we should do here. Is it possible to make things public for tests only? Otherwise, I wonder if it's possible for us to return some awaitable on cache write complete.
Can this impact regular on-disk cache?