Search in sources :

Example 16 with InvalidStateStoreException

use of org.apache.kafka.streams.errors.InvalidStateStoreException in project kafka by apache.

the class ReadOnlyWindowStoreStub method backwardFetch.

@Override
public KeyValueIterator<Windowed<K>, V> backwardFetch(final K keyFrom, final K keyTo, final Instant timeFrom, final Instant timeTo) throws IllegalArgumentException {
    final long timeFromTs = ApiUtils.validateMillisecondInstant(timeFrom, prepareMillisCheckFailMsgPrefix(timeFrom, "timeFrom"));
    final long timeToTs = ApiUtils.validateMillisecondInstant(timeTo, prepareMillisCheckFailMsgPrefix(timeTo, "timeTo"));
    if (!open) {
        throw new InvalidStateStoreException("Store is not open");
    }
    final List<KeyValue<Windowed<K>, V>> results = new ArrayList<>();
    for (long now = timeToTs; now >= timeFromTs; now--) {
        final NavigableMap<K, V> kvMap = data.get(now);
        if (kvMap != null) {
            final NavigableMap<K, V> kvSubMap;
            if (keyFrom == null && keyFrom == null) {
                kvSubMap = kvMap;
            } else if (keyFrom == null) {
                kvSubMap = kvMap.headMap(keyTo, true);
            } else if (keyTo == null) {
                kvSubMap = kvMap.tailMap(keyFrom, true);
            } else {
                // keyFrom != null and KeyTo != null
                kvSubMap = kvMap.subMap(keyFrom, true, keyTo, true);
            }
            for (final Entry<K, V> entry : kvSubMap.descendingMap().entrySet()) {
                results.add(new KeyValue<>(new Windowed<>(entry.getKey(), new TimeWindow(now, now + windowSize)), entry.getValue()));
            }
        }
    }
    final Iterator<KeyValue<Windowed<K>, V>> iterator = results.iterator();
    return new KeyValueIterator<Windowed<K>, V>() {

        @Override
        public void close() {
        }

        @Override
        public Windowed<K> peekNextKey() {
            throw new UnsupportedOperationException("peekNextKey() not supported in " + getClass().getName());
        }

        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public KeyValue<Windowed<K>, V> next() {
            return iterator.next();
        }
    };
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Windowed(org.apache.kafka.streams.kstream.Windowed) InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator)

Example 17 with InvalidStateStoreException

use of org.apache.kafka.streams.errors.InvalidStateStoreException in project kafka by apache.

the class ReadOnlyWindowStoreStub method fetchAll.

@Override
public KeyValueIterator<Windowed<K>, V> fetchAll(final Instant timeFrom, final Instant timeTo) {
    if (!open) {
        throw new InvalidStateStoreException("Store is not open");
    }
    final List<KeyValue<Windowed<K>, V>> results = new ArrayList<>();
    for (final long now : data.keySet()) {
        if (!(now >= timeFrom.toEpochMilli() && now <= timeTo.toEpochMilli())) {
            continue;
        }
        final NavigableMap<K, V> kvMap = data.get(now);
        if (kvMap != null) {
            for (final Entry<K, V> entry : kvMap.entrySet()) {
                results.add(new KeyValue<>(new Windowed<>(entry.getKey(), new TimeWindow(now, now + windowSize)), entry.getValue()));
            }
        }
    }
    final Iterator<KeyValue<Windowed<K>, V>> iterator = results.iterator();
    return new KeyValueIterator<Windowed<K>, V>() {

        @Override
        public void close() {
        }

        @Override
        public Windowed<K> peekNextKey() {
            throw new UnsupportedOperationException("peekNextKey() not supported in " + getClass().getName());
        }

        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public KeyValue<Windowed<K>, V> next() {
            return iterator.next();
        }
    };
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Windowed(org.apache.kafka.streams.kstream.Windowed) InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator)

Example 18 with InvalidStateStoreException

use of org.apache.kafka.streams.errors.InvalidStateStoreException in project kafka by apache.

the class QueryableStoreProviderTest method shouldThrowExceptionWhenWindowStoreWithPartitionDoesntExists.

