Search in sources :

Example 11 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method shouldThrowProcessorStateExceptionOnFlushIfStoreThrowsAnException.

@Test
public void shouldThrowProcessorStateExceptionOnFlushIfStoreThrowsAnException() 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 flush() {
            throw new RuntimeException("KABOOM!");
        }
    };
    stateManager.register(stateStore, stateStore.stateRestoreCallback);
    try {
        stateManager.flush();
        fail("Should throw ProcessorStateException if store flush 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 ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method shouldCloseAllStoresEvenIfStoreThrowsExcepiton.

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

        @Override
        public void close() {
            throw new RuntimeException("KABOOM!");
        }
    };
    final MockStateStore stateStore2 = new MockStateStore(storeName + "2", true) {

        @Override
        public void close() {
            closedStore.set(true);
        }
    };
    stateManager.register(stateStore1, stateStore1.stateRestoreCallback);
    stateManager.register(stateStore2, stateStore2.stateRestoreCallback);
    try {
        stateManager.close(Collections.<TopicPartition, Long>emptyMap());
    } catch (final ProcessorStateException expected) {
    /* ignode */
    }
    Assert.assertTrue(closedStore.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockStateStore(org.apache.kafka.test.MockStateStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 13 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException 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 14 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManager method close.

/**
 * {@link StateStore#close() Close} all stores (even in case of failure).
 * Log all exception and re-throw the first exception that did occur at the end.
 * @throws ProcessorStateException if any error happens when closing the state stores
 */
@Override
public void close(final Map<TopicPartition, Long> ackedOffsets) throws ProcessorStateException {
    ProcessorStateException firstException = null;
    // are not closed by a ProcessorNode yet
    if (!stores.isEmpty()) {
        log.debug("Closing its state manager and all the registered state stores");
        for (final StateStore store : stores.values()) {
            log.debug("Closing storage engine {}", store.name());
            try {
                store.close();
            } catch (final Exception e) {
                if (firstException == null) {
                    firstException = new ProcessorStateException(String.format("%sFailed to close state store %s", logPrefix, store.name()), e);
                }
                log.error("Failed to close state store {}: ", store.name(), e);
            }
        }
        if (ackedOffsets != null) {
            checkpoint(ackedOffsets);
        }
        stores.clear();
    }
    if (firstException != null) {
        throw firstException;
    }
}
Also used : StateStore(org.apache.kafka.streams.processor.StateStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) IOException(java.io.IOException)

Example 15 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManager method updateStandbyStates.

List<ConsumerRecord<byte[], byte[]>> updateStandbyStates(final TopicPartition storePartition, final List<ConsumerRecord<byte[], byte[]>> records) {
    final long limit = offsetLimit(storePartition);
    List<ConsumerRecord<byte[], byte[]>> remainingRecords = null;
    final List<KeyValue<byte[], byte[]>> restoreRecords = new ArrayList<>();
    // restore states from changelog records
    final BatchingStateRestoreCallback restoreCallback = getBatchingRestoreCallback(restoreCallbacks.get(storePartition.topic()));
    long lastOffset = -1L;
    int count = 0;
    for (final ConsumerRecord<byte[], byte[]> record : records) {
        if (record.offset() < limit) {
            restoreRecords.add(KeyValue.pair(record.key(), record.value()));
            lastOffset = record.offset();
        } else {
            if (remainingRecords == null) {
                remainingRecords = new ArrayList<>(records.size() - count);
            }
            remainingRecords.add(record);
        }
        count++;
    }
    if (!restoreRecords.isEmpty()) {
        try {
            restoreCallback.restoreAll(restoreRecords);
        } catch (final Exception e) {
            throw new ProcessorStateException(String.format("%sException caught while trying to restore state from %s", logPrefix, storePartition), e);
        }
    }
    // record the restored offset for its change log partition
    restoredOffsets.put(storePartition, lastOffset + 1);
    return remainingRecords;
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) BatchingStateRestoreCallback(org.apache.kafka.streams.processor.BatchingStateRestoreCallback) ArrayList(java.util.ArrayList) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) OffsetCheckpoint(org.apache.kafka.streams.state.internals.OffsetCheckpoint) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) IOException(java.io.IOException)

Aggregations

ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)68 Test (org.junit.Test)23 IOException (java.io.IOException)19 File (java.io.File)15 RocksDBException (org.rocksdb.RocksDBException)11 StreamsException (org.apache.kafka.streams.errors.StreamsException)7 StateStore (org.apache.kafka.streams.processor.StateStore)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 WriteBatch (org.rocksdb.WriteBatch)5 ParseException (java.text.ParseException)4 Map (java.util.Map)4 MetricName (org.apache.kafka.common.MetricName)4 TopicPartition (org.apache.kafka.common.TopicPartition)4 LockException (org.apache.kafka.streams.errors.LockException)4 MockKeyValueStore (org.apache.kafka.test.MockKeyValueStore)4 MockStateStore (org.apache.kafka.test.MockStateStore)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)3