TaiJuWu commented on code in PR #20061:
URL: https://github.com/apache/kafka/pull/20061#discussion_r2199874395


##########
coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/KRaftCoordinatorMetadataDelta.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.coordinator.common.runtime;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.image.MetadataDelta;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * An implementation of {@link CoordinatorMetadataDelta} that wraps the KRaft 
MetadataDelta.
+ */
+public class KRaftCoordinatorMetadataDelta implements CoordinatorMetadataDelta 
{
+
+    final MetadataDelta metadataDelta;
+
+    public KRaftCoordinatorMetadataDelta(MetadataDelta metadataDelta) {
+        this.metadataDelta = metadataDelta;
+    }
+
+    @Override
+    public Collection<Uuid> createdTopicIds() {
+        if (metadataDelta == null || metadataDelta.topicsDelta() == null) {
+            return Set.of();
+        }
+        return metadataDelta.topicsDelta().createdTopicIds();
+    }
+
+    @Override
+    public Collection<Uuid> changedTopicIds() {
+        if (metadataDelta == null || metadataDelta.topicsDelta() == null) {
+            return Set.of();
+        }
+        return metadataDelta.topicsDelta().changedTopics().keySet();
+    }
+
+    @Override
+    public Set<Uuid> deletedTopicIds() {
+        if (metadataDelta == null || metadataDelta.topicsDelta() == null) {
+            return Set.of();
+        }
+        return metadataDelta.topicsDelta().deletedTopicIds();
+    }
+
+
+    @Override
+    public String toString() {
+        return metadataDelta.toString();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (o == null || !o.getClass().equals(this.getClass())) return false;
+        KRaftCoordinatorMetadataDelta other = (KRaftCoordinatorMetadataDelta) 
o;
+        return metadataDelta.equals(other.metadataDelta);
+    }
+
+    @Override
+    public int hashCode() {
+        return metadataDelta.hashCode();
+    }
+
+    @Override
+    public CoordinatorMetadataImage image() {
+        return new KRaftCoordinatorMetadataImage(metadataDelta.image());
+    }

Review Comment:
   This method can be moved to above `toString`.



##########
coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/KRaftCoordinatorMetadataImage.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.coordinator.common.runtime;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.image.ClusterImage;
+import org.apache.kafka.image.MetadataDelta;
+import org.apache.kafka.image.MetadataImage;
+import org.apache.kafka.image.TopicImage;
+import org.apache.kafka.metadata.BrokerRegistration;
+import org.apache.kafka.metadata.PartitionRegistration;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * An implementation of {@link CoordinatorMetadataImage} that wraps the KRaft 
MetadataImage.
+ */
+public class KRaftCoordinatorMetadataImage implements CoordinatorMetadataImage 
{
+
+    private final MetadataImage metadataImage;
+
+    public KRaftCoordinatorMetadataImage(MetadataImage metadataImage) {
+        this.metadataImage = metadataImage;
+    }
+
+    @Override
+    public Set<Uuid> topicIds() {
+        return 
Collections.unmodifiableSet(metadataImage.topics().topicsById().keySet());
+    }
+
+    @Override
+    public Set<String> topicNames() {
+        return 
Collections.unmodifiableSet(metadataImage.topics().topicsByName().keySet());
+    }
+
+    @Override
+    public Optional<TopicMetadata> topicMetadata(Uuid topicId) {
+        TopicImage topicImage = metadataImage.topics().getTopic(topicId);
+        if (topicImage == null) return Optional.empty();
+
+        ClusterImage clusterImage = metadataImage.cluster();
+        if (clusterImage == null) return Optional.empty();
+
+        return Optional.of(new KraftTopicMetadata(topicImage, clusterImage));
+    }
+
+    @Override
+    public Optional<TopicMetadata> topicMetadata(String topicName) {
+        TopicImage topicImage = metadataImage.topics().getTopic(topicName);
+        if (topicImage == null) return Optional.empty();
+
+        ClusterImage clusterImage = metadataImage.cluster();
+        if (clusterImage == null) return Optional.empty();
+
+        return Optional.of(new KraftTopicMetadata(topicImage, clusterImage));
+    }
+
+    @Override
+    public CoordinatorMetadataDelta emptyDelta() {
+        return new KRaftCoordinatorMetadataDelta(new 
MetadataDelta(metadataImage));
+    }
+
+    @Override
+    public long version() {
+        return metadataImage.offset();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return metadataImage.isEmpty();
+    }
+
+    @Override
+    public String toString() {
+        return metadataImage.toString();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (o == null || !o.getClass().equals(this.getClass())) return false;
+        KRaftCoordinatorMetadataImage other = (KRaftCoordinatorMetadataImage) 
o;
+        return metadataImage.equals(other.metadataImage);
+    }
+
+    @Override
+    public int hashCode() {
+        return metadataImage.hashCode();
+    }
+
+    public static class KraftTopicMetadata implements TopicMetadata {
+        private final TopicImage topicImage;
+        private final ClusterImage clusterImage;
+
+        public KraftTopicMetadata(TopicImage topicImage, ClusterImage 
clusterImage) {
+            this.topicImage = topicImage;
+            this.clusterImage = clusterImage;
+        }
+
+        @Override
+        public String name() {
+            return topicImage.name();
+        }
+
+        @Override
+        public Uuid id() {
+            return topicImage.id();
+        }
+
+        @Override
+        public int partitionCount() {
+            return topicImage.partitions().size();
+        }
+
+        @Override
+        public List<String> partitionRacks(int partition) {
+            List<String> racks = new ArrayList<>();
+            PartitionRegistration partitionRegistration = 
topicImage.partitions().get(partition);
+            if (partitionRegistration != null) {
+                for (int replicaId : partitionRegistration.replicas) {
+                    BrokerRegistration broker = clusterImage.broker(replicaId);
+                    if (broker != null) {
+                        broker.rack().ifPresent(racks::add);
+                    }
+                }
+                return racks;
+            } else {
+                return List.of();
+            }
+        }
+    }
+}

Review Comment:
   Could you add new line for all files?



##########
coordinator-common/src/main/java/org/apache/kafka/coordinator/common/runtime/CoordinatorRuntime.java:
##########
@@ -2474,37 +2472,37 @@ public void scheduleUnloadOperation(
      * @param delta     The metadata delta.
      */
     public void onNewMetadataImage(
-        MetadataImage newImage,
-        MetadataDelta delta
+        CoordinatorMetadataImage newImage,
+        CoordinatorMetadataDelta delta
     ) {
         throwIfNotRunning();
-        log.debug("Scheduling applying of a new metadata image with offset 
{}.", newImage.offset());
+        log.debug("Scheduling applying of a new metadata image with offset 
{}.", newImage.version());
 
         // Update global image.
         metadataImage = newImage;
 
         // Push an event for each coordinator.
         coordinators.keySet().forEach(tp -> {
-            scheduleInternalOperation("UpdateImage(tp=" + tp + ", offset=" + 
newImage.offset() + ")", tp, () -> {
+            scheduleInternalOperation("UpdateImage(tp=" + tp + ", offset=" + 
newImage.version() + ")", tp, () -> {

Review Comment:
   Should we change the `offset=` to `version=`?
   There are many similar issues in this PR .



-- 
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