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();
}
};
}
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();
}
};
}
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)));
}
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);
});
}
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;
}
});
}
Aggregations