ueshin commented on code in PR #43340: URL: https://github.com/apache/spark/pull/43340#discussion_r1355948844
########## sql/core/src/main/scala/org/apache/spark/sql/execution/python/PythonPlannerRunner.scala: ########## @@ -0,0 +1,177 @@ +/* + * 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.python + +import java.io.{BufferedInputStream, BufferedOutputStream, DataInputStream, DataOutputStream, EOFException, InputStream} +import java.nio.ByteBuffer +import java.nio.channels.SelectionKey +import java.util.HashMap + +import scala.jdk.CollectionConverters._ + +import net.razorvine.pickle.Pickler + +import org.apache.spark.{JobArtifactSet, SparkEnv, SparkException} +import org.apache.spark.api.python.{PythonFunction, PythonWorker, PythonWorkerUtils, SpecialLengths} +import org.apache.spark.internal.config.BUFFER_SIZE +import org.apache.spark.internal.config.Python._ +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.util.DirectByteBufferOutputStream + +/** + * A helper class to run Python functions in Spark driver. + */ +abstract class PythonPlannerRunner[T](func: PythonFunction) { + + protected val workerModule: String + + protected def writeToPython(dataOut: DataOutputStream, pickler: Pickler): Unit + + protected def receiveFromPython(dataIn: DataInputStream): T + + def runInPython(): T = { + val env = SparkEnv.get + val bufferSize: Int = env.conf.get(BUFFER_SIZE) + val authSocketTimeout = env.conf.get(PYTHON_AUTH_SOCKET_TIMEOUT) + val reuseWorker = env.conf.get(PYTHON_WORKER_REUSE) + val localdir = env.blockManager.diskBlockManager.localDirs.map(f => f.getPath()).mkString(",") + val simplifiedTraceback: Boolean = SQLConf.get.pysparkSimplifiedTraceback + val workerMemoryMb = SQLConf.get.pythonUDTFAnalyzerMemory + + val jobArtifactUUID = JobArtifactSet.getCurrentJobArtifactState.map(_.uuid) + + val envVars = new HashMap[String, String](func.envVars) + val pythonExec = func.pythonExec + val pythonVer = func.pythonVer + val pythonIncludes = func.pythonIncludes.asScala.toSet + val broadcastVars = func.broadcastVars.asScala.toSeq + val maybeAccumulator = Option(func.accumulator).map(_.copyAndReset()) + + envVars.put("SPARK_LOCAL_DIRS", localdir) + if (reuseWorker) { + envVars.put("SPARK_REUSE_WORKER", "1") + } + if (simplifiedTraceback) { + envVars.put("SPARK_SIMPLIFIED_TRACEBACK", "1") + } + workerMemoryMb.foreach { memoryMb => + envVars.put("PYSPARK_UDTF_ANALYZER_MEMORY_MB", memoryMb.toString) + } Review Comment: This part seems to be UDTF analyzer specific so not reusable. should use a more generic name, or add another abstract method to additionally setup the `envVars`? -- 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]
