Skip to content

Commit 5ca65cc

Browse files
GH-3176: Take default.dsl.store into account
Fixes: #3176 Currently, the applications need to use special customizers to use in-memory state stores. Kafka Streams provides a property to configure that - `default.dsl.store` (KIP-591), which the framework does not take into account when creating the `StreamsBuilder` since it ignores any config and always relies on customizations after the fact. This change allows this property to work in the framework so that applications can easily switch to in-memory state stores. **Auto-cherry-pick to `3.1.x` & `3.0.x`**
1 parent f1e48f4 commit 5ca65cc

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

spring-kafka/src/main/java/org/springframework/kafka/config/StreamsBuilderFactoryBean.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import org.apache.kafka.streams.KafkaClientSupplier;
2828
import org.apache.kafka.streams.KafkaStreams;
2929
import org.apache.kafka.streams.StreamsBuilder;
30+
import org.apache.kafka.streams.StreamsConfig;
3031
import org.apache.kafka.streams.Topology;
32+
import org.apache.kafka.streams.TopologyConfig;
3133
import org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler;
3234
import org.apache.kafka.streams.processor.StateRestoreListener;
3335
import org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier;
@@ -58,6 +60,7 @@
5860
* @author Gary Russell
5961
* @author Julien Wittouck
6062
* @author Sanghyeok An
63+
* @author Cédric Schaller
6164
*
6265
* @since 1.1.4
6366
*/
@@ -328,7 +331,7 @@ protected StreamsBuilder createInstance() {
328331
Assert.state(this.properties != null,
329332
"streams configuration properties must not be null");
330333
}
331-
StreamsBuilder builder = new StreamsBuilder();
334+
StreamsBuilder builder = createStreamBuilder();
332335
this.infrastructureCustomizer.configureBuilder(builder);
333336
return builder;
334337
}
@@ -442,6 +445,17 @@ public void afterSingletonsInstantiated() {
442445
}
443446
}
444447

448+
private StreamsBuilder createStreamBuilder() {
449+
if (this.properties == null) {
450+
return new StreamsBuilder();
451+
}
452+
else {
453+
StreamsConfig streamsConfig = new StreamsConfig(this.properties);
454+
TopologyConfig topologyConfig = new TopologyConfig(streamsConfig);
455+
return new StreamsBuilder(topologyConfig);
456+
}
457+
}
458+
445459
/**
446460
* Called whenever a {@link KafkaStreams} is added or removed.
447461
*
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.kafka.config;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
import org.apache.kafka.common.serialization.Serdes;
28+
import org.apache.kafka.streams.StreamsBuilder;
29+
import org.apache.kafka.streams.StreamsConfig;
30+
import org.apache.kafka.streams.kstream.KStream;
31+
import org.apache.kafka.streams.kstream.KTable;
32+
import org.apache.kafka.streams.kstream.Materialized;
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.Test;
35+
36+
import org.springframework.beans.factory.annotation.Value;
37+
import org.springframework.context.annotation.Bean;
38+
import org.springframework.context.annotation.Configuration;
39+
import org.springframework.kafka.annotation.EnableKafkaStreams;
40+
import org.springframework.kafka.annotation.KafkaStreamsDefaultConfiguration;
41+
import org.springframework.kafka.test.EmbeddedKafkaBroker;
42+
import org.springframework.kafka.test.context.EmbeddedKafka;
43+
import org.springframework.test.annotation.DirtiesContext;
44+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
45+
46+
/**
47+
* @author Cédric Schaller
48+
*/
49+
@SpringJUnitConfig
50+
@DirtiesContext
51+
@EmbeddedKafka
52+
public class StreamsBuilderFactoryBeanInMemoryStateStoreTests {
53+
54+
private static Path stateStoreDir;
55+
56+
@BeforeAll
57+
static void beforeAll() throws IOException {
58+
stateStoreDir = Files.createTempDirectory(StreamsBuilderFactoryBeanInMemoryStateStoreTests.class.getSimpleName());
59+
}
60+
61+
@Test
62+
void testStateStoreIsInMemory() {
63+
// Testing that an in-memory state store is used requires accessing the internal state of StreamsBuilder via reflection
64+
// Therefore, we check the non-existence of RocksDB files instead
65+
assertThat(stateStoreDir).isEmptyDirectory();
66+
}
67+
68+
@Configuration
69+
@EnableKafkaStreams
70+
static class KafkaStreamsConfig {
71+
72+
@Value("${" + EmbeddedKafkaBroker.SPRING_EMBEDDED_KAFKA_BROKERS + "}")
73+
private String brokerAddresses;
74+
75+
@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
76+
public KafkaStreamsConfiguration kStreamsConfigWithInMemoryStateStores() {
77+
Map<String, Object> props = new HashMap<>();
78+
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "should-be-stored-in-memory");
79+
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, this.brokerAddresses);
80+
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.StringSerde.class);
81+
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class);
82+
props.put(StreamsConfig.STATE_DIR_CONFIG, stateStoreDir.toString());
83+
84+
// Property introduced with KIP-591 (Kafka 3.2) and deprecated (but still supported) with Kafka 3.7
85+
props.put(StreamsConfig.DEFAULT_DSL_STORE_CONFIG, "in_memory");
86+
return new KafkaStreamsConfiguration(props);
87+
}
88+
89+
@Bean
90+
public KTable<?, ?> table(StreamsBuilder builder) {
91+
KStream<Object, Object> stream = builder.stream("source-topic");
92+
return stream.groupByKey()
93+
.count(Materialized.as("store"));
94+
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)