dongjoon-hyun commented on code in PR #60262:
URL: https://github.com/apache/spark/pull/60262#discussion_r4521385946
##########
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.
Review Comment:
This relies on invariants in other files: `ColumnarStreamingWrite.abort`
skips a committed micro-batch, and `WriteToDataSourceV2Exec.writeWithV2` aborts
after a failed commit. It is the same as `NativeDataSourceWriter` (lines
557-558), so it is consistent within this PR. As a possible follow-up for both
writers, a `close()` on `ColumnarWriter` / `ColumnarStreamWriter`, called by
`ColumnarBatchWrite` / `ColumnarStreamingWrite` after a successful commit and
in a `finally` of `abort`, would keep the lifecycle where `committed` is
tracked.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ffi/NativeDataSource.scala:
##########
@@ -569,9 +569,10 @@ class NativeDataSourceWriter(
}
/**
- * Writes to a native data source for a streaming write. Its handle is closed
when the streaming
- * query creates its next stream writer, or, for the last stream writer of the
query, once the
- * handle becomes unreachable, see [[NativeStreamWriters]].
+ * Writes to a native data source for a streaming write, one micro-batch. As
for a batch write, its
Review Comment:
nit: The stream writer is now single-use: after `commit` or `abort`,
`handle.get` throws the internal error "The native handle was already
released." The Scaladoc of `ColumnarStreamWriter` in `ColumnarDataSource.scala`
still says "one micro-batch at a time", which reads as if a writer could be
reused for later epochs. The one-epoch assumption is only stated in a comment
of `ColumnarStreamingWrite`. Could the trait's Scaladoc say that Spark uses a
writer for a single micro-batch and does not call it after `commit` or `abort`?
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/ffi/NativeDataSource.scala:
##########
@@ -1412,19 +1417,23 @@ 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
+ * if that one is already closed.
+ */
def register(queryId: String, handle: NativeHandle): Unit = {
- // Forgets the queries whose last stream writer was released.
+ // Forgets the queries whose last stream writer became unreachable.
writers.values().removeIf(_.get() == null)
Option(writers.put(queryId, new WeakReference(handle))).flatMap(r =>
Option(r.get()))
.foreach(_.close())
Review Comment:
For a stream writer that is neither committed nor aborted, this fallback
closes it only after the next `create_writer` has returned, because
`NativeDataSource.streamWriter` calls `register` after `createWriter` (line
174). For a library whose `create_writer` waits for the previous writer to
release its destination, which is the case in the PR description, the fallback
cannot help: the new `create_writer` blocks until the old handle is
garbage-collected. The window is narrow (in `writeWithV2`, only between
`createBatchWriterFactory` and the `try`), so this is minor, but how about
closing the previous stream writer of the query before calling `create_writer`,
and registering the new handle afterwards? Then "closed when the query creates
its next one" would also hold before the next `create_writer`.
##########
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 = {
Review Comment:
nit: `commit` and `abort` here are now the same as those of
`NativeDataSourceWriter` (lines 553-568) except for the epoch ID (`-1L` there).
Could they share a small helper, for example in `NativeCalls`, so that a future
lifecycle change does not need to be made twice?
##########
sql/core/src/main/resources/org/apache/spark/sql/datasource/spark_datasource.h:
##########
@@ -137,10 +137,10 @@
* NULL handle as an error. Spark releases each handle exactly once, with
the close function of
* its kind: close, close_reader, close_stream_reader, close_writer or
close_data_writer, and no
* other function releases a handle. A handle is not always released right
after its last use: a
- * batch reader whose partitions are never planned and the last stream
writer of a query are
- * released once they are no longer referenced (see Lifecycle above). A
reader or writer handle
- * must not depend on the data source handle it was created from, because
Spark closes the data
- * source handle right after creating it.
+ * batch reader whose partitions are never planned, and a stream writer that
is neither committed
+ * nor aborted, may only be released once they are no longer referenced (see
Lifecycle above). A
Review Comment:
nit: This reads as if a stream writer that is neither committed nor aborted
is released only once it is no longer referenced, while Lifecycle above (lines
122-124) says that it is closed when the query creates its next one, or once it
is no longer referenced. How about something like "a batch reader whose
partitions are never planned may only be released once it is no longer
referenced, and a stream writer that is neither committed nor aborted once the
query creates its next one or it is no longer referenced (see Lifecycle above)"?
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/v2/ffi/NativeDataSourceSuite.scala:
##########
@@ -1072,14 +1108,14 @@ class NativeDataSourceSuite extends QueryTest with
SharedSparkSession {
query.asInstanceOf[StreamingQueryWrapper].streamingQuery.lastExecution.executedPlan
assert(plan.collectFirst { case write: V2TableWriteExec => write.query }
.exists(_.isInstanceOf[MicroBatchScanExec]), plan)
+ // Each micro-batch creates a new stream writer, which is closed once it
committed, so the
+ // writer of the last micro-batch is closed too while the query still
runs.
+ assert((0 to 2).forall(epoch => new File(dir, s"_CLOSED_$epoch").isFile))
Review Comment:
nit: The close at commit is covered here and by the `_CLOSED_0` check of
"keep the order of the calls whatever the callers of the adapter do", through
the `_CLOSED_<epoch>` markers. Optionally, the test library's `createWriter`
could record an order violation when the previous stream writer of the same
`query_id` is still open. That would assert the scenario of the PR description
directly: the previous writer still holds its destination when Spark calls
`create_writer` for the next micro-batch.
--
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]