Search in sources :

Example 21 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class StreamThreadTest method shouldShutdownTaskManagerOnCloseWithoutStart.

@Test
public void shouldShutdownTaskManagerOnCloseWithoutStart() {
    final Consumer<byte[], byte[]> consumer = EasyMock.createNiceMock(Consumer.class);
    final ConsumerGroupMetadata consumerGroupMetadata = mock(ConsumerGroupMetadata.class);
    expect(consumer.groupMetadata()).andStubReturn(consumerGroupMetadata);
    expect(consumerGroupMetadata.groupInstanceId()).andReturn(Optional.empty());
    EasyMock.replay(consumerGroupMetadata);
    final TaskManager taskManager = EasyMock.createNiceMock(TaskManager.class);
    expect(taskManager.producerClientIds()).andStubReturn(Collections.emptySet());
    taskManager.shutdown(true);
    EasyMock.expectLastCall();
    EasyMock.replay(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 = buildStreamThread(consumer, taskManager, config, topologyMetadata).updateThreadMetadata(getSharedAdminClientId(CLIENT_ID));
    thread.shutdown();
    verify(taskManager);
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) Test(org.junit.Test)

Example 22 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class StreamThreadTest method runAndVerifyFailedStreamThreadRecording.

public void runAndVerifyFailedStreamThreadRecording(final boolean shouldFail) {
    final Consumer<byte[], byte[]> consumer = EasyMock.createNiceMock(Consumer.class);
    final ConsumerGroupMetadata consumerGroupMetadata = mock(ConsumerGroupMetadata.class);
    expect(consumer.groupMetadata()).andStubReturn(consumerGroupMetadata);
    expect(consumerGroupMetadata.groupInstanceId()).andReturn(Optional.empty());
    EasyMock.replay(consumer, consumerGroupMetadata);
    final TaskManager taskManager = EasyMock.createNiceMock(TaskManager.class);
    expect(taskManager.producerClientIds()).andStubReturn(Collections.emptySet());
    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, (e, b) -> {
    }, null) {

        @Override
        void runOnce() {
            setState(StreamThread.State.PENDING_SHUTDOWN);
            if (shouldFail) {
                throw new StreamsException(Thread.currentThread().getName());
            }
        }
    };
    EasyMock.replay(taskManager);
    thread.updateThreadMetadata("metadata");
    thread.run();
    final Metric failedThreads = StreamsTestUtils.getMetricByName(metrics.metrics(), "failed-stream-threads", "stream-metrics");
    assertThat(failedThreads.metricValue(), is(shouldFail ? 1.0 : 0.0));
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamsException(org.apache.kafka.streams.errors.StreamsException) LogContext(org.apache.kafka.common.utils.LogContext) Metric(org.apache.kafka.common.Metric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)

Example 23 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class TaskManagerTest method shouldCommitAllActiveTasksThatNeedCommittingOnHandleRevocationWithEosV2.

@Test
public void shouldCommitAllActiveTasksThatNeedCommittingOnHandleRevocationWithEosV2() {
    final StreamsProducer producer = mock(StreamsProducer.class);
    setUpTaskManager(ProcessingMode.EXACTLY_ONCE_V2);
    final StateMachineTask task00 = new StateMachineTask(taskId00, taskId00Partitions, true);
    final Map<TopicPartition, OffsetAndMetadata> offsets00 = singletonMap(t1p0, new OffsetAndMetadata(0L, null));
    task00.setCommittableOffsetsAndMetadata(offsets00);
    task00.setCommitNeeded();
    final StateMachineTask task01 = new StateMachineTask(taskId01, taskId01Partitions, true);
    final Map<TopicPartition, OffsetAndMetadata> offsets01 = singletonMap(t1p1, new OffsetAndMetadata(1L, null));
    task01.setCommittableOffsetsAndMetadata(offsets01);
    task01.setCommitNeeded();
    final StateMachineTask task02 = new StateMachineTask(taskId02, taskId02Partitions, true);
    final Map<TopicPartition, OffsetAndMetadata> offsets02 = singletonMap(t1p2, new OffsetAndMetadata(2L, null));
    task02.setCommittableOffsetsAndMetadata(offsets02);
    final StateMachineTask task10 = new StateMachineTask(taskId10, taskId10Partitions, false);
    final Map<TopicPartition, OffsetAndMetadata> expectedCommittedOffsets = new HashMap<>();
    expectedCommittedOffsets.putAll(offsets00);
    expectedCommittedOffsets.putAll(offsets01);
    final Map<TaskId, Set<TopicPartition>> assignmentActive = mkMap(mkEntry(taskId00, taskId00Partitions), mkEntry(taskId01, taskId01Partitions), mkEntry(taskId02, taskId02Partitions));
    final Map<TaskId, Set<TopicPartition>> assignmentStandby = mkMap(mkEntry(taskId10, taskId10Partitions));
    expectRestoreToBeCompleted(consumer, changeLogReader);
    expect(activeTaskCreator.createTasks(anyObject(), eq(assignmentActive))).andReturn(asList(task00, task01, task02));
    expect(activeTaskCreator.threadProducer()).andReturn(producer);
    activeTaskCreator.closeAndRemoveTaskProducerIfNeeded(taskId00);
    expect(standbyTaskCreator.createTasks(eq(assignmentStandby))).andReturn(singletonList(task10));
    final ConsumerGroupMetadata groupMetadata = new ConsumerGroupMetadata("appId");
    expect(consumer.groupMetadata()).andReturn(groupMetadata);
    producer.commitTransaction(expectedCommittedOffsets, groupMetadata);
    expectLastCall();
    task00.committedOffsets();
    EasyMock.expectLastCall();
    task01.committedOffsets();
    EasyMock.expectLastCall();
    task02.committedOffsets();
    EasyMock.expectLastCall();
    task10.committedOffsets();
    EasyMock.expectLastCall();
    replay(activeTaskCreator, standbyTaskCreator, consumer, changeLogReader);
    taskManager.handleAssignment(assignmentActive, assignmentStandby);
    assertThat(taskManager.tryToCompleteRestoration(time.milliseconds(), null), is(true));
    assertThat(task00.state(), is(Task.State.RUNNING));
    assertThat(task01.state(), is(Task.State.RUNNING));
    assertThat(task02.state(), is(Task.State.RUNNING));
    assertThat(task10.state(), is(Task.State.RUNNING));
    taskManager.handleRevocation(taskId00Partitions);
    assertThat(task00.commitNeeded, is(false));
    assertThat(task01.commitNeeded, is(false));
    assertThat(task02.commitPrepared, is(false));
    assertThat(task10.commitPrepared, is(false));
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) HashSet(java.util.HashSet) Collections.emptySet(java.util.Collections.emptySet) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Test(org.junit.Test)

Example 24 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class TaskManagerTest method shouldCommitViaProducerIfEosEnabled.

private void shouldCommitViaProducerIfEosEnabled(final ProcessingMode processingMode, final StreamsProducer producer, final Map<TopicPartition, OffsetAndMetadata> offsetsT01, final Map<TopicPartition, OffsetAndMetadata> offsetsT02) {
    setUpTaskManager(processingMode);
    final StateMachineTask task01 = new StateMachineTask(taskId01, taskId01Partitions, true);
    task01.setCommittableOffsetsAndMetadata(offsetsT01);
    task01.setCommitNeeded();
    taskManager.addTask(task01);
    final StateMachineTask task02 = new StateMachineTask(taskId02, taskId02Partitions, true);
    task02.setCommittableOffsetsAndMetadata(offsetsT02);
    task02.setCommitNeeded();
    taskManager.addTask(task02);
    reset(consumer);
    expect(consumer.groupMetadata()).andStubReturn(new ConsumerGroupMetadata("appId"));
    replay(activeTaskCreator, consumer, producer);
    taskManager.commitAll();
    verify(producer, consumer);
}
Also used : ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata)

