rich7420 commented on code in PR #7181: URL: https://github.com/apache/datafusion-comet/pull/7181#discussion_r4708391270
########## spark/src/main/scala/org/apache/comet/PythonUdfMonitor.scala: ########## @@ -0,0 +1,111 @@ +/* + * 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.comet + +import java.util.concurrent.{ConcurrentHashMap, TimeUnit} +import java.util.concurrent.atomic.{AtomicInteger, AtomicLong} + +import org.apache.spark.{SparkEnv, TaskContext} +import org.apache.spark.internal.Logging + +/** + * Stops the native Python UDF calls of killed tasks. Spark's Python runner has a `MonitorThread` + * that destroys the worker of a killed task. A native UDF runs in the embedded interpreter on the + * task thread instead, where neither a Java interrupt nor a stopped input reaches it. So one + * daemon thread per task polls its kill state. Once the task is killed, its later calls fail + * without entering Python. A call still running `spark.python.task.killTimeout` after the kill + * gets an exception raised in it, once so that its cleanup can run, unless that is disabled. + */ +object PythonUdfMonitor extends Logging { Review Comment: Could we add this file to `FILTERS["pyarrow_udf"]` in `compute-changes.py`, so later PRs touching only this file run the kill tests? ########## spark/src/test/spark-4.1+/org/apache/spark/sql/comet/CometArrowPythonUdfSuite.scala: ########## @@ -583,6 +590,105 @@ class CometArrowPythonUdfSuite extends CometTestBase { } } + /** + * Runs `job` in a job group, waits until `started` exists, cancels the group, and checks that + * the task ends as killed. + */ + private def assertCancelKillsTask(started: File)(job: => Any): Unit = { + val killed = new CountDownLatch(1) + val listener = new SparkListener { + override def onTaskEnd(taskEnd: SparkListenerTaskEnd): Unit = taskEnd.reason match { + case _: TaskKilled => killed.countDown() + case _ => + } + } + val group = "comet-arrow-udf-kill" + spark.sparkContext.addSparkListener(listener) + try { + val running = Future { + spark.sparkContext.setJobGroup(group, "native Arrow UDF loop", interruptOnCancel = true) + // The job group lives in this pooled thread's local properties; clear it for later work. + try job + finally spark.sparkContext.clearJobGroup() + }(ExecutionContext.global) + val deadline = 60.seconds.fromNow + while (!started.exists()) { + assert(deadline.hasTimeLeft(), "the UDF did not start") + Thread.sleep(50) + } + spark.sparkContext.cancelJobGroup(group) + // The monitor waits spark.python.task.killTimeout, 2 seconds, before interrupting. + assert(killed.await(30, TimeUnit.SECONDS), "the task running the UDF was not killed") Review Comment: Could we also assert the task ends at least `killTimeout` after cancellation? Both kill tests still pass for me when the monitor interrupts immediately. -- 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]
