Skip to content

Commit e88711f

Browse files
committed
GH-1482: Make Topic Config Update Optional
Resolves #1482
1 parent ec2608b commit e88711f

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

spring-kafka/src/main/java/org/springframework/kafka/core/KafkaAdmin.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public class KafkaAdmin extends KafkaResourceFactory
9292

9393
private boolean initializingContext;
9494

95+
private boolean modifyTopicConfigs;
96+
9597
/**
9698
* Create an instance with an {@link AdminClient} based on the supplied
9799
* configuration.
@@ -140,6 +142,16 @@ public void setAutoCreate(boolean autoCreate) {
140142
this.autoCreate = autoCreate;
141143
}
142144

145+
/**
146+
* Set to true to compare the current topic configuration properties with those in the
147+
* {@link NewTopic} bean, and update if different.
148+
* @param modifyTopicConfigs true to check and update configs if necessary.
149+
* @since 2.8.7
150+
*/
151+
public void setModifyTopicConfigs(boolean modifyTopicConfigs) {
152+
this.modifyTopicConfigs = modifyTopicConfigs;
153+
}
154+
143155
@Override
144156
public Map<String, Object> getConfigurationProperties() {
145157
Map<String, Object> configs2 = new HashMap<>(this.configs);
@@ -254,16 +266,19 @@ private void addOrModifyTopicsIfNeeded(AdminClient adminClient, Collection<NewTo
254266
if (topicsWithPartitionMismatches.size() > 0) {
255267
createMissingPartitions(adminClient, topicsWithPartitionMismatches);
256268
}
257-
Map<ConfigResource, List<ConfigEntry>> mismatchingConfigs =
258-
checkTopicsForConfigMismatches(adminClient, topics);
259-
if (!mismatchingConfigs.isEmpty()) {
260-
adjustConfigMismatches(adminClient, topics, mismatchingConfigs);
269+
if (this.modifyTopicConfigs) {
270+
Map<ConfigResource, List<ConfigEntry>> mismatchingConfigs =
271+
checkTopicsForConfigMismatches(adminClient, topics);
272+
if (!mismatchingConfigs.isEmpty()) {
273+
adjustConfigMismatches(adminClient, topics, mismatchingConfigs);
274+
}
261275
}
262276
}
263277
}
264278

265279
private Map<ConfigResource, List<ConfigEntry>> checkTopicsForConfigMismatches(
266280
AdminClient adminClient, Collection<NewTopic> topics) {
281+
267282
List<ConfigResource> configResources = topics.stream()
268283
.map(topic -> new ConfigResource(Type.TOPIC, topic.name()))
269284
.collect(Collectors.toList());
@@ -297,10 +312,10 @@ private Map<ConfigResource, List<ConfigEntry>> checkTopicsForConfigMismatches(
297312
}
298313
catch (InterruptedException ie) {
299314
Thread.currentThread().interrupt();
300-
throw new KafkaException("Interrupted while getting topic descriptions", ie);
315+
throw new KafkaException("Interrupted while getting topic descriptions:" + topics, ie);
301316
}
302317
catch (ExecutionException | TimeoutException ex) {
303-
throw new KafkaException("Failed to obtain topic descriptions", ex);
318+
throw new KafkaException("Failed to obtain topic descriptions:" + topics, ex);
304319
}
305320
}
306321

spring-kafka/src/test/java/org/springframework/kafka/core/KafkaAdminTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public void testAddTopicsAndAddPartitions() throws Exception {
106106
new DirectFieldAccessor(this.topic1).setPropertyValue("numPartitions", Optional.of(4));
107107
new DirectFieldAccessor(this.topic2).setPropertyValue("numPartitions", Optional.of(3));
108108
this.admin.initialize();
109+
this.admin.setModifyTopicConfigs(true);
109110

110111
int n = 0;
111112
await().until(() -> {
@@ -212,11 +213,12 @@ public void alreadyExists() throws Exception {
212213
AtomicReference<Method> addTopics = new AtomicReference<>();
213214
AtomicReference<Method> modifyTopics = new AtomicReference<>();
214215
ReflectionUtils.doWithMethods(KafkaAdmin.class, m -> {
215-
m.setAccessible(true);
216216
if (m.getName().equals("addTopics")) {
217+
m.setAccessible(true);
217218
addTopics.set(m);
218219
}
219220
else if (m.getName().equals("createMissingPartitions")) {
221+
m.setAccessible(true);
220222
modifyTopics.set(m);
221223
}
222224
});

0 commit comments

Comments
 (0)