Search in sources :

Example 46 with ConsumerMetadata

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

the class KafkaConsumerTest method testPollIdleRatio.

@Test
public void testPollIdleRatio() {
    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);
    // MetricName object to check
    Metrics metrics = consumer.metrics;
    MetricName pollIdleRatio = metrics.metricName("poll-idle-ratio-avg", "consumer-metrics");
    // Test default value
    assertEquals(Double.NaN, consumer.metrics().get(pollIdleRatio).metricValue());
    // 1st poll
    // Spend 50ms in poll so value = 1.0
    consumer.kafkaConsumerMetrics.recordPollStart(time.milliseconds());
    time.sleep(50);
    consumer.kafkaConsumerMetrics.recordPollEnd(time.milliseconds());
    assertEquals(1.0d, consumer.metrics().get(pollIdleRatio).metricValue());
    // 2nd poll
    // Spend 50m outside poll and 0ms in poll so value = 0.0
    time.sleep(50);
    consumer.kafkaConsumerMetrics.recordPollStart(time.milliseconds());
    consumer.kafkaConsumerMetrics.recordPollEnd(time.milliseconds());
    // Avg of first two data points
    assertEquals((1.0d + 0.0d) / 2, consumer.metrics().get(pollIdleRatio).metricValue());
    // 3rd poll
    // Spend 25ms outside poll and 25ms in poll so value = 0.5
    time.sleep(25);
    consumer.kafkaConsumerMetrics.recordPollStart(time.milliseconds());
    time.sleep(25);
    consumer.kafkaConsumerMetrics.recordPollEnd(time.milliseconds());
    // Avg of three data points
    assertEquals((1.0d + 0.0d + 0.5d) / 3, consumer.metrics().get(pollIdleRatio).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