laskoviymishka commented on code in PR #3564: URL: https://github.com/apache/iceberg-rust/pull/3564#discussion_r4777074624
########## crates/iceberg/src/transaction/snapshot/merging.rs: ########## @@ -0,0 +1,1417 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Persistent producer for snapshot actions that add and remove data and +//! delete files. + +use std::collections::{HashMap, HashSet}; + +use typed_builder::TypedBuilder; +use uuid::Uuid; + +use super::manifest_filter::ManifestFilterManager; +use super::{ + AddedManifests, SnapshotCommitInput, current_manifests, generate_snapshot_id, + manifest_list_path, manifest_path, validate_partition_value, write_added_manifest, + write_snapshot_commit, +}; +use crate::error::{Result, invalid_data}; +use crate::spec::{ + DataContentType, DataFile, DataFileFormat, FormatVersion, ManifestContentType, ManifestFile, + Operation, +}; +use crate::table::Table; +use crate::transaction::ActionCommit; + +/// What a merging action asks the producer to do. +/// +/// Fixed for one logical execution; the producer derives everything else from +/// it and the table of the current attempt. The action validates the +/// table-independent parts, such as content types, before each attempt. A path +/// may be both removed and added: the producer marks the old entry `Deleted` +/// and writes a new `Added` one, so whether to allow it is the action's call. +#[derive(TypedBuilder)] +pub(crate) struct MergingIntent { + /// Overrides the generated commit UUID. + #[builder(default, setter(strip_option))] + commit_uuid: Option<Uuid>, + operation: Operation, + #[builder(default)] + snapshot_properties: HashMap<String, String>, + #[builder(default)] + added_data_files: Vec<DataFile>, + #[builder(default)] + added_delete_files: Vec<DataFile>, + #[builder(default)] + removed_data_files: Vec<DataFile>, + #[builder(default)] + removed_delete_files: Vec<DataFile>, + /// Whether a removed file that is no longer live fails the commit. + /// + /// Off by default: a retry after a concurrent commit removed the same + /// file still commits, which is harmless for actions such as a + /// merge-on-read delete replacing a position delete file. Actions that + /// must not double-remove (a rewrite whose replacement files would + /// otherwise duplicate rows) turn it on. + #[builder(default)] + fail_missing_delete_paths: bool, + /// Whether the summary resets table totals (a full-table overwrite). + #[builder(default)] + truncate_full_table: bool, +} + +impl MergingIntent { + pub(crate) fn commit_uuid(&self) -> Option<Uuid> { + self.commit_uuid + } + + pub(crate) fn operation(&self) -> &Operation { + &self.operation + } + + pub(crate) fn snapshot_properties(&self) -> &HashMap<String, String> { + &self.snapshot_properties + } + + pub(crate) fn added_data_files(&self) -> &[DataFile] { + &self.added_data_files + } + + pub(crate) fn added_delete_files(&self) -> &[DataFile] { + &self.added_delete_files + } + + pub(crate) fn removed_data_files(&self) -> &[DataFile] { + &self.removed_data_files + } + + pub(crate) fn removed_delete_files(&self) -> &[DataFile] { + &self.removed_delete_files + } + + pub(crate) fn fail_missing_delete_paths(&self) -> bool { + self.fail_missing_delete_paths + } + + pub(crate) fn truncate_full_table(&self) -> bool { + self.truncate_full_table + } +} + +/// Retry-persistent state for an action that adds and removes data and delete +/// files. +/// +/// One producer lives for one logical execution of an action and is driven once +/// per attempt via [`MergingSnapshotProducer::apply`]. Per attempt it takes the +/// current snapshot's manifests, rewrites those that reference a removed file, +/// appends manifests for the added files, and writes a manifest list. Identity +/// (commit UUID, snapshot id) is fixed on first use; rewritten and added +/// manifests are reused across attempts while their inputs are unchanged. +pub(crate) struct MergingSnapshotProducer { + intent: MergingIntent, + /// Fixed when the producer is created, so every attempt shares one UUID. + commit_uuid: Uuid, + /// Resolved against the table on the first attempt, then stable. + snapshot_id: Option<i64>, + /// Incremented per attempt; distinguishes manifest-list paths. + attempt: u64, + /// Numbers every manifest this producer writes, including rewrites. Never + /// reset, so paths never collide across attempts. + manifest_counter: u64, + data_filter: ManifestFilterManager, + delete_filter: ManifestFilterManager, + /// Manifests written for the added files, reused across attempts. + added_manifests: Option<AddedManifests>, + /// Metadata files written by this producer (manifests and every attempt's + /// manifest list). + // TODO(#3417): consumed by terminal cleanup once it is wired up. + owned_artifacts: HashSet<String>, +} + +impl MergingSnapshotProducer { + pub(crate) fn new(intent: MergingIntent) -> Self { + Self { + commit_uuid: intent.commit_uuid().unwrap_or_else(Uuid::now_v7), + data_filter: ManifestFilterManager::builder() + .content(ManifestContentType::Data) + .files_to_remove(intent.removed_data_files()) + .fail_missing_delete_paths(intent.fail_missing_delete_paths()) + .build(), + delete_filter: ManifestFilterManager::builder() + .content(ManifestContentType::Deletes) + .files_to_remove(intent.removed_delete_files()) Review Comment: Following up on my earlier ask for a delete-file removal test: could we add one that removes a Parquet position or equality delete as well? The only test that sets `removed_delete_files` removes a Puffin DV ([merging.rs](https://github.com/apache/iceberg-rust/blob/3e9d279db899d6b84e9286ff9feac79d2f607c51/crates/iceberg/src/transaction/snapshot/merging.rs#L1151-L1175)), so the `RemovedFileIdentity::File` path for delete files never runs. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
