Search in sources :

Example 31 with ConsumerMetadata

use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.

the class KafkaConsumerTest method testUnsubscribeShouldTriggerPartitionsRevokedWithValidGeneration.

@Test
public void testUnsubscribeShouldTriggerPartitionsRevokedWithValidGeneration() {
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, Collections.singletonMap(topic, 1));
    Node node = metadata.fetch().nodes().get(0);
    CooperativeStickyAssignor assignor = new CooperativeStickyAssignor();
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, false, groupInstanceId);
    initializeSubscriptionWithSingleTopic(consumer, getExceptionConsumerRebalanceListener());
    prepareRebalance(client, node, assignor, singletonList(tp0), null);
    RuntimeException assignmentException = assertThrows(RuntimeException.class, () -> consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE)));
    assertEquals(partitionAssigned + singleTopicPartition, assignmentException.getCause().getMessage());
    RuntimeException unsubscribeException = assertThrows(RuntimeException.class, consumer::unsubscribe);
    assertEquals(partitionRevoked + singleTopicPartition, unsubscribeException.getCause().getMessage());
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) Node(org.apache.kafka.common.Node) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 32 with ConsumerMetadata

use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.

the class KafkaConsumerTest method testAutoCommitSentBeforePositionUpdate.

@Test
public void testAutoCommitSentBeforePositionUpdate() {
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, Collections.singletonMap(topic, 1));
    Node node = metadata.fetch().nodes().get(0);
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
    consumer.subscribe(singleton(topic), getConsumerRebalanceListener(consumer));
    Node coordinator = prepareRebalance(client, node, assignor, singletonList(tp0), null);
    consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE));
    consumer.poll(Duration.ZERO);
    // respond to the outstanding fetch so that we have data available on the next poll
    client.respondFrom(fetchResponse(tp0, 0, 5), node);
    client.poll(0, time.milliseconds());
    time.sleep(autoCommitIntervalMs);
    client.prepareResponseFrom(fetchResponse(tp0, 5, 0), node);
    // no data has been returned to the user yet, so the committed offset should be 0
    AtomicBoolean commitReceived = prepareOffsetCommitResponse(client, coordinator, tp0, 0);
    consumer.poll(Duration.ZERO);
    assertTrue(commitReceived.get());
    consumer.close(Duration.ofMillis(0));
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Node(org.apache.kafka.common.Node) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 33 with ConsumerMetadata

use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.

the class KafkaConsumerTest method setUpConsumerWithRecordsToPoll.

private KafkaConsumer<String, String> setUpConsumerWithRecordsToPoll(TopicPartition tp, int recordCount, Deserializer<String> deserializer) {
    Cluster cluster = TestUtils.singletonCluster(tp.topic(), 1);
    Node node = cluster.nodes().get(0);
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, Collections.singletonMap(topic, 1));
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupId, groupInstanceId, Optional.of(deserializer), false);
    consumer.subscribe(singleton(topic), getConsumerRebalanceListener(consumer));
    prepareRebalance(client, node, assignor, singletonList(tp), null);
    consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE));
    client.prepareResponseFrom(fetchResponse(tp, 0, recordCount), node);
    return consumer;
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) Node(org.apache.kafka.common.Node) Cluster(org.apache.kafka.common.Cluster) MockClient(org.apache.kafka.clients.MockClient)

Example 34 with ConsumerMetadata

use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.

the class KafkaConsumerTest method verifyHeartbeatSent.

@Test
public void verifyHeartbeatSent() throws Exception {
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, Collections.singletonMap(topic, 1));
    Node node = metadata.fetch().nodes().get(0);
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
    consumer.subscribe(singleton(topic), getConsumerRebalanceListener(consumer));
    Node coordinator = prepareRebalance(client, node, assignor, singletonList(tp0), null);
    // initial fetch
    client.prepareResponseFrom(fetchResponse(tp0, 0, 0), node);
    consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE));
    assertEquals(singleton(tp0), consumer.assignment());
    AtomicBoolean heartbeatReceived = prepareHeartbeatResponse(client, coordinator, Errors.NONE);
    // heartbeat interval is 2 seconds
    time.sleep(heartbeatIntervalMs);
    Thread.sleep(heartbeatIntervalMs);
    consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE));
    assertTrue(heartbeatReceived.get());
    consumer.close(Duration.ofMillis(0));
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Node(org.apache.kafka.common.Node) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 35 with ConsumerMetadata

use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.

the class KafkaConsumerTest method testMeasureCommitSyncDuration.

@Test
public void testMeasureCommitSyncDuration() {
    Time time = new MockTime(Duration.ofSeconds(1).toMillis());
    SubscriptionState subscription = new SubscriptionState(new LogContext(), OffsetResetStrategy.EARLIEST);
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, Collections.singletonMap(topic, 2));
    Node node = metadata.fetch().nodes().get(0);
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
    consumer.assign(singletonList(tp0));
    client.prepareResponseFrom(FindCoordinatorResponse.prepareResponse(Errors.NONE, groupId, node), node);
    Node coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    client.prepareResponseFrom(offsetCommitResponse(Collections.singletonMap(tp0, Errors.NONE)), coordinator);
    consumer.commitSync(Collections.singletonMap(tp0, new OffsetAndMetadata(10L)));
    final Metric metric = consumer.metrics().get(consumer.metrics.metricName("commit-sync-time-ns-total", "consumer-metrics"));
    assertTrue((Double) metric.metricValue() >= Duration.ofMillis(999).toNanos());
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) SubscriptionState(org.apache.kafka.clients.consumer.internals.SubscriptionState) Node(org.apache.kafka.common.Node) LogContext(org.apache.kafka.common.utils.LogContext) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) Metric(org.apache.kafka.common.Metric) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Aggregations

MockClient (org.apache.kafka.clients.MockClient)46 ConsumerMetadata (org.apache.kafka.clients.consumer.internals.ConsumerMetadata)46 Test (org.junit.jupiter.api.Test)41 Node (org.apache.kafka.common.Node)35 HashMap (java.util.HashMap)10 LinkedHashMap (java.util.LinkedHashMap)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 TopicPartition (org.apache.kafka.common.TopicPartition)7 SubscriptionState (org.apache.kafka.clients.consumer.internals.SubscriptionState)6 LogContext (org.apache.kafka.common.utils.LogContext)6 MockTime (org.apache.kafka.common.utils.MockTime)5 Time (org.apache.kafka.common.utils.Time)5 OptionalLong (java.util.OptionalLong)4 ClientRequest (org.apache.kafka.clients.ClientRequest)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 Cluster (org.apache.kafka.common.Cluster)3 FetchRequest (org.apache.kafka.common.requests.FetchRequest)3 ExecutorService (java.util.concurrent.ExecutorService)2 ConsumerMetrics (org.apache.kafka.clients.consumer.internals.ConsumerMetrics)2