juliuszsompolski commented on code in PR #41315: URL: https://github.com/apache/spark/pull/41315#discussion_r1263105233
########## connector/connect/server/src/main/scala/org/apache/spark/sql/connect/execution/ExecuteThreadRunner.scala: ########## @@ -0,0 +1,174 @@ +/* + * 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.connect.execution + +import scala.util.control.NonFatal + +import com.google.protobuf.Message +import org.apache.commons.lang3.StringUtils + +import org.apache.spark.SparkSQLException +import org.apache.spark.connect.proto +import org.apache.spark.internal.Logging +import org.apache.spark.sql.connect.common.ProtoUtils +import org.apache.spark.sql.connect.planner.SparkConnectPlanner +import org.apache.spark.sql.connect.service.ExecuteHolder +import org.apache.spark.sql.connect.utils.ErrorUtils +import org.apache.spark.util.Utils + +/** + * This class launches the actual execution in an execution thread. The execution pushes the + * responses to a ExecuteResponseObserver in executeHolder. + */ +private[connect] class ExecuteThreadRunner(executeHolder: ExecuteHolder) extends Logging { + + // The newly created thread will inherit all InheritableThreadLocals used by Spark, + // e.g. SparkContext.localProperties. If considering implementing a threadpool, + // forwarding of thread locals needs to be taken into account. + private var executionThread: Thread = new ExecutionThread() + + private var interrupted: Boolean = false + + /** Launches the execution in a background thread, returns immediately. */ + def start(): Unit = { + executionThread.start() + } + + /** Joins the background execution thread after it is finished. */ + def join(): Unit = { + executionThread.join() + } + + /** Interrupt the executing thread. */ + def interrupt(): Unit = { + synchronized { + interrupted = true + executionThread.interrupt() + } + } + + private def execute(): Unit = { + // Outer execute handles errors. + // Separate it from executeInternal to save on indent and improve readability. + try { + try { + executeInternal() + } catch { + // Need to catch throwable instead of NonFatal, because e.g. InterruptedException is fatal. + case e: Throwable => + logDebug(s"Exception in execute: $e") + // Always cancel all remaining execution after error. + executeHolder.sessionHolder.session.sparkContext.cancelJobsWithTag(executeHolder.jobTag) Review Comment: This doesn't wait and doesn't throw, it's just sends an async ping to DAGScheduler to cancel whatever is left running. See SparkContext.cancelJobsWithTag -> DAGScheduler.cancelJobsWithTag. This is "to be safe and clean everything up" like in https://github.com/apache/spark/blob/master/sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkExecuteStatementOperation.scala#L248 in Thriftserver I plan to add two more types of cancelation: - per-query (requires operation_id, to be introduces in a next PR that also deals with query reattach) - per user settable tag (very similar to SparkContext.addJobTag / cancelJobsWithTag, but on a Spark Connet SparkSession -- 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]
