Search in sources :

Example 1 with TaskCorruptedException

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

the class StreamThreadTest method shouldCatchHandleCorruptionOnTaskCorruptedExceptionPath.

@Test
@SuppressWarnings("unchecked")
public void shouldCatchHandleCorruptionOnTaskCorruptedExceptionPath() {
    final TaskManager taskManager = EasyMock.createNiceMock(TaskManager.class);
    expect(taskManager.producerClientIds()).andStubReturn(Collections.emptySet());
    final Consumer<byte[], byte[]> consumer = mock(Consumer.class);
    final ConsumerGroupMetadata consumerGroupMetadata = mock(ConsumerGroupMetadata.class);
    consumer.subscribe((Collection<String>) anyObject(), anyObject());
    EasyMock.expectLastCall().anyTimes();
    consumer.unsubscribe();
    EasyMock.expectLastCall().anyTimes();
    expect(consumer.groupMetadata()).andStubReturn(consumerGroupMetadata);
    expect(consumerGroupMetadata.groupInstanceId()).andReturn(Optional.empty());
    EasyMock.replay(consumerGroupMetadata);
    final Task task1 = mock(Task.class);
    final Task task2 = mock(Task.class);
    final TaskId taskId1 = new TaskId(0, 0);
    final TaskId taskId2 = new TaskId(0, 2);
    final Set<TaskId> corruptedTasks = singleton(taskId1);
    expect(task1.state()).andReturn(Task.State.RUNNING).anyTimes();
    expect(task1.id()).andReturn(taskId1).anyTimes();
    expect(task2.state()).andReturn(Task.State.RUNNING).anyTimes();
    expect(task2.id()).andReturn(taskId2).anyTimes();
    expect(taskManager.handleCorruption(corruptedTasks)).andReturn(true);
    EasyMock.replay(task1, task2, taskManager, consumer);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, CLIENT_ID, StreamsConfig.METRICS_LATEST, mockTime);
    final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
    topologyMetadata.buildAndRewriteTopology();
    final StreamThread thread = new StreamThread(mockTime, config, null, consumer, consumer, null, null, taskManager, streamsMetrics, topologyMetadata, CLIENT_ID, new LogContext(""), new AtomicInteger(), new AtomicLong(Long.MAX_VALUE), new LinkedList<>(), null, HANDLER, null) {

        @Override
        void runOnce() {
            setState(State.PENDING_SHUTDOWN);
            throw new TaskCorruptedException(corruptedTasks);
        }
    }.updateThreadMetadata(getSharedAdminClientId(CLIENT_ID));
    thread.run();
    verify(taskManager);
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) LogContext(org.apache.kafka.common.utils.LogContext) LinkedList(java.util.LinkedList) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) TaskCorruptedException(org.apache.kafka.streams.errors.TaskCorruptedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) Test(org.junit.Test)

Example 2 with TaskCorruptedException

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

the class StreamThreadTest method shouldCatchTaskMigratedExceptionOnOnTaskCorruptedExceptionPath.

@Test
@SuppressWarnings("unchecked")
public void shouldCatchTaskMigratedExceptionOnOnTaskCorruptedExceptionPath() {
    final TaskManager taskManager = EasyMock.createNiceMock(TaskManager.class);
    expect(taskManager.producerClientIds()).andStubReturn(Collections.emptySet());
    final Consumer<byte[], byte[]> consumer = mock(Consumer.class);
    final ConsumerGroupMetadata consumerGroupMetadata = mock(ConsumerGroupMetadata.class);
    expect(consumer.groupMetadata()).andStubReturn(consumerGroupMetadata);
    expect(consumerGroupMetadata.groupInstanceId()).andReturn(Optional.empty());
    consumer.subscribe((Collection<String>) anyObject(), anyObject());
    EasyMock.expectLastCall().anyTimes();
    consumer.unsubscribe();
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(consumerGroupMetadata);
    final Task task1 = mock(Task.class);
    final Task task2 = mock(Task.class);
    final TaskId taskId1 = new TaskId(0, 0);
    final TaskId taskId2 = new TaskId(0, 2);
    final Set<TaskId> corruptedTasks = singleton(taskId1);
    expect(task1.state()).andReturn(Task.State.RUNNING).anyTimes();
    expect(task1.id()).andReturn(taskId1).anyTimes();
    expect(task2.state()).andReturn(Task.State.RUNNING).anyTimes();
    expect(task2.id()).andReturn(taskId2).anyTimes();
    taskManager.handleCorruption(corruptedTasks);
    expectLastCall().andThrow(new TaskMigratedException("Task migrated", new RuntimeException("non-corrupted task migrated")));
    taskManager.handleLostAll();
    expectLastCall();
    EasyMock.replay(task1, task2, taskManager, consumer);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, CLIENT_ID, StreamsConfig.METRICS_LATEST, mockTime);
    final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
    topologyMetadata.buildAndRewriteTopology();
    final StreamThread thread = new StreamThread(mockTime, config, null, consumer, consumer, null, null, taskManager, streamsMetrics, topologyMetadata, CLIENT_ID, new LogContext(""), new AtomicInteger(), new AtomicLong(Long.MAX_VALUE), new LinkedList<>(), null, HANDLER, null) {

        @Override
        void runOnce() {
            setState(State.PENDING_SHUTDOWN);
            throw new TaskCorruptedException(corruptedTasks);
        }
    }.updateThreadMetadata(getSharedAdminClientId(CLIENT_ID));
    thread.setState(StreamThread.State.STARTING);
    thread.runLoop();
    verify(taskManager);
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) LogContext(org.apache.kafka.common.utils.LogContext) LinkedList(java.util.LinkedList) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) TaskCorruptedException(org.apache.kafka.streams.errors.TaskCorruptedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException) Test(org.junit.Test)

