risingwave_storage/hummock/
time_travel_version_cache.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::future::Future;
16use std::pin::Pin;
17
18use futures::FutureExt;
19use futures::future::Shared;
20use moka::sync::Cache;
21use risingwave_common::id::TableId;
22use risingwave_hummock_sdk::HummockEpoch;
23
24use crate::hummock::HummockResult;
25use crate::hummock::local_version::pinned_version::PinnedVersion;
26
27type InflightResult = Shared<Pin<Box<dyn Future<Output = HummockResult<PinnedVersion>> + Send>>>;
28
29/// A naive cache to reduce number of RPC sent to meta node.
30pub struct SimpleTimeTravelVersionCache {
31    cache: Cache<(TableId, HummockEpoch), InflightResult>,
32}
33
34impl SimpleTimeTravelVersionCache {
35    pub fn new(capacity: u64) -> Self {
36        let cache = Cache::builder().max_capacity(capacity).build();
37        Self { cache }
38    }
39
40    pub async fn get_or_insert(
41        &self,
42        table_id: TableId,
43        epoch: HummockEpoch,
44        fetch: impl Future<Output = HummockResult<PinnedVersion>> + Send + 'static,
45    ) -> HummockResult<PinnedVersion> {
46        self.cache
47            .entry((table_id, epoch))
48            .or_insert_with_if(
49                || fetch.boxed().shared(),
50                |inflight| {
51                    if let Some(result) = inflight.peek() {
52                        return result.is_err();
53                    }
54                    false
55                },
56            )
57            .value()
58            .clone()
59            .await
60    }
61}