dajac commented on code in PR #13443:
URL: https://github.com/apache/kafka/pull/13443#discussion_r1189296779


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/RangeAssignor.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.assignor;
+
+import org.apache.kafka.common.Uuid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static java.lang.Math.min;
+
+/**
+ * This Range Assignor inherits properties of both the range assignor and the 
sticky assignor.
+ * The properties are as follows:
+ * <ol>
+ * <li> Each member must get at least one partition from every topic that it 
is subscribed to. The only exception is when
+ * the number of subscribed members is greater than the number of partitions 
for that topic. (Range) </li>
+ * <li> Partitions should be assigned to members in a way that facilitates the 
join operation when required. (Range)
+ * This can only be done if every member is subscribed to the same topics and 
the topics are co-partitioned.
+ * Two streams are co-partitioned if the following conditions are met:
+ * <ul>
+ * <li> The keys must have the same schemas. </li>
+ * <li> The topics involved must have the same number of partitions. </li>
+ * </ul>
+ * </li>
+ * <li> Members should retain as much of their previous assignment as possible 
to reduce the number of partition movements during reassignment. (Sticky) </li>
+ * </ol>
+ */
+public class RangeAssignor implements PartitionAssignor {
+    private static final Logger log = 
LoggerFactory.getLogger(RangeAssignor.class);
+
+    public static final String RANGE_ASSIGNOR_NAME = "range";
+
+    @Override
+    public String name() {
+        return RANGE_ASSIGNOR_NAME;
+    }
+
+    /**
+     * Pair of memberId and remaining partitions to meet the quota.
+     */
+    private static class MemberWithRemainingAssignments {
+        /**
+         * Member Id.
+         */
+        private final String memberId;
+        /**
+         * Number of partitions required to meet the assignment quota.
+         */
+        private final Integer remaining;
+
+        public MemberWithRemainingAssignments(String memberId, Integer 
remaining) {
+            this.memberId = memberId;
+            this.remaining = remaining;
+        }
+    }
+
+    /**
+     * @return Map of topicIds to a list of members subscribed to them.
+     */
+    private Map<Uuid, List<String>> membersPerTopic(final AssignmentSpec 
assignmentSpec) {
+        Map<Uuid, List<String>> membersPerTopic = new HashMap<>();
+        Map<String, AssignmentMemberSpec> membersData = 
assignmentSpec.members();
+
+        membersData.forEach((memberId, memberMetadata) -> {
+            Collection<Uuid> topics = memberMetadata.subscribedTopicIds();
+            for (Uuid topicId: topics) {
+                // Only topics that are present in both the subscribed topics 
list and the topic metadata should be considered for assignment.
+                if (assignmentSpec.topics().containsKey(topicId)) {
+                    membersPerTopic
+                        .computeIfAbsent(topicId, k -> new ArrayList<>())
+                        .add(memberId);
+                } else {
+                    log.warn("Member " + memberId + " subscribed to topic " + 
topicId + " which doesn't exist in the topic metadata");
+                }
+            }
+        });
+
+        return membersPerTopic;
+    }
+
+    /**
+     * <p> The algorithm includes the following steps:
+     * <ol>
+     * <li> Generate a map of <code>membersPerTopic</code> using the given 
member subscriptions.</li>
+     * <li> Generate a list of members 
(<code>potentiallyUnfilledMembers</code>) that have not met the minimum 
required quota of partitions for the assignment AND
+     * get a list (<code>assignedStickyPartitionsPerTopic</code>) of 
partitions that will be retained in the new assignment.</li>
+     * <li> Add members from the <code>potentiallyUnfilledMembers</code> list 
to the <code>unfilledMembersPerTopic</code> map if they haven't met the total 
required quota
+     * i.e. minRequiredQuota + 1, if the member is designated to receive one 
of the excess partitions OR minRequiredQuota otherwise. </li>
+     * <li> Generate a list of unassigned partitions by calculating the 
difference between the total partitions for the topic and the assigned (sticky) 
partitions. </li>
+     * <li> Check if unfilled members exist for the current topicId and assign 
partitions to them in ranges from the <code>unassignedPartitionsPerTopic</code> 
map
+     *      based on the remaining partitions value stored. </li>
+     * </ol>
+     * </p>
+     */
+    @Override
+    public GroupAssignment assign(final AssignmentSpec assignmentSpec) throws 
PartitionAssignorException {
+        Map<String, MemberAssignment> newAssignment = new HashMap<>();
+
+        // Step 1

Review Comment:
   Ok. I was not aware of this. Sorry for this.



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/RangeAssignor.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.assignor;
+
+import org.apache.kafka.common.Uuid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static java.lang.Math.min;
+
+/**
+ * This Range Assignor inherits properties of both the range assignor and the 
sticky assignor.
+ * The properties are as follows:
+ * <ol>
+ * <li> Each member must get at least one partition from every topic that it 
is subscribed to. The only exception is when
+ * the number of subscribed members is greater than the number of partitions 
for that topic. (Range) </li>
+ * <li> Partitions should be assigned to members in a way that facilitates the 
join operation when required. (Range)
+ * This can only be done if every member is subscribed to the same topics and 
the topics are co-partitioned.
+ * Two streams are co-partitioned if the following conditions are met:
+ * <ul>
+ * <li> The keys must have the same schemas. </li>
+ * <li> The topics involved must have the same number of partitions. </li>
+ * </ul>
+ * </li>
+ * <li> Members should retain as much of their previous assignment as possible 
to reduce the number of partition movements during reassignment. (Sticky) </li>
+ * </ol>
+ */
+public class RangeAssignor implements PartitionAssignor {
+    private static final Logger log = 
LoggerFactory.getLogger(RangeAssignor.class);
+
+    public static final String RANGE_ASSIGNOR_NAME = "range";
+
+    @Override
+    public String name() {
+        return RANGE_ASSIGNOR_NAME;
+    }
+
+    /**
+     * Pair of memberId and remaining partitions to meet the quota.
+     */
+    private static class MemberWithRemainingAssignments {
+        /**
+         * Member Id.
+         */
+        private final String memberId;
+        /**
+         * Number of partitions required to meet the assignment quota.
+         */
+        private final Integer remaining;
+
+        public MemberWithRemainingAssignments(String memberId, Integer 
remaining) {
+            this.memberId = memberId;
+            this.remaining = remaining;
+        }
+    }
+
+    /**
+     * @return Map of topicIds to a list of members subscribed to them.
+     */
+    private Map<Uuid, List<String>> membersPerTopic(final AssignmentSpec 
assignmentSpec) {
+        Map<Uuid, List<String>> membersPerTopic = new HashMap<>();
+        Map<String, AssignmentMemberSpec> membersData = 
assignmentSpec.members();
+
+        membersData.forEach((memberId, memberMetadata) -> {
+            Collection<Uuid> topics = memberMetadata.subscribedTopicIds();
+            for (Uuid topicId: topics) {
+                // Only topics that are present in both the subscribed topics 
list and the topic metadata should be considered for assignment.
+                if (assignmentSpec.topics().containsKey(topicId)) {
+                    membersPerTopic
+                        .computeIfAbsent(topicId, k -> new ArrayList<>())
+                        .add(memberId);
+                } else {
+                    log.warn("Member " + memberId + " subscribed to topic " + 
topicId + " which doesn't exist in the topic metadata");
+                }
+            }
+        });
+
+        return membersPerTopic;
+    }
+
+    /**
+     * <p> The algorithm includes the following steps:
+     * <ol>
+     * <li> Generate a map of <code>membersPerTopic</code> using the given 
member subscriptions.</li>
+     * <li> Generate a list of members 
(<code>potentiallyUnfilledMembers</code>) that have not met the minimum 
required quota of partitions for the assignment AND
+     * get a list (<code>assignedStickyPartitionsPerTopic</code>) of 
partitions that will be retained in the new assignment.</li>
+     * <li> Add members from the <code>potentiallyUnfilledMembers</code> list 
to the <code>unfilledMembersPerTopic</code> map if they haven't met the total 
required quota
+     * i.e. minRequiredQuota + 1, if the member is designated to receive one 
of the excess partitions OR minRequiredQuota otherwise. </li>
+     * <li> Generate a list of unassigned partitions by calculating the 
difference between the total partitions for the topic and the assigned (sticky) 
partitions. </li>
+     * <li> Check if unfilled members exist for the current topicId and assign 
partitions to them in ranges from the <code>unassignedPartitionsPerTopic</code> 
map
+     *      based on the remaining partitions value stored. </li>
+     * </ol>
+     * </p>
+     */
+    @Override
+    public GroupAssignment assign(final AssignmentSpec assignmentSpec) throws 
PartitionAssignorException {
+        Map<String, MemberAssignment> newAssignment = new HashMap<>();
+
+        // Step 1
+        Map<Uuid, List<String>> membersPerTopic = 
membersPerTopic(assignmentSpec);
+
+        membersPerTopic.forEach((topicId, membersForTopic) -> {
+
+            AssignmentTopicMetadata topicData = 
assignmentSpec.topics().get(topicId);
+            int numPartitionsForTopic = topicData.numPartitions();
+            int minRequiredQuota = numPartitionsForTopic / 
membersForTopic.size();
+            // Each member can get only ONE extra partition per topic after 
receiving the minimum quota.
+            int numMembersWithExtraPartition = numPartitionsForTopic % 
membersForTopic.size();
+
+            // Idle members case : When the number of members subscribed to a 
topic is greater than the total number of partitions,
+            // all members get assigned via the "extra partitions" logic since 
minRequiredQuota = 0.
+
+            // Step 2

Review Comment:
   Ack.



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