Search in sources :

Example 6 with MemoryRecordsBuilder

use of org.apache.kafka.common.record.MemoryRecordsBuilder in project kafka by apache.

the class FetcherTest method testFetcherMetrics.

/*
     * Send multiple requests. Verify that the client side quota metrics have the right values
     */
@Test
public void testFetcherMetrics() {
    subscriptions.assignFromUser(singleton(tp));
    subscriptions.seek(tp, 0);
    MetricName maxLagMetric = metrics.metricName("records-lag-max", metricGroup, "");
    MetricName partitionLagMetric = metrics.metricName(tp + ".records-lag", metricGroup, "");
    Map<MetricName, KafkaMetric> allMetrics = metrics.metrics();
    KafkaMetric recordsFetchLagMax = allMetrics.get(maxLagMetric);
    // recordsFetchLagMax should be initialized to negative infinity
    assertEquals(Double.NEGATIVE_INFINITY, recordsFetchLagMax.value(), EPSILON);
    // recordsFetchLagMax should be hw - fetchOffset after receiving an empty FetchResponse
    fetchRecords(MemoryRecords.EMPTY, Errors.NONE, 100L, 0);
    assertEquals(100, recordsFetchLagMax.value(), EPSILON);
    KafkaMetric partitionLag = allMetrics.get(partitionLagMetric);
    assertEquals(100, partitionLag.value(), EPSILON);
    // recordsFetchLagMax should be hw - offset of the last message after receiving a non-empty FetchResponse
    MemoryRecordsBuilder builder = MemoryRecords.builder(ByteBuffer.allocate(1024), CompressionType.NONE, TimestampType.CREATE_TIME);
    for (int v = 0; v < 3; v++) builder.appendWithOffset((long) v, Record.NO_TIMESTAMP, "key".getBytes(), String.format("value-%d", v).getBytes());
    fetchRecords(builder.build(), Errors.NONE, 200L, 0);
    assertEquals(197, recordsFetchLagMax.value(), EPSILON);
    // verify de-registration of partition lag
    subscriptions.unsubscribe();
    assertFalse(allMetrics.containsKey(partitionLagMetric));
}
Also used : MetricName(org.apache.kafka.common.MetricName) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Test(org.junit.Test)

Aggregations

MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)6 Test (org.junit.Test)3 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2 MetricName (org.apache.kafka.common.MetricName)2 KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)2 ByteBuffer (java.nio.ByteBuffer)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)1 FetchResponse (org.apache.kafka.common.requests.FetchResponse)1 PartitionData (org.apache.kafka.common.requests.FetchResponse.PartitionData)1 OffsetFetchResponse (org.apache.kafka.common.requests.OffsetFetchResponse)1 Before (org.junit.Before)1