Example 25 with ConsumerGroupMetadata

use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.

the class StreamThreadTest method shouldTransmitTaskManagerMetrics.

@Test
public void shouldTransmitTaskManagerMetrics() {
    final Consumer<byte[], byte[]> consumer = EasyMock.createNiceMock(Consumer.class);
    final ConsumerGroupMetadata consumerGroupMetadata = mock(ConsumerGroupMetadata.class);
    expect(consumer.groupMetadata()).andStubReturn(consumerGroupMetadata);
    expect(consumerGroupMetadata.groupInstanceId()).andReturn(Optional.empty());
    EasyMock.replay(consumer, consumerGroupMetadata);
    final TaskManager taskManager = EasyMock.createNiceMock(TaskManager.class);
    final MetricName testMetricName = new MetricName("test_metric", "", "", new HashMap<>());
    final Metric testMetric = new KafkaMetric(new Object(), testMetricName, (Measurable) (config, now) -> 0, null, new MockTime());
    final Map<MetricName, Metric> dummyProducerMetrics = singletonMap(testMetricName, testMetric);
    expect(taskManager.producerMetrics()).andReturn(dummyProducerMetrics);
    EasyMock.replay(taskManager);
    final StreamsMetricsImpl streamsMetrics = new StreamsMetricsImpl(metrics, CLIENT_ID, StreamsConfig.METRICS_LATEST, mockTime);
    final TopologyMetadata topologyMetadata = new TopologyMetadata(internalTopologyBuilder, config);
    topologyMetadata.buildAndRewriteTopology();
    final StreamThread thread = buildStreamThread(consumer, taskManager, config, topologyMetadata);
    assertThat(dummyProducerMetrics, is(thread.producerMetrics()));
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) ConsumedInternal(org.apache.kafka.streams.kstream.internals.ConsumedInternal) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) KafkaException(org.apache.kafka.common.KafkaException) Utils.mkProperties(org.apache.kafka.common.utils.Utils.mkProperties) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) StreamsException(org.apache.kafka.streams.errors.StreamsException) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) MockApiProcessor(org.apache.kafka.test.MockApiProcessor) Cluster(org.apache.kafka.common.Cluster) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) ProcessorSupplier(org.apache.kafka.streams.processor.api.ProcessorSupplier) LogContext(org.apache.kafka.common.utils.LogContext) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Duration(java.time.Duration) Map(java.util.Map) TimestampType(org.apache.kafka.common.record.TimestampType) MockKeyValueStoreBuilder(org.apache.kafka.test.MockKeyValueStoreBuilder) ProcessorContext(org.apache.kafka.streams.processor.api.ProcessorContext) ThreadMetadata(org.apache.kafka.streams.ThreadMetadata) TestUtils(org.apache.kafka.test.TestUtils) ReferenceContainer(org.apache.kafka.streams.processor.internals.assignment.ReferenceContainer) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) PartitionInfo(org.apache.kafka.common.PartitionInfo) Measurable(org.apache.kafka.common.metrics.Measurable) MockClientSupplier(org.apache.kafka.test.MockClientSupplier) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) Metrics(org.apache.kafka.common.metrics.Metrics) Stream(java.util.stream.Stream) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Assert.assertFalse(org.junit.Assert.assertFalse) Node(org.apache.kafka.common.Node) Matchers.is(org.hamcrest.Matchers.is) KafkaMetricsContext(org.apache.kafka.common.metrics.KafkaMetricsContext) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) EasyMock.mock(org.easymock.EasyMock.mock) ArrayList(java.util.ArrayList) MetricsContext(org.apache.kafka.common.metrics.MetricsContext) Assert.assertSame(org.junit.Assert.assertSame) CHECKPOINT_FILE_NAME(org.apache.kafka.streams.processor.internals.StateManagerUtil.CHECKPOINT_FILE_NAME) PunctuationType(org.apache.kafka.streams.processor.PunctuationType) BiConsumer(java.util.function.BiConsumer) Processor(org.apache.kafka.streams.processor.api.Processor) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TaskCorruptedException(org.apache.kafka.streams.errors.TaskCorruptedException) Before(org.junit.Before) Properties(java.util.Properties) Producer(org.apache.kafka.clients.producer.Producer) LogAndContinueExceptionHandler(org.apache.kafka.streams.errors.LogAndContinueExceptionHandler) EasyMock.niceMock(org.easymock.EasyMock.niceMock) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) File(java.io.File) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assert.assertNull(org.junit.Assert.assertNull) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) Assert(org.junit.Assert) MockProducer(org.apache.kafka.clients.producer.MockProducer) Assert.assertEquals(org.junit.Assert.assertEquals) Matchers.isA(org.hamcrest.Matchers.isA) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) Stores(org.apache.kafka.streams.state.Stores) MockRebalanceListener(org.apache.kafka.clients.consumer.internals.MockRebalanceListener) InternalStreamsBuilder(org.apache.kafka.streams.kstream.internals.InternalStreamsBuilder) LogAndSkipOnInvalidTimestamp(org.apache.kafka.streams.processor.LogAndSkipOnInvalidTimestamp) ContextualProcessor(org.apache.kafka.streams.processor.api.ContextualProcessor) Collections.singleton(java.util.Collections.singleton) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) Serdes(org.apache.kafka.common.serialization.Serdes) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) Assert.fail(org.junit.Assert.fail) Consumer(org.apache.kafka.clients.consumer.Consumer) TopicPartition(org.apache.kafka.common.TopicPartition) ClientUtils.getSharedAdminClientId(org.apache.kafka.streams.processor.internals.ClientUtils.getSharedAdminClientId) Collection(java.util.Collection) KeyValue(org.apache.kafka.streams.KeyValue) UUID(java.util.UUID) MaterializedInternal(org.apache.kafka.streams.kstream.internals.MaterializedInternal) Bytes(org.apache.kafka.common.utils.Bytes) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) List(java.util.List) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) Materialized(org.apache.kafka.streams.kstream.Materialized) Optional(java.util.Optional) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) StreamsConfig(org.apache.kafka.streams.StreamsConfig) TaskId(org.apache.kafka.streams.processor.TaskId) Assert.assertThrows(org.junit.Assert.assertThrows) CoreMatchers.not(org.hamcrest.CoreMatchers.not) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) HashSet(java.util.HashSet) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) TaskMigratedException(org.apache.kafka.streams.errors.TaskMigratedException) Record(org.apache.kafka.streams.processor.api.Record) Collections.singletonMap(java.util.Collections.singletonMap) LinkedList(java.util.LinkedList) JmxReporter(org.apache.kafka.common.metrics.JmxReporter) Utils(org.apache.kafka.common.utils.Utils) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Collections.emptyMap(java.util.Collections.emptyMap) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Matchers.empty(org.hamcrest.Matchers.empty) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) MockStateRestoreListener(org.apache.kafka.test.MockStateRestoreListener) Assert.assertNotNull(org.junit.Assert.assertNotNull) EasyMock.expect(org.easymock.EasyMock.expect) StoreBuilder(org.apache.kafka.streams.state.StoreBuilder) EasyMock.expectLastCall(org.easymock.EasyMock.expectLastCall) MockTimestampExtractor(org.apache.kafka.test.MockTimestampExtractor) InvalidOffsetException(org.apache.kafka.clients.consumer.InvalidOffsetException) StreamsTestUtils(org.apache.kafka.test.StreamsTestUtils) OffsetCheckpoint(org.apache.kafka.streams.state.internals.OffsetCheckpoint) EasyMock.verify(org.easymock.EasyMock.verify) Collections(java.util.Collections) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) MetricName(org.apache.kafka.common.MetricName) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) Metric(org.apache.kafka.common.Metric) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) EasyMock.anyObject(org.easymock.EasyMock.anyObject) StreamsMetricsImpl(org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Aggregations

ConsumerGroupMetadata (org.apache.kafka.clients.consumer.ConsumerGroupMetadata)64 TopicPartition (org.apache.kafka.common.TopicPartition)34 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)29 Test (org.junit.Test)28 HashMap (java.util.HashMap)25 Test (org.junit.jupiter.api.Test)24 StreamsMetricsImpl (org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)14 TaskId (org.apache.kafka.streams.processor.TaskId)12 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 LogContext (org.apache.kafka.common.utils.LogContext)8 Collection (java.util.Collection)7 LinkedList (java.util.LinkedList)7 Map (java.util.Map)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 KafkaException (org.apache.kafka.common.KafkaException)7 TimeoutException (org.apache.kafka.common.errors.TimeoutException)7 TaskCorruptedException (org.apache.kafka.streams.errors.TaskCorruptedException)7 Collections.emptySet (java.util.Collections.emptySet)6 HashSet (java.util.HashSet)6