dongjoon-hyun commented on code in PR #60681:
URL: https://github.com/apache/spark/pull/60681#discussion_r4645335031
##########
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:
The scaladoc says the events that this queue drops weaken the limit. The way
to avoid that is `spark.scheduler.listenerbus.eventqueue.scanLimit.capacity`,
but the queue name only appears in this literal. Could we define it as a
constant, as `StreamingQueryListenerBus.STREAM_EVENT_QUERY` and the queue names
in `LiveListenerBus` are, and mention the capacity config next to that sentence?
##########
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:
Both checks read local properties, which a child thread inherits
(`SparkContext.localProperties` clones them in `childValue`). A thread that is
created while a root execution or a stream runs keeps
`spark.sql.execution.root.id` or `sql.streaming.queryId` for its whole life, so
every later query on that thread skips the limit:
- With a stale root ID, the query is taken as nested in an execution that
has already ended. It is neither registered nor tagged, and the listener does
not know the stale root.
- With a stale stream query ID, the query is taken as part of a stream.
For example, a thread pool whose threads are first started from a
`foreachBatch` function inherits both, and a batch query that the application
later runs on that pool is not limited. Treating a stale root ID as nested is
existing behavior, but it now also decides whether the limit applies. If fixing
it is out of scope, could the doc list it with the other cases that 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:
`Utils.bytesToString` rounds to one decimal place (and shows 1 GiB as
`1024.0 MiB`), so the message can contradict itself:
- With `maxScanBytes=100g`, a query that is cancelled 30 MiB over the limit
fails with "read at least 100.0 GiB ..., more than the limit of 100.0 GiB".
- `maxScanBytes=1g` is shown as `1024.0 MiB` rather than as the value the
user set.
How about adding the exact byte counts, e.g. `107374182400 bytes (100.0
GiB)`? `ScanLimitListener.cancelReason` has the same issue.
--
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]