-
Notifications
You must be signed in to change notification settings - Fork 14.8k
KAFKA-19831: Improved error handling in DefaultStateUpdater. #20767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nikita-Shupletsov
wants to merge
5
commits into
apache:trunk
Choose a base branch
from
Nikita-Shupletsov:KAFKA-19831
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+362
−37
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d549788
KAFKA-19831: Improved error handling in DefaultStateUpdater.
Nikita-Shupletsov 80d297f
Apply suggestion from @mjsax
Nikita-Shupletsov cf9cf54
Merge branch 'apache:trunk' into KAFKA-19831
Nikita-Shupletsov f98c083
Merge branch 'trunk' into KAFKA-19831
Nikita-Shupletsov e6de6cd
Added comments to the test to make it easier to understand what's bei…
Nikita-Shupletsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
...rc/test/java/org/apache/kafka/streams/integration/StateUpdaterFailureIntegrationTest.java
This file contains hidden or 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,149 @@ | ||
| /* | ||
| * 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.streams.integration; | ||
|
|
||
| import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
| import org.apache.kafka.common.serialization.Serdes; | ||
| import org.apache.kafka.common.utils.MockTime; | ||
| import org.apache.kafka.streams.KafkaStreams; | ||
| import org.apache.kafka.streams.StreamsConfig; | ||
| import org.apache.kafka.streams.TopologyWrapper; | ||
| import org.apache.kafka.streams.errors.ProcessorStateException; | ||
| import org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster; | ||
| import org.apache.kafka.streams.processor.StateStore; | ||
| import org.apache.kafka.streams.processor.StateStoreContext; | ||
| import org.apache.kafka.streams.state.KeyValueStore; | ||
| import org.apache.kafka.streams.state.StoreBuilder; | ||
| import org.apache.kafka.streams.state.internals.AbstractStoreBuilder; | ||
| import org.apache.kafka.test.MockApiProcessorSupplier; | ||
| import org.apache.kafka.test.MockKeyValueStore; | ||
| import org.apache.kafka.test.TestUtils; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestInfo; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Duration; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static org.apache.kafka.streams.utils.TestUtils.safeUniqueTestName; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class StateUpdaterFailureIntegrationTest { | ||
|
|
||
| private static final int NUM_BROKERS = 1; | ||
| protected static final String INPUT_TOPIC_NAME = "input-topic"; | ||
| private static final int NUM_PARTITIONS = 6; | ||
|
|
||
| private final EmbeddedKafkaCluster cluster = new EmbeddedKafkaCluster(NUM_BROKERS); | ||
|
|
||
| private Properties streamsConfiguration; | ||
| private final MockTime mockTime = cluster.time; | ||
| private KafkaStreams streams; | ||
|
|
||
| @BeforeEach | ||
| public void before(final TestInfo testInfo) throws InterruptedException, IOException { | ||
| cluster.start(); | ||
| cluster.createTopic(INPUT_TOPIC_NAME, NUM_PARTITIONS, 1); | ||
| streamsConfiguration = new Properties(); | ||
| final String safeTestName = safeUniqueTestName(testInfo); | ||
| streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "app-" + safeTestName); | ||
| streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, cluster.bootstrapServers()); | ||
| streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
| streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getPath()); | ||
| streamsConfiguration.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, 0); | ||
| streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 100L); | ||
| streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.IntegerSerde.class); | ||
| streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class); | ||
| streamsConfiguration.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 2); | ||
|
|
||
| } | ||
|
|
||
| @AfterEach | ||
| public void after() { | ||
| cluster.stop(); | ||
| if (streams != null) { | ||
| streams.close(Duration.ofSeconds(30)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The conditions that we need to meet: | ||
| * <p><ul> | ||
| * <li>We have an unhandled task in {@link org.apache.kafka.streams.processor.internals.DefaultStateUpdater}</li> | ||
| * <li>StreamThread is not running, so {@link org.apache.kafka.streams.processor.internals.TaskManager#handleExceptionsFromStateUpdater} is not called anymore</li> | ||
| * <li>The task throws exception in {@link org.apache.kafka.streams.processor.internals.Task#maybeCheckpoint(boolean)} while being processed by {@code DefaultStateUpdater}</li> | ||
| * <li>{@link org.apache.kafka.streams.processor.internals.TaskManager#shutdownStateUpdater} tries to clean up all tasks that are left in the {@code DefaultStateUpdater}</li> | ||
| * </ul><p> | ||
| * If all conditions are met, {@code TaskManager} needs to be able to handle the failed task from the {@code DefaultStateUpdater} correctly and not hang. | ||
| */ | ||
| @Test | ||
| public void correctlyHandleFlushErrorsDuringRebalance() throws Exception { | ||
| final AtomicInteger numberOfStoreInits = new AtomicInteger(); | ||
| final AtomicReference<KafkaStreams.State> currentState = new AtomicReference<>(); | ||
|
|
||
| final StoreBuilder<KeyValueStore<Object, Object>> storeBuilder = new AbstractStoreBuilder<>("testStateStore", Serdes.Integer(), Serdes.ByteArray(), new MockTime()) { | ||
|
|
||
| @Override | ||
| public KeyValueStore<Object, Object> build() { | ||
| return new MockKeyValueStore(name, false) { | ||
|
|
||
| @Override | ||
| public void init(final StateStoreContext stateStoreContext, final StateStore root) { | ||
| super.init(stateStoreContext, root); | ||
| numberOfStoreInits.incrementAndGet(); | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() { | ||
| // we want to throw the ProcessorStateException here only when the rebalance finished(we reassigned the 3 tasks from the removed thread to the existing thread) | ||
| // we use waitForCondition to wait until the current state is PENDING_SHUTDOWN to make sure the Stream Thread will not handle the exception and we can get to in TaskManager#shutdownStateUpdater | ||
| if (numberOfStoreInits.get() == 9) { | ||
| try { | ||
| TestUtils.waitForCondition(() -> currentState.get() == KafkaStreams.State.PENDING_SHUTDOWN, "Streams never reached PENDING_SHUTDOWN state"); | ||
| } catch (final InterruptedException e) { | ||
mjsax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new RuntimeException(e); | ||
| } | ||
| throw new ProcessorStateException("flush"); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| final TopologyWrapper topology = new TopologyWrapper(); | ||
| topology.addSource("ingest", INPUT_TOPIC_NAME); | ||
| topology.addProcessor("my-processor", new MockApiProcessorSupplier<>(), "ingest"); | ||
| topology.addStateStore(storeBuilder, "my-processor"); | ||
|
|
||
| streams = new KafkaStreams(topology, streamsConfiguration); | ||
| streams.setStateListener((newState, oldState) -> currentState.set(newState)); | ||
| streams.start(); | ||
|
|
||
| TestUtils.waitForCondition(() -> currentState.get() == KafkaStreams.State.RUNNING, "Streams never reached RUNNING state"); | ||
|
|
||
| streams.removeStreamThread(); | ||
|
|
||
| // Before shutting down, we want the tasks to be reassigned | ||
| TestUtils.waitForCondition(() -> numberOfStoreInits.get() == 9, "Streams never reinitialized the store enough times"); | ||
|
|
||
| assertTrue(streams.close(Duration.ofSeconds(60))); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I understand this condition. Can you elaborate?
Also: if we fail here, how do we ensure that the test fails? This would be executed on the background
StreamsThreadnot the actual "main" thread running the test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to reproduce the issue, we need to fail during a shutdown.
so what the test does:
in other words: the problem I am trying to fix here reproduces if: we are in rebalance, we have some unhandled tasks in the task updater which can throw an exception, which can kill the thread, and we are trying to close the app.
in this case the stream thread will not do its thing, which in turn will not call TaskManager, which will not call
stateUpdater.drainExceptionsAndFailedTasks, which will not remove all failed tasks from the lists. which in turn will causestateUpdater.tasksto return the failed tasks. and TaskManager will try to delete them.but because the thread is dead, the delete will never succeed, as the dead thread will never complete the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a comment to the test to explain what exactly we are trying to achieve here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that we do want to throw here to trigger an error on the state-updater thread. But it's not clear to me, why we would need to block/wait here until we go to "pending shutdown"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what needs to happen to reproduce the issue:
the stream thread is already down(so, we want this condition: https://github.com/apache/kafka/blob/trunk/streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java#L933 to be false), we also want to have a task in the state updater. and this task needs to throw an exception.
maybe there is an easier way to achieve these conditions, but that's what I was able to come up with
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean before the state-updater crashes? Interesting. Not clear to me why though? Can you explain further?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes. when the stream thread is running it calls StreamThread#checkStateUpdater, which, in turn, calls TaskManager, which calls StateUpdater#drainExceptionsAndFailedTasks.
so it will delete the failed task from the list of tasks(hence StateUpdater#tasks will not return them anymore) and trigger shutdown.
and when we get to the shutdown, we will not have anything in the state updater, so we will not try to remove anything from in the shutdownStateUpdater method, hence we will not get stuck.
it technically can be slightly different: we have some tasks in the state updater, one of them fails and kills the thread, which will be picked up by the stream thread. but if after that we some how add more tasks to the state updater, we will get the same issue.
But I couldn't figure out if it's even possible to add new tasks to the state updater when the thread is shitting down, as it happens only during rebalance(but here I may be wrong)