gengliangwang commented on code in PR #60681: URL: https://github.com/apache/spark/pull/60681#discussion_r4621420151
########## sql/core/src/main/scala/org/apache/spark/sql/execution/ScanLimitListener.scala: ########## @@ -0,0 +1,223 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.execution + +import java.util.Properties +import java.util.concurrent.ConcurrentHashMap + +import scala.collection.mutable + +import org.apache.spark.InternalAccumulator +import org.apache.spark.internal.Logging +import org.apache.spark.internal.LogKeys.{CONFIG, EXECUTION_ID, NUM_BYTES, NUM_BYTES_MAX} +import org.apache.spark.scheduler._ +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.util.Utils +import org.apache.spark.util.collection.OpenHashMap + +/** + * Enforces `spark.sql.execution.maxScanBytes`. It adds up the bytes read by the tasks of every + * root SQL execution registered with it, and cancels the jobs of an execution once the total is + * over the limit of that execution. + * + * The bytes are the task input metric `bytesRead`. It arrives with the executor heartbeats while + * a task runs and with the task end event, so an execution is cancelled some time after it went + * over its limit, not at that point. + * + * A task retry, a speculative task and a new attempt of a stage read the data of a partition + * again. To keep that from counting against the limit, the listener keeps one value per + * partition of a stage, the largest that any attempt reported. + * + * A job can depend on a stage that a job of another execution is already running, and that stage + * is not submitted again. So the listener learns from the start of each job which stages it + * depends on, and counts what a stage reads against every execution that depends on it. + * + * @param cancelJobsWithTag cancels the active jobs that have the given tag, for the given reason + */ +private[sql] class ScanLimitListener(cancelJobsWithTag: (String, String) => Unit) + extends SparkListener with Logging { + + // The registered executions by root execution ID. The event handlers read it without the lock, + // to return early when no execution has a limit. + private val executions = new ConcurrentHashMap[Long, ExecutionScanState]() + + // The stages of the registered executions by stage ID. Guarded by `this`, as is all the state + // of the executions and the stages. + private val stages = new mutable.HashMap[Int, StageScanState]() + + /** + * Starts to track a root execution. All its jobs, including the ones of its nested executions, + * must have `jobTag`. + */ + def register(rootExecutionId: Long, maxScanBytes: Long, jobTag: String): Unit = { + executions.put(rootExecutionId, new ExecutionScanState(rootExecutionId, maxScanBytes, jobTag)) + } + + /** Stops tracking a root execution. */ + def unregister(rootExecutionId: Long): Unit = synchronized { + val execution = executions.remove(rootExecutionId) + stages.filterInPlace { (_, stage) => + stage.executions -= execution + stage.executions.nonEmpty + } + } + + /** The bytes that an execution had read when it went over its limit, if it did. */ + def bytesReadOverLimit(rootExecutionId: Long): Option[Long] = synchronized { + Option(executions.get(rootExecutionId)).flatMap(_.bytesReadOverLimit) + } + + /** The registered execution of a job with the given properties, if any. */ + private def executionOf(properties: Properties): Option[ExecutionScanState] = { + Option(properties) + .flatMap(p => Option(p.getProperty(SQLExecution.EXECUTION_ROOT_ID_KEY))) + .flatMap(rootExecutionId => Option(executions.get(rootExecutionId.toLong))) + } + + override def onJobStart(event: SparkListenerJobStart): Unit = { Review Comment: Done in 5eccf93f1cc7: I kept the tracking and closed the gap. `onJobStart` now also records the bytes-read accumulator of each running stage from `event.stageInfos` (a stage that hasn't been submitted has null metrics and is skipped). So heartbeats count for tasks that start after the job does, even when an execution without a limit submitted the stage. Tasks that were already running when the job started still count only when they end. The class doc says so, and a new test in ScanLimitListenerSuite covers the case. I kept it because without it the second execution doesn't just undercount: nothing the shared stage reads counts toward its limit. The stage also keeps running for it after the first execution is cancelled, because DAGScheduler only kills stages that no other job needs. -- 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]
