wgtmac commented on code in PR #576:
URL: https://github.com/apache/iceberg-cpp/pull/576#discussion_r2988918243
##########
src/iceberg/table.cc:
##########
@@ -87,6 +87,7 @@ Status Table::Refresh() {
ICEBERG_ASSIGN_OR_RAISE(auto refreshed_table,
catalog_->LoadTable(identifier_));
if (metadata_location_ != refreshed_table->metadata_file_location()) {
metadata_ = std::move(refreshed_table->metadata_);
+ metadata_location_ =
std::string(refreshed_table->metadata_file_location());
Review Comment:
This looks like a bug that can be fixed separately and merged quickly
##########
src/iceberg/transaction.cc:
##########
@@ -378,6 +389,43 @@ Result<std::shared_ptr<Table>> Transaction::Commit() {
return ctx_->table;
}
+Result<std::shared_ptr<Table>> Transaction::CommitOnce() {
+ auto refresh_result = ctx_->table->Refresh();
Review Comment:
It seems unnecessary to issue a `Refresh()` call on the first attempt since
it may prohibit fast commit. Can we change to only call Refresh during retry?
##########
src/iceberg/update/snapshot_update.cc:
##########
@@ -218,14 +218,27 @@ Result<std::vector<ManifestFile>>
SnapshotUpdate::WriteDeleteManifests(
}
int64_t SnapshotUpdate::SnapshotId() {
- if (!snapshot_id_.has_value()) {
+ while (!snapshot_id_.has_value() ||
+ base().SnapshotById(snapshot_id_.value()).has_value()) {
Review Comment:
Doesn't `SnapshotUtil::GenerateSnapshotId` below have the same loop?
##########
src/iceberg/util/retry_util.h:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <algorithm>
+#include <chrono>
+#include <functional>
Review Comment:
Unused?
##########
src/iceberg/test/retry_util_test.cc:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/util/retry_util.h"
+
+#include <gtest/gtest.h>
+
+#include "iceberg/result.h"
+#include "iceberg/test/matchers.h"
+
+namespace iceberg {
+
+// --------------------------------------------------------------------------
+// Test: Successful on first attempt — no retries
+// --------------------------------------------------------------------------
+TEST(RetryRunnerTest, SuccessOnFirstAttempt) {
Review Comment:
We only have test on RetryRunner. Can we add some integration test directly
using Table or Transaction?
##########
src/iceberg/transaction.cc:
##########
@@ -378,6 +389,43 @@ Result<std::shared_ptr<Table>> Transaction::Commit() {
return ctx_->table;
}
+Result<std::shared_ptr<Table>> Transaction::CommitOnce() {
+ auto refresh_result = ctx_->table->Refresh();
+ if (!refresh_result.has_value()) {
Review Comment:
Let's reuse macros like `ICEBERG_RETURN_UNEXPECTED` and
`ICEBERG_ASSIGN_OR_RAISE` to write less lines.
##########
src/iceberg/update/pending_update.h:
##########
@@ -58,6 +58,9 @@ class ICEBERG_EXPORT PendingUpdate : public ErrorCollector {
/// \brief Return the kind of this pending update.
virtual Kind kind() const = 0;
+ /// \brief Whether this update can be retried after a commit conflict.
+ virtual bool IsRetryable() const { return true; }
Review Comment:
I think the Java impl has other types of updates that disallow retry, e.g.
UpdateSchema. We might need to evaluate if we want to keep the same behavior.
##########
src/iceberg/transaction.cc:
##########
@@ -378,6 +389,43 @@ Result<std::shared_ptr<Table>> Transaction::Commit() {
return ctx_->table;
}
+Result<std::shared_ptr<Table>> Transaction::CommitOnce() {
+ auto refresh_result = ctx_->table->Refresh();
+ if (!refresh_result.has_value()) {
+ return std::unexpected(refresh_result.error());
+ }
+
+ if (ctx_->metadata_builder->base() != ctx_->table->metadata().get()) {
+ ctx_->metadata_builder =
+ TableMetadataBuilder::BuildFrom(ctx_->table->metadata().get());
+ for (const auto& update : pending_updates_) {
+ auto commit_status = update->Commit();
Review Comment:
Should we directly call `this->Apply(*update)` to avoid an indirection?
##########
src/iceberg/transaction.cc:
##########
@@ -346,23 +347,33 @@ Result<std::shared_ptr<Table>> Transaction::Commit() {
return ctx_->table;
}
- std::vector<std::unique_ptr<TableRequirement>> requirements;
- switch (ctx_->kind) {
- case TransactionKind::kCreate: {
- ICEBERG_ASSIGN_OR_RAISE(requirements,
TableRequirements::ForCreateTable(updates));
- } break;
- case TransactionKind::kUpdate: {
- ICEBERG_ASSIGN_OR_RAISE(
- requirements,
- TableRequirements::ForUpdateTable(*ctx_->metadata_builder->base(),
updates));
-
- } break;
+ Result<std::shared_ptr<Table>> commit_result;
+ if (!CanRetry()) {
Review Comment:
The non-retry path and CommitOnce() both build requirements and call
UpdateTable, but through different code paths. Consider unifying: even the
non-retry case could go through CommitOnce() with num_retries=0, which would
eliminate the branching and reduce maintenance burden.
##########
src/iceberg/util/retry_util.h:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <algorithm>
+#include <chrono>
+#include <functional>
+#include <random>
+#include <thread>
+#include <vector>
+
+#include "iceberg/result.h"
+
+namespace iceberg {
+
+/// \brief Configuration for retry behavior
+struct RetryConfig {
+ /// Maximum number of retry attempts (not including the first attempt)
+ int32_t num_retries = 4;
+ /// Minimum wait time between retries in milliseconds
+ int32_t min_wait_ms = 100;
+ /// Maximum wait time between retries in milliseconds
+ int32_t max_wait_ms = 60 * 1000; // 1 minute
+ /// Total maximum time for all retries in milliseconds
+ int32_t total_timeout_ms = 30 * 60 * 1000; // 30 minutes
+ /// Exponential backoff scale factor
+ double scale_factor = 2.0;
+};
+
+/// \brief Utility class for running tasks with retry logic
+class RetryRunner {
+ public:
+ /// \brief Construct a RetryRunner with the given configuration
+ explicit RetryRunner(RetryConfig config = {}) : config_(std::move(config)) {}
+
+ /// \brief Specify error types that should trigger a retry.
+ RetryRunner& OnlyRetryOn(std::initializer_list<ErrorKind> error_kinds) {
+ only_retry_on_ = std::vector<ErrorKind>(error_kinds);
+ return *this;
+ }
+
+ /// \brief Specify a single error type that should trigger a retry
+ RetryRunner& OnlyRetryOn(ErrorKind error_kind) {
+ only_retry_on_ = std::vector<ErrorKind>{error_kind};
+ return *this;
+ }
+
+ /// \brief Specify error types that should stop retries immediately
+ RetryRunner& StopRetryOn(std::initializer_list<ErrorKind> error_kinds) {
Review Comment:
It would be good to document the priority between OnlyRetryOn and
StopRetryOn.
--
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]