spark-test commented on code in PR #60262:
URL: https://github.com/apache/spark/pull/60262#discussion_r4519803467
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ffi/NativeDataSource.scala:
##########
@@ -593,17 +594,21 @@ class NativeDataSourceStreamWriter(
location, name, schema, timeZoneId, writerState, partitionId, taskId,
epochId)
}
- // Spark does not abort a committed micro-batch, see ColumnarStreamingWrite.
override def commit(epochId: Long, messages: Array[WriterCommitMessage]):
Unit = {
call(name, "commit a write to") {
library.commit(handle.get, epochId,
NativeWriterCommitMessage.toBytes(messages))
}
+ // Not closed if the commit fails: Spark then aborts the micro-batch,
which closes it. Spark
+ // does not abort a committed micro-batch, see ColumnarStreamingWrite.
+ handle.close()
}
override def abort(epochId: Long, messages: Array[WriterCommitMessage]):
Unit = {
- call(name, "abort a write to") {
- library.abort(handle.get, epochId,
NativeWriterCommitMessage.toBytes(messages))
- }
+ Utils.tryWithSafeFinally {
+ call(name, "abort a write to") {
+ library.abort(handle.get, epochId,
NativeWriterCommitMessage.toBytes(messages))
+ }
+ }(handle.close())
Review Comment:
**Nit (P3):** Every abort that `NativeDataSourceSuite` reaches succeeds, and
native_sink's `abortWrite` has no failure step, so this `finally` close for a
failing library abort never runs in a test. From the code, a rewrite to
`call(...) { library.abort(...) }` followed by `handle.close()` would keep the
suite green, and a micro-batch whose library abort fails would then keep its
writer open until the restarted query's next `create_writer` (through
`NativeStreamWriters.register`) or a garbage collection, which is the
held-writer failure this PR fixes. The suite already checks the release after
the other failures native_sink can inject: the stream reader after each failing
call, and both writers after a failed write or commit. Could `abortWrite` get
an `abort` failure step right after it sets `closed_marker`, with an
adapter-level test like the one in "keep the order of the calls whatever the
callers of the adapter do" that fails the abort without a commit and asserts
`_CLOSED_0`?
The batch writer's `abort` has the same untested branch on the base branch;
the same step could cover it, but that is optional here.
**Verification:**
- **Regression:** A streaming write whose library abort fails, driven
through the streaming write adapter without a commit, reports the abort's
native data source error and still closes its stream writer, and this check
fails when the stream writer closes its handle only after a successful abort.
- **Behavior:** The existing native data source lifecycle and streaming
tests keep passing with the new failure step, which fails an abort only when
the fail option names it: committed and aborted micro-batches still close their
writers, and the test library records no order violation.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ffi/NativeDataSource.scala:
##########
@@ -1412,17 +1417,21 @@ object NativeHandle extends Logging {
}
/**
- * The stream writers of the streaming queries, by query ID. The Data Source
V2 streaming write
- * has no hook for when the query stops, but a micro-batch streaming query
creates a new stream
- * writer for each micro-batch, after the previous micro-batch committed or
aborted its write, and
- * a restarted query creates a new one. So the previous stream writer of a
query is closed when
- * the query creates a new one, and the last one when it becomes unreachable.
+ * The stream writers of the streaming queries, by query ID, to close those
that are neither
+ * committed nor aborted: a stream writer closes its handle once its
micro-batch is committed or
+ * aborted. The Data Source V2 streaming write has no hook for when the query
stops, but a
+ * micro-batch streaming query creates a new stream writer for each
micro-batch, and a restarted
+ * query creates a new one. So a previous stream writer of a query that is
still open is closed
+ * when the query creates a new one, and the last one when it becomes
unreachable.
*/
object NativeStreamWriters {
// Weak, so that the last stream writer of a query can still become
unreachable.
private val writers = new ConcurrentHashMap[String,
WeakReference[NativeHandle]]()
- /** Registers the new stream writer of the query, and closes its previous
one. */
+ /**
+ * Registers the new stream writer of the query, and closes its previous
one, which has no effect
Review Comment:
**Nit (P3):** The body comment of `register`, `// Forgets the queries whose
last stream writer was released.`, no longer describes the `removeIf(_.get() ==
null)` right below it. With this change, `commit` and `abort` release the
handle while the micro-batch's plan still references it, through the
`MicroBatchWrite` and `ColumnarStreamingWrite` that hold the writer, and
`NativeHandle.close()` only runs the `Cleanable`, so the `WeakReference` in the
map stays set. A query whose last stream writer was closed at commit therefore
keeps its entry until the handle is garbage-collected. Before this change, the
last writer was released only by the Cleaner once it became unreachable, so
being released and having its weak reference cleared coincided. Something like
`// Forgets the queries whose last stream writer became unreachable.` would
match both the check and the "can still become unreachable" comment on
`writers`.
**Verification:**
- **Inspection:** The comment in the stream writer registry's register
method describes the entries it removes as those whose last stream writer
became unreachable, agrees with the weak-reference check it precedes and with
the registry's Scaladoc, and no code of the registry changes.
--
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]