hubcio commented on code in PR #4740:
URL: https://github.com/apache/iggy/pull/4740#discussion_r4474442678


##########
core/connectors/sinks/iceberg_sink/src/router/commit.rs:
##########
@@ -0,0 +1,554 @@
+// 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.
+
+use crate::router::format_error_chain;
+use http::StatusCode;
+use iceberg::spec::DataFile;
+use iceberg::table::Table;
+use iceberg::transaction::{ApplyTransactionAction, Transaction};
+use iceberg::{Catalog, ErrorKind, TableIdent};
+use iggy_connector_sdk::Error;
+use iggy_connector_sdk::retry::{RetryPolicy, is_transient_status, retry_async};
+use std::collections::HashMap;
+use std::fmt;
+use tracing::{error, info};
+use uuid::Uuid;
+
+/// Snapshot summary property naming the append, so that a retry can tell
+/// whether the catalog applied an earlier attempt whose response was lost.
+pub(crate) const COMMIT_ID_PROPERTY: &str = "iggy.commit-id";
+
+/// Appends `data_files` to `table` in one snapshot and retries a failed commit
+/// with `retry`. Every retry first looks for the snapshot of an earlier 
attempt.
+pub(crate) async fn commit_data_files(
+    table: &Table,
+    catalog: &dyn Catalog,
+    data_files: Vec<DataFile>,
+    retry: RetryPolicy,
+) -> Result<(), Error> {
+    let commit_id = Uuid::new_v4().to_string();
+    let transaction = Transaction::new(table);
+    // The check reads every manifest of the table at once on each commit, and 
the data files
+    // carry fresh UUID names it could never match.
+    let action = transaction
+        .fast_append()
+        .with_check_duplicate(false)
+        .set_snapshot_properties(HashMap::from([(
+            COMMIT_ID_PROPERTY.to_owned(),
+            commit_id.clone(),
+        )]))
+        .add_data_files(data_files);
+    let transaction = action.apply(transaction).map_err(|err| {
+        let chain = format_error_chain(&err);
+        error!(
+            "Failed to apply transaction on table with UUID: {}, Error: {}",
+            table.metadata().uuid(),
+            chain
+        );
+        Error::TransactionApplyError(chain)
+    })?;
+
+    // Data files are kept on commit failure: the catalog may have applied the
+    // commit before the error surfaced, and deleting files referenced by a
+    // committed snapshot would corrupt the table.
+    let context = format!("Commit to Iceberg table {}", table.identifier());
+    let identifier = table.identifier();
+    let transaction = &transaction;
+    let commit_id = commit_id.as_str();
+    let mut attempted = false;
+    retry_async(retry, &context, is_transient, move || {

Review Comment:
   warning: these retries extend the synchronous sink callback's hold on a host 
runtime worker, so concurrent catalog failures can stall unrelated connectors. 
execute synchronous sink callbacks on a bounded blocking pool.



##########
core/connectors/sinks/iceberg_sink/src/router/commit.rs:
##########
@@ -0,0 +1,554 @@
+// 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.
+
+use crate::router::format_error_chain;
+use http::StatusCode;
+use iceberg::spec::DataFile;
+use iceberg::table::Table;
+use iceberg::transaction::{ApplyTransactionAction, Transaction};
+use iceberg::{Catalog, ErrorKind, TableIdent};
+use iggy_connector_sdk::Error;
+use iggy_connector_sdk::retry::{RetryPolicy, is_transient_status, retry_async};
+use std::collections::HashMap;
+use std::fmt;
+use tracing::{error, info};
+use uuid::Uuid;
+
+/// Snapshot summary property naming the append, so that a retry can tell
+/// whether the catalog applied an earlier attempt whose response was lost.
+pub(crate) const COMMIT_ID_PROPERTY: &str = "iggy.commit-id";
+
+/// Appends `data_files` to `table` in one snapshot and retries a failed commit
+/// with `retry`. Every retry first looks for the snapshot of an earlier 
attempt.
+pub(crate) async fn commit_data_files(
+    table: &Table,
+    catalog: &dyn Catalog,
+    data_files: Vec<DataFile>,
+    retry: RetryPolicy,
+) -> Result<(), Error> {
+    let commit_id = Uuid::new_v4().to_string();
+    let transaction = Transaction::new(table);
+    // The check reads every manifest of the table at once on each commit, and 
the data files
+    // carry fresh UUID names it could never match.
+    let action = transaction
+        .fast_append()
+        .with_check_duplicate(false)
+        .set_snapshot_properties(HashMap::from([(
+            COMMIT_ID_PROPERTY.to_owned(),
+            commit_id.clone(),
+        )]))
+        .add_data_files(data_files);
+    let transaction = action.apply(transaction).map_err(|err| {
+        let chain = format_error_chain(&err);
+        error!(
+            "Failed to apply transaction on table with UUID: {}, Error: {}",
+            table.metadata().uuid(),
+            chain
+        );
+        Error::TransactionApplyError(chain)
+    })?;
+
+    // Data files are kept on commit failure: the catalog may have applied the
+    // commit before the error surfaced, and deleting files referenced by a
+    // committed snapshot would corrupt the table.
+    let context = format!("Commit to Iceberg table {}", table.identifier());
+    let identifier = table.identifier();
+    let transaction = &transaction;
+    let commit_id = commit_id.as_str();
+    let mut attempted = false;
+    retry_async(retry, &context, is_transient, move || {
+        let retrying = std::mem::replace(&mut attempted, true);
+        commit_once(catalog, identifier, transaction, commit_id, retrying)
+    })
+    .await
+    .map_err(|failure| {
+        let chain = format_error_chain(&failure.error.error);
+        error!(
+            "Failed to commit transaction on table with UUID: {} after {} 
attempts, Error: {}",
+            table.metadata().uuid(),
+            failure.attempts,
+            chain
+        );
+        Error::CatalogCommitError(chain)
+    })
+}
+
+/// A failed catalog call, with the HTTP status of the catalog's answer when 
it gave one.
+#[derive(Debug)]
+struct CatalogFailure {

Review Comment:
   simplification: `CatalogFailure` caches a status that only `is_transient` 
consumes. pass `iceberg::Error` through `retry_async` and call 
`response_status` inside `is_transient`.



-- 
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]

Reply via email to