Search in sources :

Example 1 with StateStoreMetadata

use of org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata in project kafka by apache.

the class ProcessorStateManagerTest method shouldOverrideOffsetsWhenRestoreAndProcess.

@Test
public void shouldOverrideOffsetsWhenRestoreAndProcess() throws IOException {
    final Map<TopicPartition, Long> offsets = singletonMap(persistentStorePartition, 99L);
    checkpoint.write(offsets);
    final ProcessorStateManager stateMgr = getStateManager(Task.TaskType.ACTIVE);
    try {
        stateMgr.registerStore(persistentStore, persistentStore.stateRestoreCallback, null);
        stateMgr.initializeStoreOffsetsFromCheckpoint(true);
        final StateStoreMetadata storeMetadata = stateMgr.storeMetadata(persistentStorePartition);
        assertThat(storeMetadata, notNullValue());
        assertThat(storeMetadata.offset(), equalTo(99L));
        stateMgr.restore(storeMetadata, singletonList(consumerRecord));
        assertThat(storeMetadata.offset(), equalTo(100L));
        // should ignore irrelevant topic partitions
        stateMgr.updateChangelogOffsets(mkMap(mkEntry(persistentStorePartition, 220L), mkEntry(irrelevantPartition, 9000L)));
        stateMgr.checkpoint();
        assertThat(stateMgr.storeMetadata(irrelevantPartition), equalTo(null));
        assertThat(storeMetadata.offset(), equalTo(220L));
    } finally {
        stateMgr.close();
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) StateStoreMetadata(org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata) Test(org.junit.Test)

Example 2 with StateStoreMetadata

use of org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata in project kafka by apache.

the class ProcessorStateManagerTest method shouldThrowIfRestoreCallbackThrows.

@Test
public void shouldThrowIfRestoreCallbackThrows() {
    final ProcessorStateManager stateMgr = getStateManager(Task.TaskType.ACTIVE);
    stateMgr.registerStore(persistentStore, (key, value) -> {
        throw new RuntimeException("KABOOM!");
    }, null);
    final StateStoreMetadata storeMetadata = stateMgr.storeMetadata(persistentStorePartition);
    try {
        stateMgr.restore(storeMetadata, singletonList(consumerRecord));
        fail("should have thrown processor state exception when IO exception happens");
    } catch (final ProcessorStateException e) {
    // pass
    }
}
Also used : StateStoreMetadata(org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 3 with StateStoreMetadata

use of org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata in project kafka by apache.

the class ProcessorStateManagerTest method shouldRestoreTimestampedStoreWithConverter.

@Test
public void shouldRestoreTimestampedStoreWithConverter() {
    final ProcessorStateManager stateMgr = getStateManager(Task.TaskType.ACTIVE);
    final MockKeyValueStore store = getConverterStore();
    try {
        stateMgr.registerStore(store, store.stateRestoreCallback, null);
        final StateStoreMetadata storeMetadata = stateMgr.storeMetadata(persistentStorePartition);
        assertThat(storeMetadata, notNullValue());
        stateMgr.restore(storeMetadata, singletonList(consumerRecord));
        assertThat(store.keys.size(), is(1));
        assertTrue(store.keys.contains(key));
        // we just check timestamped value length
        assertEquals(17, store.values.get(0).length);
    } finally {
        stateMgr.close();
    }
}
Also used : StateStoreMetadata(org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) Test(org.junit.Test)

Example 4 with StateStoreMetadata

use of org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata in project kafka by apache.

the class ProcessorStateManagerTest method shouldWriteCheckpointForPersistentStore.

@Test
public void shouldWriteCheckpointForPersistentStore() throws IOException {
    final ProcessorStateManager stateMgr = getStateManager(Task.TaskType.ACTIVE);
    try {
        stateMgr.registerStore(persistentStore, persistentStore.stateRestoreCallback, null);
        stateMgr.initializeStoreOffsetsFromCheckpoint(true);
        final StateStoreMetadata storeMetadata = stateMgr.storeMetadata(persistentStorePartition);
        assertThat(storeMetadata, notNullValue());
        stateMgr.restore(storeMetadata, singletonList(consumerRecord));
        stateMgr.checkpoint();
        final Map<TopicPartition, Long> read = checkpoint.read();
        assertThat(read, equalTo(singletonMap(persistentStorePartition, 100L)));
    } finally {
        stateMgr.close();
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) StateStoreMetadata(org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata) Test(org.junit.Test)

Example 5 with StateStoreMetadata

use of org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata in project kafka by apache.

the class ProcessorStateManagerTest method shouldRestoreStoreWithRestoreCallback.

@Test
public void shouldRestoreStoreWithRestoreCallback() {
    final MockRestoreCallback restoreCallback = new MockRestoreCallback();
    final KeyValue<byte[], byte[]> expectedKeyValue = KeyValue.pair(keyBytes, valueBytes);
    final ProcessorStateManager stateMgr = getStateManager(Task.TaskType.ACTIVE);
    try {
        stateMgr.registerStore(persistentStore, restoreCallback, null);
        final StateStoreMetadata storeMetadata = stateMgr.storeMetadata(persistentStorePartition);
        assertThat(storeMetadata, notNullValue());
        stateMgr.restore(storeMetadata, singletonList(consumerRecord));
        assertThat(restoreCallback.restored.size(), is(1));
        assertTrue(restoreCallback.restored.contains(expectedKeyValue));
        assertEquals(Collections.singletonMap(persistentStorePartition, 101L), stateMgr.changelogOffsets());
    } finally {
        stateMgr.close();
    }
}
Also used : MockRestoreCallback(org.apache.kafka.test.MockRestoreCallback) StateStoreMetadata(org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata) Test(org.junit.Test)

Aggregations

StateStoreMetadata (org.apache.kafka.streams.processor.internals.ProcessorStateManager.StateStoreMetadata)10 Test (org.junit.Test)7 TopicPartition (org.apache.kafka.common.TopicPartition)5 ExecutionException (java.util.concurrent.ExecutionException)2 InvalidOffsetException (org.apache.kafka.clients.consumer.InvalidOffsetException)2 KafkaException (org.apache.kafka.common.KafkaException)2 TimeoutException (org.apache.kafka.common.errors.TimeoutException)2 StreamsException (org.apache.kafka.streams.errors.StreamsException)2 TaskCorruptedException (org.apache.kafka.streams.errors.TaskCorruptedException)2 HashSet (java.util.HashSet)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)1 MockKeyValueStore (org.apache.kafka.test.MockKeyValueStore)1 MockRestoreCallback (org.apache.kafka.test.MockRestoreCallback)1