forked from apache/kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAFKA-14693; Kafka node should halt instead of exit (apache#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 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. Reviewers: Colin Patrick McCabe <[email protected]>, Jason Gustafson <[email protected]>
- Loading branch information
Showing
7 changed files
with
192 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
server-common/src/main/java/org/apache/kafka/server/fault/ProcessExitingFaultHandler.java
This file was deleted.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
...er-common/src/main/java/org/apache/kafka/server/fault/ProcessTerminatingFaultHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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 = true; | ||
private Runnable action = () -> { }; | ||
|
||
/** | ||
* Set if halt or exit should be used. | ||
* | ||
* When {@code value} is {@code false} {@code Exit.exit} is called, otherwise {@code Exit.halt} is | ||
* called. The default value is {@code true}. | ||
* | ||
* The default implementation of {@code Exit.exit} calls {@code Runtime.exit} which | ||
* blocks on all of the shutdown hooks executing. | ||
* | ||
* The default implementation of {@code Exit.halt} calls {@code Runtime.halt} which | ||
* forcibly terminates the JVM. | ||
*/ | ||
public Builder setShouldHalt(boolean value) { | ||
shouldHalt = value; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the {@code Runnable} to run when handling a fault. | ||
*/ | ||
public Builder setAction(Runnable action) { | ||
this.action = Objects.requireNonNull(action); | ||
return this; | ||
} | ||
|
||
public ProcessTerminatingFaultHandler build() { | ||
return new ProcessTerminatingFaultHandler(shouldHalt, action); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...ommon/src/test/java/org/apache/kafka/server/fault/ProcessTerminatingFaultHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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.concurrent.atomic.AtomicBoolean; | ||
import org.apache.kafka.common.utils.Exit; | ||
import org.apache.kafka.common.utils.Exit.Procedure; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
final public class ProcessTerminatingFaultHandlerTest { | ||
private static Procedure terminatingProcedure(AtomicBoolean called) { | ||
return (statusCode, message) -> { | ||
assertEquals(1, statusCode); | ||
assertNull(message); | ||
called.set(true); | ||
}; | ||
} | ||
|
||
@Test | ||
public void testExitIsCalled() { | ||
AtomicBoolean exitCalled = new AtomicBoolean(false); | ||
Exit.setExitProcedure(terminatingProcedure(exitCalled)); | ||
|
||
AtomicBoolean actionCalled = new AtomicBoolean(false); | ||
Runnable action = () -> { | ||
assertFalse(exitCalled.get()); | ||
actionCalled.set(true); | ||
}; | ||
|
||
try { | ||
new ProcessTerminatingFaultHandler.Builder() | ||
.setShouldHalt(false) | ||
.setAction(action) | ||
.build() | ||
.handleFault("", null); | ||
} finally { | ||
Exit.resetExitProcedure(); | ||
} | ||
|
||
assertTrue(exitCalled.get()); | ||
assertTrue(actionCalled.get()); | ||
} | ||
|
||
@Test | ||
public void testHaltIsCalled() { | ||
AtomicBoolean haltCalled = new AtomicBoolean(false); | ||
Exit.setHaltProcedure(terminatingProcedure(haltCalled)); | ||
|
||
AtomicBoolean actionCalled = new AtomicBoolean(false); | ||
Runnable action = () -> { | ||
assertFalse(haltCalled.get()); | ||
actionCalled.set(true); | ||
}; | ||
|
||
try { | ||
new ProcessTerminatingFaultHandler.Builder() | ||
.setAction(action) | ||
.build() | ||
.handleFault("", null); | ||
} finally { | ||
Exit.resetHaltProcedure(); | ||
} | ||
|
||
assertTrue(haltCalled.get()); | ||
assertTrue(actionCalled.get()); | ||
} | ||
} |