Search in sources :

Example 1 with SubscriptionState

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

the class KafkaConsumerTest method testMeasureCommittedDuration.

@Test
public void testMeasureCommittedDuration() {
    long offset1 = 10000;
    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));
    // lookup coordinator
    client.prepareResponseFrom(FindCoordinatorResponse.prepareResponse(Errors.NONE, groupId, node), node);
    Node coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    // fetch offset for one topic
    client.prepareResponseFrom(offsetResponse(Collections.singletonMap(tp0, offset1), Errors.NONE), coordinator);
    consumer.committed(Collections.singleton(tp0)).get(tp0).offset();
    final Metric metric = consumer.metrics().get(consumer.metrics.metricName("committed-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)

Example 2 with SubscriptionState

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

the class KafkaConsumerTest method testResetUsingAutoResetPolicy.

@Test
public void testResetUsingAutoResetPolicy() {
    SubscriptionState subscription = new SubscriptionState(new LogContext(), OffsetResetStrategy.LATEST);
    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, groupId, groupInstanceId, false);
    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(offsetResponse(Collections.singletonMap(tp0, -1L), Errors.NONE), coordinator);
    client.prepareResponse(listOffsetsResponse(Collections.singletonMap(tp0, 50L)));
    consumer.poll(Duration.ZERO);
    assertEquals(50L, consumer.position(tp0));
}
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) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 3 with SubscriptionState

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

the class KafkaConsumerTest method testOffsetIsValidAfterSeek.

@Test
public void testOffsetIsValidAfterSeek() {
    SubscriptionState subscription = new SubscriptionState(new LogContext(), OffsetResetStrategy.LATEST);
    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, Optional.empty(), false);
    consumer.assign(singletonList(tp0));
    consumer.seek(tp0, 20L);
    consumer.poll(Duration.ZERO);
    assertEquals(subscription.validPosition(tp0).offset, 20L);
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) SubscriptionState(org.apache.kafka.clients.consumer.internals.SubscriptionState) LogContext(org.apache.kafka.common.utils.LogContext) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 4 with SubscriptionState

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

the class SubscriptionStateBenchmark method setup.

@Setup(Level.Trial)
public void setup() {
    Set<TopicPartition> assignment = new HashSet<>(topicCount * partitionCount);
    IntStream.range(0, topicCount).forEach(topicId -> IntStream.range(0, partitionCount).forEach(partitionId -> assignment.add(new TopicPartition(String.format("topic-%04d", topicId), partitionId))));
    subscriptionState = new SubscriptionState(new LogContext(), OffsetResetStrategy.EARLIEST);
    subscriptionState.assignFromUser(assignment);
    SubscriptionState.FetchPosition position = new SubscriptionState.FetchPosition(0L, Optional.of(0), new Metadata.LeaderAndEpoch(Optional.of(new Node(0, "host", 9092)), Optional.of(10)));
    assignment.forEach(topicPartition -> {
        subscriptionState.seekUnvalidated(topicPartition, position);
        subscriptionState.completeValidation(topicPartition);
    });
}
Also used : IntStream(java.util.stream.IntStream) TopicPartition(org.apache.kafka.common.TopicPartition) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Setup(org.openjdk.jmh.annotations.Setup) Measurement(org.openjdk.jmh.annotations.Measurement) Mode(org.openjdk.jmh.annotations.Mode) Param(org.openjdk.jmh.annotations.Param) Metadata(org.apache.kafka.clients.Metadata) Set(java.util.Set) Scope(org.openjdk.jmh.annotations.Scope) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) State(org.openjdk.jmh.annotations.State) Warmup(org.openjdk.jmh.annotations.Warmup) Benchmark(org.openjdk.jmh.annotations.Benchmark) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) SubscriptionState(org.apache.kafka.clients.consumer.internals.SubscriptionState) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) LogContext(org.apache.kafka.common.utils.LogContext) Level(org.openjdk.jmh.annotations.Level) Fork(org.openjdk.jmh.annotations.Fork) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) SubscriptionState(org.apache.kafka.clients.consumer.internals.SubscriptionState) TopicPartition(org.apache.kafka.common.TopicPartition) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) LogContext(org.apache.kafka.common.utils.LogContext) HashSet(java.util.HashSet) Setup(org.openjdk.jmh.annotations.Setup)

