dajac commented on code in PR #23530:
URL: https://github.com/apache/kafka/pull/23530#discussion_r4513077201


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupMetadataManager.java:
##########
@@ -6561,14 +6602,19 @@ public void replay(
                 return;
             }
 
+            List<String> inconsistencies = new ArrayList<>();
             if (oldMember.memberEpoch() != LEAVE_GROUP_MEMBER_EPOCH) {

Review Comment:
   `StreamsGroupMember.memberEpoch()` is an `Integer` and it is `null` when 
only the `StreamsGroupMemberMetadata` record of the member was replayed before 
its tombstone. This happens in the cases handled by this PR, when both the 
`StreamsGroupCurrentMemberAssignment` record of the member and its tombstone 
were removed. The comparison then throws a `NullPointerException`, so the load 
still fails. Should we use `Objects.equals` here?
   
   ```suggestion
               if (!Objects.equals(oldMember.memberEpoch(), 
LEAVE_GROUP_MEMBER_EPOCH)) {
   ```
   
   The following test reproduces it. It passes with the suggestion.
   
   <details>
   <summary>Test</summary>
   
   ```java
       @Test
       public void 
testReplayStreamsGroupMemberMetadataTombstoneWithoutCurrentMemberAssignment() {
           GroupMetadataManagerTestContext context = new 
GroupMetadataManagerTestContext.Builder()
               .build();
   
           // Only the StreamsGroupMemberMetadata record of the member is 
replayed, so the member
           // has no member epoch.
           
context.replay(StreamsCoordinatorRecordHelpers.newStreamsGroupMemberRecord(
               "foo",
               streamsGroupMemberBuilderWithDefaults("m1").build()
           ));
   
           // The member tombstone is authoritative and removes the member.
           
context.replay(StreamsCoordinatorRecordHelpers.newStreamsGroupMemberTombstoneRecord("foo",
 "m1"));
           assertEquals(Map.of(), 
context.groupMetadataManager.streamsGroup("foo").members());
       }
   ```
   </details>



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupMetadataManager.java:
##########
@@ -1508,7 +1508,22 @@ ConsumerGroup convertToConsumerGroup(ClassicGroup 
classicGroup, List<Coordinator
     private void removeGroup(
         String groupId
     ) {
-        groups.remove(groupId);
+        Group group = groups.remove(groupId);
+
+        // Clean up the state kept outside of the group. The group may still 
be subscribed to
+        // topics or have share state partition metadata here: when the group 
coordinator loads
+        // concurrently with compaction, intermediate tombstone records can be 
missed, so the
+        // group metadata tombstone is treated as authoritative.
+        if (group instanceof ModernGroup<?> modernGroup) {
+            modernGroup.subscribedTopicNames().keySet()
+                .forEach(topicName -> unsubscribeGroupFromTopic(groupId, 
topicName));
+        } else if (group instanceof StreamsGroup streamsGroup) {
+            streamsGroup.topology().ifPresent(topology -> 
topology.requiredTopics()

Review Comment:
   This also fixes the same issue as KAFKA-21176 for streams groups. 
`maybeDeleteEmptyStreamsGroup` removes an empty streams group without replaying 
its tombstones, so the topics of its topology were left in `groupsByTopics`. 
Should we extend `testClassicGroupJoinWithEmptyStreamsGroup` to cover it? It 
needs a topology in the group and a check of `groupsSubscribedToTopic` at the 
end.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupMetadataManager.java:
##########
@@ -1508,7 +1508,22 @@ ConsumerGroup convertToConsumerGroup(ClassicGroup 
classicGroup, List<Coordinator
     private void removeGroup(
         String groupId
     ) {
-        groups.remove(groupId);
+        Group group = groups.remove(groupId);
+
+        // Clean up the state kept outside of the group. The group may still 
be subscribed to

Review Comment:
   nit: This is not only about the load. `convertToClassicGroup`, 
`maybeDeleteEmptyConsumerGroup` and `maybeDeleteEmptyStreamsGroup` call 
`removeGroup` without replaying the tombstones of the group, so this is the 
only clean up in those cases. Should we mention it in the comment?



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/modern/ModernGroup.java:
##########
@@ -349,6 +349,14 @@ public void removeTargetAssignment(String memberId) {
         targetAssignment.remove(memberId);
     }
 
+    /**
+     * Removes the target assignment of every member.
+     */
+    public void clearTargetAssignment() {

Review Comment:
   Should we add a test for this one in `ConsumerGroupTest` and 
`ShareGroupTest`? Same for `StreamsGroup.clearTargetAssignment` in 
`StreamsGroupTest`. We could also extend the `testRemoveMember` tests to verify 
that the target assignment of the member is removed.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupMetadataManagerTest.java:
##########
@@ -25273,6 +25279,39 @@ public void 
testReplayConsumerGroupMemberMetadataTombstone() {
         assertThrows(GroupIdNotFoundException.class, () -> 
context.groupMetadataManager.consumerGroup("bar"));
     }
 
+    @Test
+    public void 
testReplayConsumerGroupMemberMetadataTombstoneWithMissingSiblingTombstones() {
+        GroupMetadataManagerTestContext context = new 
GroupMetadataManagerTestContext.Builder()
+            .build();
+
+        Uuid topicId = Uuid.randomUuid();
+        ConsumerGroupMember member = new ConsumerGroupMember.Builder("m1")
+            .setState(MemberState.STABLE)
+            .setMemberEpoch(10)
+            .setPreviousMemberEpoch(9)
+            .setSubscribedTopicNames(List.of("bar"))
+            .setAssignedPartitions(toAssignmentWithEpochs(mkAssignment(
+                mkTopicAssignment(topicId, 0, 1, 2)), 10))
+            .build();
+
+        // Create the group and the member, including their target and current 
assignments.
+        
context.replay(GroupCoordinatorRecordHelpers.newConsumerGroupMemberSubscriptionRecord("foo",
 member));
+        
context.replay(GroupCoordinatorRecordHelpers.newConsumerGroupEpochRecord("foo", 
10, 0));
+        
context.replay(GroupCoordinatorRecordHelpers.newConsumerGroupTargetAssignmentRecord("foo",
 "m1", mkAssignment(
+            mkTopicAssignment(topicId, 0, 1, 2))));
+        
context.replay(GroupCoordinatorRecordHelpers.newConsumerGroupTargetAssignmentMetadataRecord("foo",
 10, 12345L));
+        
context.replay(GroupCoordinatorRecordHelpers.newConsumerGroupCurrentAssignmentRecord("foo",
 member));
+        assertEquals(Set.of("foo"), 
context.groupMetadataManager.groupsSubscribedToTopic("bar"));
+
+        // Replay the ConsumerGroupMemberMetadata tombstone without the 
ConsumerGroupCurrentMemberAssignment
+        // and ConsumerGroupTargetAssignmentMember tombstones, as can happen 
when the coordinator loads
+        // concurrently with compaction. The member is removed along with its 
target assignment.
+        
context.replay(GroupCoordinatorRecordHelpers.newConsumerGroupMemberSubscriptionTombstoneRecord("foo",
 "m1"));
+        assertThrows(UnknownMemberIdException.class, () -> 
context.groupMetadataManager.consumerGroup("foo").getOrMaybeCreateMember("m1", 
false));
+        
assertFalse(context.groupMetadataManager.consumerGroup("foo").targetAssignment().containsKey("m1"));

Review Comment:
   nit: Should we verify the entire target assignment?
   
   ```suggestion
           assertEquals(Map.of(), 
context.groupMetadataManager.consumerGroup("foo").targetAssignment());
   ```
   
   The same applies to the other new tests: the share tests, and the streams 
tests using `targetAssignment("m1", Optional.empty()).isEmpty()`.



##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/GroupMetadataManagerTest.java:
##########
@@ -25759,19 +26004,15 @@ public void 
testReplayStreamsGroupTargetAssignmentMetadataTombstoneExisting() {
             )
             .build();
 
-        IllegalStateException e = assertThrows(
-            IllegalStateException.class,
-            () -> 
context.replay(StreamsCoordinatorRecordHelpers.newStreamsGroupTargetAssignmentMetadataTombstoneRecord("foo"))
-        );
-        assertEquals("Received a tombstone record to delete target assignment 
of foo but the assignment still has 1 members.",
-            e.getMessage());
-
-        
context.replay(StreamsCoordinatorRecordHelpers.newStreamsGroupTargetAssignmentTombstoneRecord("foo",
 "m1"));
-
+        // The target assignment still has a member, i.e. the 
StreamsGroupTargetAssignmentMember
+        // tombstone has not been seen. This can happen when the coordinator 
loads while compaction
+        // removed it. The metadata tombstone is authoritative: it replays the 
missing member
+        // target assignment tombstone and resets the assignment epoch.

Review Comment:
   nit: The metadata tombstone does not replay anything here. How about the 
following?
   
   ```suggestion
           // The target assignment still has a member, i.e. the 
StreamsGroupTargetAssignmentMember
           // tombstone has not been seen. This can happen when the coordinator 
loads while compaction
           // removed it. The metadata tombstone is authoritative: the target 
assignment is removed
           // along with the epoch.
   ```



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