Skip to content

Commit

Permalink
fix: version in get
Browse files Browse the repository at this point in the history
  • Loading branch information
fengys1996 committed Feb 2, 2024
1 parent 4bab4d6 commit 2cad4da
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/catalog/src/kvbackend/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::any::Any;
use std::fmt::Debug;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::usize;

Expand Down Expand Up @@ -190,8 +190,14 @@ impl KvBackend for CachedMetaKvBackend {
async fn get(&self, key: &[u8]) -> Result<Option<KeyValue>> {
let _timer = METRIC_CATALOG_KV_GET.start_timer();

let pre_version = Arc::new(Mutex::new(None));

let init = async {
let version_clone = pre_version.clone();
let _timer = METRIC_CATALOG_KV_REMOTE_GET.start_timer();

version_clone.lock().unwrap().replace(self.version());

self.kv_backend.get(key).await.map(|val| {
val.with_context(|| CacheNotGetSnafu {
key: String::from_utf8_lossy(key),
Expand All @@ -202,7 +208,7 @@ impl KvBackend for CachedMetaKvBackend {
// currently moka doesn't have `optionally_try_get_with_by_ref`
// TODO(fys): change to moka method when available
// https://github.com/moka-rs/moka/issues/254
match self.cache.try_get_with_by_ref(key, init).await {
let ret = match self.cache.try_get_with_by_ref(key, init).await {
Ok(val) => Ok(Some(val)),
Err(e) => match e.as_ref() {
CacheNotGet { .. } => Ok(None),
Expand All @@ -211,7 +217,18 @@ impl KvBackend for CachedMetaKvBackend {
}
.map_err(|e| GetKvCache {
err_msg: e.to_string(),
})
});

if pre_version
.lock()
.unwrap()
.as_ref()
.map_or(false, |v| !self.validate_version(*v))
{
self.cache.invalidate(key).await;
}

ret
}
}

Expand Down

0 comments on commit 2cad4da

Please sign in to comment.