@Test
public void shouldThrowExceptionWhenWindowStoreWithPartitionDoesntExists() {
    final int partition = numStateStorePartitions + 1;
    final InvalidStateStoreException thrown = assertThrows(InvalidStateStoreException.class, () -> storeProvider.getStore(StoreQueryParameters.fromNameAndType(windowStore, QueryableStoreTypes.windowStore()).withPartition(partition)).fetch("1", System.currentTimeMillis()));
    assertThat(thrown.getMessage(), equalTo(String.format("The specified partition %d for store %s does not exist.", partition, windowStore)));
}
Also used : InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) Test(org.junit.Test)

Example 19 with InvalidStateStoreException

use of org.apache.kafka.streams.errors.InvalidStateStoreException in project kafka by apache.

the class QueryableStateIntegrationTest method verifyAllKVKeys.

private void verifyAllKVKeys(final List<KafkaStreams> streamsList, final KafkaStreams streams, final KafkaStreamsTest.StateListenerStub stateListener, final Set<String> keys, final String storeName, final long timeout, final boolean pickInstanceByPort) throws Exception {
    retryOnExceptionWithTimeout(timeout, () -> {
        final List<String> noMetadataKeys = new ArrayList<>();
        final List<String> nullStoreKeys = new ArrayList<>();
        final List<String> nullValueKeys = new ArrayList<>();
        final Map<String, Exception> exceptionalKeys = new TreeMap<>();
        final StringSerializer serializer = new StringSerializer();
        for (final String key : keys) {
            try {
                final KeyQueryMetadata queryMetadata = streams.queryMetadataForKey(storeName, key, serializer);
                if (queryMetadata == null || queryMetadata.equals(KeyQueryMetadata.NOT_AVAILABLE)) {
                    noMetadataKeys.add(key);
                    continue;
                }
                if (!pickInstanceByPort) {
                    assertThat("Should have standbys to query from", !queryMetadata.standbyHosts().isEmpty());
                }
                final int index = queryMetadata.activeHost().port();
                final KafkaStreams streamsWithKey = pickInstanceByPort ? streamsList.get(index) : streams;
                final ReadOnlyKeyValueStore<String, Long> store = IntegrationTestUtils.getStore(storeName, streamsWithKey, true, keyValueStore());
                if (store == null) {
                    nullStoreKeys.add(key);
                    continue;
                }
                if (store.get(key) == null) {
                    nullValueKeys.add(key);
                }
            } catch (final InvalidStateStoreException e) {
                if (stateListener.mapStates.get(KafkaStreams.State.REBALANCING) < 1) {
                    throw new NoRetryException(new AssertionError(String.format("Received %s for key %s and expected at least one rebalancing state, but had none", e.getClass().getName(), key)));
                }
            } catch (final Exception e) {
                exceptionalKeys.put(key, e);
            }
        }
        assertNoKVKeyFailures(storeName, timeout, noMetadataKeys, nullStoreKeys, nullValueKeys, exceptionalKeys);
    });
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) UnknownStateStoreException(org.apache.kafka.streams.errors.UnknownStateStoreException) IOException(java.io.IOException) InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) NoRetryException(org.apache.kafka.test.NoRetryException) KeyQueryMetadata(org.apache.kafka.streams.KeyQueryMetadata) InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) NoRetryException(org.apache.kafka.test.NoRetryException) StringSerializer(org.apache.kafka.common.serialization.StringSerializer)

Example 20 with InvalidStateStoreException

use of org.apache.kafka.streams.errors.InvalidStateStoreException in project kafka by apache.

the class StoreQueryIntegrationTest method shouldQueryStoresAfterAddingAndRemovingStreamThread.

