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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/assignor/MockAssignor.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.assignor;
+
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Mock implementation of {@link TaskAssignor} that assigns tasks to members 
in a round-robin fashion, with a bit of stickiness.
+ */
+public class MockAssignor implements TaskAssignor {
+
+    public static final String MOCK_ASSIGNOR_NAME = "mock";
+
+    @Override
+    public String name() {
+        return MOCK_ASSIGNOR_NAME;
+    }
+
+    @Override
+    public GroupAssignment assign(
+        final GroupSpec groupSpec,
+        final TopologyDescriber topologyDescriber
+    ) throws TaskAssignorException {
+
+        Map<String, MemberAssignment> newTargetAssignment = new HashMap<>();
+        Map<String, String[]> subtopologyToActiveMember = new HashMap<>();
+
+        for (String subtopology : topologyDescriber.subtopologies()) {
+            int numberOfPartitions = topologyDescriber.numTasks(subtopology);
+            subtopologyToActiveMember.put(subtopology, new 
String[numberOfPartitions]);
+        }
+
+        // Copy existing assignment and fill temporary data structures
+        for (Map.Entry<String, AssignmentMemberSpec> memberEntry : 
groupSpec.members().entrySet()) {
+            final String memberId = memberEntry.getKey();
+            final AssignmentMemberSpec memberSpec = memberEntry.getValue();
+
+            Map<String, Set<Integer>> activeTasks = new 
HashMap<>(memberSpec.activeTasks());
+
+            newTargetAssignment.put(memberId, new 
MemberAssignment(activeTasks, new HashMap<>(), new HashMap<>()));
+            for (Map.Entry<String, Set<Integer>> entry : 
activeTasks.entrySet()) {
+                final String subtopologyId = entry.getKey();
+                final Set<Integer> taskIds = entry.getValue();
+                final String[] activeMembers = 
subtopologyToActiveMember.get(subtopologyId);
+                for (int taskId : taskIds) {
+                    if (activeMembers[taskId] != null) {
+                        throw new TaskAssignorException(
+                            "Task " + taskId + " of subtopology " + 
subtopologyId + " is assigned to multiple members.");
+                    }
+                    activeMembers[taskId] = memberId;
+                }
+            }
+        }
+
+        // Define priority queue to sort members by task count
+        PriorityQueue<MemberAndTaskCount> memberAndTaskCount = new 
PriorityQueue<>(Comparator.comparingInt(m -> m.taskCount));
+        memberAndTaskCount.addAll(
+            newTargetAssignment.keySet().stream()
+                .map(memberId -> new MemberAndTaskCount(memberId,
+                    
newTargetAssignment.get(memberId).activeTasks().values().stream().mapToInt(Set::size).sum()))
+                .collect(Collectors.toSet())
+        );
+
+        // Assign unassigned tasks to members with the fewest tasks
+        for (Map.Entry<String, String[]> entry : 
subtopologyToActiveMember.entrySet()) {
+            final String subtopologyId = entry.getKey();
+            final String[] activeMembers = entry.getValue();
+            for (int i = 0; i < activeMembers.length; i++) {
+                if (activeMembers[i] == null) {
+                    final MemberAndTaskCount m = memberAndTaskCount.poll();
+                    if (m == null) {
+                        throw new TaskAssignorException("No member available 
to assign task " + i + " of subtopology " + subtopologyId);

Review Comment:
   Done



##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/assignor/MockAssignor.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.assignor;
+
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Mock implementation of {@link TaskAssignor} that assigns tasks to members 
in a round-robin fashion, with a bit of stickiness.
+ */
+public class MockAssignor implements TaskAssignor {
+
+    public static final String MOCK_ASSIGNOR_NAME = "mock";
+
+    @Override
+    public String name() {
+        return MOCK_ASSIGNOR_NAME;
+    }
+
+    @Override
+    public GroupAssignment assign(
+        final GroupSpec groupSpec,
+        final TopologyDescriber topologyDescriber
+    ) throws TaskAssignorException {
+
+        Map<String, MemberAssignment> newTargetAssignment = new HashMap<>();
+        Map<String, String[]> subtopologyToActiveMember = new HashMap<>();
+
+        for (String subtopology : topologyDescriber.subtopologies()) {
+            int numberOfPartitions = topologyDescriber.numTasks(subtopology);
+            subtopologyToActiveMember.put(subtopology, new 
String[numberOfPartitions]);
+        }
+
+        // Copy existing assignment and fill temporary data structures
+        for (Map.Entry<String, AssignmentMemberSpec> memberEntry : 
groupSpec.members().entrySet()) {
+            final String memberId = memberEntry.getKey();
+            final AssignmentMemberSpec memberSpec = memberEntry.getValue();
+
+            Map<String, Set<Integer>> activeTasks = new 
HashMap<>(memberSpec.activeTasks());
+
+            newTargetAssignment.put(memberId, new 
MemberAssignment(activeTasks, new HashMap<>(), new HashMap<>()));
+            for (Map.Entry<String, Set<Integer>> entry : 
activeTasks.entrySet()) {
+                final String subtopologyId = entry.getKey();
+                final Set<Integer> taskIds = entry.getValue();
+                final String[] activeMembers = 
subtopologyToActiveMember.get(subtopologyId);
+                for (int taskId : taskIds) {
+                    if (activeMembers[taskId] != null) {
+                        throw new TaskAssignorException(
+                            "Task " + taskId + " of subtopology " + 
subtopologyId + " is assigned to multiple members.");

Review Comment:
   Done



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