简体   繁体   中英

Cannot query local state store in Kafka Streams Application

I'm building a kafka streams application with spring-kafka to group records by key and apply some business logic. I'm following the configuration stated on spring-kafka-streams doc , but the problem is that when I want to retrieve a value from the local store I get the following error:

org.apache.kafka.streams.errors.InvalidStateStoreException: The state store, user-data-response-count, may have migrated to another instance.
  at org.apache.kafka.streams.state.internals.QueryableStoreProvider.getStore(QueryableStoreProvider.java:60)
  at org.apache.kafka.streams.KafkaStreams.store(KafkaStreams.java:1053)
  at com.umantis.management.service.UserDataManagementService.broadcastUserDataRequest(UserDataManagementService.java:121)

Here is my KafkaStreamsConfiguration:

@Configuration
@EnableConfigurationProperties(EventsKafkaProperties.class)
@EnableKafka
@EnableKafkaStreams
public class KafkaConfiguration {

@Value("${app.kafka.streams.application-id}")
private String applicationId;

// This contains both the bootstrap servers and the schema registry url
@Autowired
private EventsKafkaProperties eventsKafkaProperties;

@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
public StreamsConfig streamsConfig() {
    Map<String, Object> props = new HashMap<>();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, this.eventsKafkaProperties.getBrokers());
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, this.eventsKafkaProperties.getSchemaRegistryUrl());
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

    return new StreamsConfig(props);
}

@Bean
public KGroupedStream<String, UserDataResponse> responseKStream(StreamsBuilder streamsBuilder, TopicUtils topicUtils) {
    final Map<String, String> serdeConfig = Collections.singletonMap("schema.registry.url", this.eventsKafkaProperties.getSchemaRegistryUrl());

    final Serde<UserDataResponse> valueSpecificAvroSerde = new SpecificAvroSerde<>();
    valueSpecificAvroSerde.configure(serdeConfig, false);

    return streamsBuilder
            .stream("myTopic", Consumed.with(Serdes.String(), valueSpecificAvroSerde))
            .groupByKey();
}

And here is my service code failing on getKafkaStreams().store :

@Slf4j
@Service
public class UserDataManagementService {

    private static final String RESPONSE_COUNT_STORE = "user-data-response-count";

    @Autowired
    private StreamsBuilderFactoryBean streamsBuilderFactory;

    public UserDataResponse broadcastUserDataRequest() {
        this.responseGroupStream.count(Materialized.as(RESPONSE_COUNT_STORE));

        if (!this.streamsBuilderFactory.isRunning()) {
            throw new KafkaStoreNotAvailableException();
        }

        // here we should have a single running kafka instance
        ReadOnlyKeyValueStore<String, Long> countStore =
                this.streamsBuilderFactory.getKafkaStreams().store(RESPONSE_COUNT_STORE, QueryableStoreTypes.keyValueStore());

        ...
    }

Context: I'm running the app on a single instance in a spring boot test and I'm ensuring the kafka instance is on a running state. I've searched on documentation from apache on this issue, but my case does not appear to match.

Can anyone point me what I'm doing wrong and a possible solution?

I'm quite new on Kafka Streams, so any help would be highly appreciated.

Ok, just saw that I was asking if the streams factory was running but I wasn't asking if the kakfa streams instance was actually running.

Polling over streamsBuilderFactory.getKafkaStreams().state solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM