Flink: DynamicIcebergSink writer and write-result aggregator keep using a dropped table's location after the table is re-created with the same name
Apache Iceberg version: 1.12.0 (reproduced). The code involved appears unchanged on main (flink/v2.2), so the same behaviour is expected there.
Query engine: Flink (DynamicIcebergSink, DataStream API, upsert mode)
Please describe the bug 🐞
Summary
DynamicWriter and DynamicWriteResultAggregator cache per-table objects (task writer factories / manifest output file factories) that capture the Table loaded when the entry was created. The caches are keyed by table name (plus schema id / spec id for the writer), have no TTL and are never invalidated. When a table is dropped and re-created with the same name while the job is running, the cache key can match the old entry, so the sink keeps writing data files and manifests for the dropped table's location.
On Amazon S3 Tables this fails at the next checkpoint with AccessDeniedException (403) Could not access through this access point, because the dropped table's storage is no longer accessible. The TaskManager fails, the job restarts, the caches are rebuilt from scratch and the replayed records are written to the new table.
Root cause
Line numbers at tag apache-iceberg-1.12.0, flink/v2.2.
DynamicWriter#taskWriterFactoriesis anLRUCache<WriteTarget, RowDataTaskWriterFactory>(DynamicWriter.java:59,82).WriteTargetconsists of table name, branch, schema id, spec id, upsert mode and equality fields (DynamicWriter.java:93-99), with no table identity such as the table UUID.- On a cache miss the factory loads the table once and captures it as
() -> table(DynamicWriter.java:105-106,133-134). Thewritersmap is cleared after everyprepareCommit()(DynamicWriter.java:198), but the factories stay in the cache, so new writers in later checkpoints reuse the staleTable(location,FileIO). DynamicWriteResultAggregator#outputFileFactoriesAndFormatVersionsand#specsareLRUCaches keyed by table name only (DynamicWriteResultAggregator.java:71-72,99-100).outputFileFactoryAndFormatVersion()captures the loaded table the same way (DynamicWriteResultAggregator.java:210-230), so manifests are also created for the stale table.TableMetadataCache(used byDynamicRecordProcessor) refreshes aftercacheRefreshMsand sees the re-created table, so records reach the writer with the new table's schema id / spec id. A newly created table starts again at schema id 0 / spec id 0, which hits the staleWriteTargetentry.
Reproduction
DataStream job with DynamicIcebergSink (REST catalog on Amazon S3 Tables, upsert mode, equality field id, immediateTableUpdate(true)):
- Write some rows to table
db.t(schema(id INT PK, val STRING)) and let a checkpoint commit them. - Drop the table from the catalog (
dropTable(db.t, purge=true)) while the job keeps running. - Write a row for
db.tagain, with the same or a different schema. The table is re-created (schema id 0, spec id 0). - At the next checkpoint:
Sink: Writer ... switched from RUNNING to FAILED
java.io.IOException: Could not perform checkpoint N for operator Sink: Writer
Caused by: java.io.IOException: software.amazon.awssdk.services.s3.model.AccessDeniedException: Could not access through this access point (Service: S3, Status Code: 403, ...)
at org.apache.iceberg.parquet.ParquetWriter.close(ParquetWriter.java:285)
at org.apache.iceberg.io.DataWriter.close(DataWriter.java:83)
at org.apache.iceberg.io.BaseTaskWriter$BaseRollingWriter.closeCurrent(BaseTaskWriter.java:435)
...
at org.apache.iceberg.flink.sink.UnpartitionedDeltaWriter.close(UnpartitionedDeltaWriter.java:69)
at org.apache.iceberg.io.BaseTaskWriter.complete(BaseTaskWriter.java:148)
Expected: The sink writes to the re-created table.
Actual: The writer writes to the dropped table's location and the checkpoint fails. After the restart the job recovers, because the caches are empty.
Impact
- On Amazon S3 Tables, every same-name drop + re-create causes one job failure / restart.
- CDC pipelines that replicate
DROP TABLE/CREATE TABLE(e.g. Debezium) hit this whenever a table is re-created with the same name.
Proposed fix
Either or both:
- Include the table identity (table UUID) in the cache keys of
DynamicWriterandDynamicWriteResultAggregator, e.g. by carrying the UUID resolved byTableMetadataCacheinDynamicRecordInternal/DynamicWriteResult. - Validate the cached
Tablewhen a new writer is created for a checkpoint (e.g. comparetable.uuid()with a freshly loaded /TableMetadataCachevalue), and rebuild the factory on mismatch. Optionally bound the cache entries bycacheRefreshMslikeTableMetadataCache.
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
- [x] I cannot contribute a fix for this bug at this time