Copilot commented on code in PR #24250:
URL: https://github.com/apache/kafka/pull/24250#discussion_r4663614658
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsMembershipManager.java:
##########
@@ -1203,39 +1281,37 @@ 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))
{
+
+ if
(!previouslyAssignedAssignment.assignedPartitions.equals(ownedTopicPartitionsFromSubscriptionState))
{
Review Comment:
On a partition mismatch, this throws from the network-thread poll instead of
reporting the promised fatal error to the application. The network thread only
logs the exception, leaving reconciliation marked in progress so subsequent
polls cannot retry. Enqueue an `ErrorEvent`, clear the reconciliation marker,
and transition to FATAL before returning.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsMembershipManager.java:
##########
@@ -1162,23 +1231,32 @@ private void maybeReconcile() {
"current assignment.");
return;
}
- if (reconciliationInProgress) {
+
+ if (isReconciliationInProgress()) {
log.trace("Ignoring reconciliation attempt. Another reconciliation
is already in progress. Assignment {}" +
" will be handled in the next reconciliation loop.",
targetAssignment);
return;
}
+ if (isPartialAssignmentReconciled() && (metadata.updateVersion() ==
lastReconciledMetadataVersion)) {
+ metadata.requestUpdate(true);
+ return;
+ }
+
markReconciliationInProgress();
- SortedSet<StreamsRebalanceData.TaskId> assignedActiveTasks =
toTaskIdSet(targetAssignment.activeTasks);
- SortedSet<StreamsRebalanceData.TaskId> ownedActiveTasks =
toTaskIdSet(currentAssignment.activeTasks);
+ final LocalAssignment assignmentToReconcile = reconcilingAssignment;
+ final LocalAssignment previouslyAssignedAssignment = currentAssignment;
Review Comment:
If `onTasksAssigned` fails after `applyAssignment` succeeds,
`currentAssignment` still holds the old assignment, so the next reconciliation
fails the partition-consistency check instead of retrying. `leaveGroup()` also
revokes the old task set rather than the applied one. Track the applied
task/partition assignment separately and use it for ownership checks, retries,
and group leave, while retaining the callback-completed assignment for
acknowledgement.
##########
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);
Review Comment:
Pruning the cache using only resolved tasks discards counts still needed by
unresolved tasks. If one snapshot supplies an unresolved task's input A and a
later snapshot supplies B but omits A, the task can remain unresolved even
though both counts have been observed. Retain input topics from the full
captured target, including unresolved tasks.
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsMembershipManager.java:
##########
@@ -1162,23 +1231,32 @@ private void maybeReconcile() {
"current assignment.");
return;
}
- if (reconciliationInProgress) {
+
+ if (isReconciliationInProgress()) {
log.trace("Ignoring reconciliation attempt. Another reconciliation
is already in progress. Assignment {}" +
" will be handled in the next reconciliation loop.",
targetAssignment);
return;
}
+ if (isPartialAssignmentReconciled() && (metadata.updateVersion() ==
lastReconciledMetadataVersion)) {
+ metadata.requestUpdate(true);
+ return;
+ }
+
markReconciliationInProgress();
- SortedSet<StreamsRebalanceData.TaskId> assignedActiveTasks =
toTaskIdSet(targetAssignment.activeTasks);
- SortedSet<StreamsRebalanceData.TaskId> ownedActiveTasks =
toTaskIdSet(currentAssignment.activeTasks);
+ final LocalAssignment assignmentToReconcile = reconcilingAssignment;
+ final LocalAssignment previouslyAssignedAssignment = currentAssignment;
+
+ SortedSet<StreamsRebalanceData.TaskId> assignedActiveTasks =
toTaskIdSet(assignmentToReconcile.activeTasks);
+ SortedSet<StreamsRebalanceData.TaskId> ownedActiveTasks =
toTaskIdSet(previouslyAssignedAssignment.activeTasks);
Review Comment:
After task A's revocation succeeds, A remains in the state used for later
revocations until the replacement assignment completes. If applying that
assignment fails, a retry or `leaveGroup()` can revoke A again, contrary to the
promised duplicate-revocation prevention. Record successful revocations
independently and exclude those task IDs from later revocation requests, while
retaining failed revocations for retry and preserving the applied partition
snapshot for consistency checks.
##########
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:
A successful callback enables partitions and prunes the cache before the
outer handler checks whether reconciliation was aborted, contrary to the
promised late-callback protection. After fencing or group leave, this still
changes subscription flags for an obsolete assignment. Guard these side effects
with the active-attempt and abort checks, and update
`StreamsMembershipManagerReconciliationTest.testAssignmentCallbackSuccessAfterFencingDoesEnablePartitions`
to expect no additional enable call.
--
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]