Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,16 @@ public void onLoad(NamespaceBundle bundle) {
}
groupCoordinator.handleGroupImmigration(name.getPartitionIndex());
}
KafkaTopicManager.removeTopicManagerCache(name.toString());
KopBrokerLookupManager.removeTopicManagerCache(name.toString());
// deReference topic when unload
KopBrokerLookupManager.removeTopicManagerCache(topic);
KafkaTopicManager.deReference(topic);

// For non-partitioned topic.
if (!name.isPartitioned()) {
String partitionedZeroTopicName = name.getPartition(0).toString();
KafkaTopicManager.deReference(partitionedZeroTopicName);
KopBrokerLookupManager.removeTopicManagerCache(partitionedZeroTopicName);
}
}
} else {
log.error("Failed to get owned topic list for "
Expand Down Expand Up @@ -180,8 +188,16 @@ public void unLoad(NamespaceBundle bundle) {
groupCoordinator.handleGroupEmigration(name.getPartitionIndex());
}
// deReference topic when unload
KopBrokerLookupManager.removeTopicManagerCache(name.toString());
KafkaTopicManager.deReference(name.toString());
KopBrokerLookupManager.removeTopicManagerCache(topic);
KafkaTopicManager.deReference(topic);

// For non-partitioned topic.
if (!name.isPartitioned()) {
String partitionedZeroTopicName = name.getPartition(0).toString();
KafkaTopicManager.deReference(partitionedZeroTopicName);
KopBrokerLookupManager.removeTopicManagerCache(partitionedZeroTopicName);
}

}
} else {
log.error("Failed to get owned topic list for "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ protected void handleTopicMetadataRequest(KafkaHeaderAndRequest metadataHar,
// 2. topics provided, get provided topics.
CompletableFuture<Map<String, List<TopicName>>> pulsarTopicsFuture;

// Map for <partition-zero, non-partitioned-topic>, use for findBroker
// e.g. <persistent://public/default/topic1-partition-0, persistent://public/default/topic1>
final Map<String, TopicName> nonPartitionedTopicMap = Maps.newConcurrentMap();

if (topics == null || topics.isEmpty()) {
// clean all cache when get all metadata for librdkafka(<1.0.0).
KafkaTopicManager.clearTopicManagerCache();
Expand Down Expand Up @@ -505,7 +509,7 @@ protected void handleTopicMetadataRequest(KafkaHeaderAndRequest metadataHar,
} else {
pulsarTopicsFuture = new CompletableFuture<>();
// get only the provided topics
final Map<String, List<TopicName>> pulsarTopics = new ConcurrentHashMap<>();
final Map<String, List<TopicName>> pulsarTopics = Maps.newConcurrentMap();

List<String> requestTopics = metadataRequest.topics();
final int topicsNumber = requestTopics.size();
Expand Down Expand Up @@ -615,14 +619,14 @@ protected void handleTopicMetadataRequest(KafkaHeaderAndRequest metadataHar,
}
addTopicPartition.accept(topic, partitionedTopicMetadata.partitions);
} else {
log.error("Topic {} is a non-partitioned topic", topic);
allTopicMetadata.add(
new TopicMetadata(
Errors.INVALID_TOPIC_EXCEPTION,
topic,
isInternalTopic(fullTopicName),
Collections.emptyList()));
completeOneTopic.run();
// In case non-partitioned topic, treat as a one partitioned topic.
nonPartitionedTopicMap.put(TopicName
.get(fullTopicName)
.getPartition(0)
.toString(),
TopicName.get(fullTopicName)
);
addTopicPartition.accept(topic, 1);
}
}
});
Expand Down Expand Up @@ -672,63 +676,68 @@ protected void handleTopicMetadataRequest(KafkaHeaderAndRequest metadataHar,
List<PartitionMetadata> partitionMetadatas = Collections
.synchronizedList(Lists.newArrayListWithExpectedSize(partitionsNumber));

list.forEach(topicName ->
findBroker(topicName)
.whenComplete(((partitionMetadata, throwable) -> {
if (throwable != null || partitionMetadata == null) {
log.warn("[{}] Request {}: Exception while find Broker metadata",
ctx.channel(), metadataHar.getHeader(), throwable);
partitionMetadatas.add(newFailedPartitionMetadata(topicName));
} else {
Node newNode = partitionMetadata.leader();
synchronized (allNodes) {
if (!allNodes.stream().anyMatch(node1 -> node1.equals(newNode))) {
allNodes.add(newNode);
list.forEach(topicName -> {
// For non-partitioned topic.
TopicName realTopicName = nonPartitionedTopicMap.getOrDefault(topicName.toString(), topicName);
findBroker(realTopicName)
.whenComplete(((partitionMetadata, throwable) -> {
if (throwable != null || partitionMetadata == null) {
log.warn("[{}] Request {}: Exception while find Broker metadata",
ctx.channel(), metadataHar.getHeader(), throwable);
partitionMetadatas.add(newFailedPartitionMetadata(realTopicName));
} else {
Node newNode = partitionMetadata.leader();
synchronized (allNodes) {
if (!allNodes.stream().anyMatch(node1 -> node1.equals(newNode))) {
allNodes.add(newNode);
}
}
partitionMetadatas.add(partitionMetadata);
}
partitionMetadatas.add(partitionMetadata);
}

// whether completed this topic's partitions list.
int finishedPartitions = partitionsCompleted.incrementAndGet();
if (log.isDebugEnabled()) {
log.debug("[{}] Request {}: FindBroker for topic {}, partitions found/all: {}/{}.",
ctx.channel(), metadataHar.getHeader(),
topic, finishedPartitions, partitionsNumber);
}
if (finishedPartitions == partitionsNumber) {
// new TopicMetadata for this topic
allTopicMetadata.add(
new TopicMetadata(
Errors.NONE,
// The topic returned to Kafka clients should be the same with what it sent
topic,
isInternalTopic(new KopTopic(topic).getFullName()),
partitionMetadatas));

// whether completed all the topics requests.
int finishedTopics = topicsCompleted.incrementAndGet();
// whether completed this topic's partitions list.
int finishedPartitions = partitionsCompleted.incrementAndGet();
if (log.isDebugEnabled()) {
log.debug("[{}] Request {}: Completed findBroker for topic {}, "
+ "partitions found/all: {}/{}. \n dump All Metadata:",
ctx.channel(), metadataHar.getHeader(), topic,
finishedTopics, topicsNumber);

allTopicMetadata.stream()
.forEach(data -> log.debug("TopicMetadata response: {}", data.toString()));
log.debug("[{}] Request {}: FindBroker for topic {}, partitions found/all: {}/{}.",
ctx.channel(), metadataHar.getHeader(),
topic, finishedPartitions, partitionsNumber);
}
if (finishedTopics == topicsNumber) {
// TODO: confirm right value for controller_id
MetadataResponse finalResponse =
new MetadataResponse(
allNodes,
clusterName,
controllerId,
allTopicMetadata);
resultFuture.complete(finalResponse);
if (finishedPartitions == partitionsNumber) {
// new TopicMetadata for this topic
allTopicMetadata.add(
new TopicMetadata(
Errors.NONE,
// The topic returned to Kafka clients should be
// the same with what it sent
topic,
isInternalTopic(new KopTopic(topic).getFullName()),
partitionMetadatas));

// whether completed all the topics requests.
int finishedTopics = topicsCompleted.incrementAndGet();
if (log.isDebugEnabled()) {
log.debug("[{}] Request {}: Completed findBroker for topic {}, "
+ "partitions found/all: {}/{}. \n dump All Metadata:",
ctx.channel(), metadataHar.getHeader(), topic,
finishedTopics, topicsNumber);

allTopicMetadata.stream()
.forEach(data -> log.debug("TopicMetadata response: {}",
data.toString()));
}
if (finishedTopics == topicsNumber) {
// TODO: confirm right value for controller_id
MetadataResponse finalResponse =
new MetadataResponse(
allNodes,
clusterName,
controllerId,
allTopicMetadata);
resultFuture.complete(finalResponse);
}
}
}
})));
}));
});
});
});
}
Expand Down Expand Up @@ -2084,7 +2093,6 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
private CompletableFuture<Optional<String>>
getProtocolDataToAdvertise(InetSocketAddress pulsarAddress,
TopicName topic) {

CompletableFuture<Optional<String>> returnFuture = new CompletableFuture<>();

if (pulsarAddress == null) {
Expand Down Expand Up @@ -2178,7 +2186,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
}

if (lookupDataContainsAddress(data, hostAndPort)) {
topicManager.KOP_ADDRESS_CACHE.put(topic.toString(), returnFuture);
KafkaTopicManager.KOP_ADDRESS_CACHE.put(topic.toString(), returnFuture);
returnFuture.complete(data.getProtocol(KafkaProtocolHandler.PROTOCOL_NAME));
return;
}
Expand Down Expand Up @@ -2225,8 +2233,8 @@ public CompletableFuture<PartitionMetadata> findBroker(TopicName topic) {
.thenCompose(address -> getProtocolDataToAdvertise(address, topic))
.whenComplete((stringOptional, throwable) -> {
if (!stringOptional.isPresent() || throwable != null) {
log.error("Not get advertise data for Kafka topic:{}. throwable",
topic, throwable);
log.error("Not get advertise data for Kafka topic:{}. throwable: [{}]",
topic, throwable.getMessage());
KafkaTopicManager.removeTopicManagerCache(topic.toString());
returnFuture.complete(null);
return;
Expand Down
Loading