wangzhigang1999 commented on code in PR #7150: URL: https://github.com/apache/kyuubi/pull/7150#discussion_r2591742635
########## extensions/spark/kyuubi-spark-shutdown-watchdog/src/main/java/org/apache/spark/kyuubi/shutdown/watchdog/ShutdownWatchdog.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.kyuubi.shutdown.watchdog; + +import com.google.common.annotations.VisibleForTesting; +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.IntConsumer; +import org.apache.spark.SparkConf; +import org.apache.spark.util.SparkExitCode; +import org.slf4j.Logger; + +/** + * Timer-backed watchdog that forces the Spark driver to exit if a graceful shutdown stalls. + * + * <p>Implementation is deliberately pure Java so the plugin has no Scala runtime dependency. + */ +public final class ShutdownWatchdog { + + // Align with Spark's standard exit codes for consistency with Spark driver failures. + static final int EXIT_CODE_FORCED_TERMINATION = SparkExitCode.UNCAUGHT_EXCEPTION(); + static final int EXIT_CODE_WATCHDOG_FAILURE = SparkExitCode.UNCAUGHT_EXCEPTION_TWICE(); + + private static final AtomicReference<Thread> WATCHDOG_THREAD_REF = new AtomicReference<>(); + private static volatile IntConsumer exitFn = System::exit; + + private ShutdownWatchdog() {} + + static void setExitFn(IntConsumer fn) { + exitFn = fn != null ? fn : System::exit; + } + + static void startIfNeeded(SparkConf sparkConf, Logger logger) { + Objects.requireNonNull(sparkConf, "sparkConf"); + Objects.requireNonNull(logger, "logger"); + + if (!SparkShutdownWatchdogConf.isEnabled(sparkConf)) { + logger.info( + "Shutdown Watchdog is disabled via {}=false.", + SparkShutdownWatchdogConf.SHUTDOWN_WATCHDOG_ENABLED_KEY); + return; + } + + final long timeoutMillis = SparkShutdownWatchdogConf.getTimeoutMillis(sparkConf); + if (timeoutMillis <= 0L) { + logger.info("Shutdown Watchdog is disabled because timeout <= 0."); + return; + } + + Thread existing = WATCHDOG_THREAD_REF.get(); + if (existing != null && existing.isAlive()) { + logger.warn("Shutdown Watchdog is already running, ignoring duplicate start request."); + return; + } + + final Thread watchdogThread = + new Thread(() -> runWatchdogLoop(timeoutMillis, logger), "spark-shutdown-watchdog"); Review Comment: fixed -- 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]
