Search in sources :

Example 51 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project kafka by apache.

the class ProcessorStateManager method checkpoint.

@Override
public void checkpoint() {
    // checkpoint those stores that are only logged and persistent to the checkpoint file
    final Map<TopicPartition, Long> checkpointingOffsets = new HashMap<>();
    for (final StateStoreMetadata storeMetadata : stores.values()) {
        if (storeMetadata.commitCallback != null && !storeMetadata.corrupted) {
            try {
                storeMetadata.commitCallback.onCommit();
            } catch (final IOException e) {
                throw new ProcessorStateException(format("%sException caught while trying to checkpoint store, " + "changelog partition %s", logPrefix, storeMetadata.changelogPartition), e);
            }
        }
        // store is logged, persistent, not corrupted, and has a valid current offset
        if (storeMetadata.changelogPartition != null && storeMetadata.stateStore.persistent() && !storeMetadata.corrupted) {
            final long checkpointableOffset = checkpointableOffsetFromChangelogOffset(storeMetadata.offset);
            checkpointingOffsets.put(storeMetadata.changelogPartition, checkpointableOffset);
        }
    }
    log.debug("Writing checkpoint: {}", checkpointingOffsets);
    try {
        checkpointFile.write(checkpointingOffsets);
    } catch (final IOException e) {
        log.warn("Failed to write offset checkpoint file to [{}]." + " This may occur if OS cleaned the state.dir in case when it located in ${java.io.tmpdir} directory." + " This may also occur due to running multiple instances on the same machine using the same state dir." + " Changing the location of state.dir may resolve the problem.", checkpointFile, e);
    }
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) IOException(java.io.IOException) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException)

Example 52 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project kafka by apache.

the class ProcessorStateManagerTest method shouldThrowIllegalStateIfInitializingOffsetsForCorruptedTasks.

@Test
public void shouldThrowIllegalStateIfInitializingOffsetsForCorruptedTasks() {
    final ProcessorStateManager stateMgr = getStateManager(Task.TaskType.ACTIVE, true);
    try {
        stateMgr.registerStore(persistentStore, persistentStore.stateRestoreCallback, null);
        stateMgr.markChangelogAsCorrupted(mkSet(persistentStorePartition));
        final ProcessorStateException thrown = assertThrows(ProcessorStateException.class, () -> stateMgr.initializeStoreOffsetsFromCheckpoint(true));
        assertTrue(thrown.getCause() instanceof IllegalStateException);
    } finally {
        stateMgr.close();
    }
}
Also used : ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 53 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project kafka by apache.

the class ProcessorStateManagerTest method shouldThrowIfLoadCheckpointThrows.

@Test
public void shouldThrowIfLoadCheckpointThrows() throws Exception {
    final ProcessorStateManager stateMgr = getStateManager(Task.TaskType.ACTIVE);
    stateMgr.registerStore(persistentStore, persistentStore.stateRestoreCallback, null);
    final File file = new File(stateMgr.baseDir(), CHECKPOINT_FILE_NAME);
    file.createNewFile();
    final FileWriter writer = new FileWriter(file);
    writer.write("abcdefg");
    writer.close();
    try {
        stateMgr.initializeStoreOffsetsFromCheckpoint(true);
        fail("should have thrown processor state exception when IO exception happens");
    } catch (final ProcessorStateException e) {
    // pass
    }
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 54 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project kafka by apache.

the class ProcessorStateManagerTest method shouldCloseAllStoresEvenIfStoreThrowsException.

@Test
public void shouldCloseAllStoresEvenIfStoreThrowsException() {
    final AtomicBoolean closedStore = new AtomicBoolean(false);
    final MockKeyValueStore stateStore1 = new MockKeyValueStore(persistentStoreName, true) {

        @Override
        public void close() {
            throw new RuntimeException("KABOOM!");
        }
    };
    final MockKeyValueStore stateStore2 = new MockKeyValueStore(persistentStoreTwoName, true) {

        @Override
        public void close() {
            closedStore.set(true);
        }
    };
    final ProcessorStateManager stateManager = getStateManager(Task.TaskType.ACTIVE);
    stateManager.registerStore(stateStore1, stateStore1.stateRestoreCallback, null);
    stateManager.registerStore(stateStore2, stateStore2.stateRestoreCallback, null);
    try {
        stateManager.close();
    } catch (final ProcessorStateException expected) {
    /* ignore */
    }
    Assert.assertTrue(closedStore.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 55 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project kafka by apache.

the class RocksDBStore method openRocksDB.

void openRocksDB(final DBOptions dbOptions, final ColumnFamilyOptions columnFamilyOptions) {
    final List<ColumnFamilyDescriptor> columnFamilyDescriptors = Collections.singletonList(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, columnFamilyOptions));
    final List<ColumnFamilyHandle> columnFamilies = new ArrayList<>(columnFamilyDescriptors.size());
    try {
        db = RocksDB.open(dbOptions, dbDir.getAbsolutePath(), columnFamilyDescriptors, columnFamilies);
        dbAccessor = new SingleColumnFamilyAccessor(columnFamilies.get(0));
    } catch (final RocksDBException e) {
        throw new ProcessorStateException("Error opening store " + name + " at location " + dbDir.toString(), e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) ArrayList(java.util.ArrayList) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

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