You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "cmccabe (via GitHub)" <gi...@apache.org> on 2023/02/10 18:12:25 UTC

[GitHub] [kafka] cmccabe commented on a diff in pull request #13227: KAFKA-14693; Kafka node should halt instead of exit

cmccabe commented on code in PR #13227:
URL: https://github.com/apache/kafka/pull/13227#discussion_r1103072726


##########
server-common/src/main/java/org/apache/kafka/server/fault/ProcessTerminatingFaultHandler.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.kafka.server.fault;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.kafka.common.utils.Exit;
+
+
+/**
+ * This is a fault handler which terminates the JVM process.
+ */
+final public class ProcessTerminatingFaultHandler implements FaultHandler {
+    private static final Logger log = LoggerFactory.getLogger(ProcessTerminatingFaultHandler.class);
+
+    private final Runnable action;
+    private final boolean shouldHalt;
+
+    private ProcessTerminatingFaultHandler(boolean shouldHalt, Runnable action) {
+        this.action = action;
+        this.shouldHalt = shouldHalt;
+    }
+
+    @Override
+    public RuntimeException handleFault(String failureMessage, Throwable cause) {
+        if (cause == null) {
+            log.error("Encountered fatal fault: {}", failureMessage);
+        } else {
+            log.error("Encountered fatal fault: {}", failureMessage, cause);
+        }
+
+        try {
+            action.run();
+        } catch (Throwable e) {
+            log.error("Failed to run terminating action.", e);
+        }
+
+        int statusCode = 1;
+        if (shouldHalt) {
+            Exit.halt(statusCode);
+        } else {
+            Exit.exit(statusCode);
+        }
+
+        return null;
+    }
+
+    /**
+     * Same as {@code exitingWithAction} with a no-op action.
+     */
+    public static FaultHandler exiting() {
+        return exitingWithAction(() -> { });
+    }
+
+    /**
+     * Handle faults by running an action and calling {@code Exit.exit}.
+     *
+     * The default implementation of {@code Exit.exit} calls {@code Runtime.exit} which
+     * waits on all of the shutdown hooks executing.
+     */
+    public static FaultHandler exitingWithAction(Runnable action) {

Review Comment:
   This feels awkward to me. If it's creating a new object "create", "build", etc. should be somewhere in the name. Like `createExitingFaultHandlerWithAction`. But that's a long name. Plus now we have a combinatorial explosion of builder functions. createWithXAndY, createWithXAndYAndZ. Not good.
   
   If you want to make this nicer than just exposing the constructor then just make a builder. `ProcessTerminatingFaultHandler.Builder().setHalt(true).build()` is easy to read and makes it easy to add new arguments if we want to.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org