Search in sources :

Example 66 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class KeyValueStoreMaterializerTest method shouldCreateKeyValueStoreWithTheProvidedInnerStore.

@Test
public void shouldCreateKeyValueStoreWithTheProvidedInnerStore() {
    final KeyValueBytesStoreSupplier supplier = EasyMock.createNiceMock(KeyValueBytesStoreSupplier.class);
    final InMemoryKeyValueStore<Bytes, byte[]> store = new InMemoryKeyValueStore<>("name", Serdes.Bytes(), Serdes.ByteArray());
    EasyMock.expect(supplier.name()).andReturn("name").anyTimes();
    EasyMock.expect(supplier.get()).andReturn(store);
    EasyMock.replay(supplier);
    final MaterializedInternal<String, Integer, KeyValueStore<Bytes, byte[]>> materialized = new MaterializedInternal<>(Materialized.<String, Integer>as(supplier), nameProvider, storePrefix);
    final KeyValueStoreMaterializer<String, Integer> materializer = new KeyValueStoreMaterializer<>(materialized);
    final StoreBuilder<KeyValueStore<String, Integer>> builder = materializer.materialize();
    final KeyValueStore<String, Integer> built = builder.build();
    final StateStore inner = ((WrappedStateStore) built).inner();
    assertThat(inner, CoreMatchers.<StateStore>equalTo(store));
}
Also used : WrappedStateStore(org.apache.kafka.streams.state.internals.WrappedStateStore) WrappedStateStore(org.apache.kafka.streams.state.internals.WrappedStateStore) CachedStateStore(org.apache.kafka.streams.state.internals.CachedStateStore) StateStore(org.apache.kafka.streams.processor.StateStore) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) Bytes(org.apache.kafka.common.utils.Bytes) KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier) MaterializedInternal(org.apache.kafka.streams.kstream.internals.MaterializedInternal) KeyValueStoreMaterializer(org.apache.kafka.streams.kstream.internals.KeyValueStoreMaterializer) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) Test(org.junit.Test)

Example 67 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class StreamsPartitionAssignorTest method shouldNotLoopInfinitelyOnMissingMetadataAndShouldNotCreateRelatedTasks.

@Test
public void shouldNotLoopInfinitelyOnMissingMetadataAndShouldNotCreateRelatedTasks() throws Exception {
    final StreamsBuilder builder = new StreamsBuilder();
    final InternalTopologyBuilder internalTopologyBuilder = StreamsBuilderTest.internalTopologyBuilder(builder);
    internalTopologyBuilder.setApplicationId(applicationId);
    KStream<Object, Object> stream1 = builder.stream("topic1").selectKey(new KeyValueMapper<Object, Object, Object>() {

        @Override
        public Object apply(final Object key, final Object value) {
            return null;
        }
    }).groupByKey().count(Materialized.<Object, Long, KeyValueStore<Bytes, byte[]>>as("count")).toStream().map(new KeyValueMapper<Object, Long, KeyValue<Object, Object>>() {

        @Override
        public KeyValue<Object, Object> apply(final Object key, final Long value) {
            return null;
        }
    });
    builder.stream("unknownTopic").selectKey(new KeyValueMapper<Object, Object, Object>() {

        @Override
        public Object apply(final Object key, final Object value) {
            return null;
        }
    }).join(stream1, new ValueJoiner() {

        @Override
        public Object apply(final Object value1, final Object value2) {
            return null;
        }
    }, JoinWindows.of(0));
    final UUID uuid = UUID.randomUUID();
    final String client = "client1";
    mockTaskManager(Collections.<TaskId>emptySet(), Collections.<TaskId>emptySet(), UUID.randomUUID(), internalTopologyBuilder);
    configurePartitionAssignor(Collections.<String, Object>emptyMap());
    final MockInternalTopicManager mockInternalTopicManager = new MockInternalTopicManager(streamsConfig, mockClientSupplier.restoreConsumer);
    partitionAssignor.setInternalTopicManager(mockInternalTopicManager);
    final Map<String, PartitionAssignor.Subscription> subscriptions = new HashMap<>();
    final Set<TaskId> emptyTasks = Collections.emptySet();
    subscriptions.put(client, new PartitionAssignor.Subscription(Collections.singletonList("unknownTopic"), new SubscriptionInfo(uuid, emptyTasks, emptyTasks, userEndPoint).encode()));
    final Map<String, PartitionAssignor.Assignment> assignment = partitionAssignor.assign(metadata, subscriptions);
    final Map<String, Integer> expectedCreatedInternalTopics = new HashMap<>();
    expectedCreatedInternalTopics.put(applicationId + "-count-repartition", 3);
    expectedCreatedInternalTopics.put(applicationId + "-count-changelog", 3);
    assertThat(mockInternalTopicManager.readyTopics, equalTo(expectedCreatedInternalTopics));
    final List<TopicPartition> expectedAssignment = Arrays.asList(new TopicPartition("topic1", 0), new TopicPartition("topic1", 1), new TopicPartition("topic1", 2), new TopicPartition(applicationId + "-count-repartition", 0), new TopicPartition(applicationId + "-count-repartition", 1), new TopicPartition(applicationId + "-count-repartition", 2));
    assertThat(new HashSet<>(assignment.get(client).partitions()), equalTo(new HashSet<>(expectedAssignment)));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) TaskId(org.apache.kafka.streams.processor.TaskId) HashMap(java.util.HashMap) MockInternalTopicManager(org.apache.kafka.test.MockInternalTopicManager) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) SubscriptionInfo(org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo) Bytes(org.apache.kafka.common.utils.Bytes) ValueJoiner(org.apache.kafka.streams.kstream.ValueJoiner) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor) UUID(java.util.UUID) HashSet(java.util.HashSet) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) TopicPartition(org.apache.kafka.common.TopicPartition) StreamsBuilderTest(org.apache.kafka.streams.StreamsBuilderTest) Test(org.junit.Test)

