chickenchickenlove commented on code in PR #24250:
URL: https://github.com/apache/kafka/pull/24250#discussion_r4667571204
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsMembershipManager.java:
##########
@@ -1286,36 +1364,77 @@ private CompletableFuture<Void> assignTasks(final
SortedSet<StreamsRebalanceData
.collect(Collectors.joining(", "))
);
- final SortedSet<TopicPartition> partitionsToAssign =
topicPartitionsForActiveTasks(activeTasksToAssign);
- final SortedSet<TopicPartition> partitionsToAssignNotPreviouslyOwned =
- partitionsToAssignNotPreviouslyOwned(partitionsToAssign,
topicPartitionsForActiveTasks(ownedActiveTasks));
+ final Cluster metadataSnapshot = metadata.fetch();
+ final int metadataVersion = metadata.updateVersion();
+
+ // Include all resolvable active tasks in the target assignment,
including those already applied.
+ // During partial reconciliation, passing only newly resolved tasks
would cause TaskManager
+ // to treat previously assigned tasks omitted from the callback as
removed.
+ final SortedSet<StreamsRebalanceData.TaskId>
resolvedActiveTasksToAssign =
filterTasksResolvableFromLocalMetadata(activeTasksToAssign, metadataSnapshot);
+ final SortedSet<TopicPartition> resolvedPartitionsToAssign =
topicPartitionsForActiveTasks(resolvedActiveTasksToAssign, metadataSnapshot);
+
+ // Request a metadata update if any task has an unknown input topic
partition count
+ // or no valid input partition according to the locally known counts.
+ if (resolvedActiveTasksToAssign.size() < activeTasksToAssign.size()) {
+ metadata.requestUpdate(true);
+ }
+ final SortedSet<TopicPartition> ownedAssignedPartitions =
previousAssignedAssignment.assignedPartitions;
+ final SortedSet<TopicPartition> partitionsToAssignNotPreviouslyOwned =
partitionsToAssignNotPreviouslyOwned(
+ resolvedPartitionsToAssign,
+ ownedAssignedPartitions
+ );
+ final Map<String, SortedSet<Integer>> resolvedActiveTasks =
toTasksAssignment(resolvedActiveTasksToAssign);
+
+ // Capture the resolved portion of the target assignment.
+ // It becomes currentAssignment only after reconciliation completes
successfully.
+ final LocalAssignment resolvedLocalAssignment = new LocalAssignment(
+ assignmentToReconcile.localEpoch,
+ resolvedActiveTasks,
+ assignmentToReconcile.standbyTasks,
+ assignmentToReconcile.warmupTasks,
+ assignmentToReconcile.isGroupReady,
+ resolvedPartitionsToAssign
+ );
+
+ // Skip applying the assignment and invoking the callback if the
resolved tasks
+ // and partitions match the previously reconciled assignment at the
same local epoch.
+ if
(resolvedPartitionsToAssign.equals(previousAssignedAssignment.assignedPartitions)
&&
+
resolvedLocalAssignment.reconcilingCompleted(previousAssignedAssignment)
+ ) {
+ return CompletableFuture.completedFuture(new
AssignTasksResult(null, metadataVersion));
+ }
// Enqueue event to app thread to apply assignment within poll() and
invoke callback.
- // The app thread will trigger ApplyAssignmentEvent to update
subscription state on background thread.
+ // The app thread will trigger Apply to update subscription state on
background thread.
CompletableFuture<Void> partitionsAssignedAndCallbackExecuted =
enqueueStreamsPartitionsAssignedEvent(
- partitionsToAssign,
+ resolvedPartitionsToAssign,
partitionsToAssignNotPreviouslyOwned,
new StreamsRebalanceData.Assignment(
- activeTasksToAssign,
+ resolvedActiveTasksToAssign,
standbyTasksToAssign,
warmupTasksToAssign,
+ resolvedLocalAssignment.assignedPartitions,
+ resolvedLocalAssignment.localEpoch,
isGroupReady
)
);
- partitionsAssignedAndCallbackExecuted.whenComplete((__, callbackError)
-> {
+
+ // Retain cached partition counts only for input topics of the tasks
in the new assignment.
+ final Set<String> assignedInputTopics =
assignedInputTopics(resolvedLocalAssignment);
+
+ return partitionsAssignedAndCallbackExecuted.whenComplete((__,
callbackError) -> {
if (callbackError == null) {
-
subscriptionState.enablePartitionsAwaitingCallback(partitionsToAssign);
+
subscriptionState.enablePartitionsAwaitingCallback(resolvedLocalAssignment.assignedPartitions);
+ topicPartitionsCache.keySet().retainAll(assignedInputTopics);
Review Comment:
Good point. The first commit included a check to prevent late callbacks from
changing state, but I removed it to make the change easier to backport.
Even before this PR, `enablePartitionsAwaitingCallback()` could be called if
the assignment callback completed after fencing. We could consider moving both
that call and the cache pruning introduced in this PR into the success path in
`maybeReconcile()`.
Would it be okay if I opened a new Jira ticket for this and addressed it in
a follow-up 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]