Search in sources :

Example 11 with TaskMigratedException

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

the class TaskManager method suspendTasksAndState.

/**
 * Similar to shutdownTasksAndState, however does not close the task managers, in the hope that
 * soon the tasks will be assigned again
 * @throws TaskMigratedException if the task producer got fenced (EOS only)
 */
void suspendTasksAndState() {
    log.debug("Suspending all active tasks {} and standby tasks {}", active.runningTaskIds(), standby.runningTaskIds());
    final AtomicReference<RuntimeException> firstException = new AtomicReference<>(null);
    firstException.compareAndSet(null, active.suspend());
    firstException.compareAndSet(null, standby.suspend());
    // remove the changelog partitions from restore consumer
    restoreConsumer.unsubscribe();
    final Exception exception = firstException.get();
    if (exception != null) {
        throw new StreamsException(logPrefix + "failed to suspend stream tasks", exception);
    }
}
Also used : StreamsException(org.apache.kafka.streams.errors.StreamsException) AtomicReference(java.util.concurrent.atomic.AtomicReference) TaskIdFormatException(org.apache.kafka.streams.errors.TaskIdFormatException) StreamsException(org.apache.kafka.streams.errors.StreamsException) TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException)

Example 12 with TaskMigratedException

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

the class StreamTask method commitOffsets.

/**
 * @throws TaskMigratedException if committing offsets failed (non-EOS)
 *                               or if the task producer got fenced (EOS)
 */
private void commitOffsets(final boolean startNewTransaction) {
    try {
        if (commitOffsetNeeded) {
            log.trace("Committing offsets");
            final Map<TopicPartition, OffsetAndMetadata> consumedOffsetsAndMetadata = new HashMap<>(consumedOffsets.size());
            for (final Map.Entry<TopicPartition, Long> entry : consumedOffsets.entrySet()) {
                final TopicPartition partition = entry.getKey();
                final long offset = entry.getValue() + 1;
                consumedOffsetsAndMetadata.put(partition, new OffsetAndMetadata(offset));
                stateMgr.putOffsetLimit(partition, offset);
            }
            if (eosEnabled) {
                producer.sendOffsetsToTransaction(consumedOffsetsAndMetadata, applicationId);
                producer.commitTransaction();
                transactionInFlight = false;
                if (startNewTransaction) {
                    transactionInFlight = true;
                    producer.beginTransaction();
                }
            } else {
                consumer.commitSync(consumedOffsetsAndMetadata);
            }
            commitOffsetNeeded = false;
        } else if (eosEnabled && !startNewTransaction && transactionInFlight) {
            // need to make sure to commit txn for suspend case
            producer.commitTransaction();
            transactionInFlight = false;
        }
    } catch (final CommitFailedException | ProducerFencedException fatal) {
        throw new TaskMigratedException(this, fatal);
    }
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) HashMap(java.util.HashMap) Map(java.util.Map) CommitFailedException(org.apache.kafka.clients.consumer.CommitFailedException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException)

Example 13 with TaskMigratedException

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

the class StreamThreadTest method shouldCloseTaskAsZombieAndRemoveFromActiveTasksIfProducerWasFencedWhileProcessing.

