use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.
the class KafkaConsumerTest method testRegexSubscription.
@Test
public void testRegexSubscription() {
String unmatchedTopic = "unmatched";
ConsumerMetadata metadata = createMetadata(subscription);
MockClient client = new MockClient(time, metadata);
Map<String, Integer> partitionCounts = new HashMap<>();
partitionCounts.put(topic, 1);
partitionCounts.put(unmatchedTopic, 1);
topicIds.put(unmatchedTopic, Uuid.randomUuid());
initMetadata(client, partitionCounts);
Node node = metadata.fetch().nodes().get(0);
KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
prepareRebalance(client, node, singleton(topic), assignor, singletonList(tp0), null);
consumer.subscribe(Pattern.compile(topic), getConsumerRebalanceListener(consumer));
client.prepareMetadataUpdate(RequestTestUtils.metadataUpdateWithIds(1, partitionCounts, topicIds));
consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE));
assertEquals(singleton(topic), consumer.subscription());
assertEquals(singleton(tp0), consumer.assignment());
consumer.close(Duration.ofMillis(0));
}
use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.
the class KafkaConsumerTest method testClosingConsumerUnregistersConsumerMetrics.
@Test
public void testClosingConsumerUnregistersConsumerMetrics() {
Time time = new MockTime(1L);
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, new RoundRobinAssignor(), true, groupInstanceId);
consumer.subscribe(singletonList(topic));
assertTrue(consumerMetricPresent(consumer, "last-poll-seconds-ago"));
assertTrue(consumerMetricPresent(consumer, "time-between-poll-avg"));
assertTrue(consumerMetricPresent(consumer, "time-between-poll-max"));
consumer.close();
assertFalse(consumerMetricPresent(consumer, "last-poll-seconds-ago"));
assertFalse(consumerMetricPresent(consumer, "time-between-poll-avg"));
assertFalse(consumerMetricPresent(consumer, "time-between-poll-max"));
}
use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata 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));
}
use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.
the class KafkaConsumerTest method testManualAssignmentChangeWithAutoCommitDisabled.
@Test
public void testManualAssignmentChangeWithAutoCommitDisabled() {
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);
// lookup coordinator
client.prepareResponseFrom(FindCoordinatorResponse.prepareResponse(Errors.NONE, groupId, node), node);
Node coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
// manual assignment
consumer.assign(singleton(tp0));
consumer.seekToBeginning(singleton(tp0));
// fetch offset for one topic
client.prepareResponseFrom(offsetResponse(Collections.singletonMap(tp0, 0L), Errors.NONE), coordinator);
assertEquals(0, consumer.committed(Collections.singleton(tp0)).get(tp0).offset());
// verify that assignment immediately changes
assertEquals(consumer.assignment(), singleton(tp0));
// there shouldn't be any need to lookup the coordinator or fetch committed offsets.
// we just lookup the starting position and send the record fetch.
client.prepareResponse(listOffsetsResponse(Collections.singletonMap(tp0, 10L)));
client.prepareResponse(fetchResponse(tp0, 10L, 1));
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1));
assertEquals(1, records.count());
assertEquals(11L, consumer.position(tp0));
// new manual assignment
consumer.assign(singleton(t2p0));
// verify that assignment immediately changes
assertEquals(consumer.assignment(), singleton(t2p0));
// the auto commit is disabled, so no offset commit request should be sent
for (ClientRequest req : client.requests()) assertNotSame(req.requestBuilder().apiKey(), ApiKeys.OFFSET_COMMIT);
client.requests().clear();
consumer.close();
}
use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.
the class KafkaConsumerTest method testSubscriptionOnInvalidTopic.
@Test
public void testSubscriptionOnInvalidTopic() {
ConsumerMetadata metadata = createMetadata(subscription);
MockClient client = new MockClient(time, metadata);
initMetadata(client, Collections.singletonMap(topic, 1));
Cluster cluster = metadata.fetch();
// Invalid topic name due to space
String invalidTopicName = "topic abc";
List<MetadataResponse.TopicMetadata> topicMetadata = new ArrayList<>();
topicMetadata.add(new MetadataResponse.TopicMetadata(Errors.INVALID_TOPIC_EXCEPTION, invalidTopicName, false, Collections.emptyList()));
MetadataResponse updateResponse = RequestTestUtils.metadataResponse(cluster.nodes(), cluster.clusterResource().clusterId(), cluster.controller().id(), topicMetadata);
client.prepareMetadataUpdate(updateResponse);
KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
consumer.subscribe(singleton(invalidTopicName), getConsumerRebalanceListener(consumer));
assertThrows(InvalidTopicException.class, () -> consumer.poll(Duration.ZERO));
}
Aggregations