apourchet commented on code in PR #15972:
URL: https://github.com/apache/kafka/pull/15972#discussion_r1606917357


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/RackUtils.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.streams.processor.internals.assignment;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.kafka.common.Cluster;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.PartitionInfo;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.TopicPartitionInfo;
+import org.apache.kafka.streams.processor.internals.InternalTopicManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RackUtils {
+    private static final Logger LOG = LoggerFactory.getLogger(RackUtils.class);
+
+    public static Map<TopicPartition, Set<String>> 
getRacksForTopicPartition(final Cluster cluster,
+                                                                             
final InternalTopicManager internalTopicManager,
+                                                                             
final Set<TopicPartition> topicPartitions,
+                                                                             
final boolean isChangelog) {
+        final Set<String> topicsToDescribe = new HashSet<>();
+        if (isChangelog) {
+            
topicsToDescribe.addAll(topicPartitions.stream().map(TopicPartition::topic).collect(
+                Collectors.toSet()));
+        } else {
+            topicsToDescribe.addAll(topicsWithStaleMetadata(cluster, 
topicPartitions));
+        }
+
+        final Set<TopicPartition> topicsWithUpToDateMetadata = 
topicPartitions.stream()
+            .filter(partition -> !topicsToDescribe.contains(partition.topic()))
+            .collect(Collectors.toSet());
+        final Map<TopicPartition, Set<String>> racksForTopicPartition = 
knownRacksForPartition(
+            cluster, topicsWithUpToDateMetadata);
+
+        final Map<String, List<TopicPartitionInfo>> freshTopicPartitionInfo =
+            describeTopics(internalTopicManager, topicsToDescribe);
+        freshTopicPartitionInfo.forEach((topic, partitionInfos) -> {
+            partitionInfos.forEach(partitionInfo -> {
+                final int partition = partitionInfo.partition();
+                final TopicPartition topicPartition = new 
TopicPartition(topic, partition);
+                final List<Node> replicas = partitionInfo.replicas();
+                if (replicas == null || replicas.isEmpty()) {
+                    LOG.error("No replicas found for topic partition {}: {}", 
topic, partition);
+                    return;
+                }
+
+                final Set<String> racks = 
replicas.stream().filter(Node::hasRack).map(Node::rack).collect(
+                    Collectors.toSet());
+                racksForTopicPartition.computeIfAbsent(topicPartition, k -> 
new HashSet<>());
+                racksForTopicPartition.get(topicPartition).addAll(racks);
+            });
+        });
+
+        return racksForTopicPartition;
+    }
+
+    public static Set<String> topicsWithStaleMetadata(final Cluster cluster, 
final Set<TopicPartition> topicPartitions) {
+        final Set<String> topicsWithStaleMetadata = new HashSet<>();
+        for (final TopicPartition topicPartition : topicPartitions) {
+            final PartitionInfo partitionInfo = 
cluster.partition(topicPartition);
+            if (partitionInfo == null) {
+                LOG.error("TopicPartition {} doesn't exist in cluster", 
topicPartition);
+                continue;

Review Comment:
   This isn't how the current rack aware code works:
   ```            for (final TopicPartition topicPartition : topicPartitions) {
                   final PartitionInfo partitionInfo = 
fullMetadata.partition(topicPartition);
                   if (partitionInfo == null) {
                       log.error("TopicPartition {} doesn't exist in cluster", 
topicPartition);
                       return false;
                   }
                   final Node[] replica = partitionInfo.replicas();
                   if (replica == null || replica.length == 0) {
                       topicsToDescribe.add(topicPartition.topic());
                       continue;
                   }
                   for (final Node node : replica) {
                       if (node.hasRack()) {
                           racksForPartition.computeIfAbsent(topicPartition, k 
-> new HashSet<>()).add(node.rack());
                       } else {
                           log.warn("Node {} for topic partition {} doesn't 
have rack", node, topicPartition);
                           return false;
                       }
                   }
               }
   ```
   Above is the logic of `populateTopicsToDescribe`, which evidently uses the 
`(replica == null || replica.length == 0)` condition to decide to fetch further 
topic information, yet considers `cluster.partition(topicPartition)` to be an 
error, which causes the RackAwareAssignor to be turned off entirely.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to