This is an automated email from the ASF dual-hosted git repository.

chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new cf7b4a98b86 MINOR: Improve documentation to clarify preferred replica 
(#20890)
cf7b4a98b86 is described below

commit cf7b4a98b86494076c01d202a616edb3ec817cb1
Author: Nick Guo <[email protected]>
AuthorDate: Tue Nov 25 04:37:04 2025 +0800

    MINOR: Improve documentation to clarify preferred replica (#20890)
    
    Improve documentation to clarify preferred replica
    
    Reviewers: Chia-Ping Tsai <[email protected]>
---
 .../main/java/org/apache/kafka/common/PartitionInfo.java |  3 ++-
 .../scala/unit/kafka/server/MetadataRequestTest.scala    | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/clients/src/main/java/org/apache/kafka/common/PartitionInfo.java 
b/clients/src/main/java/org/apache/kafka/common/PartitionInfo.java
index 0577b4200a3..ba831d2fb71 100644
--- a/clients/src/main/java/org/apache/kafka/common/PartitionInfo.java
+++ b/clients/src/main/java/org/apache/kafka/common/PartitionInfo.java
@@ -70,7 +70,8 @@ public class PartitionInfo {
     }
 
     /**
-     * The complete set of replicas for this partition regardless of whether 
they are alive or up-to-date
+     * The complete set of replicas for this partition regardless of whether 
they are alive or up-to-date. The preferred replica
+     * is the head of the list.
      */
     public Node[] replicas() {
         return replicas;
diff --git a/core/src/test/scala/unit/kafka/server/MetadataRequestTest.scala 
b/core/src/test/scala/unit/kafka/server/MetadataRequestTest.scala
index 181fd2f644c..47200721690 100644
--- a/core/src/test/scala/unit/kafka/server/MetadataRequestTest.scala
+++ b/core/src/test/scala/unit/kafka/server/MetadataRequestTest.scala
@@ -204,6 +204,22 @@ class MetadataRequestTest extends 
AbstractMetadataRequestTest {
     }
   }
 
+  @Test
+  def testPartitionInfoPreferredReplica(): Unit = {
+    val replicaAssignment = Map(0 -> Seq(1, 2, 0))
+    val topic = "testPartitionInfoPreferredReplicaTopic"
+    createTopicWithAssignment(topic, replicaAssignment)
+
+    val response = sendMetadataRequest(new 
MetadataRequest.Builder(Seq(topic).asJava, true).build())
+    val cluster = response.buildCluster()
+    val partitionInfos = cluster.partitionsForTopic(topic).asScala
+    assertEquals(1, partitionInfos.size)
+
+    val partitionInfo = partitionInfos.head
+    val preferredReplicaId = replicaAssignment(partitionInfo.partition()).head
+    assertEquals(preferredReplicaId, partitionInfo.replicas().head.id())
+  }
+
   @Test
   def testReplicaDownResponse(): Unit = {
     val replicaDownTopic = "replicaDown"

Reply via email to