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

[GitHub] [kafka] jsancio opened a new pull request, #13227: KAFKA-14693; Kafka node should halt instead of exit

jsancio opened a new pull request, #13227:
URL: https://github.com/apache/kafka/pull/13227

   Extend the implementation of ProcessTerminatingFaultHandler to support calling either Exit.halt or Exit.exit. Change the fault handler used by the Controller thread and the KRaft thread to use a halting fault handler.
   
   Those two threads cannot call Exit.exit because Runtime.exit joins on the default shutdown hook thread. The shutdown hook thread joins on the controller and kraft thread terminating. This causes a deadlock.
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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


[GitHub] [kafka] jsancio merged pull request #13227: KAFKA-14693; Kafka node should halt instead of exit

Posted by "jsancio (via GitHub)" <gi...@apache.org>.
jsancio merged PR #13227:
URL: https://github.com/apache/kafka/pull/13227


-- 
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


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

Posted by "cmccabe (via GitHub)" <gi...@apache.org>.
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


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

Posted by "hachikuji (via GitHub)" <gi...@apache.org>.
hachikuji commented on code in PR #13227:
URL: https://github.com/apache/kafka/pull/13227#discussion_r1105019444


##########
server-common/src/main/java/org/apache/kafka/server/fault/ProcessTerminatingFaultHandler.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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 java.util.Objects;
+import org.apache.kafka.common.utils.Exit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 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.shouldHalt = shouldHalt;
+        this.action = action;
+    }
+
+    @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;
+    }
+
+    public static final class Builder {
+        private boolean shouldHalt = false;

Review Comment:
   Kind of feels like `shouldHalt` should be true by default. All the usages outside of tests are setting it. Maybe we could even call it `ProcessHaltingFaultHandler` and get rid of the option.



-- 
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


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

Posted by "jsancio (via GitHub)" <gi...@apache.org>.
jsancio commented on code in PR #13227:
URL: https://github.com/apache/kafka/pull/13227#discussion_r1104870347


##########
core/src/main/scala/kafka/server/SharedServer.scala:
##########
@@ -60,7 +60,10 @@ class StandardFaultHandlerFactory extends FaultHandlerFactory {
     action: Runnable
   ): FaultHandler = {
     if (fatal) {
-      new ProcessExitingFaultHandler(action)
+      new ProcessTerminatingFaultHandler.Builder()
+        .setHalt(true)

Review Comment:
   I agree. I went with `setShouldHalt`.



-- 
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


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

Posted by "jsancio (via GitHub)" <gi...@apache.org>.
jsancio commented on code in PR #13227:
URL: https://github.com/apache/kafka/pull/13227#discussion_r1103137743


##########
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:
   Sounds good. I added a builder pattern.



-- 
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


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

Posted by "hachikuji (via GitHub)" <gi...@apache.org>.
hachikuji commented on code in PR #13227:
URL: https://github.com/apache/kafka/pull/13227#discussion_r1103438546


##########
core/src/main/scala/kafka/server/SharedServer.scala:
##########
@@ -60,7 +60,10 @@ class StandardFaultHandlerFactory extends FaultHandlerFactory {
     action: Runnable
   ): FaultHandler = {
     if (fatal) {
-      new ProcessExitingFaultHandler(action)
+      new ProcessTerminatingFaultHandler.Builder()
+        .setHalt(true)

Review Comment:
   nit: `setHalt` reads a bit awkward to me. How about `shouldHalt` or `haltOnTermination`?



-- 
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


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

Posted by "jsancio (via GitHub)" <gi...@apache.org>.
jsancio commented on code in PR #13227:
URL: https://github.com/apache/kafka/pull/13227#discussion_r1105034231


##########
server-common/src/main/java/org/apache/kafka/server/fault/ProcessTerminatingFaultHandler.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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 java.util.Objects;
+import org.apache.kafka.common.utils.Exit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 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.shouldHalt = shouldHalt;
+        this.action = action;
+    }
+
+    @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;
+    }
+
+    public static final class Builder {
+        private boolean shouldHalt = false;

Review Comment:
   I agree. I'll change the default to true. I want to keep the option to use exit in the future.
   
   We can fix the KRaft IO thread if we don't wait on the `exit` call. We can do this if we call `exit` from a daemon thread and let the KRaft IO thread terminate immediately.
   
   The KRaft controller thread is a little bit more complicated since fatal exception are handled in a few places in the code.



-- 
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


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

Posted by "hachikuji (via GitHub)" <gi...@apache.org>.
hachikuji commented on code in PR #13227:
URL: https://github.com/apache/kafka/pull/13227#discussion_r1103438546


##########
core/src/main/scala/kafka/server/SharedServer.scala:
##########
@@ -60,7 +60,10 @@ class StandardFaultHandlerFactory extends FaultHandlerFactory {
     action: Runnable
   ): FaultHandler = {
     if (fatal) {
-      new ProcessExitingFaultHandler(action)
+      new ProcessTerminatingFaultHandler.Builder()
+        .setHalt(true)

Review Comment:
   nit: `setHalt` reads a bit awkward to me. How about `shouldHalt` or `setHaltOnTerminate`?



-- 
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