Example 3 with TaskCorruptedException

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

the class StreamThreadTest method shouldEnforceRebalanceWhenTaskCorruptedExceptionIsThrownForAnActiveTask.

@Test
@SuppressWarnings("unchecked")
public void shouldEnforceRebalanceWhenTaskCorruptedExceptionIsThrownForAnActiveTask() {
    final TaskManager taskManager = EasyMock.createNiceMock(TaskManager.class);
    expect(taskManager.producerClientIds()).andStubReturn(Collections.emptySet());
    final Consumer<byte[], byte[]> consumer = mock(Consumer.class);
    final ConsumerGroupMetadata consumerGroupMetadata = mock(ConsumerGroupMetadata.class);
    expect(consumer.groupMetadata()).andStubReturn(consumerGroupMetadata);
    expect(consumerGroupMetadata.groupInstanceId()).andReturn(Optional.empty());
    consumer.subscribe((Collection<String>) anyObject(), anyObject());
    EasyMock.expectLastCall().anyTimes();
    consumer.unsubscribe();
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(consumerGroupMetadata);
    final Task task1 = mock(Task.class);
    final Task task2 = mock(Task.class);
    final TaskId taskId1 = new TaskId(0, 0);
    final TaskId taskId2 = new TaskId(0, 2);
    final Set<TaskId> corruptedTasks = singleton(taskId1);
    expect(task1.state()).andReturn(Task.State.RUNNING).anyTimes();
    expect(task1.id()).andReturn(taskId1).anyTimes();
    expect(task2.state()).andReturn(Task.State.CREATED).anyTimes();
    expect(task2.id()).andReturn(taskId2).anyTimes();
    expect(taskManager.handleCorruption(corruptedTasks)).andReturn(true);
    consumer.enforceRebalance();
    expectLastCall();
    EasyMock.replay(task1, task2, taskManager, consumer);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, CLIENT_ID, StreamsConfig.METRICS_LATEST, mockTime);
    final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
    topologyMetadata.buildAndRewriteTopology();
    final StreamThread thread = new StreamThread(mockTime, eosEnabledConfig, null, consumer, consumer, null, null, taskManager, streamsMetrics, topologyMetadata, CLIENT_ID, new LogContext(""), new AtomicInteger(), new AtomicLong(Long.MAX_VALUE), new LinkedList<>(), null, HANDLER, null) {

        @Override
        void runOnce() {
            setState(State.PENDING_SHUTDOWN);
            throw new TaskCorruptedException(corruptedTasks);
        }
    }.updateThreadMetadata(getSharedAdminClientId(CLIENT_ID));
    thread.setState(StreamThread.State.STARTING);
    thread.runLoop();
    verify(taskManager);
    verify(consumer);
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) LogContext(org.apache.kafka.common.utils.LogContext) LinkedList(java.util.LinkedList) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) TaskCorruptedException(org.apache.kafka.streams.errors.TaskCorruptedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) Test(org.junit.Test)

Example 4 with TaskCorruptedException

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

the class TaskManagerTest method shouldNotFailForTimeoutExceptionOnCommitWithEosAlpha.

