nsivabalan opened a new pull request, #21097:
URL: https://github.com/apache/hudi/pull/21097

   ### Describe the issue this Pull Request addresses
   
   Addresses #20657.
   
   A row-writer bulk insert task attempt whose results Spark does not use (it 
failed, was killed, or another attempt of the same partition was used) can 
leave data files behind that are not in the commit. The row writer writes at 
final paths, and the only cleanup is marker reconciliation at commit, which 
deletes invalid files *that exist on storage when it runs*. Two gaps:
   
   1. **Kill/failure path.** `HoodieDatasetBulkInsertHelper` closed every open 
file in `finally`, and `BulkInsertDataInternalWriterHelper.abort()` was a 
no-op. On object stores (S3A) closing a file is what publishes it, so an 
attempt that is cancelled after the job ended (e.g. an attempt of a stage 
resubmitted after a `FetchFailed`, cancelled with `TaskKilled (Stage finished)` 
once the job completes) publishes its partial files *after* reconciliation and 
after the commit completed.
   2. **Superseded attempt that succeeds.** The row writer never asks the 
driver whether its attempt may commit, so an attempt that finishes after 
another attempt was used, or after its stage finished, keeps its files.
   
   Each row-writer attempt uses a random file id prefix, so such a file is a 
separate file group under a completed instant: with the metadata table 
disabled, a storage listing reads it as committed data and queries return 
duplicate rows (and table services such as clustering carry them forward). With 
the bucket index writers the file id is deterministic, so the leftover file 
lands in the same file group as the winner's.
   
   ### Summary and Changelog
   
   A task attempt whose output Spark does not use now leaves no data files 
behind.
   
   - `BulkInsertDataInternalWriterHelper` tracks every `HoodieRowCreateHandle` 
it (or a subclass) creates. `abort()` now deletes every file of the attempt, 
files still open and files already closed on a partition change or size 
roll-over. After `abort()`, `close()` is a no-op and `getWriteStatuses()` fails.
   - `HoodieRowCreateHandle#abort()`: closes the writer if still open (to 
release resources) and deletes the file.
   - `HoodieDatasetBulkInsertHelper#writeTaskAttempt` (extracted from the 
`mapPartitions` closure): files are closed only on success; on any failure the 
attempt is aborted (an abort failure is attached as a suppressed exception to 
the original one).
   - After a successful write, the attempt asks the driver's 
`OutputCommitCoordinator` whether it may commit, as Spark's own writers do 
(`SparkHadoopMapRedUtil.commitTask`). If denied, it throws 
`CommitDeniedException` (reported as `TaskCommitDenied`, which does not count 
towards task failures) and its files are discarded. The coordinator authorizes 
one attempt per partition and denies every attempt that asks after its stage 
finished. New shim `org.apache.spark.HoodieTaskCommitCoordination` (the 
coordinator is `private[spark]`); it honors Spark's 
`spark.hadoop.outputCommitCoordination.enabled`.
     - The request is the attempt's last step, after its files are closed: 
Spark 3.4 fails the stage if an authorized attempt fails afterwards (3.3, 3.5, 
4.x release the lock instead), so nothing is left to fail once authorized.
   - Bucket and consistent-hashing bucket writers register their handles and 
implement `closeOpenHandles()` in place of overriding `close()`.
   - Files `abort()` cannot delete are logged at ERROR as orphans (they are not 
in any commit but would be listed as data) and reported through the suppressed 
exception; a metric would not survive, since Spark drops accumulator updates of 
failed and killed tasks.
   
   Tests (`TestRowWriterTaskAttemptCleanup`), each verified to **fail against 
the previous behavior** (no-op `abort()`, `finally { close() }`, no commit 
check) and pass with this change:
   - `abort()` removes both closed and open files (sorted and unsorted input), 
and `close()` after it publishes nothing; same for the bucket index writer.
   - A bulk insert whose task fails part-way through its input, with one 
partition's file closed and the next open, leaves no data files (sorted and 
unsorted).
   - An attempt that wrote all its rows but is denied commit (another attempt 
of the partition was authorized first) leaves no data files.
   - Positive control: an authorized attempt keeps exactly the files in its 
write statuses.
   
   Existing `TestHoodieRowCreateHandle` and `TestHoodieDatasetBulkInsertHelper` 
pass.
   
   Out of scope, follow-ups:
   - Aborting S3A streams through Hadoop's `Abortable` instead of 
close-then-delete. Closing an open file on abort still briefly publishes it 
before the delete; aborting the stream would never publish it.
   - The non-row-writer insert path (`SparkLazyInsertIterable` / 
`CopyOnWriteInsertHandler`) does not close open handles on failure, but a 
superseded attempt that completes after the commit keeps its files there too; 
the same commit check would apply.
   
   ### Impact
   
   No public API or config change. Row-writer bulk insert (and row-writer 
clustering, which uses the same helper) now consults Spark's output commit 
coordinator once per task attempt (one RPC to the driver). Attempts that fail, 
are killed, or are denied commit delete their files on the executor.
   
   ### Risk Level
   
   medium
   
   The change is on the bulk-insert write path. The commit check follows the 
protocol Spark's own writers use and runs only after all files of the attempt 
are closed; it can be disabled with Spark's 
`spark.hadoop.outputCommitCoordination.enabled=false`. The coordinator 
semantics were checked against the Spark 3.3, 3.4, 3.5, 4.0 and 4.1 sources 
(same `canCommit` signature; stage state is kept across resubmitted stage 
attempts and removed when the stage finishes, so late attempts are denied). The 
new tests were run against both the old and the new behavior.
   
   ### Documentation Update
   
   none
   
   ### Contributor's checklist
   
   - [x] Read through [contributor's 
guide](https://hudi.apache.org/contribute/how-to-contribute)
   - [x] Enough context is provided in the sections above
   - [x] Adequate tests were added if applicable
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to