Search in sources :

Example 1 with InMemoryKeyValueStore

use of org.apache.kafka.streams.state.internals.InMemoryKeyValueStore in project kafka by apache.

the class StreamTaskTest method shouldCheckpointOffsetsOnCommit.

@SuppressWarnings("unchecked")
@Test
public void shouldCheckpointOffsetsOnCommit() throws Exception {
    final String storeName = "test";
    final String changelogTopic = ProcessorStateManager.storeChangelogTopic("appId", storeName);
    final InMemoryKeyValueStore inMemoryStore = new InMemoryKeyValueStore(storeName, null, null) {

        @Override
        public void init(final ProcessorContext context, final StateStore root) {
            context.register(root, true, null);
        }

        @Override
        public boolean persistent() {
            return true;
        }
    };
    final ProcessorTopology topology = new ProcessorTopology(Collections.<ProcessorNode>emptyList(), Collections.<String, SourceNode>emptyMap(), Collections.<String, SinkNode>emptyMap(), Collections.<StateStore>singletonList(inMemoryStore), Collections.singletonMap(storeName, changelogTopic), Collections.<StateStore>emptyList());
    final TopicPartition partition = new TopicPartition(changelogTopic, 0);
    final NoOpRecordCollector recordCollector = new NoOpRecordCollector() {

        @Override
        public Map<TopicPartition, Long> offsets() {
            return Collections.singletonMap(partition, 543L);
        }
    };
    restoreStateConsumer.updatePartitions(changelogTopic, Collections.singletonList(new PartitionInfo(changelogTopic, 0, null, new Node[0], new Node[0])));
    restoreStateConsumer.updateEndOffsets(Collections.singletonMap(partition, 0L));
    restoreStateConsumer.updateBeginningOffsets(Collections.singletonMap(partition, 0L));
    final StreamsMetrics streamsMetrics = new MockStreamsMetrics(new Metrics());
    final TaskId taskId = new TaskId(0, 0);
    final MockTime time = new MockTime();
    final StreamsConfig config = createConfig(baseDir);
    final StreamTask streamTask = new StreamTask(taskId, "appId", partitions, topology, consumer, changelogReader, config, streamsMetrics, stateDirectory, new ThreadCache("testCache", 0, streamsMetrics), time, recordCollector);
    time.sleep(config.getLong(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG));
    streamTask.commit();
    final OffsetCheckpoint checkpoint = new OffsetCheckpoint(new File(stateDirectory.directoryForTask(taskId), ProcessorStateManager.CHECKPOINT_FILE_NAME));
    assertThat(checkpoint.read(), equalTo(Collections.singletonMap(partition, 544L)));
}
Also used : OffsetCheckpoint(org.apache.kafka.streams.state.internals.OffsetCheckpoint) TaskId(org.apache.kafka.streams.processor.TaskId) NoOpRecordCollector(org.apache.kafka.test.NoOpRecordCollector) StateStore(org.apache.kafka.streams.processor.StateStore) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) NoOpProcessorContext(org.apache.kafka.test.NoOpProcessorContext) Metrics(org.apache.kafka.common.metrics.Metrics) StreamsMetrics(org.apache.kafka.streams.StreamsMetrics) TopicPartition(org.apache.kafka.common.TopicPartition) ThreadCache(org.apache.kafka.streams.state.internals.ThreadCache) PartitionInfo(org.apache.kafka.common.PartitionInfo) StreamsMetrics(org.apache.kafka.streams.StreamsMetrics) File(java.io.File) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) MockTime(org.apache.kafka.common.utils.MockTime) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 2 with InMemoryKeyValueStore

use of org.apache.kafka.streams.state.internals.InMemoryKeyValueStore 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 3 with InMemoryKeyValueStore

use of org.apache.kafka.streams.state.internals.InMemoryKeyValueStore in project kafkastreams-cep by fhussonnois.

the class SharedVersionedBufferTest method getInMemorySharedBuffer.

@SuppressWarnings("unchecked")
private <K, V> SharedVersionedBufferStore<K, V> getInMemorySharedBuffer(Serde<K> keySerDe, Serde<V> valueSerDe) {
    InMemoryKeyValueStore test = new InMemoryKeyValueStore("test", Serdes.Bytes(), Serdes.ByteArray());
    SharedVersionedBufferStore<K, V> store = new SharedVersionedBufferStoreImpl<>(test, keySerDe, valueSerDe);
    store.init(new NoOpProcessorContext(), null);
    return store;
}
Also used : NoOpProcessorContext(org.apache.kafka.test.NoOpProcessorContext) SharedVersionedBufferStoreImpl(com.github.fhuss.kafka.streams.cep.state.internal.SharedVersionedBufferStoreImpl) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore)

Example 4 with InMemoryKeyValueStore

use of org.apache.kafka.streams.state.internals.InMemoryKeyValueStore in project kafka by apache.

the class TimestampedKeyValueStoreMaterializerTest method shouldCreateKeyValueStoreWithTheProvidedInnerStore.

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

Example 5 with InMemoryKeyValueStore

use of org.apache.kafka.streams.state.internals.InMemoryKeyValueStore in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldStoreAndReturnStateStores.

@Test
public void shouldStoreAndReturnStateStores() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            // noinspection unchecked
            final KeyValueStore<String, Long> stateStore = (KeyValueStore<String, Long>) context().getStateStore("my-state");
            stateStore.put(key, (stateStore.get(key) == null ? 0 : stateStore.get(key)) + value);
            stateStore.put("all", (stateStore.get("all") == null ? 0 : stateStore.get("all")) + value);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    final KeyValueStore<String, Long> store = new InMemoryKeyValueStore<>("my-state", Serdes.String(), Serdes.Long());
    context.register(store, false, null);
    store.init(context, store);
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("bar", 50L);
    assertEquals(5L, (long) store.get("foo"));
    assertEquals(50L, (long) store.get("bar"));
    assertEquals(55L, (long) store.get("all"));
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) Test(org.junit.Test)

Aggregations

InMemoryKeyValueStore (org.apache.kafka.streams.state.internals.InMemoryKeyValueStore)5 Test (org.junit.Test)4 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)3 MaterializedInternal (org.apache.kafka.streams.kstream.internals.MaterializedInternal)2 StateStore (org.apache.kafka.streams.processor.StateStore)2 KeyValueBytesStoreSupplier (org.apache.kafka.streams.state.KeyValueBytesStoreSupplier)2 NoOpProcessorContext (org.apache.kafka.test.NoOpProcessorContext)2 SharedVersionedBufferStoreImpl (com.github.fhuss.kafka.streams.cep.state.internal.SharedVersionedBufferStoreImpl)1 File (java.io.File)1 PartitionInfo (org.apache.kafka.common.PartitionInfo)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 Metrics (org.apache.kafka.common.metrics.Metrics)1 Bytes (org.apache.kafka.common.utils.Bytes)1 MockTime (org.apache.kafka.common.utils.MockTime)1 StreamsConfig (org.apache.kafka.streams.StreamsConfig)1 StreamsMetrics (org.apache.kafka.streams.StreamsMetrics)1 KeyValueStoreMaterializer (org.apache.kafka.streams.kstream.internals.KeyValueStoreMaterializer)1 TimestampedKeyValueStoreMaterializer (org.apache.kafka.streams.kstream.internals.TimestampedKeyValueStoreMaterializer)1 AbstractProcessor (org.apache.kafka.streams.processor.AbstractProcessor)1 MockProcessorContext (org.apache.kafka.streams.processor.MockProcessorContext)1