From 0abf483a3bad60e4930582cf60ae76f35381e212 Mon Sep 17 00:00:00 2001 From: Baljit Singh Date: Thu, 3 Dec 2026 17:19:27 -0500 Subject: [PATCH 1/2] Optimize Optional use inside Kafka Streams InteractiveQueryService Signed-off-by: Baljit Singh --- .../kafka/streams/InteractiveQueryService.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java index 0cd3ff04e5..05f487f439 100644 --- a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java +++ b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java @@ -21,7 +21,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @@ -48,7 +47,7 @@ /** * Services pertinent to the interactive query capabilities of Kafka Streams. This class * provides services such as querying for a particular store, which instance is hosting a - * particular store etc. This is part of the public API of the kafka streams binder, and + * particular store etc. This is part of the public API of the Kafka Streams binder, and * the users can inject this service in their applications to make use of it. * * @author Soby Chacko @@ -57,6 +56,7 @@ * @author Nico Pommerening * @author Chris Bono * @author Artem Bilan + * @author Baljit Singh * @since 2.1.0 */ public class InteractiveQueryService { @@ -74,7 +74,7 @@ public class InteractiveQueryService { /** * Constructor for InteractiveQueryService. * @param kafkaStreamsRegistry holding {@link KafkaStreamsRegistry} - * @param binderConfigurationProperties kafka Streams binder configuration properties + * @param binderConfigurationProperties Kafka Streams binder configuration properties */ public InteractiveQueryService(KafkaStreamsRegistry kafkaStreamsRegistry, KafkaStreamsBinderConfigurationProperties binderConfigurationProperties) { @@ -187,7 +187,7 @@ private boolean filterByThreadName(KafkaStreams streams) { } /** - * Gets the current {@link HostInfo} that the calling kafka streams application is + * Gets the current {@link HostInfo} that the calling Kafka Streams application is * running on. * * Note that the end user applications must provide `application.server` as a @@ -232,8 +232,8 @@ public HostInfo getHostInfo(String store, K key, Serializer serializer) { try { final KeyQueryMetadata keyQueryMetadata = this.kafkaStreamsRegistry.getKafkaStreams() .stream() - .map((k) -> Optional.ofNullable(k.queryMetadataForKey(store, key, serializer))) - .filter(Optional::isPresent).map(Optional::get).findFirst().orElse(null); + .map(k -> k.queryMetadataForKey(store, key, serializer)) + .filter(Objects::nonNull).findFirst().orElse(null); if (keyQueryMetadata != null) { return keyQueryMetadata.activeHost(); } @@ -275,8 +275,8 @@ private RetryTemplate getRetryTemplate() { public KeyQueryMetadata getKeyQueryMetadata(String store, K key, Serializer serializer) { return this.kafkaStreamsRegistry.getKafkaStreams() .stream() - .map((k) -> Optional.ofNullable(k.queryMetadataForKey(store, key, serializer))) - .filter(Optional::isPresent).map(Optional::get).findFirst().orElse(null); + .map(k -> k.queryMetadataForKey(store, key, serializer)) + .filter(Objects::nonNull).findFirst().orElse(null); } /** From 77eb0473a8a95737ad8fac55c3e691c64b59a265 Mon Sep 17 00:00:00 2001 From: Baljit Singh Date: Thu, 3 Dec 2026 17:25:17 -0500 Subject: [PATCH 2/2] Reuse RetryTemplate in Kafka Streams InteractiveQueryService Signed-off-by: Baljit Singh --- .../streams/InteractiveQueryService.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java index 05f487f439..33c7bbd1fd 100644 --- a/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java +++ b/binders/kafka-binder/spring-cloud-stream-binder-kafka-streams/src/main/java/org/springframework/cloud/stream/binder/kafka/streams/InteractiveQueryService.java @@ -69,6 +69,8 @@ public class InteractiveQueryService { private final KafkaStreamsVersionAgnosticTopologyInfoFacade topologyInfoFacade; + private final RetryTemplate retryTemplate; + private StoreQueryParametersCustomizer storeQueryParametersCustomizer; /** @@ -81,6 +83,12 @@ public InteractiveQueryService(KafkaStreamsRegistry kafkaStreamsRegistry, this.kafkaStreamsRegistry = kafkaStreamsRegistry; this.binderConfigurationProperties = binderConfigurationProperties; this.topologyInfoFacade = new KafkaStreamsVersionAgnosticTopologyInfoFacade(); + + var stateStoreRetry = this.binderConfigurationProperties.getStateStoreRetry(); + this.retryTemplate = new RetryTemplate(RetryPolicy.builder() + .maxRetries(stateStoreRetry.getMaxAttempts()) + .delay(Duration.ofMillis(stateStoreRetry.getBackoffPeriod())) + .build()); } /** @@ -103,7 +111,7 @@ public T getQueryableStore(String storeName, QueryableStoreType storeType AtomicReference> storeQueryParametersAtomicReference = new AtomicReference<>(storeQueryParams); try { - return getRetryTemplate().execute(() -> { + return retryTemplate.execute(() -> { T store = null; Throwable throwable = null; if (contextSpecificKafkaStreams != null) { @@ -224,8 +232,6 @@ public HostInfo getCurrentHostInfo() { * @return the {@link HostInfo} where the key for the provided store is hosted currently */ public HostInfo getHostInfo(String store, K key, Serializer serializer) { - final RetryTemplate retryTemplate = getRetryTemplate(); - try { return retryTemplate.execute(() -> { Throwable throwable = null; @@ -251,17 +257,6 @@ public HostInfo getHostInfo(String store, K key, Serializer serializer) { } } - private RetryTemplate getRetryTemplate() { - - var stateStoreRetry = this.binderConfigurationProperties.getStateStoreRetry(); - RetryPolicy retryPolicy = RetryPolicy.builder() - .maxRetries(stateStoreRetry.getMaxAttempts()) - .delay(Duration.ofMillis(stateStoreRetry.getBackoffPeriod())) - .build(); - - return new RetryTemplate(retryPolicy); - } - /** * Retrieves and returns the {@link KeyQueryMetadata} associated with the given combination of * key and state store. If none found, it will return null.