Example 5 with SubscriptionState

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

the class KafkaConsumerTest method newConsumer.

private KafkaConsumer<String, String> newConsumer(Time time, KafkaClient client, Metadata metadata, PartitionAssignor assignor, int rebalanceTimeoutMs, int sessionTimeoutMs, int heartbeatIntervalMs, boolean autoCommitEnabled, int autoCommitIntervalMs) {
    // create a consumer with mocked time and mocked network
    String clientId = "mock-consumer";
    String groupId = "mock-group";
    String metricGroupPrefix = "consumer";
    long retryBackoffMs = 100;
    long requestTimeoutMs = 30000;
    boolean excludeInternalTopics = true;
    int minBytes = 1;
    int maxBytes = Integer.MAX_VALUE;
    int maxWaitMs = 500;
    int fetchSize = 1024 * 1024;
    int maxPollRecords = Integer.MAX_VALUE;
    boolean checkCrcs = true;
    Deserializer<String> keyDeserializer = new StringDeserializer();
    Deserializer<String> valueDeserializer = new StringDeserializer();
    OffsetResetStrategy autoResetStrategy = OffsetResetStrategy.EARLIEST;
    List<PartitionAssignor> assignors = Arrays.asList(assignor);
    ConsumerInterceptors<String, String> interceptors = null;
    Metrics metrics = new Metrics();
    SubscriptionState subscriptions = new SubscriptionState(autoResetStrategy);
    ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(client, metadata, time, retryBackoffMs, requestTimeoutMs);
    ConsumerCoordinator consumerCoordinator = new ConsumerCoordinator(consumerClient, groupId, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, assignors, metadata, subscriptions, metrics, metricGroupPrefix, time, retryBackoffMs, autoCommitEnabled, autoCommitIntervalMs, interceptors, excludeInternalTopics);
    Fetcher<String, String> fetcher = new Fetcher<>(consumerClient, minBytes, maxBytes, maxWaitMs, fetchSize, maxPollRecords, checkCrcs, keyDeserializer, valueDeserializer, metadata, subscriptions, metrics, metricGroupPrefix, time, retryBackoffMs);
    return new KafkaConsumer<>(clientId, consumerCoordinator, keyDeserializer, valueDeserializer, fetcher, interceptors, time, consumerClient, metrics, subscriptions, metadata, retryBackoffMs, requestTimeoutMs);
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) ConsumerCoordinator(org.apache.kafka.clients.consumer.internals.ConsumerCoordinator) Metrics(org.apache.kafka.common.metrics.Metrics) ConsumerNetworkClient(org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient) SubscriptionState(org.apache.kafka.clients.consumer.internals.SubscriptionState) Fetcher(org.apache.kafka.clients.consumer.internals.Fetcher) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor)

Aggregations

SubscriptionState (org.apache.kafka.clients.consumer.internals.SubscriptionState)9 LogContext (org.apache.kafka.common.utils.LogContext)8 MockClient (org.apache.kafka.clients.MockClient)6 ConsumerMetadata (org.apache.kafka.clients.consumer.internals.ConsumerMetadata)6 Node (org.apache.kafka.common.Node)6 Test (org.junit.jupiter.api.Test)6 ConsumerCoordinator (org.apache.kafka.clients.consumer.internals.ConsumerCoordinator)2 ConsumerNetworkClient (org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient)2 Fetcher (org.apache.kafka.clients.consumer.internals.Fetcher)2 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)2 Metric (org.apache.kafka.common.Metric)2 Metrics (org.apache.kafka.common.metrics.Metrics)2 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)2 MockTime (org.apache.kafka.common.utils.MockTime)2 Time (org.apache.kafka.common.utils.Time)2 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 IntStream (java.util.stream.IntStream)1