squah-confluent commented on code in PR #24269:
URL: https://github.com/apache/kafka/pull/24269#discussion_r4598100223


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/modern/consumer/ConsumerGroup.java:
##########
@@ -643,7 +652,7 @@ public Optional<String> preferredServerAssignor() {
      */
     public Optional<String> preferredServerAssignor(long committedOffset) {
         return serverAssignors.entrySet(committedOffset).stream()
-            .max(Map.Entry.comparingByValue())
+            .max(SERVER_ASSIGNOR_VOTES_COMPARATOR)
             .map(Map.Entry::getKey);

Review Comment:
   Shall we factor this out instead of duplicating the logic to decide the 
preferred server assignor?



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/modern/consumer/ConsumerGroupTest.java:
##########
@@ -727,6 +736,32 @@ public void testPreferredServerAssignor() {
         );
     }
 
+    @Test
+    public void testPreferredServerAssignorWithTiedVote() {
+        ConsumerGroup consumerGroup = createConsumerGroup("foo");
+
+        ConsumerGroupMember member1 = new 
ConsumerGroupMember.Builder("member1")
+            .setServerAssignorName("uniform")
+            .build();
+        ConsumerGroupMember member2 = new 
ConsumerGroupMember.Builder("member2")
+            .setServerAssignorName("range")
+            .build();
+
+        consumerGroup.updateMember(member1);
+        consumerGroup.updateMember(member2);
+
+        // Range and uniform have one vote each. Updating a member removes its 
vote and adds it
+        // back, which reorders the stored votes. The preferred assignor 
should be range every time.

Review Comment:
   This relies on the keys colliding which only happens for the `HashMap` in 
`computePreferredServerAssignor`, so only two iterations are needed for the 
`HashMap` coverage.
   
   The `TimelineHashMap` order never changes in this test.
   
   One way to force a re-ordering is to expand the capacity of the maps, by 
adding more elements.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/modern/consumer/ConsumerGroupTest.java:
##########
@@ -687,15 +687,24 @@ public void testPreferredServerAssignor() {
             .build();
 
         // Member 1 has removed it assignor but this is not reflected in the 
group yet so
-        // we pass the updated member. The assignor should be range or uniform.
-        Optional<String> assignor = 
consumerGroup.computePreferredServerAssignor(member1, updatedMember1);
-        assertTrue(assignor.equals(Optional.of("range")) || 
assignor.equals(Optional.of("uniform")));
+        // we pass the updated member. Range and uniform have one vote each so 
the assignor
+        // should be range, the name that sorts first.

Review Comment:
   ```suggestion
           // should be range, which comes before uniform alphabetically.
   ```



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/modern/consumer/ConsumerGroup.java:
##########


Review Comment:
   Claude thinks there is a future optimization we can do:
   `hasPreferredServerAssignorChanged(member, updatedMember)` recomputes the 
assignor even when the member's preferred assignor has not changed.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupMetadataManagerTest.java:
##########
@@ -2923,6 +2923,91 @@ member3RejoinId, new MemberAssignmentImpl(mkAssignment(
         );
     }
 
+    @Test
+    public void testHeartbeatsKeepGroupEpochWhenServerAssignorVoteIsTied() {
+        String groupId = "fooup";
+        String memberId1 = Uuid.randomUuid().toString();
+        String memberId2 = Uuid.randomUuid().toString();
+
+        Uuid fooTopicId = Uuid.randomUuid();
+        String fooTopicName = "foo";
+
+        MockPartitionAssignor uniformAssignor = new 
MockPartitionAssignor("uniform");
+        MockPartitionAssignor rangeAssignor = new 
MockPartitionAssignor("range");
+        GroupAssignment groupAssignment = new GroupAssignment(Map.of(
+            memberId1, new MemberAssignmentImpl(mkAssignment(
+                mkTopicAssignment(fooTopicId, 0, 1, 2)
+            )),
+            memberId2, new MemberAssignmentImpl(mkAssignment(
+                mkTopicAssignment(fooTopicId, 3, 4, 5)
+            ))
+        ));
+        uniformAssignor.prepareGroupAssignment(groupAssignment);
+        rangeAssignor.prepareGroupAssignment(groupAssignment);
+
+        MetadataImage metadataImage = new MetadataImageBuilder()
+            .addTopic(fooTopicId, fooTopicName, 6)
+            .addRacks()
+            .build();
+
+        // Consumer group with one vote for the uniform assignor and one vote 
for the range assignor.
+        GroupMetadataManagerTestContext context = new 
GroupMetadataManagerTestContext.Builder()
+            
.withConfig(GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNORS_CONFIG, 
List.of(uniformAssignor, rangeAssignor))
+            .withMetadataImage(new 
KRaftCoordinatorMetadataImage(metadataImage))
+            .withConsumerGroup(new ConsumerGroupBuilder(groupId, 10)
+                .withMember(new ConsumerGroupMember.Builder(memberId1)
+                    .setState(MemberState.STABLE)
+                    .setMemberEpoch(10)
+                    .setPreviousMemberEpoch(9)
+                    .setRebalanceTimeoutMs(5000)
+                    .setClientId(DEFAULT_CLIENT_ID)
+                    .setClientHost(DEFAULT_CLIENT_ADDRESS.toString())
+                    .setSubscribedTopicNames(List.of("foo"))
+                    .setServerAssignorName("uniform")
+                    .setAssignedPartitions(toAssignmentWithEpochs(mkAssignment(
+                        mkTopicAssignment(fooTopicId, 0, 1, 2)), 10))
+                    .build())
+                .withMember(new ConsumerGroupMember.Builder(memberId2)
+                    .setState(MemberState.STABLE)
+                    .setMemberEpoch(10)
+                    .setPreviousMemberEpoch(9)
+                    .setRebalanceTimeoutMs(5000)
+                    .setClientId(DEFAULT_CLIENT_ID)
+                    .setClientHost(DEFAULT_CLIENT_ADDRESS.toString())
+                    .setSubscribedTopicNames(List.of("foo"))
+                    .setServerAssignorName("range")
+                    .setAssignedPartitions(toAssignmentWithEpochs(mkAssignment(
+                        mkTopicAssignment(fooTopicId, 3, 4, 5)), 10))
+                    .build())
+                .withAssignment(memberId1, mkAssignment(
+                    mkTopicAssignment(fooTopicId, 0, 1, 2)))
+                .withAssignment(memberId2, mkAssignment(
+                    mkTopicAssignment(fooTopicId, 3, 4, 5)))
+                .withAssignmentEpoch(10)
+                .withMetadataHash(computeGroupHash(Map.of(
+                    fooTopicName, computeTopicHash(fooTopicName, new 
KRaftCoordinatorMetadataImage(metadataImage))
+                ))))
+            .build();
+
+        // Both members heartbeat several times with unchanged votes. The tied 
vote resolves
+        // to the same assignor on every heartbeat so the group stays at epoch 
10.
+        Map<String, Integer> memberEpochs = new HashMap<>(Map.of(memberId1, 
10, memberId2, 10));
+        for (int round = 0; round < 5; round++) {
+            for (String memberId : List.of(memberId1, memberId2)) {
+                CoordinatorResult<ConsumerGroupHeartbeatResponseData, 
CoordinatorRecord> result = context.consumerGroupHeartbeat(
+                    new ConsumerGroupHeartbeatRequestData()
+                        .setGroupId(groupId)
+                        .setMemberId(memberId)
+                        .setMemberEpoch(memberEpochs.get(memberId)));
+                memberEpochs.put(memberId, result.response().memberEpoch());
+
+                assertEquals(List.of(), result.records());
+                assertEquals(10, result.response().memberEpoch());
+                assertEquals(10, 
context.groupMetadataManager.consumerGroup(groupId).groupEpoch());

Review Comment:
   nit: The other tests don't assert on this. Technically it's implied by the 
empty records above.
   ```suggestion
   ```



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupMetadataManagerTest.java:
##########
@@ -2923,6 +2923,91 @@ member3RejoinId, new MemberAssignmentImpl(mkAssignment(
         );
     }
 
+    @Test
+    public void testHeartbeatsKeepGroupEpochWhenServerAssignorVoteIsTied() {
+        String groupId = "fooup";
+        String memberId1 = Uuid.randomUuid().toString();
+        String memberId2 = Uuid.randomUuid().toString();
+
+        Uuid fooTopicId = Uuid.randomUuid();
+        String fooTopicName = "foo";
+
+        MockPartitionAssignor uniformAssignor = new 
MockPartitionAssignor("uniform");
+        MockPartitionAssignor rangeAssignor = new 
MockPartitionAssignor("range");
+        GroupAssignment groupAssignment = new GroupAssignment(Map.of(
+            memberId1, new MemberAssignmentImpl(mkAssignment(
+                mkTopicAssignment(fooTopicId, 0, 1, 2)
+            )),
+            memberId2, new MemberAssignmentImpl(mkAssignment(
+                mkTopicAssignment(fooTopicId, 3, 4, 5)
+            ))
+        ));
+        uniformAssignor.prepareGroupAssignment(groupAssignment);
+        rangeAssignor.prepareGroupAssignment(groupAssignment);
+
+        MetadataImage metadataImage = new MetadataImageBuilder()
+            .addTopic(fooTopicId, fooTopicName, 6)
+            .addRacks()
+            .build();
+
+        // Consumer group with one vote for the uniform assignor and one vote 
for the range assignor.
+        GroupMetadataManagerTestContext context = new 
GroupMetadataManagerTestContext.Builder()
+            
.withConfig(GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNORS_CONFIG, 
List.of(uniformAssignor, rangeAssignor))
+            .withMetadataImage(new 
KRaftCoordinatorMetadataImage(metadataImage))
+            .withConsumerGroup(new ConsumerGroupBuilder(groupId, 10)
+                .withMember(new ConsumerGroupMember.Builder(memberId1)
+                    .setState(MemberState.STABLE)
+                    .setMemberEpoch(10)
+                    .setPreviousMemberEpoch(9)
+                    .setRebalanceTimeoutMs(5000)
+                    .setClientId(DEFAULT_CLIENT_ID)
+                    .setClientHost(DEFAULT_CLIENT_ADDRESS.toString())
+                    .setSubscribedTopicNames(List.of("foo"))
+                    .setServerAssignorName("uniform")
+                    .setAssignedPartitions(toAssignmentWithEpochs(mkAssignment(
+                        mkTopicAssignment(fooTopicId, 0, 1, 2)), 10))
+                    .build())
+                .withMember(new ConsumerGroupMember.Builder(memberId2)
+                    .setState(MemberState.STABLE)
+                    .setMemberEpoch(10)
+                    .setPreviousMemberEpoch(9)
+                    .setRebalanceTimeoutMs(5000)
+                    .setClientId(DEFAULT_CLIENT_ID)
+                    .setClientHost(DEFAULT_CLIENT_ADDRESS.toString())
+                    .setSubscribedTopicNames(List.of("foo"))
+                    .setServerAssignorName("range")
+                    .setAssignedPartitions(toAssignmentWithEpochs(mkAssignment(
+                        mkTopicAssignment(fooTopicId, 3, 4, 5)), 10))
+                    .build())
+                .withAssignment(memberId1, mkAssignment(
+                    mkTopicAssignment(fooTopicId, 0, 1, 2)))
+                .withAssignment(memberId2, mkAssignment(
+                    mkTopicAssignment(fooTopicId, 3, 4, 5)))
+                .withAssignmentEpoch(10)
+                .withMetadataHash(computeGroupHash(Map.of(
+                    fooTopicName, computeTopicHash(fooTopicName, new 
KRaftCoordinatorMetadataImage(metadataImage))
+                ))))
+            .build();
+
+        // Both members heartbeat several times with unchanged votes. The tied 
vote resolves
+        // to the same assignor on every heartbeat so the group stays at epoch 
10.
+        Map<String, Integer> memberEpochs = new HashMap<>(Map.of(memberId1, 
10, memberId2, 10));
+        for (int round = 0; round < 5; round++) {

Review Comment:
   Similar to the comment in the other test: if we heartbeat member1, then 
member2, then member1, everything after that is not too interesting.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupMetadataManagerTest.java:
##########
@@ -2923,6 +2923,91 @@ member3RejoinId, new MemberAssignmentImpl(mkAssignment(
         );
     }
 
+    @Test
+    public void testHeartbeatsKeepGroupEpochWhenServerAssignorVoteIsTied() {
+        String groupId = "fooup";
+        String memberId1 = Uuid.randomUuid().toString();
+        String memberId2 = Uuid.randomUuid().toString();
+
+        Uuid fooTopicId = Uuid.randomUuid();
+        String fooTopicName = "foo";
+
+        MockPartitionAssignor uniformAssignor = new 
MockPartitionAssignor("uniform");
+        MockPartitionAssignor rangeAssignor = new 
MockPartitionAssignor("range");
+        GroupAssignment groupAssignment = new GroupAssignment(Map.of(
+            memberId1, new MemberAssignmentImpl(mkAssignment(
+                mkTopicAssignment(fooTopicId, 0, 1, 2)
+            )),
+            memberId2, new MemberAssignmentImpl(mkAssignment(
+                mkTopicAssignment(fooTopicId, 3, 4, 5)
+            ))
+        ));
+        uniformAssignor.prepareGroupAssignment(groupAssignment);
+        rangeAssignor.prepareGroupAssignment(groupAssignment);
+
+        MetadataImage metadataImage = new MetadataImageBuilder()
+            .addTopic(fooTopicId, fooTopicName, 6)
+            .addRacks()
+            .build();
+
+        // Consumer group with one vote for the uniform assignor and one vote 
for the range assignor.
+        GroupMetadataManagerTestContext context = new 
GroupMetadataManagerTestContext.Builder()
+            
.withConfig(GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNORS_CONFIG, 
List.of(uniformAssignor, rangeAssignor))
+            .withMetadataImage(new 
KRaftCoordinatorMetadataImage(metadataImage))
+            .withConsumerGroup(new ConsumerGroupBuilder(groupId, 10)
+                .withMember(new ConsumerGroupMember.Builder(memberId1)
+                    .setState(MemberState.STABLE)
+                    .setMemberEpoch(10)
+                    .setPreviousMemberEpoch(9)
+                    .setRebalanceTimeoutMs(5000)
+                    .setClientId(DEFAULT_CLIENT_ID)
+                    .setClientHost(DEFAULT_CLIENT_ADDRESS.toString())
+                    .setSubscribedTopicNames(List.of("foo"))
+                    .setServerAssignorName("uniform")
+                    .setAssignedPartitions(toAssignmentWithEpochs(mkAssignment(
+                        mkTopicAssignment(fooTopicId, 0, 1, 2)), 10))
+                    .build())
+                .withMember(new ConsumerGroupMember.Builder(memberId2)
+                    .setState(MemberState.STABLE)
+                    .setMemberEpoch(10)
+                    .setPreviousMemberEpoch(9)
+                    .setRebalanceTimeoutMs(5000)
+                    .setClientId(DEFAULT_CLIENT_ID)
+                    .setClientHost(DEFAULT_CLIENT_ADDRESS.toString())
+                    .setSubscribedTopicNames(List.of("foo"))
+                    .setServerAssignorName("range")
+                    .setAssignedPartitions(toAssignmentWithEpochs(mkAssignment(
+                        mkTopicAssignment(fooTopicId, 3, 4, 5)), 10))
+                    .build())
+                .withAssignment(memberId1, mkAssignment(
+                    mkTopicAssignment(fooTopicId, 0, 1, 2)))
+                .withAssignment(memberId2, mkAssignment(
+                    mkTopicAssignment(fooTopicId, 3, 4, 5)))
+                .withAssignmentEpoch(10)
+                .withMetadataHash(computeGroupHash(Map.of(
+                    fooTopicName, computeTopicHash(fooTopicName, new 
KRaftCoordinatorMetadataImage(metadataImage))
+                ))))
+            .build();
+
+        // Both members heartbeat several times with unchanged votes. The tied 
vote resolves
+        // to the same assignor on every heartbeat so the group stays at epoch 
10.
+        Map<String, Integer> memberEpochs = new HashMap<>(Map.of(memberId1, 
10, memberId2, 10));

Review Comment:
   Do we really need the map if the epoch is expected to be a constant 10 
during the test?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to