@Test
public void shouldCloseTaskAsZombieAndRemoveFromActiveTasksIfProducerWasFencedWhileProcessing() throws Exception {
    internalTopologyBuilder.addSource(null, "source", null, null, null, topic1);
    internalTopologyBuilder.addSink("sink", "dummyTopic", null, null, null, "source");
    final StreamThread thread = createStreamThread(clientId, new StreamsConfig(configProps(true)), true);
    final MockConsumer<byte[], byte[]> consumer = clientSupplier.consumer;
    consumer.updatePartitions(topic1, Collections.singletonList(new PartitionInfo(topic1, 1, null, null, null)));
    thread.setState(StreamThread.State.RUNNING);
    thread.rebalanceListener.onPartitionsRevoked(null);
    final Map<TaskId, Set<TopicPartition>> activeTasks = new HashMap<>();
    final List<TopicPartition> assignedPartitions = new ArrayList<>();
    // assign single partition
    assignedPartitions.add(t1p1);
    activeTasks.put(task1, Collections.singleton(t1p1));
    thread.taskManager().setAssignmentMetadata(activeTasks, Collections.<TaskId, Set<TopicPartition>>emptyMap());
    final MockConsumer<byte[], byte[]> mockConsumer = (MockConsumer<byte[], byte[]>) thread.consumer;
    mockConsumer.assign(assignedPartitions);
    mockConsumer.updateBeginningOffsets(Collections.singletonMap(t1p1, 0L));
    thread.rebalanceListener.onPartitionsAssigned(assignedPartitions);
    thread.runOnce(-1);
    assertThat(thread.tasks().size(), equalTo(1));
    final MockProducer producer = clientSupplier.producers.get(0);
    // change consumer subscription from "pattern" to "manual" to be able to call .addRecords()
    consumer.updateBeginningOffsets(Collections.singletonMap(assignedPartitions.iterator().next(), 0L));
    consumer.unsubscribe();
    consumer.assign(new HashSet<>(assignedPartitions));
    consumer.addRecord(new ConsumerRecord<>(topic1, 1, 0, new byte[0], new byte[0]));
    mockTime.sleep(config.getLong(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG) + 1);
    thread.runOnce(-1);
    assertThat(producer.history().size(), equalTo(1));
    assertFalse(producer.transactionCommitted());
    mockTime.sleep(config.getLong(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG) + 1L);
    TestUtils.waitForCondition(new TestCondition() {

        @Override
        public boolean conditionMet() {
            return producer.commitCount() == 1;
        }
    }, "StreamsThread did not commit transaction.");
    producer.fenceProducer();
    mockTime.sleep(config.getLong(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG) + 1L);
    consumer.addRecord(new ConsumerRecord<>(topic1, 1, 0, new byte[0], new byte[0]));
    try {
        thread.runOnce(-1);
        fail("Should have thrown TaskMigratedException");
    } catch (final TaskMigratedException expected) {
    /* ignore */
    }
    TestUtils.waitForCondition(new TestCondition() {

        @Override
        public boolean conditionMet() {
            return thread.tasks().isEmpty();
        }
    }, "StreamsThread did not remove fenced zombie task.");
    assertThat(producer.commitCount(), equalTo(1L));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Set(java.util.Set) HashSet(java.util.HashSet) MockProducer(org.apache.kafka.clients.producer.MockProducer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TopicPartition(org.apache.kafka.common.TopicPartition) TestCondition(org.apache.kafka.test.TestCondition) PartitionInfo(org.apache.kafka.common.PartitionInfo) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) StreamsConfig(org.apache.kafka.streams.StreamsConfig) TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException) InternalStreamsBuilderTest(org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest) Test(org.junit.Test)

Example 14 with TaskMigratedException

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

the class AssignedStreamsTasksTest method shouldCloseTaskOnSuspendIfTaskMigratedException.

@Test
public void shouldCloseTaskOnSuspendIfTaskMigratedException() {
    mockTaskInitialization();
    t1.suspend();
    EasyMock.expectLastCall().andThrow(new TaskMigratedException(t1));
    t1.close(false, true);
    EasyMock.expectLastCall();
    EasyMock.replay(t1);
    assertThat(suspendTask(), nullValue());
    assertTrue(assignedTasks.previousTaskIds().isEmpty());
    EasyMock.verify(t1);
}
Also used : TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException) Test(org.junit.Test)

Example 15 with TaskMigratedException

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

the class AssignedStreamsTasksTest method shouldCloseTaskOnMaybePunctuateStreamTimeIfTaskMigratedException.

@Test
public void shouldCloseTaskOnMaybePunctuateStreamTimeIfTaskMigratedException() {
    mockTaskInitialization();
    t1.maybePunctuateStreamTime();
    EasyMock.expectLastCall().andThrow(new TaskMigratedException(t1));
    t1.close(false, true);
    EasyMock.expectLastCall();
    EasyMock.replay(t1);
    addAndInitTask();
    try {
        assignedTasks.punctuate();
        fail("Should have thrown TaskMigratedException.");
    } catch (final TaskMigratedException expected) {
    /* ignore */
    }
    assertThat(assignedTasks.runningTaskIds(), equalTo(Collections.EMPTY_SET));
    EasyMock.verify(t1);
}
Also used : TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException) Test(org.junit.Test)

Aggregations

TaskMigratedException (org.apache.kafka.streams.errors.TaskMigratedException)16 Test (org.junit.Test)9 TopicPartition (org.apache.kafka.common.TopicPartition)6 HashMap (java.util.HashMap)4 ProducerFencedException (org.apache.kafka.common.errors.ProducerFencedException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 StreamsException (org.apache.kafka.streams.errors.StreamsException)3 Map (java.util.Map)2 MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)2 StreamsConfig (org.apache.kafka.streams.StreamsConfig)2 InternalStreamsBuilderTest (org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest)2 TaskId (org.apache.kafka.streams.processor.TaskId)2 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CommitFailedException (org.apache.kafka.clients.consumer.CommitFailedException)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 InvalidOffsetException (org.apache.kafka.clients.consumer.InvalidOffsetException)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1