gengliangwang opened a new pull request, #60262:
URL: https://github.com/apache/spark/pull/60262

   ### What changes were proposed in this pull request?
   
   A native data source closes the writer of a batch write right after Spark 
commits it, and after Spark aborts it (`NativeDataSourceWriter`). It did not 
close the writer of a micro-batch of a streaming write: 
`NativeDataSourceStreamWriter.commit` and `abort` left its handle open, and 
`NativeStreamWriters.register` closed it only when the query created the stream 
writer of its next micro-batch, after `create_writer` of that micro-batch, or, 
for the last micro-batch of a query, once the handle became unreachable.
   
   This PR closes the handle of a stream writer after a successful commit and 
after an abort, as for a batch write:
   
   - `NativeDataSourceStreamWriter.commit` closes its handle once the commit 
succeeded. When the commit fails, the handle stays open, since Spark then 
aborts the micro-batch; `ColumnarStreamingWrite` does not abort a committed 
micro-batch.
   - `NativeDataSourceStreamWriter.abort` closes its handle in a `finally`.
   - `NativeStreamWriters` still closes the previous stream writer of a query 
when the query creates a new one, and the last one once it becomes unreachable, 
but that now only matters for a stream writer that is neither committed nor 
aborted. `NativeHandle.close` releases a handle at most once 
(`Cleaner.Cleanable.clean`), so closing a handle that a commit or an abort 
already closed has no effect.
   - `spark_datasource.h` and `docs/sql-native-data-sources.md` describe the 
new lifecycle: Spark closes a stream writer once it committed or aborted its 
micro-batch, and a stream writer that Spark neither commits nor aborts is 
closed when the query creates its next one, or once it is no longer referenced. 
The header's example of a handle that is released once it is no longer 
referenced, possibly on a JVM Cleaner thread, is now a batch reader whose 
partitions are never planned, instead of the last stream writer of a query.
   
   ### Why are the changes needed?
   
   A library that releases an exclusive resource in `close_writer`, such as a 
lock on its destination, fails or blocks at the second micro-batch of a 
streaming query: the writer of the first micro-batch still holds the resource 
when Spark calls `create_writer` for the second one. The writer of the last 
micro-batch of a query is even closed only once the JVM collects it. The batch 
writer avoids both by closing its handle right after its commit or its abort, 
and the stream writer now does the same.
   
   This was found in the review of the Rust SDK for native data sources: 
https://github.com/apache/spark/pull/59520#discussion_r4515939460
   
   ### Does this PR introduce _any_ user-facing change?
   
   No, compared to the released versions: native data sources ship first in 
Spark 4.4.0. Within the unreleased branches, a native data source library now 
gets `close_writer` for the writer of a micro-batch right after Spark commits 
or aborts the micro-batch, instead of when the next micro-batch creates its 
writer, or, for the last micro-batch of a query, once the JVM collects the 
writer.
   
   ### How was this patch tested?
   
   Three tests of `NativeDataSourceSuite` now also check when Spark closes the 
writer of a micro-batch, and fail without this change:
   
   - "keep the order of the calls whatever the callers of the adapter do": a 
committed micro-batch closes its writer right away, as a committed batch write 
does.
   - "abort a micro-batch whose write failed": the aborted micro-batch closes 
its writer.
   - "streaming read and write": the writer of every micro-batch, the last one 
included, is closed while the query still runs.
   
   `build/sbt 'sql/testOnly *NativeDataSourceSuite'` passes on Java 25: 83 
tests succeed, and 2 are canceled because they need a Java version before 22, 
or pandas and PyArrow. `sql/scalastyle` and `sql/Test/scalastyle` pass too.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to