gengliangwang commented on code in PR #60681:
URL: https://github.com/apache/spark/pull/60681#discussion_r4645514564
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SQLExecution.scala:
##########
@@ -237,7 +251,22 @@ object SQLExecution extends Logging {
val interruptOnCancel =
sparkSession.sessionState.conf.getConf(SQLConf.INTERRUPT_ON_CANCEL)
sc.setInterruptOnCancel(interruptOnCancel)
}
+ val maxScanBytes =
sparkSession.sessionState.conf.getConf(SQLConf.MAX_SCAN_BYTES)
+ var scanLimitListener: Option[ScanLimitListener] = None
try {
+ // A root execution with a scan limit is tracked by the listener that
enforces the limit.
+ // The jobs of its nested executions have its root execution ID and its
job tag, so they
+ // count towards the same limit. Streaming queries are not limited:
neither their batches
+ // nor the queries that their sources run on the thread of the stream
while they plan a
+ // batch, with whichever session they hold. The thread of a stream has
its query ID for as
+ // long as it runs. Creating the listener fails once the listener bus is
stopped, so it is
+ // done in this `try`, whose `finally` restores the state of the thread.
+ if (existingRootId == null && maxScanBytes >= 0 &&
+ sc.getLocalProperty(StreamExecution.QUERY_ID_KEY) == null) {
Review Comment:
Done in faaef96d208f: Fixing how nested executions are detected is out of
scope here, so the config doc, and the comment at the check, now say that a
thread inherits the local properties of the thread that starts it, so queries
on a thread started while a query or a streaming query runs (such as one a
foreachBatch function starts) count as part of that query, even after it ends,
and are not limited.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala:
##########
@@ -2362,6 +2362,17 @@ private[sql] object QueryExecutionErrors extends
QueryErrorsBase with ExecutionE
cause = null)
}
+ def scanBytesLimitExceededError(
+ scannedBytes: Long, maxScanBytes: Long, cause: Throwable):
SparkException = {
+ new SparkException(
+ errorClass = "SCAN_BYTES_LIMIT_EXCEEDED",
+ messageParameters = Map(
+ "scannedBytes" -> Utils.bytesToString(scannedBytes),
+ "limit" -> Utils.bytesToString(maxScanBytes),
Review Comment:
Done in faaef96d208f: Both `SCAN_BYTES_LIMIT_EXCEEDED` and
`ScanLimitListener.cancelReason` now show the exact byte count before the
rounded size, e.g. `107374182400 bytes (100.0 GiB)`, and `ScanLimitSuite`
matches the new format.
##########
sql/core/src/main/scala/org/apache/spark/sql/internal/SharedState.scala:
##########
@@ -128,6 +128,22 @@ private[sql] class SharedState(
statusStore
}
+ /**
+ * The listener that enforces `spark.sql.execution.maxScanBytes`. It is
created, and starts to
+ * receive events, when the first query with a limit runs, and it receives
them until the
+ * SparkContext stops: while no query has a limit, its event handlers return
at once. It has a
+ * queue of its own on the listener bus, so that it does not slow down the
listeners of the UI
+ * and they do not delay or drop its events. The events that its own queue
drops when it is full
+ * still weaken the limit.
+ */
+ private[sql] lazy val scanLimitListener: ScanLimitListener = {
+ // The listener is never removed, so its function must not capture this
SharedState.
+ val sc = sparkContext
+ val listener = new ScanLimitListener(sc.cancelJobsWithTag(_, _))
+ sc.listenerBus.addToQueue(listener, "scanLimit")
Review Comment:
Done in faaef96d208f: Added `ScanLimitListener.QUEUE_NAME`, used it in
`SharedState`, and added a mention of
`spark.scheduler.listenerbus.eventqueue.scanLimit.capacity` to the sentence
about dropped events.
--
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]