use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.
the class FetcherTest method testGetOffsetsForTimesWhenSomeTopicPartitionLeadersDisconnectException.
@Test
public void testGetOffsetsForTimesWhenSomeTopicPartitionLeadersDisconnectException() {
buildFetcher();
final String anotherTopic = "another-topic";
final TopicPartition t2p0 = new TopicPartition(anotherTopic, 0);
subscriptions.assignFromUser(mkSet(tp0, t2p0));
client.reset();
MetadataResponse initialMetadata = RequestTestUtils.metadataUpdateWithIds(1, singletonMap(topicName, 1), topicIds);
client.updateMetadata(initialMetadata);
Map<String, Integer> partitionNumByTopic = new HashMap<>();
partitionNumByTopic.put(topicName, 1);
partitionNumByTopic.put(anotherTopic, 1);
topicIds.put("another-topic", Uuid.randomUuid());
MetadataResponse updatedMetadata = RequestTestUtils.metadataUpdateWithIds(1, partitionNumByTopic, topicIds);
client.prepareMetadataUpdate(updatedMetadata);
client.prepareResponse(listOffsetRequestMatcher(ListOffsetsRequest.LATEST_TIMESTAMP), listOffsetResponse(tp0, Errors.NONE, 1000L, 11L), true);
client.prepareResponseFrom(listOffsetResponse(tp0, Errors.NONE, 1000L, 11L), metadata.fetch().leaderFor(tp0));
Map<TopicPartition, Long> timestampToSearch = new HashMap<>();
timestampToSearch.put(tp0, ListOffsetsRequest.LATEST_TIMESTAMP);
Map<TopicPartition, OffsetAndTimestamp> offsetAndTimestampMap = fetcher.offsetsForTimes(timestampToSearch, time.timer(Long.MAX_VALUE));
assertNotNull(offsetAndTimestampMap.get(tp0), "Expect Fetcher.offsetsForTimes() to return non-null result for " + tp0);
assertEquals(11L, offsetAndTimestampMap.get(tp0).offset());
assertNotNull(metadata.fetch().partitionCountForTopic(anotherTopic));
}
use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.
the class FetcherTest method testGetTopicMetadataOfflinePartitions.
@Test
public void testGetTopicMetadataOfflinePartitions() {
buildFetcher();
assignFromUser(singleton(tp0));
// baseline ok response
MetadataResponse originalResponse = newMetadataResponse(topicName, Errors.NONE);
// create a response based on the above one with all partitions being leaderless
List<MetadataResponse.TopicMetadata> altTopics = new ArrayList<>();
for (MetadataResponse.TopicMetadata item : originalResponse.topicMetadata()) {
List<MetadataResponse.PartitionMetadata> partitions = item.partitionMetadata();
List<MetadataResponse.PartitionMetadata> altPartitions = new ArrayList<>();
for (MetadataResponse.PartitionMetadata p : partitions) {
altPartitions.add(new MetadataResponse.PartitionMetadata(p.error, p.topicPartition, // no leader
Optional.empty(), Optional.empty(), p.replicaIds, p.inSyncReplicaIds, p.offlineReplicaIds));
}
MetadataResponse.TopicMetadata alteredTopic = new MetadataResponse.TopicMetadata(item.error(), item.topic(), item.isInternal(), altPartitions);
altTopics.add(alteredTopic);
}
Node controller = originalResponse.controller();
MetadataResponse altered = RequestTestUtils.metadataResponse(originalResponse.brokers(), originalResponse.clusterId(), controller != null ? controller.id() : MetadataResponse.NO_CONTROLLER_ID, altTopics);
client.prepareResponse(altered);
Map<String, List<PartitionInfo>> topicMetadata = fetcher.getTopicMetadata(new MetadataRequest.Builder(Collections.singletonList(topicName), false), time.timer(5000L));
assertNotNull(topicMetadata);
assertNotNull(topicMetadata.get(topicName));
// noinspection ConstantConditions
assertEquals(metadata.fetch().partitionCountForTopic(topicName).longValue(), topicMetadata.get(topicName).size());
}
use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.
the class FetcherTest method testEpochSetInFetchRequest.
@Test
public void testEpochSetInFetchRequest() {
buildFetcher();
subscriptions.assignFromUser(singleton(tp0));
MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWithIds("dummy", 1, Collections.emptyMap(), Collections.singletonMap(topicName, 4), tp -> 99, topicIds);
client.updateMetadata(metadataResponse);
subscriptions.seek(tp0, 10);
assertEquals(1, fetcher.sendFetches());
// Check for epoch in outgoing request
MockClient.RequestMatcher matcher = body -> {
if (body instanceof FetchRequest) {
FetchRequest fetchRequest = (FetchRequest) body;
fetchRequest.fetchData(topicNames).values().forEach(partitionData -> {
assertTrue(partitionData.currentLeaderEpoch.isPresent(), "Expected Fetcher to set leader epoch in request");
assertEquals(99, partitionData.currentLeaderEpoch.get().longValue(), "Expected leader epoch to match epoch from metadata update");
});
return true;
} else {
fail("Should have seen FetchRequest");
return false;
}
};
client.prepareResponse(matcher, fullFetchResponse(tidp0, this.records, Errors.NONE, 100L, 0));
consumerClient.pollNoWakeup();
}
use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.
the class ConsumerNetworkClientTest method testTopicAuthorizationExceptionPropagatedFromMetadata.
@Test
public void testTopicAuthorizationExceptionPropagatedFromMetadata() {
MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("clusterId", 1, Collections.singletonMap("topic", Errors.TOPIC_AUTHORIZATION_FAILED), Collections.emptyMap());
metadata.updateWithCurrentRequestVersion(metadataResponse, false, time.milliseconds());
assertThrows(TopicAuthorizationException.class, () -> consumerClient.poll(time.timer(Duration.ZERO)));
}
use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.
the class SenderTest method testSendInOrder.
@Test
public void testSendInOrder() throws Exception {
int maxRetries = 1;
Metrics m = new Metrics();
SenderMetricsRegistry senderMetrics = new SenderMetricsRegistry(m);
try {
Sender sender = new Sender(logContext, client, metadata, this.accumulator, true, MAX_REQUEST_SIZE, ACKS_ALL, maxRetries, senderMetrics, time, REQUEST_TIMEOUT, RETRY_BACKOFF_MS, null, apiVersions);
// Create a two broker cluster, with partition 0 on broker 0 and partition 1 on broker 1
MetadataResponse metadataUpdate1 = RequestTestUtils.metadataUpdateWith(2, Collections.singletonMap("test", 2));
client.prepareMetadataUpdate(metadataUpdate1);
// Send the first message.
TopicPartition tp2 = new TopicPartition("test", 1);
appendToAccumulator(tp2, 0L, "key1", "value1");
// connect
sender.runOnce();
// send produce request
sender.runOnce();
String id = client.requests().peek().destination();
assertEquals(ApiKeys.PRODUCE, client.requests().peek().requestBuilder().apiKey());
Node node = new Node(Integer.parseInt(id), "localhost", 0);
assertEquals(1, client.inFlightRequestCount());
assertTrue(client.hasInFlightRequests());
assertTrue(client.isReady(node, time.milliseconds()), "Client ready status should be true");
assertEquals(1, sender.inFlightBatches(tp2).size());
time.sleep(900);
// Now send another message to tp2
appendToAccumulator(tp2, 0L, "key2", "value2");
// Update metadata before sender receives response from broker 0. Now partition 2 moves to broker 0
MetadataResponse metadataUpdate2 = RequestTestUtils.metadataUpdateWith(1, Collections.singletonMap("test", 2));
client.prepareMetadataUpdate(metadataUpdate2);
// Sender should not send the second message to node 0.
assertEquals(1, sender.inFlightBatches(tp2).size());
// receive the response for the previous send, and send the new batch
sender.runOnce();
assertEquals(1, client.inFlightRequestCount());
assertTrue(client.hasInFlightRequests());
assertEquals(1, sender.inFlightBatches(tp2).size());
} finally {
m.close();
}
}
Aggregations