diff --git a/src/mito2/src/engine.rs b/src/mito2/src/engine.rs index 991de9282f0..cc9e4268ecf 100644 --- a/src/mito2/src/engine.rs +++ b/src/mito2/src/engine.rs @@ -574,7 +574,7 @@ impl MitoEngine { /// Other region editing intention will result in an "invalid request" error. /// Also note that if a region is to be edited directly, we MUST not write data to it thereafter. pub async fn edit_region(&self, region_id: RegionId, edit: RegionEdit) -> Result<()> { - self.edit_region_with_kind(region_id, edit, RegionEditKind::Import) + self.edit_region_with_kind(region_id, edit, RegionEditKind::Import, true) .await } @@ -588,8 +588,13 @@ impl MitoEngine { reason: "SST ingestion cannot remove files" } ); - self.edit_region_with_kind(region_id, edit, RegionEditKind::Ingest) - .await + self.edit_region_with_kind( + region_id, + edit, + RegionEditKind::Ingest, + *INGEST_PRELOAD_SST_CACHE, + ) + .await } async fn edit_region_with_kind( @@ -597,6 +602,7 @@ impl MitoEngine { region_id: RegionId, edit: RegionEdit, kind: RegionEditKind, + preload_sst_cache: bool, ) -> Result<()> { let _timer = HANDLE_REQUEST_ELAPSED .with_label_values(&["edit_region"]) @@ -611,10 +617,6 @@ impl MitoEngine { ); let (tx, rx) = oneshot::channel(); - // Only external ingestion uses this switch. Imports retain their existing - // policy; staging publication already explicitly disables preloading. - let preload_sst_cache = - !matches!(kind, RegionEditKind::Ingest) || *INGEST_PRELOAD_SST_CACHE; let mut request = RegionEditRequest::new(region_id, edit, preload_sst_cache, tx); request.kind = kind; let request = WorkerRequest::EditRegion(request); diff --git a/src/mito2/src/engine/edit_region_test.rs b/src/mito2/src/engine/edit_region_test.rs index a6bce903777..8a8a9633a16 100644 --- a/src/mito2/src/engine/edit_region_test.rs +++ b/src/mito2/src/engine/edit_region_test.rs @@ -314,6 +314,25 @@ async fn test_edit_region_fill_cache( #[values(false, true)] flat_format: bool, #[case] index_file_size: Option, #[values(false, true)] ingestion: bool, +) { + check_edit_region_fill_cache(flat_format, index_file_size, ingestion, false).await; +} + +#[cfg(feature = "enterprise")] +#[rstest::rstest] +#[case::without_index(None)] +#[case::known_index_size(Some(7))] +#[case::unknown_index_size(Some(0))] +#[tokio::test] +async fn test_shared_ingestion_fill_cache(#[case] index_file_size: Option) { + check_edit_region_fill_cache(true, index_file_size, false, true).await; +} + +async fn check_edit_region_fill_cache( + flat_format: bool, + index_file_size: Option, + ingestion: bool, + shared: bool, ) { let mut env = TestEnv::new().await; @@ -369,9 +388,13 @@ async fn test_edit_region_fill_cache( let mut meta = FileMeta { region_id, file_id, + file_size: 1, index_version: 3, ..Default::default() }; + if shared { + meta.level = crate::sst::file::LEVEL_SHARED; + } if let Some(size) = index_file_size { meta.index_file_size = size; meta.indexes.push(ColumnIndexMetadata { @@ -401,13 +424,30 @@ async fn test_edit_region_fill_cache( committed_sequence: None, ..Default::default() }; - if ingestion { + if shared { + #[cfg(feature = "enterprise")] + { + engine + .ingest_shared_ssts(region.region_id, edit) + .await + .unwrap(); + assert_eq!( + engine + .get_region_statistic(region_id) + .unwrap() + .ingested_parquet_bytes, + 0 + ); + } + #[cfg(not(feature = "enterprise"))] + unreachable!("Shared ingestion requires enterprise"); + } else if ingestion { engine.ingest_ssts(region.region_id, edit).await.unwrap(); } else { engine.edit_region(region.region_id, edit).await.unwrap(); } - if ingestion && !*super::INGEST_PRELOAD_SST_CACHE { + if (ingestion || shared) && !*super::INGEST_PRELOAD_SST_CACHE { // Publication completes even though neither artifact is warmed. assert!( region @@ -421,6 +461,13 @@ async fn test_edit_region_fill_cache( let file_cache = cache_manager.write_cache().unwrap().file_cache(); assert!(!file_cache.contains_key(&IndexKey::new(region_id, file_id, FileType::Parquet))); assert!(!file_cache.contains_key(&IndexKey::new(region_id, file_id, FileType::Puffin(3)))); + assert!( + tokio::time::timeout(Duration::from_millis(200), rx) + .await + .is_err() + ); + assert!(!file_cache.contains_key(&IndexKey::new(region_id, file_id, FileType::Parquet))); + assert!(!file_cache.contains_key(&IndexKey::new(region_id, file_id, FileType::Puffin(3)))); return; } diff --git a/src/mito2/src/engine/table_compaction.rs b/src/mito2/src/engine/table_compaction.rs index c68d9b8dd87..8ee8fed7f80 100644 --- a/src/mito2/src/engine/table_compaction.rs +++ b/src/mito2/src/engine/table_compaction.rs @@ -30,7 +30,7 @@ use crate::manifest::action::RegionEdit; use crate::region::options::RegionOptions; use crate::region::{MitoRegionRef, RegionLeaderState, RegionRoleState}; use crate::request::{RegionEditKind, RegionEditRequest, WorkerRequest}; -use crate::sst::file::{FileMeta, SHARED_LEVEL_SLOT}; +use crate::sst::file::{FileMeta, LEVEL_SHARED, SHARED_LEVEL_SLOT}; /// A query-visible input snapshot. Callers may select a subset of `inputs` for a job. #[derive(Debug, Clone, Serialize, Deserialize)] @@ -70,6 +70,30 @@ pub fn is_table_compaction_metadata_compatible( } impl MitoEngine { + /// Installs shared ingestion references using the ingestion cache policy. + /// Keep import accounting: the same SST is referenced by multiple targets + /// and must not be counted as fresh writes at every target. + pub async fn ingest_shared_ssts(&self, region_id: RegionId, edit: RegionEdit) -> Result<()> { + ensure!( + edit.files_to_remove.is_empty() + && edit + .files_to_add + .iter() + .all(|file| file.level == LEVEL_SHARED), + InvalidRequestSnafu { + region_id, + reason: "Shared ingestion requires shared files and cannot remove files" + } + ); + self.edit_region_with_kind( + region_id, + edit, + RegionEditKind::Import, + *super::INGEST_PRELOAD_SST_CACHE, + ) + .await + } + /// Reads committed inputs while an ordinary edit or ALTER is in progress. pub fn table_compaction_snapshot( &self,