foyer-rs / foyer

Hybrid in-memory and disk cache in Rust
https://foyer.rs
Apache License 2.0
291 stars 20 forks source link

The memory cache `get` method encountered an `unwrap()` error. #763

Closed dierbei closed 2 weeks ago

dierbei commented 2 weeks ago

I have nested the foryer using a struct but when I do the unit test, I get None value, this is confusing me. please help me, thanks!

use foyer::{Cache, CacheBuilder};

pub(crate) struct BCache {
    cc: Cache<String, String>,
}

impl BCache {
    pub(crate) fn new(max_size: usize) -> Self {
        let mut cache: Cache<String, String> = CacheBuilder::new(max_size).build();

        Self {
            cc: cache,
        }
    }

    fn insert(&mut self, key: String, val: String) {
        self.cc.insert(key, val);
    }

    fn get(&mut self, key: String) -> String {
        let e = self.cc.get(&key).unwrap();
        e.value().clone()
    }

    fn remove(&mut self, key: String) {
        self.cc.remove(&key);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_cache() {
        let mut cache = BCache::new(2);
        cache.insert("hello".to_string(), "world".to_string());
        assert_eq!(cache.get("hello".to_string()), "world".to_string());
    }
}

error msg:

called `Option::unwrap()` on a `None` value
thread 'cache::tests::test_cache' panicked at src/cache.rs:21:35:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: rust_begin_unwind
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:74:14
   2: core::panicking::panic
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:148:5
   3: core::option::unwrap_failed
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/option.rs:2020:5
   4: core::option::Option<T>::unwrap
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/option.rs:970:21
   5: untitled::cache::BCache::get
             at ./src/cache.rs:21:17
   6: untitled::cache::tests::test_cache::{{closure}}
             at ./src/cache.rs:38:20
   7: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/future/future.rs:123:9
   8: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/future/future.rs:123:9
   9: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:696:57
  10: tokio::runtime::coop::with_budget
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/coop.rs:107:5
  11: tokio::runtime::coop::budget
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/coop.rs:73:5
  12: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:696:25
  13: tokio::runtime::scheduler::current_thread::Context::enter
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:423:19
  14: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:695:36
  15: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:774:68
  16: tokio::runtime::context::scoped::Scoped<T>::set
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/context/scoped.rs:40:9
  17: tokio::runtime::context::set_scheduler::{{closure}}
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/context.rs:180:26
  18: std::thread::local::LocalKey<T>::try_with
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/thread/local.rs:283:12
  19: std::thread::local::LocalKey<T>::with
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/thread/local.rs:260:9
  20: tokio::runtime::context::set_scheduler
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/context.rs:180:9
  21: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:774:27
  22: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:683:19
  23: tokio::runtime::scheduler::current_thread::CurrentThread::block_on::{{closure}}
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:191:28
  24: tokio::runtime::context::runtime::enter_runtime
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/context/runtime.rs:65:16
  25: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/scheduler/current_thread/mod.rs:179:9
  26: tokio::runtime::runtime::Runtime::block_on_inner
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/runtime.rs:361:47
  27: tokio::runtime::runtime::Runtime::block_on
             at /Users/apple/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/tokio-1.40.0/src/runtime/runtime.rs:335:13
  28: untitled::cache::tests::test_cache
             at ./src/cache.rs:38:9
  29: untitled::cache::tests::test_cache::{{closure}}
             at ./src/cache.rs:35:26
  30: core::ops::function::FnOnce::call_once
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
  31: core::ops::function::FnOnce::call_once
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
MrCroxx commented 2 weeks ago

Hi @dierbei . Thanks for asking.

foyer uses a sharding cache. The each shard splits itd own capacity from the total capacity to reduce sync overhead.

With a capacity lower than the shards, each shard cannot get enough capacity and caused thr problem.

For your case, you need to set the shards to 1.

I think I can refine yhe builder to give a warning in this case.

dierbei commented 2 weeks ago

@MrCroxx Cool, changing shards to 1 fixed the problem;

MrCroxx commented 2 weeks ago

Good to hear that. 🙌

Let me open an iusse for the warning and more reasonable shard config.