@Test
public void shouldNotFailForTimeoutExceptionOnCommitWithEosAlpha() {
    setUpTaskManager(ProcessingMode.EXACTLY_ONCE_ALPHA);
    final StreamsProducer producer = mock(StreamsProducer.class);
    expect(activeTaskCreator.streamsProducerForTask(anyObject(TaskId.class))).andReturn(producer).andReturn(producer).andReturn(producer);
    final Map<TopicPartition, OffsetAndMetadata> offsetsT00 = singletonMap(t1p0, new OffsetAndMetadata(0L, null));
    final Map<TopicPartition, OffsetAndMetadata> offsetsT01 = singletonMap(t1p1, new OffsetAndMetadata(1L, null));
    producer.commitTransaction(offsetsT00, null);
    expectLastCall().andThrow(new TimeoutException("KABOOM!"));
    producer.commitTransaction(offsetsT00, null);
    expectLastCall();
    producer.commitTransaction(offsetsT01, null);
    expectLastCall();
    producer.commitTransaction(offsetsT01, null);
    expectLastCall();
    final StateMachineTask task00 = new StateMachineTask(taskId00, taskId00Partitions, true);
    task00.setCommittableOffsetsAndMetadata(offsetsT00);
    final StateMachineTask task01 = new StateMachineTask(taskId01, taskId01Partitions, true);
    task01.setCommittableOffsetsAndMetadata(offsetsT01);
    final StateMachineTask task02 = new StateMachineTask(taskId02, taskId02Partitions, true);
    expect(consumer.groupMetadata()).andStubReturn(null);
    replay(producer, activeTaskCreator, consumer);
    task00.setCommitNeeded();
    task01.setCommitNeeded();
    final TaskCorruptedException exception = assertThrows(TaskCorruptedException.class, () -> taskManager.commit(mkSet(task00, task01, task02)));
    assertThat(exception.corruptedTasks(), equalTo(Collections.singleton(taskId00)));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) TaskCorruptedException(org.apache.kafka.streams.errors.TaskCorruptedException) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.Test)

Example 5 with TaskCorruptedException

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

the class RecordCollectorImpl method recordSendError.

private void recordSendError(final String topic, final Exception exception, final ProducerRecord<byte[], byte[]> serializedRecord) {
    String errorMessage = String.format(SEND_EXCEPTION_MESSAGE, topic, taskId, exception.toString());
    if (isFatalException(exception)) {
        errorMessage += "\nWritten offsets would not be recorded and no more records would be sent since this is a fatal error.";
        sendException.set(new StreamsException(errorMessage, exception));
    } else if (exception instanceof ProducerFencedException || exception instanceof InvalidProducerEpochException || exception instanceof OutOfOrderSequenceException) {
        errorMessage += "\nWritten offsets would not be recorded and no more records would be sent since the producer is fenced, " + "indicating the task may be migrated out";
        sendException.set(new TaskMigratedException(errorMessage, exception));
    } else {
        if (exception instanceof RetriableException) {
            errorMessage += "\nThe broker is either slow or in bad state (like not having enough replicas) in responding the request, " + "or the connection to broker was interrupted sending the request or receiving the response. " + "\nConsider overwriting `max.block.ms` and /or " + "`delivery.timeout.ms` to a larger value to wait longer for such scenarios and avoid timeout errors";
            sendException.set(new TaskCorruptedException(Collections.singleton(taskId)));
        } else {
            if (productionExceptionHandler.handle(serializedRecord, exception) == ProductionExceptionHandlerResponse.FAIL) {
                errorMessage += "\nException handler choose to FAIL the processing, no more records would be sent.";
                sendException.set(new StreamsException(errorMessage, exception));
            } else {
                errorMessage += "\nException handler choose to CONTINUE processing in spite of this error but written offsets would not be recorded.";
                droppedRecordsSensor.record();
            }
        }
    }
    log.error(errorMessage, exception);
}
Also used : InvalidProducerEpochException(org.apache.kafka.common.errors.InvalidProducerEpochException) TaskCorruptedException(org.apache.kafka.streams.errors.TaskCorruptedException) StreamsException(org.apache.kafka.streams.errors.StreamsException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) OutOfOrderSequenceException(org.apache.kafka.common.errors.OutOfOrderSequenceException) TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException) RetriableException(org.apache.kafka.common.errors.RetriableException)

Aggregations

TaskCorruptedException (org.apache.kafka.streams.errors.TaskCorruptedException)17 TaskId (org.apache.kafka.streams.processor.TaskId)11 TopicPartition (org.apache.kafka.common.TopicPartition)10 LogContext (org.apache.kafka.common.utils.LogContext)9 Test (org.junit.Test)9 TimeoutException (org.apache.kafka.common.errors.TimeoutException)8 LinkedList (java.util.LinkedList)7 StreamsException (org.apache.kafka.streams.errors.StreamsException)7 StreamsMetricsImpl (org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)7 HashMap (java.util.HashMap)6 TaskMigratedException (org.apache.kafka.streams.errors.TaskMigratedException)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 ConsumerGroupMetadata (org.apache.kafka.clients.consumer.ConsumerGroupMetadata)5 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)5 KafkaException (org.apache.kafka.common.KafkaException)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4