Skip to content

Commit 21f3bab

Browse files
authored
chore: add snapshot location cache for attached table (#16795)
To avoid redundant extraction of snapshot location from hint file, a snapshot location cache is added at the table instance level for tables that are attached to another table.
1 parent 7e42582 commit 21f3bab

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/query/storages/fuse/src/fuse_table.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::sync::Arc;
2323

2424
use chrono::Duration;
2525
use chrono::TimeDelta;
26+
use databend_common_base::base::tokio;
2627
use databend_common_catalog::catalog::StorageDescription;
2728
use databend_common_catalog::plan::DataSourcePlan;
2829
use databend_common_catalog::plan::PartStatistics;
@@ -130,8 +131,11 @@ pub struct FuseTable {
130131

131132
table_type: FuseTableType,
132133

133-
// If this is set, reading from fuse_table should only returns the increment blocks
134+
// If this is set, reading from fuse_table should only return the increment blocks
134135
pub(crate) changes_desc: Option<ChangesDesc>,
136+
137+
// A table instance level cache of snapshot_location, if this table is attaching to someone else.
138+
attached_table_location: tokio::sync::OnceCell<String>,
135139
}
136140

137141
impl FuseTable {
@@ -238,6 +242,7 @@ impl FuseTable {
238242
table_compression: table_compression.as_str().try_into()?,
239243
table_type,
240244
changes_desc: None,
245+
attached_table_location: Default::default(),
241246
}))
242247
}
243248

@@ -368,15 +373,25 @@ impl FuseTable {
368373
let options = self.table_info.options();
369374

370375
if let Some(storage_prefix) = options.get(OPT_KEY_STORAGE_PREFIX) {
371-
// if table is attached, parse snapshot location from hint file
372-
let hint = format!("{}/{}", storage_prefix, FUSE_TBL_LAST_SNAPSHOT_HINT);
373-
let snapshot_loc = {
374-
let hint_content = self.operator.read(&hint).await?.to_vec();
375-
let snapshot_full_path = String::from_utf8(hint_content)?;
376-
let operator_info = self.operator.info();
377-
snapshot_full_path[operator_info.root().len()..].to_string()
378-
};
379-
Ok(Some(snapshot_loc))
376+
// If the table is attaching to someone else,
377+
// parse the snapshot location from the hint file.
378+
//
379+
// The snapshot location is allowed
380+
// to be fetched from the table level instance cache.
381+
let snapshot_location = self
382+
.attached_table_location
383+
.get_or_try_init(|| async {
384+
let hint =
385+
format!("{}/{}", storage_prefix, FUSE_TBL_LAST_SNAPSHOT_HINT);
386+
let hint_content = self.operator.read(&hint).await?.to_vec();
387+
let snapshot_full_path = String::from_utf8(hint_content)?;
388+
let operator_info = self.operator.info();
389+
Ok::<_, ErrorCode>(
390+
snapshot_full_path[operator_info.root().len()..].to_string(),
391+
)
392+
})
393+
.await?;
394+
Ok(Some(snapshot_location.to_owned()))
380395
} else {
381396
Ok(options
382397
.get(OPT_KEY_SNAPSHOT_LOCATION)

0 commit comments

Comments
 (0)