Search in sources :

Example 36 with ConsumerMetadata

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

the class KafkaConsumerTest method testSubscriptionChangesWithAutoCommitDisabled.

/**
 * Verify that when a consumer changes its topic subscription its assigned partitions
 * do not immediately change, and the consumed offsets of its to-be-revoked partitions
 * are not committed (when auto-commit is disabled).
 * Upon unsubscribing from subscribed topics, the assigned partitions immediately
 * change but if auto-commit is disabled the consumer offsets are not committed.
 */
@Test
public void testSubscriptionChangesWithAutoCommitDisabled() {
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    Map<String, Integer> tpCounts = new HashMap<>();
    tpCounts.put(topic, 1);
    tpCounts.put(topic2, 1);
    initMetadata(client, tpCounts);
    Node node = metadata.fetch().nodes().get(0);
    ConsumerPartitionAssignor assignor = new RangeAssignor();
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, false, groupInstanceId);
    initializeSubscriptionWithSingleTopic(consumer, getConsumerRebalanceListener(consumer));
    // mock rebalance responses
    prepareRebalance(client, node, assignor, singletonList(tp0), null);
    consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE));
    consumer.poll(Duration.ZERO);
    // verify that subscription is still the same, and now assignment has caught up
    assertEquals(singleton(topic), consumer.subscription());
    assertEquals(singleton(tp0), consumer.assignment());
    consumer.poll(Duration.ZERO);
    // subscription change
    consumer.subscribe(singleton(topic2), getConsumerRebalanceListener(consumer));
    // verify that subscription has changed but assignment is still unchanged
    assertEquals(singleton(topic2), consumer.subscription());
    assertEquals(singleton(tp0), consumer.assignment());
    // the auto commit is disabled, so no offset commit request should be sent
    for (ClientRequest req : client.requests()) assertNotSame(ApiKeys.OFFSET_COMMIT, req.requestBuilder().apiKey());
    // subscription change
    consumer.unsubscribe();
    // verify that subscription and assignment are both updated
    assertEquals(Collections.emptySet(), consumer.subscription());
    assertEquals(Collections.emptySet(), consumer.assignment());
    // the auto commit is disabled, so no offset commit request should be sent
    for (ClientRequest req : client.requests()) assertNotSame(ApiKeys.OFFSET_COMMIT, req.requestBuilder().apiKey());
    client.requests().clear();
    consumer.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) ClientRequest(org.apache.kafka.clients.ClientRequest) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 37 with ConsumerMetadata

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

the class KafkaConsumerTest method testEnforceRebalanceTriggersRebalanceOnNextPoll.

@Test
public void testEnforceRebalanceTriggersRebalanceOnNextPoll() {
    Time time = new MockTime(1L);
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
    MockRebalanceListener countingRebalanceListener = new MockRebalanceListener();
    initMetadata(client, Utils.mkMap(Utils.mkEntry(topic, 1), Utils.mkEntry(topic2, 1), Utils.mkEntry(topic3, 1)));
    consumer.subscribe(Arrays.asList(topic, topic2), countingRebalanceListener);
    Node node = metadata.fetch().nodes().get(0);
    prepareRebalance(client, node, assignor, Arrays.asList(tp0, t2p0), null);
    // a first rebalance to get the assignment, we need two poll calls since we need two round trips to finish join / sync-group
    consumer.poll(Duration.ZERO);
    consumer.poll(Duration.ZERO);
    // onPartitionsRevoked is not invoked when first joining the group
    assertEquals(countingRebalanceListener.revokedCount, 0);
    assertEquals(countingRebalanceListener.assignedCount, 1);
    consumer.enforceRebalance();
    // the next poll should trigger a rebalance
    consumer.poll(Duration.ZERO);
    assertEquals(countingRebalanceListener.revokedCount, 1);
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) MockRebalanceListener(org.apache.kafka.clients.consumer.internals.MockRebalanceListener) Node(org.apache.kafka.common.Node) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 38 with ConsumerMetadata

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

the class KafkaConsumerTest method consumerForCheckingTimeoutException.

