Iceberg Flink sinks duplicate data during recovery when used with the REST catalog
Solution
The PR, which resolves the issue is in https://github.com/apache/iceberg/pull/14517.
Apache Iceberg version
1.10.0 (latest release)
Query engine
Flink
Please describe the bug
The DynamicIcebergSink used with the Iceberg REST catalog duplicates data after recovering from a failed POST commit request. The getMaxCommittedCheckpointId() check in DynamicCommitter leads to data duplication if the first commit request, which is still being processed in the REST catalog, is committed after the getMaxCommittedCheckpointId(), but before the refresh of the base snapshot in SnapshotProducer.apply(). This situation is illustrated in the diagram below.
The commit duplication happens in the following sequence of events:
DynamicIcebergSinkstarts the checkpoint 1 (chk-1).DynamicIcebergSinkcompleteschk-1and saves the commit request 1 (CR-1) with data/delete files to the checkpoint state.DynamicCommitterfinds no commits forchk-1(maxCheckpointIdis-1) and sends the POST table update request (Req-1) to the REST catalog.- REST catalog receives the request and triggers the table update in the underlying catalog (eg JDBC).
DynamicCommitterrestarts due to a Flink task manager process failure:
- This can be any failure, even a shutdown of the Flink client JVM; one example is a
SocketTimeoutExceptionin the REST client, which happens when the REST catalog was too slow to commit to the underlying catalog, for example, due to retrying for too long.
DynamicCommitterrestarts and initiates the commit sequence:
- Loads the table from the REST catalog;
- Checks the max committed checkpoint ID in the table snapshot history and decides to commit because
Req-1has not completed yet, so themaxCheckpointIdis still-1.
Req-1completes in the REST catalog and sets the current table snapshot ID to1.DynamicCommittergets into theSnapshotProducer.apply()call and triggers theSnapshotProducer.refresh():
- This fetches the latest table snapshot
1, which has been just set by theReq-1, and is different to the one before thegetMaxCommittedCheckpointId()check ( RACE CONDITION ); SnapshotProducer.apply()sets the parent snapshot ID to1and sends a duplicate table update request (POST) to the REST catalog.
- REST catalog receives the duplicate request and commits to the underlying catalog:
- The commit lands in the catalog without any retries because the parent snapshot of the
Req-2is equal to the latest table snapshot, set byReq-1in the previous step.
This data duplication scenario is depicted in the following sequence diagram:
The IcebergSink will have the same issue due to identical code paths in IcebergCommitter and DynamicCommitter.
Willingness to contribute
- I can contribute a fix for this bug independently
- I would be willing to contribute a fix for this bug with guidance from the Iceberg community
- I cannot contribute a fix for this bug at this time
Source: apache/iceberg