@Test
public void shouldQueryStoresAfterAddingAndRemovingStreamThread() throws Exception {
    final int batch1NumMessages = 100;
    final int key = 1;
    final int key2 = 2;
    final int key3 = 3;
    final Semaphore semaphore = new Semaphore(0);
    final StreamsBuilder builder = new StreamsBuilder();
    getStreamsBuilderWithTopology(builder, semaphore);
    final Properties streamsConfiguration1 = streamsConfiguration();
    streamsConfiguration1.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 1);
    final KafkaStreams kafkaStreams1 = createKafkaStreams(builder, streamsConfiguration1);
    startApplicationAndWaitUntilRunning(singletonList(kafkaStreams1), Duration.ofSeconds(60));
    // Add thread
    final Optional<String> streamThread = kafkaStreams1.addStreamThread();
    assertThat(streamThread.isPresent(), is(true));
    until(() -> kafkaStreams1.state().isRunningOrRebalancing());
    produceValueRange(key, 0, batch1NumMessages);
    produceValueRange(key2, 0, batch1NumMessages);
    produceValueRange(key3, 0, batch1NumMessages);
    // Assert that all messages in the batches were processed in a timely manner
    assertThat(semaphore.tryAcquire(3 * batch1NumMessages, 60, TimeUnit.SECONDS), is(equalTo(true)));
    until(() -> KafkaStreams.State.RUNNING.equals(kafkaStreams1.state()));
    until(() -> {
        final QueryableStoreType<ReadOnlyKeyValueStore<Integer, Integer>> queryableStoreType = keyValueStore();
        final ReadOnlyKeyValueStore<Integer, Integer> store1 = getStore(TABLE_NAME, kafkaStreams1, queryableStoreType);
        try {
            assertThat(store1.get(key), is(notNullValue()));
            assertThat(store1.get(key2), is(notNullValue()));
            assertThat(store1.get(key3), is(notNullValue()));
            return true;
        } catch (final InvalidStateStoreException exception) {
            verifyRetrievableException(exception);
            LOG.info("Either streams wasn't running or a re-balancing took place. Will try again.");
            return false;
        }
    });
    final Optional<String> removedThreadName = kafkaStreams1.removeStreamThread();
    assertThat(removedThreadName.isPresent(), is(true));
    until(() -> kafkaStreams1.state().isRunningOrRebalancing());
    until(() -> KafkaStreams.State.RUNNING.equals(kafkaStreams1.state()));
    until(() -> {
        final QueryableStoreType<ReadOnlyKeyValueStore<Integer, Integer>> queryableStoreType = keyValueStore();
        final ReadOnlyKeyValueStore<Integer, Integer> store1 = getStore(TABLE_NAME, kafkaStreams1, queryableStoreType);
        try {
            assertThat(store1.get(key), is(notNullValue()));
            assertThat(store1.get(key2), is(notNullValue()));
            assertThat(store1.get(key3), is(notNullValue()));
            return true;
        } catch (final InvalidStateStoreException exception) {
            verifyRetrievableException(exception);
            LOG.info("Either streams wasn't running or a re-balancing took place. Will try again.");
            return false;
        }
    });
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KafkaStreams(org.apache.kafka.streams.KafkaStreams) InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) Semaphore(java.util.concurrent.Semaphore) Matchers.containsString(org.hamcrest.Matchers.containsString) ReadOnlyKeyValueStore(org.apache.kafka.streams.state.ReadOnlyKeyValueStore) Properties(java.util.Properties) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Aggregations

InvalidStateStoreException (org.apache.kafka.streams.errors.InvalidStateStoreException)46 Windowed (org.apache.kafka.streams.kstream.Windowed)14 Test (org.junit.Test)14 ReadOnlyKeyValueStore (org.apache.kafka.streams.state.ReadOnlyKeyValueStore)13 ArrayList (java.util.ArrayList)12 KeyValue (org.apache.kafka.streams.KeyValue)10 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)9 KeyValueIterator (org.apache.kafka.streams.state.KeyValueIterator)9 KafkaStreams (org.apache.kafka.streams.KafkaStreams)8 IntegrationTest (org.apache.kafka.test.IntegrationTest)7 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)6 ReadOnlySessionStore (org.apache.kafka.streams.state.ReadOnlySessionStore)6 Properties (java.util.Properties)4 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)4 KeyQueryMetadata (org.apache.kafka.streams.KeyQueryMetadata)4 ReadOnlyWindowStore (org.apache.kafka.streams.state.ReadOnlyWindowStore)4 Semaphore (java.util.concurrent.Semaphore)3 KafkaStreamsTest (org.apache.kafka.streams.KafkaStreamsTest)3 IOException (java.io.IOException)2 TreeMap (java.util.TreeMap)2