mjsax commented on code in PR #23790:
URL: https://github.com/apache/kafka/pull/23790#discussion_r4546049974


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/assignor/RackAwareStandbyPicker.java:
##########
@@ -0,0 +1,452 @@
+/*
+ * 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 org.apache.kafka.coordinator.group.Utils;
+
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.function.ToDoubleFunction;
+import java.util.stream.IntStream;
+
+/**
+ * Picks the conditions for the next standby of one task: the most tag keys 
that a process with room can meet,
+ * preferring the keys earlier in {@code rack.aware.assignment.tags}. A 
process meets a tag key when it has a value
+ * for it that no holder of the task has yet. A pick also returns the 
least-loaded process with room that meets the
+ * conditions, and the picker can tell whether a process meets them. The 
caller reports every change to a load or room
+ * with {@link #updated(Object)} or {@link #updatedAll()}.
+ * <p>
+ * The processes sit in a tree with one level per tag key, those with the same 
values sharing a leaf. Scanning every
+ * leaf per standby is too slow once there are many, e.g. one per process for 
a host tag. Each node keeps its
+ * least-loaded process with room, and its children are ordered by theirs. A 
query skips a subtree whose own value is
+ * used and stops at the first child no lighter than the best process found.
+ *
+ * @param <P> The assignor's process type.
+ */
+final class RackAwareStandbyPicker<P> {
+
+    // The value index of a process for a key it has no value for.
+    private static final int MISSING_VALUE_INDEX = -1;
+    // The key index and value index of the root, which has no own value.
+    private static final int ROOT_PLACEHOLDER_INDEX = -2;
+
+    private final ToDoubleFunction<P> processLoad;
+    private final Predicate<P> processHasRoom;
+    // The keys in the order of rack.aware.assignment.tags.
+    private final List<TagKey> keys;
+    // Per key index, the number of values.
+    private final int[] numValues;
+    // Per level from the top, the index of its key. The priority of a key is 
still its order in
+    // rack.aware.assignment.tags, not this one.
+    private final int[] tagKeysInTreeOrder;
+    // Per process, its current entry, which knows its leaf.
+    private final Map<P, QueuedProcess<P>> queuedByProcess;
+    private final Node<P> root;
+
+    // Per key index, one bit per value index, set once a holder of the task 
carries that value.
+    // Cleared by startTask.
+    private final BitSet[] usedValues;
+
+    /**
+     * @param tagKeys    The keys of {@code rack.aware.assignment.tags}; not 
empty, since without them there is nothing to pick.
+     * @param processes  All processes of the streams group, distinct; an 
earlier one wins a load tie.
+     * @param clientTags The client tags of a process.
+     * @param load       The load of a process.
+     * @param hasRoom    Whether a process can take another task; {@code 
process -> true} without a limit.
+     */
+    RackAwareStandbyPicker(
+        final List<String> tagKeys,
+        final Collection<P> processes,
+        final Function<P, Map<String, String>> clientTags,
+        final ToDoubleFunction<P> load,
+        final Predicate<P> hasRoom
+    ) {
+        if (tagKeys.isEmpty()) {
+            throw new IllegalArgumentException("Tag keys must not be empty");
+        }
+        processLoad = load;
+        processHasRoom = hasRoom;
+        // One level per tag key, so this is also the number of tag keys.
+        final int treeHeight = tagKeys.size();
+        // Per key, the index of each of its values, in the order the values 
first appear on the processes.
+        final List<Map<String, Integer>> valueIndexAllKeys = new 
ArrayList<>(treeHeight);
+        for (int keyIndex = 0; keyIndex < treeHeight; keyIndex++) {
+            valueIndexAllKeys.add(new HashMap<>());
+        }
+        // Per process in order, its value index for each key.

Review Comment:
   If we say "in order", why is the input type a `Collection`? Should we not 
either enforce to pass in something ordered like a `List`, or sort the 
`Collection` we get?



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