|
23 | 23 | #include "iceberg/expression/term.h" |
24 | 24 | #include "iceberg/sort_order.h" |
25 | 25 | #include "iceberg/test/matchers.h" |
| 26 | +#include "iceberg/test/mock_catalog.h" |
26 | 27 | #include "iceberg/test/update_test_base.h" |
27 | 28 | #include "iceberg/transform.h" |
| 29 | +#include "iceberg/type.h" |
28 | 30 | #include "iceberg/update/update_properties.h" |
| 31 | +#include "iceberg/update/update_schema.h" |
29 | 32 | #include "iceberg/update/update_sort_order.h" |
30 | 33 |
|
31 | 34 | namespace iceberg { |
@@ -94,4 +97,147 @@ TEST_F(TransactionTest, MultipleUpdatesInTransaction) { |
94 | 97 | EXPECT_EQ(*sort_order, *expected_sort_order); |
95 | 98 | } |
96 | 99 |
|
| 100 | +class TransactionRetryTest : public UpdateTestBase { |
| 101 | + protected: |
| 102 | + void SetUp() override { |
| 103 | + UpdateTestBase::SetUp(); |
| 104 | + |
| 105 | + // Create a MockCatalog and wire it to the existing table |
| 106 | + mock_catalog_ = std::make_shared<::testing::NiceMock<MockCatalog>>(); |
| 107 | + |
| 108 | + ON_CALL(*mock_catalog_, LoadTable(::testing::_)) |
| 109 | + .WillByDefault([this](const TableIdentifier&) -> Result<std::shared_ptr<Table>> { |
| 110 | + return Table::Make(table_->name(), table_->metadata(), |
| 111 | + std::string(table_->metadata_file_location()), table_->io(), |
| 112 | + mock_catalog_); |
| 113 | + }); |
| 114 | + |
| 115 | + // Create a table instance bound to the mock catalog |
| 116 | + auto result = Table::Make(table_->name(), table_->metadata(), |
| 117 | + std::string(table_->metadata_file_location()), table_->io(), |
| 118 | + mock_catalog_); |
| 119 | + ASSERT_THAT(result, IsOk()); |
| 120 | + mock_table_ = std::move(result.value()); |
| 121 | + } |
| 122 | + |
| 123 | + std::shared_ptr<::testing::NiceMock<MockCatalog>> mock_catalog_; |
| 124 | + std::shared_ptr<Table> mock_table_; |
| 125 | +}; |
| 126 | + |
| 127 | +TEST_F(TransactionRetryTest, CommitRetrySucceedsAfterConflict) { |
| 128 | + int update_call_count = 0; |
| 129 | + ON_CALL(*mock_catalog_, UpdateTable(::testing::_, ::testing::_, ::testing::_)) |
| 130 | + .WillByDefault([this, &update_call_count]( |
| 131 | + const TableIdentifier&, |
| 132 | + const std::vector<std::unique_ptr<TableRequirement>>&, |
| 133 | + const std::vector<std::unique_ptr<TableUpdate>>&) |
| 134 | + -> Result<std::shared_ptr<Table>> { |
| 135 | + ++update_call_count; |
| 136 | + if (update_call_count == 1) { |
| 137 | + return CommitFailed("conflict on first attempt"); |
| 138 | + } |
| 139 | + return Table::Make(mock_table_->name(), mock_table_->metadata(), |
| 140 | + std::string(mock_table_->metadata_file_location()), |
| 141 | + mock_table_->io(), mock_catalog_); |
| 142 | + }); |
| 143 | + |
| 144 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, mock_table_->NewTransaction()); |
| 145 | + ICEBERG_UNWRAP_OR_FAIL(auto update, txn->NewUpdateProperties()); |
| 146 | + update->Set("retry.test", "value"); |
| 147 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 148 | + |
| 149 | + auto result = txn->Commit(); |
| 150 | + EXPECT_THAT(result, IsOk()); |
| 151 | + EXPECT_EQ(update_call_count, 2); |
| 152 | +} |
| 153 | + |
| 154 | +TEST_F(TransactionRetryTest, CommitRetryExhausted) { |
| 155 | + int update_call_count = 0; |
| 156 | + ON_CALL(*mock_catalog_, UpdateTable(::testing::_, ::testing::_, ::testing::_)) |
| 157 | + .WillByDefault( |
| 158 | + [&update_call_count](const TableIdentifier&, |
| 159 | + const std::vector<std::unique_ptr<TableRequirement>>&, |
| 160 | + const std::vector<std::unique_ptr<TableUpdate>>&) |
| 161 | + -> Result<std::shared_ptr<Table>> { |
| 162 | + ++update_call_count; |
| 163 | + return CommitFailed("always conflicts"); |
| 164 | + }); |
| 165 | + |
| 166 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, mock_table_->NewTransaction()); |
| 167 | + ICEBERG_UNWRAP_OR_FAIL(auto update, txn->NewUpdateProperties()); |
| 168 | + update->Set("retry.test", "value"); |
| 169 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 170 | + |
| 171 | + auto result = txn->Commit(); |
| 172 | + EXPECT_THAT(result, IsError(ErrorKind::kCommitFailed)); |
| 173 | + EXPECT_EQ(update_call_count, 5); |
| 174 | +} |
| 175 | + |
| 176 | +TEST_F(TransactionRetryTest, CommitNonRetryableErrorStopsImmediately) { |
| 177 | + int update_call_count = 0; |
| 178 | + ON_CALL(*mock_catalog_, UpdateTable(::testing::_, ::testing::_, ::testing::_)) |
| 179 | + .WillByDefault( |
| 180 | + [&update_call_count](const TableIdentifier&, |
| 181 | + const std::vector<std::unique_ptr<TableRequirement>>&, |
| 182 | + const std::vector<std::unique_ptr<TableUpdate>>&) |
| 183 | + -> Result<std::shared_ptr<Table>> { |
| 184 | + ++update_call_count; |
| 185 | + return CommitStateUnknown("unknown state"); |
| 186 | + }); |
| 187 | + |
| 188 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, mock_table_->NewTransaction()); |
| 189 | + ICEBERG_UNWRAP_OR_FAIL(auto update, txn->NewUpdateProperties()); |
| 190 | + update->Set("retry.test", "value"); |
| 191 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 192 | + |
| 193 | + auto result = txn->Commit(); |
| 194 | + EXPECT_THAT(result, IsError(ErrorKind::kCommitStateUnknown)); |
| 195 | + EXPECT_EQ(update_call_count, 1); // Should not retry |
| 196 | +} |
| 197 | + |
| 198 | +TEST_F(TransactionRetryTest, CreateTransactionDoesNotRetry) { |
| 199 | + int update_call_count = 0; |
| 200 | + ON_CALL(*mock_catalog_, UpdateTable(::testing::_, ::testing::_, ::testing::_)) |
| 201 | + .WillByDefault( |
| 202 | + [&update_call_count](const TableIdentifier&, |
| 203 | + const std::vector<std::unique_ptr<TableRequirement>>&, |
| 204 | + const std::vector<std::unique_ptr<TableUpdate>>&) |
| 205 | + -> Result<std::shared_ptr<Table>> { |
| 206 | + ++update_call_count; |
| 207 | + return CommitFailed("conflict"); |
| 208 | + }); |
| 209 | + |
| 210 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, |
| 211 | + Transaction::Make(mock_table_, TransactionKind::kCreate)); |
| 212 | + ICEBERG_UNWRAP_OR_FAIL(auto update, txn->NewUpdateProperties()); |
| 213 | + update->Set("create.test", "value"); |
| 214 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 215 | + |
| 216 | + auto result = txn->Commit(); |
| 217 | + EXPECT_THAT(result, IsError(ErrorKind::kCommitFailed)); |
| 218 | + EXPECT_EQ(update_call_count, 1); // No retry for kCreate |
| 219 | +} |
| 220 | + |
| 221 | +TEST_F(TransactionRetryTest, NonRetryableUpdatePreventsRetry) { |
| 222 | + int update_call_count = 0; |
| 223 | + ON_CALL(*mock_catalog_, UpdateTable(::testing::_, ::testing::_, ::testing::_)) |
| 224 | + .WillByDefault( |
| 225 | + [&update_call_count](const TableIdentifier&, |
| 226 | + const std::vector<std::unique_ptr<TableRequirement>>&, |
| 227 | + const std::vector<std::unique_ptr<TableUpdate>>&) |
| 228 | + -> Result<std::shared_ptr<Table>> { |
| 229 | + ++update_call_count; |
| 230 | + return CommitFailed("conflict"); |
| 231 | + }); |
| 232 | + |
| 233 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, mock_table_->NewTransaction()); |
| 234 | + ICEBERG_UNWRAP_OR_FAIL(auto schema_update, txn->NewUpdateSchema()); |
| 235 | + schema_update->AddColumn("new_col", int64()); |
| 236 | + EXPECT_THAT(schema_update->Commit(), IsOk()); |
| 237 | + |
| 238 | + auto result = txn->Commit(); |
| 239 | + EXPECT_THAT(result, IsError(ErrorKind::kCommitFailed)); |
| 240 | + EXPECT_EQ(update_call_count, 1); |
| 241 | +} |
| 242 | + |
97 | 243 | } // namespace iceberg |
0 commit comments