Example 68 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class SinkNodeTest method shouldThrowStreamsExceptionOnInputRecordWithInvalidTimestamp.

@Test
@SuppressWarnings("unchecked")
public void shouldThrowStreamsExceptionOnInputRecordWithInvalidTimestamp() {
    final Bytes anyKey = new Bytes("any key".getBytes());
    final Bytes anyValue = new Bytes("any value".getBytes());
    // When/Then
    // ensures a negative timestamp is set for the record we send next
    context.setTime(-1);
    try {
        sink.process(anyKey, anyValue);
        fail("Should have thrown StreamsException");
    } catch (final StreamsException ignored) {
    // expected
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) StreamsException(org.apache.kafka.streams.errors.StreamsException) Test(org.junit.Test)

Example 69 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class CachingSessionStoreTest method shouldNotForwardChangedValuesDuringFlushWhenSendOldValuesDisabled.

@Test
public void shouldNotForwardChangedValuesDuringFlushWhenSendOldValuesDisabled() {
    final Windowed<Bytes> a = new Windowed<>(keyA, new SessionWindow(0, 0));
    final Windowed<String> aDeserialized = new Windowed<>("a", new SessionWindow(0, 0));
    final List<KeyValue<Windowed<String>, Change<String>>> flushed = new ArrayList<>();
    cachingStore.setFlushListener(new CacheFlushListener<Windowed<String>, String>() {

        @Override
        public void apply(final Windowed<String> key, final String newValue, final String oldValue) {
            flushed.add(KeyValue.pair(key, new Change<>(newValue, oldValue)));
        }
    }, false);
    cachingStore.put(a, "1".getBytes());
    cachingStore.flush();
    cachingStore.put(a, "2".getBytes());
    cachingStore.flush();
    assertEquals(flushed, Arrays.asList(KeyValue.pair(aDeserialized, new Change<>("1", null)), KeyValue.pair(aDeserialized, new Change<>("2", null))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) StreamsTestUtils.verifyWindowedKeyValue(org.apache.kafka.test.StreamsTestUtils.verifyWindowedKeyValue) ArrayList(java.util.ArrayList) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 70 with Bytes

use of org.apache.kafka.common.utils.Bytes in project apache-kafka-on-k8s by banzaicloud.

the class CachingSessionStoreTest method shouldForwardChangedValuesDuringFlushWhenSendOldValuesDisabledNewRecordIsNull.

@Test
public void shouldForwardChangedValuesDuringFlushWhenSendOldValuesDisabledNewRecordIsNull() {
    final Windowed<Bytes> a = new Windowed<>(keyA, new SessionWindow(0, 0));
    final Windowed<String> aDeserialized = new Windowed<>("a", new SessionWindow(0, 0));
    final List<KeyValue<Windowed<String>, Change<String>>> flushed = new ArrayList<>();
    cachingStore.setFlushListener(new CacheFlushListener<Windowed<String>, String>() {

        @Override
        public void apply(final Windowed<String> key, final String newValue, final String oldValue) {
            flushed.add(KeyValue.pair(key, new Change<>(newValue, oldValue)));
        }
    }, false);
    cachingStore.put(a, "1".getBytes());
    cachingStore.flush();
    cachingStore.put(a, "2".getBytes());
    cachingStore.flush();
    cachingStore.remove(a);
    cachingStore.flush();
    assertEquals(flushed, Arrays.asList(KeyValue.pair(aDeserialized, new Change<>("1", null)), KeyValue.pair(aDeserialized, new Change<>("2", null)), KeyValue.pair(aDeserialized, new Change<>(null, "2"))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) StreamsTestUtils.verifyWindowedKeyValue(org.apache.kafka.test.StreamsTestUtils.verifyWindowedKeyValue) ArrayList(java.util.ArrayList) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Aggregations

Bytes (org.apache.kafka.common.utils.Bytes)398 Test (org.junit.Test)309 Windowed (org.apache.kafka.streams.kstream.Windowed)84 KeyValue (org.apache.kafka.streams.KeyValue)68 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)53 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)50 Properties (java.util.Properties)49 ArrayList (java.util.ArrayList)42 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)38 StreamsConfig (org.apache.kafka.streams.StreamsConfig)35 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)35 Materialized (org.apache.kafka.streams.kstream.Materialized)33 Serdes (org.apache.kafka.common.serialization.Serdes)32 Metrics (org.apache.kafka.common.metrics.Metrics)29 KafkaStreams (org.apache.kafka.streams.KafkaStreams)28 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)27 Consumed (org.apache.kafka.streams.kstream.Consumed)25 KTable (org.apache.kafka.streams.kstream.KTable)23 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)22 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)21