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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupMetadataManager.java:
##########
@@ -4149,19 +4173,98 @@ private UpdateTargetAssignmentResult<Assignment> 
maybeUpdateTargetAssignment(
                     group.groupId(), groupEpoch, preferredServerAssignor, 
assignorTimeMs);
             }
 
-            records.addAll(assignmentResult.records());
+            return assignmentResult;
+        };
+
+        if (offloadAssignor) {
+            assignmentResultBuilder.freezeInputs();
+            Map<String, String> previousStaticMembers = 
Map.copyOf(updatedMembersAndTargetAssignment.staticMembers());
+            // The view is backed by the group's timeline data structures, 
which may have been
+            // modified by the time the executor task runs.
+            updatedMembersAndTargetAssignment.close();
+
+            executor.schedule(
+                targetAssignmentUpdateKey,
+                buildTargetAssignment::get,
+                (result, exception) -> {
+                    if (exception != null) {
+                        log.error("[GroupId {}] Failed to compute a new target 
assignment for epoch {}: {}.", group.groupId(), groupEpoch, 
exception.getMessage(), exception);
+                        return new CoordinatorResult<>(List.of());
+                    }
+
+                    try {
+                        ConsumerGroup consumerGroup = 
consumerGroup(group.groupId());
+                        if (consumerGroup.assignmentEpoch() >= groupEpoch) {
+                            // The assignment epoch is already caught up.
+                            // Writing this record would backslide it.
+                            log.debug("[GroupId {}] Discarding stale offloaded 
target assignment for epoch {} (current assignment epoch is {}).",
+                                group.groupId(), groupEpoch, 
consumerGroup.assignmentEpoch());
+                            return new CoordinatorResult<>(List.of());
+                        }
+
+                        log.debug("[GroupId {}] Received updated target 
assignment for epoch {}: {}.",
+                            group.groupId(), groupEpoch, 
result.targetAssignment());
+
+                        TargetAssignmentRecordsBuilder<Assignment> 
assignmentRecordsBuilder =
+                            new 
TargetAssignmentRecordsBuilder.ConsumerTargetAssignmentRecordsBuilder(logContext,
 group.groupId())
+                                .withAssignmentEpoch(groupEpoch)
+                                
.withAssignmentTimestampMs(result.assignmentTimestampMs())
+                                .withCurrentMemberIds(group.members().keySet())
+                                
.withPreviousStaticMembers(previousStaticMembers)
+                                
.withCurrentStaticMembers(group.staticMembers())
+                                
.withCurrentTargetAssignment(group.targetAssignment())
+                                
.withNewTargetAssignment(result.targetAssignment());
+
+                        return new 
CoordinatorResult<>(assignmentRecordsBuilder.build());
+                    } catch (GroupIdNotFoundException ex) {
+                        log.debug("[GroupId {}] Received updated target 
assignment but the consumer group no longer exists.", group.groupId());
+                        return new CoordinatorResult<>(List.of());
+                    } catch (Throwable t) {
+                        log.error("[GroupId {}] Failed to compute a new target 
assignment for epoch {}: {}.", group.groupId(), groupEpoch, t.getMessage(), t);
+                        return new CoordinatorResult<>(List.of());
+                    }
+                }
+            );
 
-            MemberAssignment newMemberAssignment = 
assignmentResult.targetAssignment().get(updatedMember.memberId());
+            appendFuture.whenComplete((__, t) -> {
+                if (t != null) {
+                    // The subscription change and group epoch have been 
reverted. We must not use
+                    // the new target assignment, since it will not correspond 
to the latest
+                    // subscription even if the group epoch matches.
+                    executor.cancel(targetAssignmentUpdateKey);

Review Comment:
   Thanks for reviewing. I replaced the `appendFuture` usage with a `(group id, 
assignment epoch)` map to track in-flight assignors. When the group epoch is 
bumped and <= an inflight assignor epoch, the inflight assignor must be using 
stale data and cancelled. The downside is that we have to be careful to do the 
check in every place we bump the group epoch.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/modern/TargetAssignmentBuilder.java:
##########
@@ -417,32 +301,31 @@ public U withTopicAssignablePartitionsMap(
     }
 
     /**
-     * Adds or updates a member. This is useful when the updated member is
-     * not yet materialized in memory.
-     *
-     * @param memberId  The member id.
-     * @param member    The member to add or update.
-     * @return This object.
+     * Takes copies of the inputs backed by mutable collections, so that
+     * {@link TargetAssignmentBuilder#build()} can run on another thread.
      */
-    public U addOrUpdateMember(
-        String memberId,
-        T member
-    ) {
-        this.updatedMembers.put(memberId, member);
-        return self();
-    }
+    public void freezeInputs() {
+        if (Thread.currentThread().getId() != creationThreadId) {

Review Comment:
   Replaced the design with three builders: `GroupSpecBuilder`, 
`TargetAssignmentBuilder` and `TargetAssignmentRecordsBuilder`.
   
   `GroupSpecBuilder` has a `withAssignorOffload(boolean)` that copies the data 
going into the GroupSpec when set.
   
   `TargetAssignmentBuilder` now takes a `GroupSpec` and is constructed and run 
on the background thread.



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