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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/CurrentAssignmentBuilder.java:
##########
@@ -0,0 +1,447 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.coordinator.group.streams;
+
+import org.apache.kafka.common.errors.FencedMemberEpochException;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.BiPredicate;
+
+/**
+ * The CurrentAssignmentBuilder class encapsulates the reconciliation engine 
of the streams group protocol. Given the current state of a
+ * member and a desired or target assignment state, the state machine takes 
the necessary steps to converge them.
+ */
+public class CurrentAssignmentBuilder {
+
+    /**
+     * The streams group member which is reconciled.
+     */
+    private final StreamsGroupMember member;
+
+    /**
+     * The target assignment epoch.
+     */
+    private int targetAssignmentEpoch;
+
+    /**
+     * The target assignment.
+     */
+    private TaskTuple targetAssignment;
+
+    /**
+     * A function which returns the current process ID of an active task or 
null if the active task
+     * is not assigned. The current process ID is the process ID of the 
current owner.
+     */
+    private BiFunction<String, Integer, String> currentActiveTaskProcessId;
+
+    /**
+     * A function which returns the current process IDs of a standby task or 
null if the standby
+     * task is not assigned. The current process IDs are the process IDs of 
all current owners.
+     */
+    private BiFunction<String, Integer, Set<String>> 
currentStandbyTaskProcessIds;
+
+    /**
+     * A function which returns the current process IDs of a warmup task or 
null if the warmup task
+     * is not assigned. The current process IDs are the process IDs of all 
current owners.
+     */
+    private BiFunction<String, Integer, Set<String>> 
currentWarmupTaskProcessIds;
+
+    /**
+     * The tasks owned by the member. This may be provided by the member in 
the StreamsGroupHeartbeat request.
+     */
+    private Optional<TaskTuple> ownedTasks = Optional.empty();
+
+    /**
+     * Constructs the CurrentAssignmentBuilder based on the current state of 
the provided streams group member.
+     *
+     * @param member The streams group member that must be reconciled.
+     */
+    public CurrentAssignmentBuilder(StreamsGroupMember member) {
+        this.member = Objects.requireNonNull(member);
+    }
+
+    /**
+     * Sets the target assignment epoch and the target assignment that the 
streams group member must be reconciled to.
+     *
+     * @param targetAssignmentEpoch The target assignment epoch.
+     * @param targetAssignment      The target assignment.
+     * @return This object.
+     */
+    public CurrentAssignmentBuilder withTargetAssignment(
+        int targetAssignmentEpoch,
+        TaskTuple targetAssignment
+    ) {
+        this.targetAssignmentEpoch = targetAssignmentEpoch;
+        this.targetAssignment = Objects.requireNonNull(targetAssignment);
+        return this;
+    }
+
+    /**
+     * Sets a BiFunction which allows to retrieve the current process ID of an 
active task. This is
+     * used by the state machine to determine if an active task is free or 
still used by another
+     * member, and if there is still a task on a specific process that is not 
yet revoked.
+     *
+     * @param currentActiveTaskProcessId A BiFunction which gets the memberId 
of a subtopology id /
+     *                                   partition id pair.
+     * @return This object.
+     */
+    public CurrentAssignmentBuilder withCurrentActiveTaskProcessId(
+        BiFunction<String, Integer, String> currentActiveTaskProcessId
+    ) {
+        this.currentActiveTaskProcessId = 
Objects.requireNonNull(currentActiveTaskProcessId);
+        return this;
+    }
+
+    /**
+     * Sets a BiFunction which allows to retrieve the current process IDs of a 
standby task. This is
+     * used by the state machine to determine if there is still a task on a 
specific process that is
+     * not yet revoked.
+     *
+     * @param currentStandbyTaskProcessIds A BiFunction which gets the 
memberIds of a subtopology
+     *                                     ids / partition ids pair.
+     * @return This object.
+     */
+    public CurrentAssignmentBuilder withCurrentStandbyTaskProcessIds(
+        BiFunction<String, Integer, Set<String>> currentStandbyTaskProcessIds
+    ) {
+        this.currentStandbyTaskProcessIds = 
Objects.requireNonNull(currentStandbyTaskProcessIds);
+        return this;
+    }
+
+    /**
+     * Sets a BiFunction which allows to retrieve the current process IDs of a 
warmup task. This is
+     * used by the state machine to determine if there is still a task on a 
specific process that is
+     * not yet revoked.
+     *
+     * @param currentWarmupTaskProcessIds A BiFunction which gets the 
memberIds of a subtopology ids
+     *                                    / partition ids pair.
+     * @return This object.
+     */
+    public CurrentAssignmentBuilder withCurrentWarmupTaskProcessIds(
+        BiFunction<String, Integer, Set<String>> currentWarmupTaskProcessIds
+    ) {
+        this.currentWarmupTaskProcessIds = 
Objects.requireNonNull(currentWarmupTaskProcessIds);
+        return this;
+    }
+
+    /**
+     * Sets the tasks currently owned by the member. This comes directly from 
the last StreamsGroupHeartbeat request. This is used to
+     * determine if the member has revoked the necessary tasks. Passing null 
into this function means that the member did not provide
+     * its owned tasks in this heartbeat.
+     *
+     * @param ownedAssignment A collection of active, standby and warm-up tasks
+     * @return This object.
+     */
+    protected CurrentAssignmentBuilder withOwnedAssignment(
+        TaskTuple ownedAssignment
+    ) {
+        this.ownedTasks = Optional.ofNullable(ownedAssignment);
+        return this;
+    }
+
+    /**
+     * Builds the next state for the member or keep the current one if it is 
not possible to move forward with the current state.
+     *
+     * @return A new StreamsGroupMember or the current one.
+     */
+    public StreamsGroupMember build() {
+        switch (member.state()) {
+            case STABLE:
+                // When the member is in the STABLE state, we verify if a newer
+                // epoch (or target assignment) is available. If it is, we can
+                // reconcile the member towards it. Otherwise, we return.
+                if (member.memberEpoch() != targetAssignmentEpoch) {
+                    return computeNextAssignment(
+                        member.memberEpoch(),
+                        member.assignedTasks()
+                    );
+                } else {
+                    return member;
+                }
+
+            case UNREVOKED_TASKS:
+                // When the member is in the UNREVOKED_TASKS state, we wait
+                // until the member has revoked the necessary tasks. They are
+                // considered revoked when they are not anymore reported in the
+                // owned tasks set in the StreamsGroupHeartbeat API.
+
+                // If the member provides its owned tasks, we verify if it 
still
+                // owns any of the revoked tasks. If it did not provide it's
+                // owned tasks, or we still own some of the revoked tasks, we
+                // cannot progress.
+                if (
+                    ownedTasks.isEmpty() || 
ownedTasks.get().containsAny(member.tasksPendingRevocation())
+                ) {
+                    return member;
+                }
+
+                // When the member has revoked all the pending tasks, it can
+                // transition to the next epoch (current + 1) and we can 
reconcile
+                // its state towards the latest target assignment.
+                return computeNextAssignment(
+                    member.memberEpoch() + 1,

Review Comment:
   Good comment! Interesting idea, but I think you are missing something. See, 
whether `member.tasksPendingRevocation` was defined in a previous heartbeat, 
which may have had a different target assignment than the latest target 
assignment. So here, all we know is that we can transition to the next target 
epoch, because we revoked all tasks that we need to according to that previous 
target assignment. This doesn't imply much about what will go on in 
`buildNewMember`, because we are using a new target assignment, which may 
require other tasks to be revoked. For example, if we find out that we actually 
need to revoke more tasks to reach the latest target assignment, we will use 
the result of `member.memberEpoch() + 1` and transition to UNREVOKED_TASKS.



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to