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());
}
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));
}
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;
}
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));
}
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());
}
Aggregations