[bug] Emit REMOVE in HudiDataFileExtractor when CLEAN deletes previous file version before incremental sync - #824
Conversation
…e incremental sync When Hudi CLEAN physically removes an old base file from storage before XTable processes the corresponding UPSERT commit incrementally, getAllBaseFiles() only returns the new file. The else-if branch in getUpdatesToPartition that emits a REMOVE for the previous version never fires, leaving a stale ADD in the downstream Delta log that causes FILE_NOT_FOUND errors on read. Fix: after the base-file loop, if an ADD was emitted but no REMOVE was emitted and HoodieWriteStat.prevCommit indicates a prior version exists, recover the old file path by reading the previous commit's metadata from the visible Hudi timeline and emit a REMOVE. Gracefully degrades (logs warning, skips) if the previous commit has been archived. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
| * @return the old {@link InternalDataFile}, or empty if the previous commit is no longer in the | ||
| * visible timeline (e.g. archived before this sync ran) | ||
| */ | ||
| Optional<InternalDataFile> recoverRemovedFile( |
There was a problem hiding this comment.
This will parse the clean metadata per file. Can we create some lightweight object that maintains an in-memory cache of the clean plans for this sync so it is more performant when this case is hit?
| boolean newBaseFileAdded = false; | ||
| boolean removeEmitted = false; | ||
| for (HoodieBaseFile baseFile : baseFiles) { | ||
| if (baseFile.getCommitTime().equals(instantToConsider.getTimestamp())) { |
There was a problem hiding this comment.
HoodieInstant.getTimestamp() was removed by the Hudi 1.2.0 upgrade (#772, merged after this
branch diverged). main's getUpdatesToPartition already uses instantToConsider.requestedTime(),
but this PR's diff still shows instantToConsider.getTimestamp() as unchanged context, and the
new recoverRemovedFile method also calls instant.getTimestamp(). Needs a rebase + both call
sites updated to requestedTime().
| String prevCommitTime, | ||
| List<PartitionValue> partitionValues) { | ||
| return timeline.getInstants().stream() | ||
| .filter(instant -> prevCommitTime.equals(instant.getTimestamp())) |
There was a problem hiding this comment.
same stale API like above. Needs rebase.
| filesToAdd.add( | ||
| buildFileWithoutStats(partitionValues, baseFiles.get(baseFiles.size() - 1))); | ||
| } else if (replacedFileIds.contains(fileId)) { | ||
| filesToRemove.add(buildFileWithoutStats(partitionValues, baseFiles.get(0))); |
There was a problem hiding this comment.
Grabs old file via live fsView, same vulnerability as the bug this PR fixes, I think fix is needed here too.
| prevInstant -> { | ||
| try { | ||
| HoodieCommitMetadata prevMeta = | ||
| HoodieCommitMetadata.fromBytes( |
There was a problem hiding this comment.
Caching / re-parsing concern as raised by @the-other-tim-brown
What is the purpose of the pull request
Fixes a bug in
HudiDataFileExtractor(source side) where XTable's incremental Hudi sync can leave a staleADD/ data-file reference in the target table's metadata, causingFILE_NOT_FOUNDon read. Affects any target format — Delta and Iceberg — since the missingREMOVEis in the format-agnosticInternalFilesDiffproduced by the source.RCA (one line)
Hudi
CLEANdeletes an old Parquet file before XTable's next incremental sync runs, soHudiDataFileExtractor.getUpdatesToPartitioncan't see the deleted file viagetAllBaseFiles()and skips emitting aREMOVEfor it — leaving a stale reference in the target's metadata (ADDin the Delta log, data-file entry in the Iceberg manifest) that points to a non-existent file; this PR recovers the deleted file path from the previous commit's metadata in the Hudi timeline (HoodieWriteStat.prevCommit) so theREMOVEis emitted correctly.Brief change log
HudiDataFileExtractor.getUpdatesToPartitionnow also takesHoodieTimelineandList<HoodieWriteStat>so it can build afileId → prevCommitmap.ADDwas emitted but noREMOVEwas, look up the previous commit's metadata viaHoodieCommitMetadata.fromBytes(timeline.getInstantDetails(prevInstant).get(), ...)and emit theREMOVEfromHoodieWriteStat.getPath().Verify this pull request
This change added tests and can be verified as follows:
TestHudiDataFileExtractorwith three unit tests forrecoverRemovedFile: previous commit present, previous commit archived, fileId not in previous commit metadata.ITConversionController.testIncrementalSyncEmitsRemoveWhenHudiCleanRunsBeforeSync, an end-to-end regression test that reproduces the race (insert → sync → upsert → insert → clean → incremental sync) and asserts Delta equivalence at 120 records.