lucasbru commented on code in PR #24250:
URL: https://github.com/apache/kafka/pull/24250#discussion_r4586061038


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsMembershipManager.java:
##########
@@ -1108,7 +1221,24 @@ private void processAssignmentReceived(Map<String, 
SortedSet<Integer>> activeTas
     }
 
     private boolean targetAssignmentReconciled() {
-        return currentAssignment.equals(targetAssignment);
+        // A new member epoch may assign the same tasks after a 
partition-count change.
+        // With client-side metadata resolution, stale but known counts can 
produce
+        // incomplete input partitions while all tasks are considered resolved.
+        // This check may therefore report completion before the client 
observes
+        // the updated counts; a later metadata update alone does not reopen 
reconciliation.
+        //
+        // This limitation is accepted when broker-provided assignment 
metadata is
+        // unavailable. Local metadata alone cannot establish which partition 
counts
+        // the coordinator used for the assignment.
+        // TODO: KAFKA-21256

Review Comment:
   KAFKA-21256 is the callback stall ticket (#23773). Should this (and the TODO 
in `assignTasks`) point to KAFKA-21188, or the KIP once it has a ticket?



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsMembershipManager.java:
##########
@@ -1189,46 +1335,55 @@ private void maybeReconcile() {
 
         SortedSet<TopicPartition> ownedTopicPartitionsFromSubscriptionState = 
new TreeSet<>(TOPIC_PARTITION_COMPARATOR);
         
ownedTopicPartitionsFromSubscriptionState.addAll(subscriptionState.assignedPartitions());
-        SortedSet<TopicPartition> ownedTopicPartitionsFromAssignedTasks =
-            topicPartitionsForActiveTasks(currentAssignment.activeTasks);
-        if 
(!ownedTopicPartitionsFromAssignedTasks.equals(ownedTopicPartitionsFromSubscriptionState))
 {
-            throw new IllegalStateException("Owned partitions from 
subscription state and owned partitions from " +
-                "assigned active tasks are not equal. " +
-                "Owned partitions from subscription state: " + 
ownedTopicPartitionsFromSubscriptionState + ", " +
-                "Owned partitions from assigned active tasks: " + 
ownedTopicPartitionsFromAssignedTasks);
+        if 
(!appliedAssignment.assignedPartitions.equals(ownedTopicPartitionsFromSubscriptionState))
 {
+            // The inconsistent assignment state cannot be safely recovered in 
place.
+            // Fail the member to stop processing and heartbeat. The 
coordinator can
+            // reassign its tasks after the member's session expires.
+            final String errorMessage = String.format(
+                "member transition to FATAl state because owned partitions 
from subscription state and owned partitions from " +
+                    "applied assignment are not equal. " +
+                    "Owned partitions from subscription state: " + 
ownedTopicPartitionsFromSubscriptionState + ", " +
+                    "Owned partitions from applied assignment: " + 
appliedAssignment.assignedPartitions
+            );
+            log.error(errorMessage);
+            markReconciliationCompleted();
+            backgroundEventHandler.add(new ErrorEvent(new 
KafkaException(errorMessage)));
+            transitionToFatal();
+            return;
         }
-        SortedSet<TopicPartition> assignedTopicPartitions = 
topicPartitionsForActiveTasks(targetAssignment.activeTasks);
-        SortedSet<TopicPartition> partitionsToRevoke = new 
TreeSet<>(ownedTopicPartitionsFromSubscriptionState);
-        partitionsToRevoke.removeAll(assignedTopicPartitions);
 
-        final CompletableFuture<Void> tasksRevoked = 
revokeActiveTasks(activeTasksToRevoke);
+        final CompletableFuture<Void> tasksRevoked = 
revokeActiveTasks(activeTasksToRevoke, previouslyAppliedAssignment);
 
-        final CompletableFuture<Void> tasksRevokedAndAssigned = 
tasksRevoked.thenCompose(__ -> {
+        final CompletableFuture<AssignTasksResult> tasksRevokedAndAssigned = 
tasksRevoked.thenCompose(__ -> {
             if (!maybeAbortReconciliation()) {
-                return assignTasks(assignedActiveTasks, ownedActiveTasks, 
assignedStandbyTasks, assignedWarmupTasks, isGroupReady);
+                return assignTasks(assignedActiveTasks, 
previouslyAppliedAssignment, assignedStandbyTasks, assignedWarmupTasks, 
isGroupReady, assignmentToReconcile);
             }
             return CompletableFuture.completedFuture(null);
         });
 
         // The current target assignment is captured to ensure that 
acknowledging the current assignment is done with
         // the same target assignment that was used when this reconciliation 
was initiated.
-        LocalAssignment currentTargetAssignment = targetAssignment;
-        tasksRevokedAndAssigned.whenComplete((__, callbackError) -> {
+        tasksRevokedAndAssigned.whenComplete((assignTasksResult, 
callbackError) -> {
             if (callbackError != null) {
                 log.error("Reconciliation failed for tasks {}",
-                    currentTargetAssignment, callbackError);
+                    assignmentToReconcile, callbackError);
                 markReconciliationCompleted();
             } else {
-                if (reconciliationInProgress && !maybeAbortReconciliation()) {
-                    currentAssignment = currentTargetAssignment;
+                if (isReconciliationInProgress() && 
!maybeAbortReconciliation()) {
+                    
subscriptionState.enablePartitionsAwaitingCallback(appliedAssignment.assignedPartitions);
+                    
subscriptionState.clearPendingRevocation(appliedAssignment.assignedPartitions);
+                    lastReconciledMetadataVersion = 
assignTasksResult.metadataVersion();
+                    currentAssignment = 
assignTasksResult.toBeCurrentAssignment;
                     transitionTo(MemberState.ACKNOWLEDGING);

Review Comment:
   I think this can get the member stuck once the coordinator moves one of the 
unresolved tasks away. The heartbeat only sends 
`ActiveTasks`/`StandbyTasks`/`WarmupTasks` when `reconciledAssignment` changed 
since the last heartbeat, and the coordinator treats missing owned tasks as 
still owning everything (`hasNotReleased` in the streams 
`CurrentAssignmentBuilder`).
   
   Example: the target is [0_0, 1_0], 0_0 can't be resolved, and we ack [1_0]. 
Then the coordinator moves 0_0 to member B. Our next heartbeat doesn't carry 
owned tasks, so the coordinator puts us in `UNREVOKED_TASKS` and waits for 0_0 
to be released. We reconcile [1_0] again, but `reconciledAssignment` is still 
[1_0], so the owned tasks aren't sent again, and B waits for 0_0 until our 
owned tasks change for some other reason (or we rejoin).
   
   The consumer avoids this because `ConsumerHeartbeatRequestManager` compares 
the whole `LocalAssignment` including the local epoch, so every ack resends the 
owned partitions. I think `StreamsGroupHeartbeatRequestManager` needs the same, 
plus a test for this scenario.



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsMembershipManager.java:
##########
@@ -1272,45 +1427,84 @@ private CompletableFuture<Void> assignTasks(final 
SortedSet<StreamsRebalanceData
                 .collect(Collectors.joining(", "))
         );
 
-        final SortedSet<TopicPartition> partitionsToAssign = 
topicPartitionsForActiveTasks(activeTasksToAssign);
-        final SortedSet<TopicPartition> partitionsToAssignNotPreviouslyOwned =
-            partitionsToAssignNotPreviouslyOwned(partitionsToAssign, 
topicPartitionsForActiveTasks(ownedActiveTasks));
+        // TODO: KAFKA-21256
+        if (assignmentToReconcile.isAssignedMetadataFromBroker()) {
+            throw new IllegalStateException("This protocol currently is not 
supported yet.");
+        }
+
+        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 = 
previouslyAppliedAssignment.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 only if the 
resolved assignment
+        // matches both the applied state and the successfully reconciled 
state.
+        if 
(resolvedPartitionsToAssign.equals(appliedAssignment.assignedPartitions) &&
+            
resolvedPartitionsToAssign.equals(currentAssignment.assignedPartitions) &&
+            
resolvedLocalAssignment.activeTasks.equals(appliedAssignment.activeTasks) &&
+            resolvedLocalAssignment.reconcilingCompleted(currentAssignment)
+        ) {
+            return CompletableFuture.completedFuture(new 
AssignTasksResult(resolvedLocalAssignment, metadataVersion));

Review Comment:
   While the assignment is partially resolved, every metadata update ends up 
here and then transitions to `ACKNOWLEDGING`, which sends a heartbeat even 
though nothing changed. Together with the forced metadata update in 
`maybeReconcile`, that's a metadata request plus a heartbeat about once per 
second with the default backoff, for as long as a task stays unresolved. The 
consumer only acks a partially resolved assignment when the local epoch changed 
(the early return on `currentAssignment.localEpoch == 
resolvedAssignment.localEpoch` in `AbstractMembershipManager`). Could we do the 
same here?



##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsMembershipManager.java:
##########
@@ -1272,45 +1427,84 @@ private CompletableFuture<Void> assignTasks(final 
SortedSet<StreamsRebalanceData
                 .collect(Collectors.joining(", "))
         );
 
-        final SortedSet<TopicPartition> partitionsToAssign = 
topicPartitionsForActiveTasks(activeTasksToAssign);
-        final SortedSet<TopicPartition> partitionsToAssignNotPreviouslyOwned =
-            partitionsToAssignNotPreviouslyOwned(partitionsToAssign, 
topicPartitionsForActiveTasks(ownedActiveTasks));
+        // TODO: KAFKA-21256
+        if (assignmentToReconcile.isAssignedMetadataFromBroker()) {
+            throw new IllegalStateException("This protocol currently is not 
supported yet.");

Review Comment:
   `assignmentToReconcile` is always the target assignment, which `updateWith` 
creates with null partitions, so this can't trigger. I'd drop the placeholder 
and the null convention for `assignedPartitions` until the KIP adds 
broker-provided metadata. Right now `isAssignedMetadataFromBroker()` returns 
true for the locally resolved assignments, which is confusing.



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