Skip to content

Commit

Permalink
chore: add snapshot location cache for attached table (#16795)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dantengsky authored Nov 8, 2024
1 parent 7e42582 commit 21f3bab
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/query/storages/fuse/src/fuse_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::sync::Arc;

use chrono::Duration;
use chrono::TimeDelta;
use databend_common_base::base::tokio;
use databend_common_catalog::catalog::StorageDescription;
use databend_common_catalog::plan::DataSourcePlan;
use databend_common_catalog::plan::PartStatistics;
Expand Down Expand Up @@ -130,8 +131,11 @@ pub struct FuseTable {

table_type: FuseTableType,

// If this is set, reading from fuse_table should only returns the increment blocks
// If this is set, reading from fuse_table should only return the increment blocks
pub(crate) changes_desc: Option<ChangesDesc>,

// A table instance level cache of snapshot_location, if this table is attaching to someone else.
attached_table_location: tokio::sync::OnceCell<String>,
}

impl FuseTable {
Expand Down Expand Up @@ -238,6 +242,7 @@ impl FuseTable {
table_compression: table_compression.as_str().try_into()?,
table_type,
changes_desc: None,
attached_table_location: Default::default(),
}))
}

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

if let Some(storage_prefix) = options.get(OPT_KEY_STORAGE_PREFIX) {
// if table is attached, parse snapshot location from hint file
let hint = format!("{}/{}", storage_prefix, FUSE_TBL_LAST_SNAPSHOT_HINT);
let snapshot_loc = {
let hint_content = self.operator.read(&hint).await?.to_vec();
let snapshot_full_path = String::from_utf8(hint_content)?;
let operator_info = self.operator.info();
snapshot_full_path[operator_info.root().len()..].to_string()
};
Ok(Some(snapshot_loc))
// If the table is attaching to someone else,
// parse the snapshot location from the hint file.
//
// The snapshot location is allowed
// to be fetched from the table level instance cache.
let snapshot_location = self
.attached_table_location
.get_or_try_init(|| async {
let hint =
format!("{}/{}", storage_prefix, FUSE_TBL_LAST_SNAPSHOT_HINT);
let hint_content = self.operator.read(&hint).await?.to_vec();
let snapshot_full_path = String::from_utf8(hint_content)?;
let operator_info = self.operator.info();
Ok::<_, ErrorCode>(
snapshot_full_path[operator_info.root().len()..].to_string(),
)
})
.await?;
Ok(Some(snapshot_location.to_owned()))
} else {
Ok(options
.get(OPT_KEY_SNAPSHOT_LOCATION)
Expand Down

0 comments on commit 21f3bab

Please sign in to comment.