-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-8602: Fix bug in stand-by task creation #7008
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
Changes from 1 commit
fd10fad
c6aa91f
5b1739b
256f40c
c3ffcc0
d27aef7
6bae8ac
5ccaf63
55ab731
06749bf
d52c922
ae3290c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * 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.streams.KafkaStreams; | ||
| import org.apache.kafka.streams.KafkaStreams.State; | ||
| import org.apache.kafka.streams.KeyValue; | ||
| import org.apache.kafka.streams.StreamsBuilder; | ||
| import org.apache.kafka.streams.StreamsConfig; | ||
| import org.apache.kafka.streams.Topology; | ||
| import org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster; | ||
| import org.apache.kafka.streams.kstream.Consumed; | ||
| import org.apache.kafka.streams.kstream.Transformer; | ||
| import org.apache.kafka.streams.processor.ProcessorContext; | ||
| import org.apache.kafka.streams.state.KeyValueStore; | ||
| import org.apache.kafka.streams.state.StoreBuilder; | ||
| import org.apache.kafka.streams.state.Stores; | ||
| import org.apache.kafka.test.IntegrationTest; | ||
| import org.apache.kafka.test.TestUtils; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
|
|
||
| import java.util.Properties; | ||
|
|
||
| @Category({IntegrationTest.class}) | ||
| public class StandByTaskCreationIntegrationTest { | ||
|
|
||
| private static final int NUM_BROKERS = 1; | ||
|
|
||
| @ClassRule | ||
| public static final EmbeddedKafkaCluster CLUSTER = new EmbeddedKafkaCluster(NUM_BROKERS); | ||
|
|
||
| private static final String INPUT_TOPIC = "input-topic"; | ||
| private static final String OUTPUT_TOPIC = "output-topic"; | ||
|
|
||
| @BeforeClass | ||
| public static void createTopics() throws InterruptedException { | ||
| CLUSTER.createTopic(INPUT_TOPIC, 2, 1); | ||
| CLUSTER.createTopic(OUTPUT_TOPIC, 2, 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need an output topic? We never use it to consume a result?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| } | ||
|
|
||
| private Properties streamsConfiguration() { | ||
| final String applicationId = "testApp"; | ||
| final Properties streamsConfiguration = new Properties(); | ||
| streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId); | ||
| streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers()); | ||
| streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory(applicationId).getPath()); | ||
| streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.Integer().getClass()); | ||
| streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.Integer().getClass()); | ||
| streamsConfiguration.put(StreamsConfig.NUM_STANDBY_REPLICAS_CONFIG, 1); | ||
| streamsConfiguration.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: no need to overwrite;
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| return streamsConfiguration; | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldNotCreateAnyStandByTasksForStateStoreWithLoggingDisabled() throws InterruptedException { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: simplify
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| final StreamsBuilder builder = new StreamsBuilder(); | ||
|
|
||
| final String stateStoreName = "myTransformState"; | ||
| final StoreBuilder<KeyValueStore<Integer, Integer>> keyValueStoreBuilder = | ||
| Stores.keyValueStoreBuilder(Stores.persistentKeyValueStore(stateStoreName), | ||
| Serdes.Integer(), | ||
| Serdes.Integer()).withLoggingDisabled(); | ||
| builder.addStateStore(keyValueStoreBuilder); | ||
|
|
||
| builder.stream(INPUT_TOPIC, Consumed.with(Serdes.Integer(), Serdes.Integer())) | ||
| .transform(() -> new Transformer<Integer, Integer, KeyValue<Integer, Integer>>() { | ||
| private KeyValueStore<Integer, Integer> state; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public void init(final ProcessorContext context) { | ||
| state = (KeyValueStore<Integer, Integer>) context.getStateStore(stateStoreName); | ||
| } | ||
|
|
||
| @Override | ||
| public KeyValue<Integer, Integer> transform(final Integer key, final Integer value) { | ||
| state.putIfAbsent(key, value); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We we actually need to use the store? Seems we can remove this code?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| final KeyValue<Integer, Integer> result = new KeyValue<>(key, value); | ||
| return result; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can just return
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| } | ||
|
|
||
| @Override | ||
| public void close() {} | ||
| }, stateStoreName) | ||
| .to(OUTPUT_TOPIC); | ||
|
|
||
| final Topology topology = builder.build(); | ||
| final KafkaStreams client1 = new KafkaStreams(topology, streamsConfiguration()); | ||
| final KafkaStreams client2 = new KafkaStreams(topology, streamsConfiguration()); | ||
|
|
||
| final boolean[] client1IsOk = {false}; // has to be a final array, otherwise flag cannot be modified in lambda | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove comment (we would make it final in any case anyway). I am wondering if it should be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the main point of the comment is that we need to use an array of booleans instead of a plain boolean variable, because otherwise I cannot declare it final and modify it within the lambda. Since I used a volatile variable now (good point, btw), the array is not needed anymore. Hence, I removed the comment. |
||
| client1.setStateListener((newState, oldState) -> { | ||
| if (newState == State.RUNNING && | ||
| client1.localThreadsMetadata().iterator().next().standbyTasks().isEmpty()) { | ||
|
|
||
| client1IsOk[0] = true; | ||
| } | ||
| }); | ||
| final boolean[] client2IsOk = {false}; // has to be a final array, otherwise flag cannot be modified in lambda | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| client2.setStateListener((newState, oldState) -> { | ||
| if (newState == State.RUNNING && | ||
| client2.localThreadsMetadata().iterator().next().standbyTasks().isEmpty()) { | ||
|
|
||
| client2IsOk[0] = true; | ||
| } | ||
| }); | ||
|
|
||
| client1.start(); | ||
| client2.start(); | ||
|
|
||
| TestUtils.waitForCondition( | ||
| () -> client1IsOk[0] && client2IsOk[0], | ||
| 30 * 1000, | ||
| "At least one client is not in state RUNNING or has a stand-by task"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we split both conditions? Maybe only wait for state
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried it and the test became flaky. The reason is that without fix the client is in
Both scenarios would let the test fail which would be OK, but not really clean. However, the test was sometimes green, which I currently do not understand. My guess would be race condition. My approach checks 'localThreadMetadata' when the state changes to |
||
|
|
||
| client1.close(); | ||
| client2.close(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.apache.kafka.clients.consumer.InvalidOffsetException; | ||
| import org.apache.kafka.clients.consumer.MockConsumer; | ||
| import org.apache.kafka.clients.consumer.OffsetResetStrategy; | ||
| import org.apache.kafka.clients.producer.MockProducer; | ||
| import org.apache.kafka.clients.producer.Producer; | ||
| import org.apache.kafka.common.Cluster; | ||
|
|
@@ -59,8 +60,10 @@ | |
| import org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl; | ||
| import org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender; | ||
| import org.apache.kafka.streams.state.KeyValueStore; | ||
| import org.apache.kafka.streams.state.StoreBuilder; | ||
| import org.apache.kafka.streams.state.internals.OffsetCheckpoint; | ||
| import org.apache.kafka.test.MockClientSupplier; | ||
| import org.apache.kafka.test.MockKeyValueStoreBuilder; | ||
| import org.apache.kafka.test.MockProcessor; | ||
| import org.apache.kafka.test.MockStateRestoreListener; | ||
| import org.apache.kafka.test.MockTimestampExtractor; | ||
|
|
@@ -70,6 +73,7 @@ | |
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.slf4j.Logger; | ||
|
|
||
| import java.io.File; | ||
| import java.time.Duration; | ||
|
|
@@ -92,6 +96,8 @@ | |
| import static org.apache.kafka.streams.processor.internals.AbstractStateManager.CHECKPOINT_FILE_NAME; | ||
| import static org.apache.kafka.streams.processor.internals.StreamThread.getSharedAdminClientId; | ||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.CoreMatchers.not; | ||
| import static org.hamcrest.CoreMatchers.nullValue; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
|
|
@@ -1057,6 +1063,69 @@ public void shouldUpdateStandbyTask() throws Exception { | |
| assertEquals(0, thread.standbyRecords().size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldCreateStandbyTask() { | ||
| final MockProcessor mockProcessor = new MockProcessor(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could reuse initialization code for L1068 - L1070
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| internalTopologyBuilder.addSource(null, "source1", null, null, null, topic1); | ||
| internalTopologyBuilder.addProcessor("processor1", () -> mockProcessor, "source1"); | ||
| internalTopologyBuilder.addStateStore(new MockKeyValueStoreBuilder("myStore", true), "processor1"); | ||
| final StreamThread.StandbyTaskCreator standbyTaskCreator = createStandbyTaskCreator(internalTopologyBuilder); | ||
|
|
||
| final StandbyTask standbyTask = standbyTaskCreator.createTask( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The standby task could also be reused IIUC
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack |
||
| new MockConsumer<>(OffsetResetStrategy.EARLIEST), | ||
| new TaskId(1, 2), | ||
| Collections.emptySet()); | ||
|
|
||
| assertThat(standbyTask, not(nullValue())); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldNotCreateStandbyTaskWithoutStateStores() { | ||
| final MockProcessor mockProcessor = new MockProcessor(); | ||
| internalTopologyBuilder.addSource(null, "source1", null, null, null, topic1); | ||
| internalTopologyBuilder.addProcessor("processor1", () -> mockProcessor, "source1"); | ||
| final StreamThread.StandbyTaskCreator standbyTaskCreator = createStandbyTaskCreator(internalTopologyBuilder); | ||
|
|
||
| final StandbyTask standbyTask = standbyTaskCreator.createTask( | ||
| new MockConsumer<>(OffsetResetStrategy.EARLIEST), | ||
| new TaskId(1, 2), | ||
| Collections.emptySet()); | ||
|
|
||
| assertThat(standbyTask, nullValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldNotCreateStandbyTaskIfStateStoresHaveLoggingDisabled() { | ||
| final MockProcessor mockProcessor = new MockProcessor(); | ||
| internalTopologyBuilder.addSource(null, "source1", null, null, null, topic1); | ||
| internalTopologyBuilder.addProcessor("processor1", () -> mockProcessor, "source1"); | ||
| final StoreBuilder storeBuilder = new MockKeyValueStoreBuilder("myStore", true); | ||
| storeBuilder.withLoggingDisabled(); | ||
| internalTopologyBuilder.addStateStore(storeBuilder, "processor1"); | ||
| final StreamThread.StandbyTaskCreator standbyTaskCreator = createStandbyTaskCreator(internalTopologyBuilder); | ||
|
|
||
| final StandbyTask standbyTask = standbyTaskCreator.createTask( | ||
| new MockConsumer<>(OffsetResetStrategy.EARLIEST), | ||
| new TaskId(1, 2), | ||
| Collections.emptySet()); | ||
|
|
||
| assertThat(standbyTask, nullValue()); | ||
| } | ||
|
|
||
| private StreamThread.StandbyTaskCreator createStandbyTaskCreator(final InternalTopologyBuilder internalTopologyBuilder) { | ||
| final LogContext logContext = new LogContext("test"); | ||
| final Logger log = logContext.logger(StreamThreadTest.class); | ||
| final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, clientId); | ||
| return new StreamThread.StandbyTaskCreator( | ||
| internalTopologyBuilder, | ||
| config, | ||
| streamsMetrics, | ||
| stateDirectory, | ||
| new MockChangelogReader(), | ||
| mockTime, | ||
| log); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldPunctuateActiveTask() { | ||
| final List<Long> punctuatedStreamTime = new ArrayList<>(); | ||
|
|
||
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.
Does this still work for optimized source tables, which read from the input topic instead?
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.
Good point. Thinking about this, StandBys for source-KTables might have been broker for a long time already... (maybe since 0.10.0.0???)
Maybe @cadonna can verify? If that is the case, we should split out a separate ticket and PR to fix StandBys for source-KTables independently.
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.
Yeah, good point! Added an integration test to verify materialized and optimized source tables.
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.
Cannot follow. You test seem to use the PAPI, and the PAPI does not provide the KTable optimization. You would need to use
StreamBuilder#table()to test the changelog optimization.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.
See
StandbyTaskCreationIntegrationTestline 128.