mConfig = config;
mZookeeperConnector = new ZookeeperConnector(mConfig);
}
private HostAndPort findLeader(TopicPartition topicPartition) {
SimpleConsumer consumer = null;
try {
LOG.info("looking up leader for topic " + topicPartition.getTopic() + " partition " +
topicPartition.getPartition());
consumer = new SimpleConsumer(mConfig.getKafkaSeedBrokerHost(),
mConfig.getKafkaSeedBrokerPort(),
100000, 64 * 1024, "leaderLookup");
List<String> topics = new ArrayList<String>();
topics.add(topicPartition.getTopic());
TopicMetadataRequest request = new TopicMetadataRequest(topics);
TopicMetadataResponse response = consumer.send(request);
List<TopicMetadata> metaData = response.topicsMetadata();
for (TopicMetadata item : metaData) {
for (PartitionMetadata part : item.partitionsMetadata()) {
if (part.partitionId() == topicPartition.getPartition()) {
return HostAndPort.fromParts(part.leader().host(), part.leader().port());
}
}
}
} finally {
if (consumer != null) {
consumer.close();
}
}
return null;
}