CreateSnapshotEvent / CommitReport are sent prematurely when using a Transaction
Apache Iceberg version
1.2.0 (latest release)
Query engine
None
Please describe the bug 🐞
Currently, a CreateSnapshotEvent or a CommitReport are both sent in SnapshotProducer#notifyListeners() right after commit was successful.
However, when using a TX, that commit is only staged and only committed when the TX itself commits. This results in premature sending of CreateSnapshotEvent / CommitReport for a table that potentially won't be committed.
This can be reproduced by adding this to TestCreateSnapshotEvent
@Test
public void snapshotEventAvailableAfterTransactionCommit() {
Transaction transaction = table.newTransaction();
transaction.newAppend().appendFile(FILE_A).commit();
// the event should only be available after the TX was committed
Assertions.assertThat(currentEvent).isNull();
transaction.commitTransaction();
Assertions.assertThat(currentEvent).isNotNull();
}
and this to TestCommitReporting
@Test
public void commitReportAvailableAfterTransactionCommit() {
String tableName = "reporting-inside-tx";
Table table =
TestTables.create(
tableDir, tableName, SCHEMA, SPEC, SortOrder.unsorted(), formatVersion, reporter);
Transaction transaction = table.newTransaction();
transaction.newAppend().appendFile(FILE_A).commit();
assertThat(reporter.lastCommitReport()).isNull();
// commit report should only be available after the TX was committed
transaction.commitTransaction();
CommitReport report = reporter.lastCommitReport();
assertThat(report).isNotNull();
assertThat(report.operation()).isEqualTo("append");
assertThat(report.snapshotId()).isEqualTo(1L);
assertThat(report.sequenceNumber()).isEqualTo(1L);
assertThat(report.tableName()).isEqualTo(tableName);
}
This issue has been automatically marked as stale because it has been open for 180 days with no activity. It will be closed in next 14 days if no further activity occurs. To permanently prevent this issue from being considered stale, add the label 'not-stale', but commenting on the issue is preferred when possible.
I'd like to work on this. My plan is for snapshot producers created by a transaction to skip notifying listeners in their own commit(), and for BaseTransaction.commitTransaction() to notify them once the transaction has committed. That covers create and replace transactions too, and it stops the event and report from being sent again each time a retried transaction re-applies its updates. CatalogTests#testCatalogWithCustomMetricsReporter currently counts the report from create/replace transactions that are never committed, so that test would commit them first. Does that sound right? cc @nastra