Skip to content

Commit

Permalink
KAFKA-14693; Kafka node should halt instead of exit (apache#13227)
Browse files Browse the repository at this point in the history
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
jsancio authored Feb 14, 2023
1 parent 9584b48 commit 10164a6
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 63 deletions.
2 changes: 2 additions & 0 deletions clients/src/main/java/org/apache/kafka/common/utils/Exit.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
*/
public class Exit {

@FunctionalInterface
public interface Procedure {
void execute(int statusCode, String message);
}

@FunctionalInterface
public interface ShutdownHookAdder {
void addShutdownHook(String name, Runnable runnable);
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/kafka/server/KafkaServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import org.apache.kafka.raft.RaftConfig
import org.apache.kafka.server.authorizer.Authorizer
import org.apache.kafka.server.common.{ApiMessageAndVersion, MetadataVersion}
import org.apache.kafka.server.common.MetadataVersion._
import org.apache.kafka.server.fault.ProcessExitingFaultHandler
import org.apache.kafka.server.fault.ProcessTerminatingFaultHandler
import org.apache.kafka.server.metrics.KafkaYammerMetrics
import org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig
import org.apache.kafka.server.util.KafkaScheduler
Expand Down Expand Up @@ -396,7 +396,7 @@ class KafkaServer(
metrics,
threadNamePrefix,
controllerQuorumVotersFuture,
fatalFaultHandler = new ProcessExitingFaultHandler()
fatalFaultHandler = new ProcessTerminatingFaultHandler.Builder().build()
)
val controllerNodes = RaftConfig.voterConnectionsToNodes(controllerQuorumVotersFuture.get()).asScala
val quorumControllerNodeProvider = RaftControllerNodeProvider(raftManager, config, controllerNodes)
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/scala/kafka/server/SharedServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import org.apache.kafka.image.publisher.{SnapshotEmitter, SnapshotGenerator}
import org.apache.kafka.metadata.MetadataRecordSerde
import org.apache.kafka.raft.RaftConfig.AddressSpec
import org.apache.kafka.server.common.ApiMessageAndVersion
import org.apache.kafka.server.fault.{FaultHandler, LoggingFaultHandler, ProcessExitingFaultHandler}
import org.apache.kafka.server.fault.{FaultHandler, LoggingFaultHandler, ProcessTerminatingFaultHandler}
import org.apache.kafka.server.metrics.KafkaYammerMetrics

import java.util
Expand Down Expand Up @@ -60,7 +60,9 @@ class StandardFaultHandlerFactory extends FaultHandlerFactory {
action: Runnable
): FaultHandler = {
if (fatal) {
new ProcessExitingFaultHandler(action)
new ProcessTerminatingFaultHandler.Builder()
.setAction(action)
.build()
} else {
new LoggingFaultHandler(name, action)
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/kafka/tools/TestRaftServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import org.apache.kafka.common.{TopicPartition, Uuid, protocol}
import org.apache.kafka.raft.errors.NotLeaderException
import org.apache.kafka.raft.{Batch, BatchReader, LeaderAndEpoch, RaftClient, RaftConfig}
import org.apache.kafka.server.common.serialization.RecordSerde
import org.apache.kafka.server.fault.ProcessExitingFaultHandler
import org.apache.kafka.server.fault.ProcessTerminatingFaultHandler
import org.apache.kafka.server.util.{CommandDefaultOptions, CommandLineUtils}
import org.apache.kafka.snapshot.SnapshotReader

Expand Down Expand Up @@ -92,7 +92,7 @@ class TestRaftServer(
metrics,
Some(threadNamePrefix),
CompletableFuture.completedFuture(RaftConfig.parseVoterConnections(config.quorumVoters)),
new ProcessExitingFaultHandler()
new ProcessTerminatingFaultHandler.Builder().build()
)

workloadGenerator = new RaftWorkloadGenerator(
Expand Down

This file was deleted.

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);
}
}
}
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());
}
}

0 comments on commit 10164a6

Please sign in to comment.