Search in sources :

Example 11 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method shouldThrowProcessorStateExceptionOnCloseIfStoreThrowsAnException.

@Test
public void shouldThrowProcessorStateExceptionOnCloseIfStoreThrowsAnException() throws IOException {
    final ProcessorStateManager stateManager = new ProcessorStateManager(taskId, Collections.singleton(changelogTopicPartition), false, stateDirectory, Collections.singletonMap(storeName, changelogTopic), changelogReader, false, logContext);
    final MockStateStore stateStore = new MockStateStore(storeName, true) {

        @Override
        public void close() {
            throw new RuntimeException("KABOOM!");
        }
    };
    stateManager.register(stateStore, stateStore.stateRestoreCallback);
    try {
        stateManager.close(Collections.<TopicPartition, Long>emptyMap());
        fail("Should throw ProcessorStateException if store close throws exception");
    } catch (final ProcessorStateException e) {
    // pass
    }
}
Also used : MockStateStore(org.apache.kafka.test.MockStateStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 12 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class TopologyTest method shouldThrowOnUnassignedStateStoreAccess.

@Test
public void shouldThrowOnUnassignedStateStoreAccess() throws Exception {
    final String sourceNodeName = "source";
    final String goodNodeName = "goodGuy";
    final String badNodeName = "badGuy";
    final Properties config = new Properties();
    config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "host:1");
    config.put(StreamsConfig.APPLICATION_ID_CONFIG, "appId");
    config.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getAbsolutePath());
    final StreamsConfig streamsConfig = new StreamsConfig(config);
    mockStoreBuilder();
    EasyMock.expect(storeBuilder.build()).andReturn(new MockStateStore("store", false));
    EasyMock.replay(storeBuilder);
    topology.addSource(sourceNodeName, "topic").addProcessor(goodNodeName, new LocalMockProcessorSupplier(), sourceNodeName).addStateStore(storeBuilder, goodNodeName).addProcessor(badNodeName, new LocalMockProcessorSupplier(), sourceNodeName);
    try {
        new ProcessorTopologyTestDriver(streamsConfig, topology.internalTopologyBuilder);
        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 : ProcessorTopologyTestDriver(org.apache.kafka.test.ProcessorTopologyTestDriver) StreamsException(org.apache.kafka.streams.errors.StreamsException) Properties(java.util.Properties) MockStateStore(org.apache.kafka.test.MockStateStore) Test(org.junit.Test)

Example 13 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class StandbyTaskTest method testUpdate.

@Test
public void testUpdate() throws IOException {
    StreamsConfig config = createConfig(baseDir);
    StandbyTask task = new StandbyTask(taskId, topicPartitions, topology, consumer, changelogReader, config, null, stateDirectory);
    task.initializeStateStores();
    final Set<TopicPartition> partition = Collections.singleton(partition2);
    restoreStateConsumer.assign(partition);
    for (ConsumerRecord<Integer, Integer> record : Arrays.asList(new ConsumerRecord<>(partition2.topic(), partition2.partition(), 10, 0L, TimestampType.CREATE_TIME, 0L, 0, 0, 1, 100), new ConsumerRecord<>(partition2.topic(), partition2.partition(), 20, 0L, TimestampType.CREATE_TIME, 0L, 0, 0, 2, 100), new ConsumerRecord<>(partition2.topic(), partition2.partition(), 30, 0L, TimestampType.CREATE_TIME, 0L, 0, 0, 3, 100))) {
        restoreStateConsumer.bufferRecord(record);
    }
    restoreStateConsumer.seekToBeginning(partition);
    task.update(partition2, restoreStateConsumer.poll(100).records(partition2));
    StandbyContextImpl context = (StandbyContextImpl) task.context();
    MockStateStore store1 = (MockStateStore) context.getStateMgr().getStore(storeName1);
    MockStateStore store2 = (MockStateStore) context.getStateMgr().getStore(storeName2);
    assertEquals(Collections.emptyList(), store1.keys);
    assertEquals(Utils.mkList(1, 2, 3), store2.keys);
    task.closeStateManager(true);
    File taskDir = stateDirectory.directoryForTask(taskId);
    OffsetCheckpoint checkpoint = new OffsetCheckpoint(new File(taskDir, ProcessorStateManager.CHECKPOINT_FILE_NAME));
    Map<TopicPartition, Long> offsets = checkpoint.read();
    assertEquals(1, offsets.size());
    assertEquals(new Long(30L + 1L), offsets.get(partition2));
}
Also used : OffsetCheckpoint(org.apache.kafka.streams.state.internals.OffsetCheckpoint) TopicPartition(org.apache.kafka.common.TopicPartition) MockStateStore(org.apache.kafka.test.MockStateStore) File(java.io.File) StreamsConfig(org.apache.kafka.streams.StreamsConfig) InternalStreamsBuilderTest(org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest) Test(org.junit.Test)

Example 14 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method testRegisterPersistentStore.

@Test
public void testRegisterPersistentStore() throws IOException {
    final TaskId taskId = new TaskId(0, 2);
    final MockStateStore persistentStore = getPersistentStore();
    final ProcessorStateManager stateMgr = new ProcessorStateManager(taskId, noPartitions, false, stateDirectory, new HashMap<String, String>() {

        {
            put(persistentStoreName, persistentStoreTopicName);
            put(nonPersistentStoreName, nonPersistentStoreName);
        }
    }, changelogReader, false, logContext);
    try {
        stateMgr.register(persistentStore, persistentStore.stateRestoreCallback);
        assertTrue(changelogReader.wasRegistered(new TopicPartition(persistentStoreTopicName, 2)));
    } finally {
        stateMgr.close(Collections.<TopicPartition, Long>emptyMap());
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) TopicPartition(org.apache.kafka.common.TopicPartition) MockStateStore(org.apache.kafka.test.MockStateStore) Test(org.junit.Test)

Example 15 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method testChangeLogOffsets.

@Test
public void testChangeLogOffsets() throws IOException {
    final TaskId taskId = new TaskId(0, 0);
    final long lastCheckpointedOffset = 10L;
    final String storeName1 = "store1";
    final String storeName2 = "store2";
    final String storeName3 = "store3";
    final String storeTopicName1 = ProcessorStateManager.storeChangelogTopic(applicationId, storeName1);
    final String storeTopicName2 = ProcessorStateManager.storeChangelogTopic(applicationId, storeName2);
    final String storeTopicName3 = ProcessorStateManager.storeChangelogTopic(applicationId, storeName3);
    final Map<String, String> storeToChangelogTopic = new HashMap<>();
    storeToChangelogTopic.put(storeName1, storeTopicName1);
    storeToChangelogTopic.put(storeName2, storeTopicName2);
    storeToChangelogTopic.put(storeName3, storeTopicName3);
    final OffsetCheckpoint checkpoint = new OffsetCheckpoint(new File(stateDirectory.directoryForTask(taskId), ProcessorStateManager.CHECKPOINT_FILE_NAME));
    checkpoint.write(Collections.singletonMap(new TopicPartition(storeTopicName1, 0), lastCheckpointedOffset));
    final TopicPartition partition1 = new TopicPartition(storeTopicName1, 0);
    final TopicPartition partition2 = new TopicPartition(storeTopicName2, 0);
    final TopicPartition partition3 = new TopicPartition(storeTopicName3, 1);
    final MockStateStore store1 = new MockStateStore(storeName1, true);
    final MockStateStore store2 = new MockStateStore(storeName2, true);
    final MockStateStore store3 = new MockStateStore(storeName3, true);
    // if there is a source partition, inherit the partition id
    final Set<TopicPartition> sourcePartitions = Utils.mkSet(new TopicPartition(storeTopicName3, 1));
    final ProcessorStateManager stateMgr = new ProcessorStateManager(taskId, sourcePartitions, // standby
    true, stateDirectory, storeToChangelogTopic, changelogReader, false, logContext);
    try {
        stateMgr.register(store1, store1.stateRestoreCallback);
        stateMgr.register(store2, store2.stateRestoreCallback);
        stateMgr.register(store3, store3.stateRestoreCallback);
        final Map<TopicPartition, Long> changeLogOffsets = stateMgr.checkpointed();
        assertEquals(3, changeLogOffsets.size());
        assertTrue(changeLogOffsets.containsKey(partition1));
        assertTrue(changeLogOffsets.containsKey(partition2));
        assertTrue(changeLogOffsets.containsKey(partition3));
        assertEquals(lastCheckpointedOffset, (long) changeLogOffsets.get(partition1));
        assertEquals(-1L, (long) changeLogOffsets.get(partition2));
        assertEquals(-1L, (long) changeLogOffsets.get(partition3));
    } finally {
        stateMgr.close(Collections.<TopicPartition, Long>emptyMap());
    }
}
Also used : OffsetCheckpoint(org.apache.kafka.streams.state.internals.OffsetCheckpoint) TaskId(org.apache.kafka.streams.processor.TaskId) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) File(java.io.File) MockStateStore(org.apache.kafka.test.MockStateStore) Test(org.junit.Test)

Aggregations

MockStateStore (org.apache.kafka.test.MockStateStore)15 Test (org.junit.Test)15 TopicPartition (org.apache.kafka.common.TopicPartition)6 TaskId (org.apache.kafka.streams.processor.TaskId)6 ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)4 File (java.io.File)2 HashMap (java.util.HashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 OffsetCheckpoint (org.apache.kafka.streams.state.internals.OffsetCheckpoint)2 Properties (java.util.Properties)1 StreamsConfig (org.apache.kafka.streams.StreamsConfig)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 InternalStreamsBuilderTest (org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest)1 StateRestoreCallback (org.apache.kafka.streams.processor.StateRestoreCallback)1 StateStore (org.apache.kafka.streams.processor.StateStore)1 MockBatchingStateRestoreListener (org.apache.kafka.test.MockBatchingStateRestoreListener)1 NoOpProcessorContext (org.apache.kafka.test.NoOpProcessorContext)1 ProcessorTopologyTestDriver (org.apache.kafka.test.ProcessorTopologyTestDriver)1