Search in sources :

Example 1 with NoRetryException

use of org.apache.kafka.test.NoRetryException 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 2 with NoRetryException

use of org.apache.kafka.test.NoRetryException in project kafka by apache.

the class QueryableStateIntegrationTest method verifyAllWindowedKeys.

private void verifyAllWindowedKeys(final List<KafkaStreams> streamsList, final KafkaStreams streams, final KafkaStreamsTest.StateListenerStub stateListenerStub, final Set<String> keys, final String storeName, final Long from, final Long to, 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(queryMetadata.standbyHosts().size(), equalTo(0));
                } else {
                    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 ReadOnlyWindowStore<String, Long> store = IntegrationTestUtils.getStore(storeName, streamsWithKey, true, QueryableStoreTypes.windowStore());
                if (store == null) {
                    nullStoreKeys.add(key);
                    continue;
                }
                if (store.fetch(key, ofEpochMilli(from), ofEpochMilli(to)) == null) {
                    nullValueKeys.add(key);
                }
            } catch (final InvalidStateStoreException e) {
                // there must have been at least one rebalance state
                if (stateListenerStub.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)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)2 KafkaStreams (org.apache.kafka.streams.KafkaStreams)2 KeyQueryMetadata (org.apache.kafka.streams.KeyQueryMetadata)2 InvalidStateStoreException (org.apache.kafka.streams.errors.InvalidStateStoreException)2 UnknownStateStoreException (org.apache.kafka.streams.errors.UnknownStateStoreException)2 NoRetryException (org.apache.kafka.test.NoRetryException)2