Skip to content

Commit

Permalink
Revert unnecessary switch to async lock
Browse files Browse the repository at this point in the history
Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk>
  • Loading branch information
passaro committed Sep 23, 2024
1 parent 4ff1f8a commit ea34668
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
12 changes: 6 additions & 6 deletions mountpoint-s3/src/data_cache/in_memory_data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use async_trait::async_trait;

use super::{BlockIndex, ChecksummedBytes, DataCache, DataCacheError, DataCacheResult};
use crate::object::ObjectId;
use crate::sync::AsyncRwLock;
use crate::sync::RwLock;

/// Simple in-memory (RAM) implementation of [DataCache]. Recommended for use in testing only.
pub struct InMemoryDataCache {
data: AsyncRwLock<HashMap<ObjectId, HashMap<BlockIndex, ChecksummedBytes>>>,
data: RwLock<HashMap<ObjectId, HashMap<BlockIndex, ChecksummedBytes>>>,
block_size: u64,
}

Expand All @@ -25,8 +25,8 @@ impl InMemoryDataCache {
}

/// Get number of caching blocks for the given cache key.
pub async fn block_count(&self, cache_key: &ObjectId) -> usize {
let data = self.data.read().await;
pub fn block_count(&self, cache_key: &ObjectId) -> usize {
let data = self.data.read().unwrap();
data.get(cache_key).map_or(0, |cache| cache.len())
}
}
Expand All @@ -42,7 +42,7 @@ impl DataCache for InMemoryDataCache {
if block_offset != block_idx * self.block_size {
return Err(DataCacheError::InvalidBlockOffset);
}
let data = self.data.read().await;
let data = self.data.read().unwrap();
let block_data = data.get(cache_key).and_then(|blocks| blocks.get(&block_idx)).cloned();
Ok(block_data)
}
Expand All @@ -57,7 +57,7 @@ impl DataCache for InMemoryDataCache {
if block_offset != block_idx * self.block_size {
return Err(DataCacheError::InvalidBlockOffset);
}
let mut data = self.data.write().await;
let mut data = self.data.write().unwrap();
let blocks = data.entry(cache_key).or_default();
blocks.insert(block_idx, bytes);
Ok(())
Expand Down
8 changes: 2 additions & 6 deletions mountpoint-s3/src/prefetch/caching_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,12 @@ mod tests {
};
assert!(first_read_count > 0);

fn get_block_count(cache: &InMemoryDataCache, id: &ObjectId) -> usize {
block_on(async move { cache.block_count(id).await })
}

// Wait until all blocks are saved to the cache before spawning a new request
let expected_block_count = expected_end_block - expected_start_block;
while get_block_count(&stream.cache, &id) < expected_block_count {
while stream.cache.block_count(&id) < expected_block_count {
thread::sleep(Duration::from_millis(10));
}
assert_eq!(expected_block_count, get_block_count(&stream.cache, &id));
assert_eq!(expected_block_count, stream.cache.block_count(&id));

let second_read_count = {
// Second request (from cache)
Expand Down

0 comments on commit ea34668

Please sign in to comment.