private KafkaConsumer<String, String> consumerForCheckingTimeoutException() {
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, singletonMap(topic, 1));
    ConsumerPartitionAssignor assignor = new RangeAssignor();
    final KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, false, groupInstanceId);
    for (int i = 0; i < 10; i++) {
        client.prepareResponse(request -> {
            time.sleep(defaultApiTimeoutMs / 10);
            return request instanceof ListOffsetsRequest;
        }, listOffsetsResponse(Collections.emptyMap(), Collections.singletonMap(tp0, Errors.UNKNOWN_TOPIC_OR_PARTITION)));
    }
    return consumer;
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) MockClient(org.apache.kafka.clients.MockClient) ListOffsetsRequest(org.apache.kafka.common.requests.ListOffsetsRequest)

Example 39 with ConsumerMetadata

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

the class KafkaConsumerTest method consumerWithPendingAuthenticationError.

private KafkaConsumer<String, String> consumerWithPendingAuthenticationError(final Time time) {
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, singletonMap(topic, 1));
    Node node = metadata.fetch().nodes().get(0);
    ConsumerPartitionAssignor assignor = new RangeAssignor();
    client.createPendingAuthenticationError(node, 0);
    return newConsumer(time, client, subscription, metadata, assignor, false, groupInstanceId);
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) Node(org.apache.kafka.common.Node) MockClient(org.apache.kafka.clients.MockClient)

Example 40 with ConsumerMetadata

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

the class KafkaConsumerTest method testPollTimeMetrics.

@Test
public void testPollTimeMetrics() {
    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, groupInstanceId);
    consumer.subscribe(singletonList(topic));
    // MetricName objects to check
    Metrics metrics = consumer.metrics;
    MetricName lastPollSecondsAgoName = metrics.metricName("last-poll-seconds-ago", "consumer-metrics");
    MetricName timeBetweenPollAvgName = metrics.metricName("time-between-poll-avg", "consumer-metrics");
    MetricName timeBetweenPollMaxName = metrics.metricName("time-between-poll-max", "consumer-metrics");
    // Test default values
    assertEquals(-1.0d, consumer.metrics().get(lastPollSecondsAgoName).metricValue());
    assertEquals(Double.NaN, consumer.metrics().get(timeBetweenPollAvgName).metricValue());
    assertEquals(Double.NaN, consumer.metrics().get(timeBetweenPollMaxName).metricValue());
    // Call first poll
    consumer.poll(Duration.ZERO);
    assertEquals(0.0d, consumer.metrics().get(lastPollSecondsAgoName).metricValue());
    assertEquals(0.0d, consumer.metrics().get(timeBetweenPollAvgName).metricValue());
    assertEquals(0.0d, consumer.metrics().get(timeBetweenPollMaxName).metricValue());
    // Advance time by 5,000 (total time = 5,000)
    time.sleep(5 * 1000L);
    assertEquals(5.0d, consumer.metrics().get(lastPollSecondsAgoName).metricValue());
    // Call second poll
    consumer.poll(Duration.ZERO);
    assertEquals(2.5 * 1000d, consumer.metrics().get(timeBetweenPollAvgName).metricValue());
    assertEquals(5 * 1000d, consumer.metrics().get(timeBetweenPollMaxName).metricValue());
    // Advance time by 10,000 (total time = 15,000)
    time.sleep(10 * 1000L);
    assertEquals(10.0d, consumer.metrics().get(lastPollSecondsAgoName).metricValue());
    // Call third poll
    consumer.poll(Duration.ZERO);
    assertEquals(5 * 1000d, consumer.metrics().get(timeBetweenPollAvgName).metricValue());
    assertEquals(10 * 1000d, consumer.metrics().get(timeBetweenPollMaxName).metricValue());
    // Advance time by 5,000 (total time = 20,000)
    time.sleep(5 * 1000L);
    assertEquals(5.0d, consumer.metrics().get(lastPollSecondsAgoName).metricValue());
    // Call fourth poll
    consumer.poll(Duration.ZERO);
    assertEquals(5 * 1000d, consumer.metrics().get(timeBetweenPollAvgName).metricValue());
    assertEquals(10 * 1000d, consumer.metrics().get(timeBetweenPollMaxName).metricValue());
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) ConsumerMetrics(org.apache.kafka.clients.consumer.internals.ConsumerMetrics) 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