gengliangwang commented on code in PR #60262:
URL: https://github.com/apache/spark/pull/60262#discussion_r4521560369
##########
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:
Done in c3454614213c: streamWriter now calls
NativeStreamWriters.closePrevious(queryId) before create_writer and registers
the new handle afterwards. The unit test, header, docs and Scaladoc now say
"before" instead of "when".
##########
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:
Agreed, the close-on-commit relies on two facts: ColumnarStreamingWrite
skips the abort of a committed micro-batch, and writeWithV2 aborts after a
failed commit. To keep that reasoning in one place, both writers now go through
NativeCalls.commitWrite / abortWrite, and the comment there points at
ColumnarBatchWrite and ColumnarStreamingWrite. I'd rather not add a close() to
ColumnarWriter / ColumnarStreamWriter in this PR. It changes the adapter's
interface for both writers, which is a different change from this fix, and the
native writers are the only implementations that hold a resource to close.
##########
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:
Done in c3454614213c: The ColumnarStreamWriter Scaladoc now says the writer
is for a single micro-batch: Spark creates a new one for each micro-batch and
does not call it after it committed or aborted.
##########
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:
Done in c3454614213c: Reworded Conventions along the lines you suggested: a
batch reader is released once it is no longer referenced, and a stream writer
that is neither committed nor aborted before the query creates its next one, or
once it is no longer referenced.
##########
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:
Done in c3454614213c: Moved commit/abort into NativeCalls.commitWrite and
abortWrite, which take the epoch ID (-1 for a batch write). Both writers
delegate to them.
##########
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:
I looked at this and left it out because it could make the tests flaky. When
a previous stream writer becomes unreachable, its WeakReference is cleared at
GC time, but close_writer runs later on the Cleaner thread. So a strict
"previous writer of this query_id is still open" check at create_writer could
fire for no real reason. For the reachable case, which closePrevious now
handles before create_writer, the NativeStreamWriters unit test checks the
order directly. The `_CLOSED_<epoch>` assertions, made while the query still
runs, cover the commit path. Every streaming test also commits or aborts each
micro-batch, so the extra check would only re-verify that path.
--
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]