juliuszsompolski commented on code in PR #41315: URL: https://github.com/apache/spark/pull/41315#discussion_r1261443353
########## connector/connect/server/src/main/scala/org/apache/spark/sql/connect/service/ExecutionHolder.scala: ########## @@ -0,0 +1,196 @@ +/* + * 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.service + +import scala.util.control.NonFatal + +import com.google.protobuf.Message +import io.grpc.stub.StreamObserver +import org.apache.commons.lang3.StringUtils + +import org.apache.spark.SparkSQLException +import org.apache.spark.connect.proto +import org.apache.spark.connect.proto.{ExecutePlanRequest, ExecutePlanResponse} +import org.apache.spark.internal.Logging +import org.apache.spark.sql.connect.artifact.SparkConnectArtifactManager +import org.apache.spark.sql.connect.common.ProtoUtils +import org.apache.spark.sql.connect.execution.{ExecutePlanResponseObserver, SparkConnectPlanExecution} +import org.apache.spark.sql.connect.planner.SparkConnectPlanner +import org.apache.spark.util.Utils + +/** + * Object used to hold the Spark Connect execution state, and perform + */ +case class ExecutionHolder(operationId: String, sessionHolder: SessionHolder) extends Logging { + + val jobGroupId = + s"User_${sessionHolder.userId}_Session_${sessionHolder.sessionId}_Request_${operationId}" + + val session = sessionHolder.session + + var executePlanRequest: Option[proto.ExecutePlanRequest] = None + + var executePlanResponseObserver: Option[ExecutePlanResponseObserver] = None + + private var executionThread: Thread = null + + private var executionError: Option[Throwable] = None + + private var interrupted: Boolean = false + + def run( + request: proto.ExecutePlanRequest, + responseObserver: StreamObserver[ExecutePlanResponse]): Unit = { + // Set the state of what needs to be run. + this.executePlanRequest = Some(request) + this.executePlanResponseObserver = Some(new ExecutePlanResponseObserver(responseObserver)) + // And start the execution. + startExecute() + } + + protected def startExecute(): Unit = { + // synchronized in case of interrupt while starting. + synchronized { + // 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. + this.executionThread = new Thread() { + override def run(): Unit = { + execute() + } + } + } + + try { + // Start execution thread.. + this.executionThread.start() + // ... and wait for execution thread to finish. + // TODO: Detach execution from RPC request. Then this can return early, and results + // are served to the client via additional RPCs from ExecutePlanResponseObserver. + this.executionThread.join() + + executionError.foreach { error => + logDebug(s"executionError: ${error}") + throw error + } + } catch { + case NonFatal(e) => + // In case of exception happening on the handler thread, interrupt the underlying execution. + this.interrupt() Review Comment: no, it was to just in case clean up execution. this was refactored sicne. -- 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]
