From e86682b5011cb373e684337dfa60cb6e6ab632e5 Mon Sep 17 00:00:00 2001 From: Tatsuya Kawano Date: Thu, 3 Aug 2023 20:44:00 +0800 Subject: [PATCH] Fix a bug that eviction listener's key-level locks are not removed when immediate notification mode is used Add more tests. --- src/future/cache.rs | 6 ++++++ src/sync/cache.rs | 2 ++ src/sync/segment.rs | 9 +++++++++ src/sync_base/base_cache.rs | 3 ++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/future/cache.rs b/src/future/cache.rs index d5206e78..4d783abb 100644 --- a/src/future/cache.rs +++ b/src/future/cache.rs @@ -2009,6 +2009,10 @@ where fn set_expiration_clock(&self, clock: Option) { self.base.set_expiration_clock(clock); } + + fn key_locks_map_is_empty(&self) -> bool { + self.base.key_locks_map_is_empty() + } } pub struct BlockingOp<'a, K, V, S>(&'a Cache); @@ -2164,6 +2168,7 @@ mod tests { assert!(!cache.contains_key(&"d")); verify_notification_vec(&cache, actual, &expected); + assert!(cache.key_locks_map_is_empty()); } #[test] @@ -2353,6 +2358,7 @@ mod tests { assert_eq!(cache.weighted_size(), 25); verify_notification_vec(&cache, actual, &expected); + assert!(cache.key_locks_map_is_empty()); } #[tokio::test] diff --git a/src/sync/cache.rs b/src/sync/cache.rs index f16b1816..23a9d002 100644 --- a/src/sync/cache.rs +++ b/src/sync/cache.rs @@ -2190,6 +2190,7 @@ mod tests { assert!(!cache.contains_key(&"d")); verify_notification_vec(&cache, actual, &expected, delivery_mode); + assert_with_mode!(cache.key_locks_map_is_empty(), delivery_mode); } } @@ -2320,6 +2321,7 @@ mod tests { assert_eq_with_mode!(cache.weighted_size(), 25, delivery_mode); verify_notification_vec(&cache, actual, &expected, delivery_mode); + assert_with_mode!(cache.key_locks_map_is_empty(), delivery_mode); } } diff --git a/src/sync/segment.rs b/src/sync/segment.rs index 61f07af3..c1728ad3 100644 --- a/src/sync/segment.rs +++ b/src/sync/segment.rs @@ -687,6 +687,13 @@ where exp_clock } + + fn key_locks_map_is_empty(&self) -> bool { + self.inner + .segments + .iter() + .all(|seg| seg.key_locks_map_is_empty()) + } } // For unit tests. @@ -916,6 +923,7 @@ mod tests { assert!(!cache.contains_key(&"d")); verify_notification_vec(&cache, actual, &expected, delivery_mode); + assert_with_mode!(cache.key_locks_map_is_empty(), delivery_mode); } } @@ -1065,6 +1073,7 @@ mod tests { assert_eq_with_mode!(cache.weighted_size(), 25, delivery_mode); verify_notification_vec(&cache, actual, &expected, delivery_mode); + assert_with_mode!(cache.key_locks_map_is_empty(), delivery_mode); } } diff --git a/src/sync_base/base_cache.rs b/src/sync_base/base_cache.rs index 86a086a5..7dd68220 100644 --- a/src/sync_base/base_cache.rs +++ b/src/sync_base/base_cache.rs @@ -2424,7 +2424,8 @@ where self.key_locks .as_ref() .map(|m| m.is_empty()) - .unwrap_or_default() + // If key_locks is None, consider it is empty. + .unwrap_or(true) } }