CTTY commented on code in PR #3474: URL: https://github.com/apache/iceberg-rust/pull/3474#discussion_r4365061883
########## crates/iceberg/src/transaction/simple_snapshot_producer.rs: ########## @@ -0,0 +1,619 @@ +// 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 append-shaped snapshot actions. + +use std::collections::{HashMap, HashSet}; + +use futures::TryStreamExt; +use futures::stream::FuturesUnordered; +use uuid::Uuid; + +use crate::error::{Result, invalid_data}; +use crate::spec::{ + DataContentType, DataFile, FormatVersion, ManifestContentType, ManifestEntry, ManifestFile, + ManifestStatus, Operation, +}; +use crate::table::Table; +use crate::transaction::ActionCommit; +use crate::transaction::snapshot_helpers::{ + SnapshotCommitInput, current_manifests, generate_snapshot_id, manifest_list_path, + manifest_path, new_manifest_writer, validate_partition_value, write_snapshot_commit, +}; +use crate::{Error, ErrorKind}; + +/// Retry-persistent state for an append-shaped action: added data files go +/// into new manifests, and the current snapshot's manifests are carried +/// forward untouched (no filtering, no merging). +/// +/// One producer lives for one logical execution of an action. It is driven +/// once per attempt via [`SimpleSnapshotProducer::apply`]: logical identity +/// (commit UUID, snapshot id) is fixed on first use, the manifests written for +/// the added files are reused across attempts while their semantic inputs are +/// unchanged, and every attempt writes a fresh manifest list under a distinct +/// path. +pub(crate) struct SimpleSnapshotProducer { + /// 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 this producer's manifests. Never reset, so reused manifests keep + /// their paths and new ones never collide with earlier attempts. + manifest_counter: u64, + /// 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): cleanup is not implemented yet; terminal cleanup consumes + // this set. + #[allow(dead_code)] + owned_artifacts: HashSet<String>, +} + +/// The manifests written for one append's added files. +/// +/// Reusable while the table's format version is unchanged. Per spec, sequence +/// number inheritance does not apply to v1 manifests, so one written before a +/// concurrent upgrade cannot carry the v2 sequence number a retry assigns and +/// must be rewritten. The schema is not a guard: the manifest's embedded schema +/// describes the files it was written for, which do not change. +struct AddedManifests { + /// One manifest today. Several once added files are grouped by partition + /// spec (#3500) or rolled over at a target size (#3499). + manifests: Vec<ManifestFile>, + format_version: FormatVersion, +} + +impl SimpleSnapshotProducer { + /// Creates a producer. `commit_uuid` overrides the generated commit UUID. + pub(crate) fn new(commit_uuid: Option<Uuid>) -> Self { + Self { + commit_uuid: commit_uuid.unwrap_or_else(Uuid::now_v7), + snapshot_id: None, + attempt: 0, + manifest_counter: 0, + added_manifests: None, + owned_artifacts: HashSet::new(), + } + } + + /// Checks that every file is a data file written under the table's default + /// partition spec with a compatible partition value. + pub(crate) fn validate_added_data_files( + &self, + table: &Table, + data_files: &[DataFile], + ) -> Result<()> { + let metadata = table.metadata(); + for data_file in data_files { + if data_file.content_type() != DataContentType::Data { Review Comment: Currently I'm copying the existing logic. How about revisiting this in #3500? These logic can be refactored and optimized at one go -- 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]
