diff --git a/rust/lance-table/src/feature_flags.rs b/rust/lance-table/src/feature_flags.rs index 85645bbcf1c..0382bff66c6 100644 --- a/rust/lance-table/src/feature_flags.rs +++ b/rust/lance-table/src/feature_flags.rs @@ -265,6 +265,8 @@ impl CoveringContract { /// overlay files before the feature is generally released. pub const ENABLE_UNSTABLE_DATA_OVERLAY_FILES_ENV: &str = "LANCE_ENABLE_UNSTABLE_DATA_OVERLAY_FILES"; +pub(crate) const AUTO_CLEANUP_INTERVAL_KEY: &str = "lance.auto_cleanup.interval"; + /// Validate a change to automatic cleanup configuration. /// /// Retention rules use `lance.auto_cleanup.policy_interval`; the legacy interval @@ -293,7 +295,7 @@ pub fn validate_auto_cleanup_config(manifest: &Manifest) -> Result<()> { .config .contains_key("lance.auto_cleanup.policy_interval"); if (has_retention_config || has_policy_interval) - && manifest.config.contains_key("lance.auto_cleanup.interval") + && manifest.config.contains_key(AUTO_CLEANUP_INTERVAL_KEY) { return Err(Error::invalid_input( "retention rules require lance.auto_cleanup.policy_interval; remove lance.auto_cleanup.interval and lance.auto_cleanup.older_than before activating retention cleanup", @@ -331,6 +333,22 @@ pub fn validate_auto_cleanup_config(manifest: &Manifest) -> Result<()> { Ok(()) } +/// Validate the final legacy interval when a transaction writes the key. +/// Inherited values may be malformed and must remain repairable. +pub(crate) fn validate_written_auto_cleanup_interval( + manifest: &Manifest, + written: bool, +) -> Result<()> { + if written && let Some(value) = manifest.config.get(AUTO_CLEANUP_INTERVAL_KEY) { + value.parse::().map_err(|_| { + Error::invalid_input(format!( + "{AUTO_CLEANUP_INTERVAL_KEY} must be an unsigned 64-bit integer, got {value:?}. Set a valid interval or remove the key to disable legacy automatic cleanup." + )) + })?; + } + Ok(()) +} + /// Validate a change to [`ZONE_WRITE_SEEDS_VERSION_KEY`]. /// /// Accepts absent, `"0"`, or `"1"`. Call before publishing a commit that diff --git a/rust/lance-table/src/transaction/action/apply.rs b/rust/lance-table/src/transaction/action/apply.rs index 98651d1536f..b7676b5ad77 100644 --- a/rust/lance-table/src/transaction/action/apply.rs +++ b/rust/lance-table/src/transaction/action/apply.rs @@ -27,6 +27,7 @@ use super::{CompositeOperation, FieldId, FieldRef, Ref}; use crate::feature_flags::{ FLAG_MANAGED_BLOBS, apply_index_object_metadata_flag, apply_zone_write_seeds_version_config, inherit_sticky_feature_flags, managed_blobs_enabled, validate_auto_cleanup_config, + validate_written_auto_cleanup_interval, }; use crate::format::overlay::staleness::field_affects_index; use crate::format::{ @@ -194,6 +195,7 @@ pub(super) struct ApplyState<'a> { /// to be written back over the assembled manifest. config: HashMap, table_metadata: HashMap, + auto_cleanup_interval_written: bool, /// Whether an action in this operation wrote or deleted /// [`ZONE_WRITE_SEEDS_VERSION_KEY`](crate::feature_flags::ZONE_WRITE_SEEDS_VERSION_KEY). /// An inherited value, even `"1"`, is not an opt-in. @@ -310,6 +312,7 @@ impl<'a> ApplyState<'a> { existing_base_paths: manifest.base_paths.clone(), config: manifest.config.clone(), table_metadata: manifest.table_metadata.clone(), + auto_cleanup_interval_written: false, zone_write_seeds_version_written: false, fragment_high_water: manifest.max_fragment_id(), field_high_water: manifest.max_field_id(), @@ -367,6 +370,7 @@ impl<'a> ApplyState<'a> { field_high_water, config: dataset_config, table_metadata, + auto_cleanup_interval_written, zone_write_seeds_version_written, updates_field_metadata, .. @@ -448,6 +452,7 @@ impl<'a> ApplyState<'a> { { validate_auto_cleanup_config(&manifest)?; } + validate_written_auto_cleanup_interval(&manifest, auto_cleanup_interval_written)?; // Both read the final schema and config, so they come after those land. apply_range_segments(&mut manifest, Some(current_manifest))?; if managed_blobs_enabled(&manifest.schema)? { @@ -661,6 +666,10 @@ impl<'a> ApplyState<'a> { &mut self.config } + pub(super) fn mark_auto_cleanup_interval_written(&mut self) { + self.auto_cleanup_interval_written = true; + } + /// Record that this operation itself writes or deletes the zone write seed /// version key, as opposed to inheriting it from the current manifest. pub(super) fn mark_zone_write_seeds_version_written(&mut self) { diff --git a/rust/lance-table/src/transaction/action/config_update.rs b/rust/lance-table/src/transaction/action/config_update.rs index 703dd890b16..62ab1179cc4 100644 --- a/rust/lance-table/src/transaction/action/config_update.rs +++ b/rust/lance-table/src/transaction/action/config_update.rs @@ -7,7 +7,7 @@ use super::apply::ApplyState; use super::footprint::UnenforcedKey; use super::proto::required; use super::{ConfigMap, Coordinate, FieldRef, Footprint}; -use crate::feature_flags::ZONE_WRITE_SEEDS_VERSION_KEY; +use crate::feature_flags::{AUTO_CLEANUP_INTERVAL_KEY, ZONE_WRITE_SEEDS_VERSION_KEY}; use crate::format::pb_actions; use crate::transaction::UpdateMap; use crate::transaction::update_map::apply_update_map; @@ -49,6 +49,13 @@ pub struct FieldMetadataUpdate { impl ConfigUpdate { pub(super) fn apply(&self, state: &mut ApplyState) -> Result<()> { if let Some(updates) = &self.config { + if updates + .update_entries + .iter() + .any(|entry| entry.key == AUTO_CLEANUP_INTERVAL_KEY) + { + state.mark_auto_cleanup_interval_written(); + } if updates.replace || updates .update_entries @@ -621,6 +628,29 @@ mod tests { apply(&manifest, vec![set_config(&[("unrelated", Some("1"))])]).unwrap(); } + #[test] + fn test_config_update_validates_explicit_auto_cleanup_interval() { + let mut manifest = backed_manifest(); + manifest + .config + .insert(AUTO_CLEANUP_INTERVAL_KEY.into(), "invalid".into()); + + let set_invalid = set_config(&[(AUTO_CLEANUP_INTERVAL_KEY, Some("invalid"))]); + let error = apply(&manifest, vec![set_invalid.clone()]).unwrap_err(); + assert!(matches!(error, Error::InvalidInput { .. })); + assert!(error.to_string().contains(AUTO_CLEANUP_INTERVAL_KEY)); + + let updated = apply( + &manifest, + vec![ + set_invalid, + set_config(&[(AUTO_CLEANUP_INTERVAL_KEY, Some("10"))]), + ], + ) + .unwrap(); + assert_eq!(updated.config[AUTO_CLEANUP_INTERVAL_KEY], "10"); + } + #[test] fn test_config_update_validates_and_activates_zone_write_seeds_version() { let error = apply( diff --git a/rust/lance-table/src/transaction/manifest_build.rs b/rust/lance-table/src/transaction/manifest_build.rs index bba113b27d6..fe9c0ba50da 100644 --- a/rust/lance-table/src/transaction/manifest_build.rs +++ b/rust/lance-table/src/transaction/manifest_build.rs @@ -11,12 +11,13 @@ //! metadata it stamps, the validation that runs before it. use crate::feature_flags::{ - FLAG_COVERED_INDEX_METADATA, FLAG_FRAGMENT_REUSE_INDEX, FLAG_INDEPENDENT_COVERING_FIELDS, - FLAG_MANAGED_BLOBS, FLAG_NON_REUSABLE_FIELD_IDS, FLAG_STABLE_ROW_IDS, - ZONE_WRITE_SEEDS_VERSION_KEY, apply_feature_flags, apply_index_object_metadata_flag, - apply_row_addr_index_feature_flag, apply_zone_write_seeds_version_config, - ensure_can_read_manifest, ensure_can_write_manifest, inherit_sticky_feature_flags, - managed_blobs_enabled, validate_auto_cleanup_config, validate_non_reusable_field_id_flags, + AUTO_CLEANUP_INTERVAL_KEY, FLAG_COVERED_INDEX_METADATA, FLAG_FRAGMENT_REUSE_INDEX, + FLAG_INDEPENDENT_COVERING_FIELDS, FLAG_MANAGED_BLOBS, FLAG_NON_REUSABLE_FIELD_IDS, + FLAG_STABLE_ROW_IDS, ZONE_WRITE_SEEDS_VERSION_KEY, apply_feature_flags, + apply_index_object_metadata_flag, apply_row_addr_index_feature_flag, + apply_zone_write_seeds_version_config, ensure_can_read_manifest, ensure_can_write_manifest, + inherit_sticky_feature_flags, managed_blobs_enabled, validate_auto_cleanup_config, + validate_non_reusable_field_id_flags, validate_written_auto_cleanup_interval, }; use crate::format::clustering::{ChangedReferences, validate_clustering_of}; @@ -1940,11 +1941,13 @@ impl Transaction { // Refusing a field metadata update that changes how a field decodes // waits for the blob nesting check, which has the more specific error. let mut decoding_check = Ok(()); + let mut auto_cleanup_interval_written = false; match &self.operation { Operation::Overwrite { config_upsert_values: Some(tm), .. } => { + auto_cleanup_interval_written = tm.contains_key(AUTO_CLEANUP_INTERVAL_KEY); manifest.config_mut().extend(tm.clone()); } Operation::UpdateConfig { @@ -1985,6 +1988,10 @@ impl Transaction { } } if let Some(config_updates) = config_updates { + auto_cleanup_interval_written = config_updates + .update_entries + .iter() + .any(|entry| entry.key == AUTO_CLEANUP_INTERVAL_KEY); let mut config = manifest.config.clone(); apply_update_map(&mut config, config_updates); manifest.config = config; @@ -2128,6 +2135,7 @@ impl Transaction { if changes_auto_cleanup_config(&self.operation) { validate_auto_cleanup_config(&manifest)?; } + validate_written_auto_cleanup_interval(&manifest, auto_cleanup_interval_written)?; apply_range_segments(&mut manifest, current_manifest)?; @@ -4310,6 +4318,29 @@ mod tests { ); } + #[test] + fn overwrite_rejects_an_invalid_auto_cleanup_interval_upsert() { + let manifest = sample_manifest(); + let transaction = Transaction::new( + manifest.version, + Operation::Overwrite { + fragments: vec![Fragment::new(0)], + schema: manifest.schema.clone(), + config_upsert_values: Some(HashMap::from([( + AUTO_CLEANUP_INTERVAL_KEY.into(), + "invalid".into(), + )])), + initial_bases: None, + }, + None, + ); + let error = transaction + .build_manifest(Some(&manifest), vec![], "txn", &default_build_config()) + .unwrap_err(); + assert!(matches!(error, Error::InvalidInput { .. })); + assert!(error.to_string().contains(AUTO_CLEANUP_INTERVAL_KEY)); + } + #[test] fn zone_write_seeds_version_config_validates_and_activates_flag() { let manifest = sample_manifest(); diff --git a/rust/lance/src/io/commit/tests.rs b/rust/lance/src/io/commit/tests.rs index b8ded2a652e..c9a1e93708b 100644 --- a/rust/lance/src/io/commit/tests.rs +++ b/rust/lance/src/io/commit/tests.rs @@ -46,30 +46,113 @@ use crate::index::vector::VectorIndexParams; use crate::index::{DatasetIndexExt, DatasetIndexInternalExt}; use crate::utils::test::{DatagenExt, FragmentCount, FragmentRowCount}; +#[rstest::rstest] +#[case::merge(false, "not-a-number")] +#[case::replace(true, "-1")] #[tokio::test] -async fn test_auto_cleanup_failure_preserves_successful_commit() { +async fn test_invalid_auto_cleanup_interval_does_not_publish( + #[case] replace: bool, + #[case] value: &str, +) { + let key = "lance.auto_cleanup.interval"; + let dir = TempStrDir::default(); let mut dataset = gen_batch() .col("id", array::step::()) - .into_ram_dataset(FragmentCount::from(2), FragmentRowCount::from(2)) + .into_dataset(&dir, FragmentCount::from(2), FragmentRowCount::from(2)) .await .unwrap(); dataset - .update_config([("lance.auto_cleanup.interval", "invalid")]) + .update_config([(key, "100"), ("owner", "analytics")]) .await .unwrap(); + let version = dataset.version_id(); + let config = dataset.manifest.config.clone(); - dataset.checkout_latest().await.unwrap(); - assert_eq!(dataset.version().version, 2); - assert_eq!( - dataset.manifest.config["lance.auto_cleanup.interval"], - "invalid" + let update = dataset.update_config([(key, value), ("other", "unpublished")]); + let error = if replace { + update.replace().await.unwrap_err() + } else { + update.await.unwrap_err() + }; + assert!(matches!(error, Error::InvalidInput { .. })); + let message = error.to_string(); + assert!( + message.contains(key) && message.contains(value), + "{message}" ); + assert!(message.contains("unsigned 64-bit integer"), "{message}"); + assert_eq!(dataset.version_id(), version); + assert_eq!(dataset.manifest.config, config); + + let reopened = Dataset::open(&dir).await.unwrap(); + assert_eq!(reopened.version_id(), version); + assert_eq!(reopened.manifest.config, config); +} + +#[rstest::rstest] +#[case::set_valid(Some("+00010"))] +#[case::remove(None)] +#[tokio::test] +async fn test_inherited_invalid_auto_cleanup_interval_can_be_repaired( + #[case] repair: Option<&str>, +) { + let key = "lance.auto_cleanup.interval"; + let dir = TempStrDir::default(); + let dataset = gen_batch() + .col("id", array::step::()) + .into_dataset(&dir, FragmentCount::from(2), FragmentRowCount::from(2)) + .await + .unwrap(); + // Persist a configuration an older writer could publish. + let mut manifest = dataset.manifest().clone(); + manifest.config.insert(key.into(), "invalid".into()); + let store = dataset.object_store(None).await.unwrap(); + write_manifest_file_to_path( + &store, + &mut manifest, + None, + &dataset.manifest_location().path, + None, + ) + .await + .unwrap(); + + let mut dataset = Dataset::open(&dir).await.unwrap(); + assert_eq!(dataset.manifest.config[key], "invalid"); assert_eq!(dataset.count_rows(None).await.unwrap(), 4); + let error = dataset.update_config([(key, "invalid")]).await.unwrap_err(); + assert!(matches!(error, Error::InvalidInput { .. })); + assert_eq!(dataset.version_id(), 1); + + dataset + .update_config([("owner", "analytics")]) + .await + .unwrap(); + dataset + .append( + gen_batch() + .col("id", array::step::()) + .into_reader_rows(RowCount::from(2), BatchCount::from(1)), + None, + ) + .await + .unwrap(); + let mut dataset = Dataset::open(&dir).await.unwrap(); + assert_eq!(dataset.version_id(), 3); + assert_eq!(dataset.manifest.config[key], "invalid"); + assert_eq!(dataset.count_rows(None).await.unwrap(), 6); let error = auto_cleanup_hook(&dataset, &dataset.manifest) .await .unwrap_err(); assert!(matches!(error, Error::Cleanup { .. })); - assert!(error.to_string().contains("lance.auto_cleanup.interval")); + assert!(error.to_string().contains(key)); + + dataset.update_config([(key, repair)]).await.unwrap(); + let dataset = Dataset::open(&dir).await.unwrap(); + assert_eq!(dataset.version_id(), 4); + assert_eq!(dataset.manifest.config.get(key).map(String::as_str), repair); + assert_eq!(dataset.manifest.config["owner"], "analytics"); + assert_eq!(dataset.count_rows(None).await.unwrap(), 6); } #[rstest::rstest]