Search in sources :

Example 1 with MockKeyValueStore

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

the class EosIntegrationTest method verifyStateIsInStoreAndOffsetsAreInCheckpoint.

private void verifyStateIsInStoreAndOffsetsAreInCheckpoint(final int partition, final Set<KeyValue<Long, Long>> expectedState) throws IOException {
    final String stateStoreDir = stateTmpDir + File.separator + "appDir" + File.separator + applicationId + File.separator + "0_" + partition + File.separator;
    // Verify that the data in the state store on disk is fully up-to-date
    final StateStoreContext context = new MockInternalProcessorContext(new Properties(), new TaskId(0, 0), new File(stateStoreDir));
    final MockKeyValueStore stateStore = new MockKeyValueStore("store", false);
    final RocksDBStore store = (RocksDBStore) new RocksDbKeyValueBytesStoreSupplier(storeName, false).get();
    store.init(context, stateStore);
    store.all().forEachRemaining(kv -> {
        final KeyValue<Long, Long> kv2 = new KeyValue<>(new BigInteger(kv.key.get()).longValue(), new BigInteger(kv.value).longValue());
        expectedState.remove(kv2);
    });
    // Verify that the checkpointed offsets match exactly with max offset of the records in the changelog
    final OffsetCheckpoint checkpoint = new OffsetCheckpoint(new File(stateStoreDir + ".checkpoint"));
    final Map<TopicPartition, Long> checkpointedOffsets = checkpoint.read();
    checkpointedOffsets.forEach(this::verifyChangelogMaxRecordOffsetMatchesCheckpointedOffset);
}
Also used : OffsetCheckpoint(org.apache.kafka.streams.state.internals.OffsetCheckpoint) TaskId(org.apache.kafka.streams.processor.TaskId) KeyValue(org.apache.kafka.streams.KeyValue) RocksDbKeyValueBytesStoreSupplier(org.apache.kafka.streams.state.internals.RocksDbKeyValueBytesStoreSupplier) Properties(java.util.Properties) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) StateStoreContext(org.apache.kafka.streams.processor.StateStoreContext) TopicPartition(org.apache.kafka.common.TopicPartition) BigInteger(java.math.BigInteger) MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) File(java.io.File) RocksDBStore(org.apache.kafka.streams.state.internals.RocksDBStore)

Example 2 with MockKeyValueStore

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

the class TopologyTest method shouldThrowOnUnassignedStateStoreAccess.

@Test
public void shouldThrowOnUnassignedStateStoreAccess() {
    final String sourceNodeName = "source";
    final String goodNodeName = "goodGuy";
    final String badNodeName = "badGuy";
    mockStoreBuilder();
    EasyMock.expect(storeBuilder.build()).andReturn(new MockKeyValueStore("store", false));
    EasyMock.replay(storeBuilder);
    topology.addSource(sourceNodeName, "topic").addProcessor(goodNodeName, new LocalMockProcessorSupplier(), sourceNodeName).addStateStore(storeBuilder, goodNodeName).addProcessor(badNodeName, new LocalMockProcessorSupplier(), sourceNodeName);
    final Properties config = new Properties();
    config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.ByteArraySerde.class);
    config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.ByteArraySerde.class);
    try {
        new TopologyTestDriver(topology, config);
        fail("Should have thrown StreamsException");
    } catch (final StreamsException e) {
        final String error = e.toString();
        final String expectedMessage = "org.apache.kafka.streams.errors.StreamsException: failed to initialize processor " + badNodeName;
        assertThat(error, equalTo(expectedMessage));
    }
}
Also used : Serdes(org.apache.kafka.common.serialization.Serdes) StreamsException(org.apache.kafka.streams.errors.StreamsException) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with MockKeyValueStore

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

the class StateManagerUtilTest method testRegisterStateStores.

