Skip to content

Commit

Permalink
Fix a bug that eviction listener's key-level locks are not removed
Browse files Browse the repository at this point in the history
when immediate notification mode is used
  • Loading branch information
tatsuya6502 committed Aug 2, 2023
1 parent d74d1e3 commit 93a6258
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/sync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,10 @@ where
pub(crate) fn set_expiration_clock(&self, clock: Option<crate::common::time::Clock>) {
self.base.set_expiration_clock(clock);
}

pub(crate) fn key_locks_map_is_empty(&self) -> bool {
self.base.key_locks_map_is_empty()
}
}

// To see the debug prints, run test as `cargo test -- --nocapture`
Expand Down Expand Up @@ -4339,6 +4343,8 @@ mod tests {
assert_eq!(a[0], (Arc::new("alice"), "a3", RemovalCause::Expired));
a.clear();
}

assert!(cache.key_locks_map_is_empty());
}

// This test ensures the key-level lock for the immediate notification
Expand Down Expand Up @@ -4469,6 +4475,8 @@ mod tests {
for (i, (actual, expected)) in actual.iter().zip(&expected).enumerate() {
assert_eq!(actual, expected, "expected[{}]", i);
}

assert!(cache.key_locks_map_is_empty());
}

// NOTE: To enable the panic logging, run the following command:
Expand Down
8 changes: 8 additions & 0 deletions src/sync_base/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ where
pub(crate) fn set_expiration_clock(&self, clock: Option<Clock>) {
self.inner.set_expiration_clock(clock);
}

pub(crate) fn key_locks_map_is_empty(&self) -> bool {

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / linux-cross (armv5te-unknown-linux-musleabi, --no-default-features)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / linux-cross (mips-unknown-linux-musl, --no-default-features)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / linux-cross (mipsel-unknown-linux-musl, --no-default-features)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (beta)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (1.65.0)

associated function `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (stable)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (stable)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (beta)

method `key_locks_map_is_empty` is never used

Check warning on line 789 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (1.65.0)

associated function `key_locks_map_is_empty` is never used
self.inner.key_locks_map_is_empty()
}
}

struct EvictionState<'a, K, V> {
Expand Down Expand Up @@ -2415,6 +2419,10 @@ where
*exp_clock = None;
}
}

fn key_locks_map_is_empty(&self) -> bool {

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / linux-cross (armv5te-unknown-linux-musleabi, --no-default-features)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / linux-cross (mips-unknown-linux-musl, --no-default-features)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / linux-cross (mipsel-unknown-linux-musl, --no-default-features)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (beta)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (1.65.0)

associated function `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (stable)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (stable)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (beta)

method `key_locks_map_is_empty` is never used

Check warning on line 2423 in src/sync_base/base_cache.rs

View workflow job for this annotation

GitHub Actions / test (1.65.0)

associated function `key_locks_map_is_empty` is never used
self.key_locks.as_ref().map(|m| m.is_empty()).unwrap_or_default()
}
}

//
Expand Down
11 changes: 9 additions & 2 deletions src/sync_base/key_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ where
S: BuildHasher,
{
fn drop(&mut self) {
if TrioArc::count(&self.lock) <= 1 {
if TrioArc::count(&self.lock) <= 2 {
self.map.remove_if(
self.hash,
|k| k == &self.key,
|_k, v| TrioArc::count(v) <= 1,
|_k, v| TrioArc::count(v) <= 2,
);
}
}
Expand Down Expand Up @@ -86,3 +86,10 @@ where
}
}
}

#[cfg(test)]
impl<K, S> KeyLockMap<K, S> {
pub(crate) fn is_empty(&self) -> bool {

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / linux-cross (armv5te-unknown-linux-musleabi, --no-default-features)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / linux-cross (mips-unknown-linux-musl, --no-default-features)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / linux-cross (mipsel-unknown-linux-musl, --no-default-features)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / test (beta)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / test (1.65.0)

associated function `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / test (stable)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / test (stable)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / test (beta)

method `is_empty` is never used

Check warning on line 92 in src/sync_base/key_lock.rs

View workflow job for this annotation

GitHub Actions / test (1.65.0)

associated function `is_empty` is never used
self.locks.len() == 0
}
}

0 comments on commit 93a6258

Please sign in to comment.