Skip to content

Commit 0ae7a0d

Browse files
committed
fix: NewSnapshotManager set auto_commit to false and restore true in SnapshotManager::Commit
1 parent ebb771a commit 0ae7a0d

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/iceberg/table.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ Result<std::shared_ptr<UpdatePartitionStatistics>> Table::NewUpdatePartitionStat
226226
Result<std::shared_ptr<SnapshotManager>> Table::NewSnapshotManager() {
227227
ICEBERG_ASSIGN_OR_RAISE(
228228
auto transaction, Transaction::Make(shared_from_this(), Transaction::Kind::kUpdate,
229-
/*auto_commit=*/true));
229+
/*auto_commit=*/false));
230230
return SnapshotManager::Make(std::move(transaction));
231231
}
232232

src/iceberg/test/snapshot_manager_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ TEST_F(SnapshotManagerTest, CreateReferencesAndRollback) {
479479
ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_));
480480
ICEBERG_UNWRAP_OR_FAIL(auto manager2, reloaded->NewSnapshotManager());
481481
manager2->RollbackTo(oldest_snapshot_id_);
482+
EXPECT_THAT(manager2->Commit(), IsOk());
482483
}
483484

484485
{
@@ -501,7 +502,6 @@ TEST_F(SnapshotManagerTest, SnapshotManagerThroughTransaction) {
501502
ICEBERG_UNWRAP_OR_FAIL(auto manager, SnapshotManager::Make(txn));
502503

503504
manager->RollbackTo(oldest_snapshot_id_);
504-
EXPECT_THAT(manager->Commit(), IsOk());
505505
EXPECT_THAT(txn->Commit(), IsOk());
506506

507507
ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_));

src/iceberg/transaction.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,22 @@ class ICEBERG_EXPORT Transaction : public std::enable_shared_from_this<Transacti
5555
/// \return the location of the metadata file
5656
std::string MetadataFileLocation(std::string_view filename) const;
5757

58+
/// \brief Enable auto-commit for this transaction.
59+
void EnableAutoCommit() { auto_commit_ = true; }
60+
61+
/// \brief Disable auto-commit for this transaction.
62+
void DisableAutoCommit() { auto_commit_ = false; }
63+
5864
/// \brief Apply the pending changes from all actions and commit.
5965
///
6066
/// \return Updated table if the transaction was committed successfully, or an error:
6167
/// - ValidationFailed: if any update cannot be applied to the current table metadata.
6268
/// - CommitFailed: if the updates cannot be committed due to conflicts.
6369
Result<std::shared_ptr<Table>> Commit();
6470

71+
/// \brief Returns true if this transaction has been committed.
72+
bool is_committed() const { return committed_; }
73+
6574
/// \brief Create a new UpdatePartitionSpec to update the partition spec of this table
6675
/// and commit the changes.
6776
Result<std::shared_ptr<UpdatePartitionSpec>> NewUpdatePartitionSpec();
@@ -140,7 +149,7 @@ class ICEBERG_EXPORT Transaction : public std::enable_shared_from_this<Transacti
140149
const Kind kind_;
141150
// Whether to auto-commit the transaction when updates are applied.
142151
// This is useful when a temporary transaction is created for a single operation.
143-
const bool auto_commit_;
152+
bool auto_commit_;
144153
// To make the state simple, we require updates are added and committed in order.
145154
bool last_update_committed_ = true;
146155
// Tracks if transaction has been committed to prevent double-commit

src/iceberg/update/snapshot_manager.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,30 @@ SnapshotManager::SnapshotManager(std::shared_ptr<Transaction> transaction)
4646
SnapshotManager::~SnapshotManager() = default;
4747

4848
SnapshotManager& SnapshotManager::Cherrypick(int64_t snapshot_id) {
49+
CommitIfRefUpdatesExist();
4950
// TODO(anyone): Implement cherrypick operation
5051
ICEBERG_BUILDER_CHECK(false, "Cherrypick operation not yet implemented");
5152
return *this;
5253
}
5354

5455
SnapshotManager& SnapshotManager::SetCurrentSnapshot(int64_t snapshot_id) {
56+
CommitIfRefUpdatesExist();
5557
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto set_snapshot, transaction_->NewSetSnapshot());
5658
set_snapshot->SetCurrentSnapshot(snapshot_id);
5759
ICEBERG_BUILDER_RETURN_IF_ERROR(set_snapshot->Commit());
5860
return *this;
5961
}
6062

6163
SnapshotManager& SnapshotManager::RollbackToTime(int64_t timestamp_ms) {
64+
CommitIfRefUpdatesExist();
6265
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto set_snapshot, transaction_->NewSetSnapshot());
6366
set_snapshot->RollbackToTime(timestamp_ms);
6467
ICEBERG_BUILDER_RETURN_IF_ERROR(set_snapshot->Commit());
6568
return *this;
6669
}
6770

6871
SnapshotManager& SnapshotManager::RollbackTo(int64_t snapshot_id) {
72+
CommitIfRefUpdatesExist();
6973
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto set_snapshot, transaction_->NewSetSnapshot());
7074
set_snapshot->RollbackTo(snapshot_id);
7175
ICEBERG_BUILDER_RETURN_IF_ERROR(set_snapshot->Commit());
@@ -168,8 +172,13 @@ SnapshotManager& SnapshotManager::SetMaxRefAgeMs(const std::string& name,
168172
}
169173

170174
Status SnapshotManager::Commit() {
175+
transaction_->EnableAutoCommit();
171176
ICEBERG_RETURN_UNEXPECTED(CheckErrors());
172-
return CommitIfRefUpdatesExist();
177+
ICEBERG_RETURN_UNEXPECTED(CommitIfRefUpdatesExist());
178+
if (!transaction_->is_committed()) {
179+
ICEBERG_RETURN_UNEXPECTED(transaction_->Commit());
180+
}
181+
return {};
173182
}
174183

175184
Result<std::shared_ptr<UpdateSnapshotReference>>

0 commit comments

Comments
 (0)