Search in sources :

Example 21 with ProcessorStateException

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

the class StateManagerUtilTest method testCloseStateManagerThrowsExceptionWhenClean.

@Test
public void testCloseStateManagerThrowsExceptionWhenClean() {
    expect(stateManager.taskId()).andReturn(taskId);
    expect(stateDirectory.lock(taskId)).andReturn(true);
    stateManager.close();
    expectLastCall().andThrow(new ProcessorStateException("state manager failed to close"));
    // The unlock logic should still be executed.
    stateDirectory.unlock(taskId);
    ctrl.checkOrder(true);
    ctrl.replay();
    final ProcessorStateException thrown = assertThrows(ProcessorStateException.class, () -> StateManagerUtil.closeStateManager(logger, "logPrefix:", true, false, stateManager, stateDirectory, TaskType.ACTIVE));
    // Thrown stateMgr exception will not be wrapped.
    assertEquals("state manager failed to close", thrown.getMessage());
    ctrl.verify();
}
Also used : ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with ProcessorStateException

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

the class StateManagerUtilTest method testCloseStateManagerThrowsExceptionWhenDirty.

@Test
public void testCloseStateManagerThrowsExceptionWhenDirty() {
    expect(stateManager.taskId()).andReturn(taskId);
    expect(stateDirectory.lock(taskId)).andReturn(true);
    stateManager.close();
    expectLastCall().andThrow(new ProcessorStateException("state manager failed to close"));
    stateDirectory.unlock(taskId);
    ctrl.checkOrder(true);
    ctrl.replay();
    assertThrows(ProcessorStateException.class, () -> StateManagerUtil.closeStateManager(logger, "logPrefix:", false, false, stateManager, stateDirectory, TaskType.ACTIVE));
    ctrl.verify();
}
Also used : ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with ProcessorStateException

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

the class StreamTaskTest method shouldThrowOnCloseCleanFlushError.

@Test
public void shouldThrowOnCloseCleanFlushError() {
    final long offset = 543L;
    EasyMock.expect(recordCollector.offsets()).andReturn(singletonMap(changelogPartition, offset));
    stateManager.flushCache();
    EasyMock.expectLastCall().andThrow(new ProcessorStateException("KABOOM!")).anyTimes();
    stateManager.flush();
    EasyMock.expectLastCall().andThrow(new AssertionError("Flush should not be called")).anyTimes();
    stateManager.checkpoint();
    EasyMock.expectLastCall().andThrow(new AssertionError("Checkpoint should not be called")).anyTimes();
    stateManager.close();
    EasyMock.expectLastCall().andThrow(new AssertionError("Close should not be called!")).anyTimes();
    EasyMock.expect(stateManager.changelogPartitions()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.expect(stateManager.changelogOffsets()).andReturn(emptyMap()).anyTimes();
    EasyMock.replay(recordCollector, stateManager);
    final MetricName metricName = setupCloseTaskMetric();
    task = createOptimizedStatefulTask(createConfig("100"), consumer);
    task.initializeIfNeeded();
    task.completeRestoration(noOpResetter -> {
    });
    // process one record to make commit needed
    task.addRecords(partition1, singletonList(getConsumerRecordWithOffsetAsTimestamp(partition1, offset)));
    task.process(100L);
    assertThrows(ProcessorStateException.class, task::prepareCommit);
    assertEquals(RUNNING, task.state());
    final double expectedCloseTaskMetric = 0.0;
    verifyCloseTaskMetric(expectedCloseTaskMetric, streamsMetrics, metricName);
    EasyMock.verify(stateManager);
    EasyMock.reset(stateManager);
    EasyMock.expect(stateManager.changelogPartitions()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.replay(stateManager);
}
Also used : MetricName(org.apache.kafka.common.MetricName) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 24 with ProcessorStateException

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

the class StreamTaskTest method shouldThrowExceptionOnCloseCleanError.

@Test
public void shouldThrowExceptionOnCloseCleanError() {
    final long offset = 543L;
    EasyMock.expect(recordCollector.offsets()).andReturn(emptyMap()).anyTimes();
    stateManager.checkpoint();
    EasyMock.expectLastCall().once();
    EasyMock.expect(stateManager.changelogPartitions()).andReturn(singleton(changelogPartition)).anyTimes();
    EasyMock.expect(stateManager.changelogOffsets()).andReturn(singletonMap(changelogPartition, offset)).anyTimes();
    stateManager.close();
    EasyMock.expectLastCall().andThrow(new ProcessorStateException("KABOOM!")).anyTimes();
    EasyMock.replay(recordCollector, stateManager);
    final MetricName metricName = setupCloseTaskMetric();
    task = createOptimizedStatefulTask(createConfig("100"), consumer);
    task.initializeIfNeeded();
    task.completeRestoration(noOpResetter -> {
    });
    task.addRecords(partition1, singletonList(getConsumerRecordWithOffsetAsTimestamp(partition1, offset)));
    task.process(100L);
    assertTrue(task.commitNeeded());
    task.suspend();
    task.prepareCommit();
    task.postCommit(true);
    assertThrows(ProcessorStateException.class, () -> task.closeClean());
    final double expectedCloseTaskMetric = 0.0;
    verifyCloseTaskMetric(expectedCloseTaskMetric, streamsMetrics, metricName);
    EasyMock.verify(stateManager);
    EasyMock.reset(stateManager);
    EasyMock.expect(stateManager.changelogPartitions()).andStubReturn(singleton(changelogPartition));
    stateManager.close();
    EasyMock.expectLastCall();
    EasyMock.replay(stateManager);
}
Also used : MetricName(org.apache.kafka.common.MetricName) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 25 with ProcessorStateException

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

the class ProcessorStateManager method flush.

/**
 * @throws TaskMigratedException recoverable error sending changelog records that would cause the task to be removed
 * @throws StreamsException fatal error when flushing the state store, for example sending changelog records failed
 *                          or flushing state store get IO errors; such error should cause the thread to die
 */
@Override
public void flush() {
    RuntimeException firstException = null;
    // attempting to flush the stores
    if (!stores.isEmpty()) {
        log.debug("Flushing all stores registered in the state manager: {}", stores);
        for (final StateStoreMetadata metadata : stores.values()) {
            final StateStore store = metadata.stateStore;
            log.trace("Flushing store {}", store.name());
            try {
                store.flush();
            } catch (final RuntimeException exception) {
                if (firstException == null) {
                    // do NOT wrap the error if it is actually caused by Streams itself
                    if (exception instanceof StreamsException)
                        firstException = exception;
                    else
                        firstException = new ProcessorStateException(format("%sFailed to flush state store %s", logPrefix, store.name()), exception);
                }
                log.error("Failed to flush state store {}: ", store.name(), exception);
            }
        }
    }
    if (firstException != null) {
        throw firstException;
    }
}
Also used : StreamsException(org.apache.kafka.streams.errors.StreamsException) CachedStateStore(org.apache.kafka.streams.state.internals.CachedStateStore) StateStore(org.apache.kafka.streams.processor.StateStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException)

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