@Test
public void testRegisterStateStores() {
    final MockKeyValueStore store1 = new MockKeyValueStore("store1", false);
    final MockKeyValueStore store2 = new MockKeyValueStore("store2", false);
    final List<StateStore> stateStores = Arrays.asList(store1, store2);
    expect(topology.stateStores()).andReturn(stateStores);
    expect(stateManager.taskId()).andReturn(taskId);
    expect(stateDirectory.lock(taskId)).andReturn(true);
    expect(stateDirectory.directoryForTaskIsEmpty(taskId)).andReturn(true);
    expect(topology.stateStores()).andReturn(stateStores);
    stateManager.registerStateStores(stateStores, processorContext);
    stateManager.initializeStoreOffsetsFromCheckpoint(true);
    expectLastCall();
    ctrl.checkOrder(true);
    ctrl.replay();
    StateManagerUtil.registerStateStores(logger, "logPrefix:", topology, stateManager, stateDirectory, processorContext);
    ctrl.verify();
}
Also used : StateStore(org.apache.kafka.streams.processor.StateStore) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with MockKeyValueStore

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

the class StreamTaskTest method createDisconnectedTask.

private StreamTask createDisconnectedTask(final StreamsConfig config) {
    final MockKeyValueStore stateStore = new MockKeyValueStore(storeName, false);
    final ProcessorTopology topology = ProcessorTopologyFactories.with(asList(source1, source2), mkMap(mkEntry(topic1, source1), mkEntry(topic2, source2)), singletonList(stateStore), emptyMap());
    final MockConsumer<byte[], byte[]> consumer = new MockConsumer<byte[], byte[]>(OffsetResetStrategy.EARLIEST) {

        @Override
        public Map<TopicPartition, OffsetAndMetadata> committed(final Set<TopicPartition> partitions) {
            throw new TimeoutException("KABOOM!");
        }
    };
    final InternalProcessorContext context = new ProcessorContextImpl(taskId, config, stateManager, streamsMetrics, null);
    return new StreamTask(taskId, partitions, topology, consumer, new TopologyConfig(null, config, new Properties()).getTaskConfig(), streamsMetrics, stateDirectory, cache, time, stateManager, recordCollector, context, logContext);
}
Also used : Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) HashSet(java.util.HashSet) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) Utils.mkProperties(org.apache.kafka.common.utils.Utils.mkProperties) Properties(java.util.Properties) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) TopologyConfig(org.apache.kafka.streams.processor.internals.namedtopology.TopologyConfig) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 5 with MockKeyValueStore

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

the class StreamTaskTest method createStatefulTask.

private StreamTask createStatefulTask(final StreamsConfig config, final boolean logged, final ProcessorStateManager stateManager) {
    final MockKeyValueStore stateStore = new MockKeyValueStore(storeName, logged);
    final ProcessorTopology topology = ProcessorTopologyFactories.with(asList(source1, source2), mkMap(mkEntry(topic1, source1), mkEntry(topic2, source2)), singletonList(stateStore), logged ? Collections.singletonMap(storeName, storeName + "-changelog") : Collections.emptyMap());
    final InternalProcessorContext context = new ProcessorContextImpl(taskId, config, stateManager, streamsMetrics, null);
    return new StreamTask(taskId, partitions, topology, consumer, new TopologyConfig(null, config, new Properties()).getTaskConfig(), streamsMetrics, stateDirectory, cache, time, stateManager, recordCollector, context, logContext);
}
Also used : MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) Utils.mkProperties(org.apache.kafka.common.utils.Utils.mkProperties) Properties(java.util.Properties) TopologyConfig(org.apache.kafka.streams.processor.internals.namedtopology.TopologyConfig)

Aggregations

MockKeyValueStore (org.apache.kafka.test.MockKeyValueStore)15 Test (org.junit.Test)11 Properties (java.util.Properties)5 ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)4 Utils.mkProperties (org.apache.kafka.common.utils.Utils.mkProperties)3 StreamsException (org.apache.kafka.streams.errors.StreamsException)3 TopologyConfig (org.apache.kafka.streams.processor.internals.namedtopology.TopologyConfig)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 StateStore (org.apache.kafka.streams.processor.StateStore)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 File (java.io.File)1 BigInteger (java.math.BigInteger)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 Serdes (org.apache.kafka.common.serialization.Serdes)1 Utils.mkSet (org.apache.kafka.common.utils.Utils.mkSet)1