Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -57,6 +56,7 @@
* @author Nico Pommerening
* @author Chris Bono
* @author Artem Bilan
* @author Baljit Singh
* @since 2.1.0
*/
public class InteractiveQueryService {
Expand All @@ -69,18 +69,26 @@ public class InteractiveQueryService {

private final KafkaStreamsVersionAgnosticTopologyInfoFacade topologyInfoFacade;

private final RetryTemplate retryTemplate;

private StoreQueryParametersCustomizer<?> storeQueryParametersCustomizer;

/**
* 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) {
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());
}

/**
Expand All @@ -103,7 +111,7 @@ public <T> T getQueryableStore(String storeName, QueryableStoreType<T> storeType
AtomicReference<StoreQueryParameters<T>> storeQueryParametersAtomicReference = new AtomicReference<>(storeQueryParams);

try {
return getRetryTemplate().execute(() -> {
return retryTemplate.execute(() -> {
T store = null;
Throwable throwable = null;
if (contextSpecificKafkaStreams != null) {
Expand Down Expand Up @@ -187,7 +195,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
Expand Down Expand Up @@ -224,16 +232,14 @@ public HostInfo getCurrentHostInfo() {
* @return the {@link HostInfo} where the key for the provided store is hosted currently
*/
public <K> HostInfo getHostInfo(String store, K key, Serializer<K> serializer) {
final RetryTemplate retryTemplate = getRetryTemplate();

try {
return retryTemplate.execute(() -> {
Throwable throwable = null;
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();
}
Expand All @@ -251,17 +257,6 @@ public <K> HostInfo getHostInfo(String store, K key, Serializer<K> 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.
Expand All @@ -275,8 +270,8 @@ private RetryTemplate getRetryTemplate() {
public <K> KeyQueryMetadata getKeyQueryMetadata(String store, K key, Serializer<K> 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);
}

/**
Expand Down