gabriellefu commented on code in PR #23588:
URL: https://github.com/apache/kafka/pull/23588#discussion_r4174277778


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/assignor/RackAwareStandbyPicker.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+/**
+ * Picks the processes for the standbys of one task over the keys of {@code 
rack.aware.assignment.tags}, whose list
+ * order is the priority. A standby goes to an eligible process whose value 
for the highest-priority key is not yet
+ * carried by a holder of the task; among those, the one with the 
lexicographically largest diversity vector over the
+ * lower-priority keys wins (one bit per key, set where its value is new to 
the task), then the assignor's tie-break
+ * decides. When no eligible process has a new value for that key, the key is 
given up and the search repeats with the
+ * next key in priority order. Once every key is given up no process can make 
the task more diverse, so the assignor's
+ * tag-blind pass places the remaining standbys.
+ * <p>
+ * For each task: {@link #startTask()}, {@link #markUsed(Object)} for the 
active owner, then per standby
+ * {@link #pickCandidates(Predicate)} followed by the assignor's own choice, 
and {@link #markUsed(Object)} for the
+ * winner.
+ *
+ * @param <P> The assignor's process type.
+ */
+final class RackAwareStandbyPicker<P> {
+
+    /** A process with the id of its value for each key, -1 where it has none. 
Ids number the values of each key. */
+    private record TaggedProcess<P>(P process, int[] valueIds) {
+    }
+
+    private final List<String> tagKeys;
+    private final List<TaggedProcess<P>> allProcesses;
+    private final Map<P, int[]> valueIdsByProcess;
+
+    // State of the task being placed, reset by startTask.
+    private final boolean[][] usedTagValues;             // per key and value 
id, whether a holder of the task carries it
+    private int priorityIndex;                           // position in 
tagKeys of the key the filter enforces
+    private List<TaggedProcess<P>> candidates;           // the pool the next 
pick filters
+
+    /**
+     * @param tagKeys    The keys of {@code rack.aware.assignment.tags}, 
highest priority first.
+     * @param processes  All processes of the group.
+     * @param clientTags The client tags of a process.
+     */
+    RackAwareStandbyPicker(
+        final List<String> tagKeys,
+        final Collection<P> processes,
+        final Function<P, Map<String, String>> clientTags
+    ) {
+        this.tagKeys = tagKeys;
+        final List<Map<String, Integer>> idsByValue = new 
ArrayList<>(tagKeys.size());
+        for (int i = 0; i < tagKeys.size(); i++) {

Review Comment:
   **Why ids instead of tag strings**
   
   The picker turns each tag value into a small number once, up front. With 
`rack.aware.assignment.tags=cluster,zone`:
   
   | group | cluster | zone | ids |
   |---|---|---|---|
   | G1 | c1 | z1 | [0, 0] |
   | G2 | c2 | z2 | [1, 1] |
   | G3 | c1 | z3 | [0, 2] |
   
   "Is this value already used by the task?" then becomes an array lookup 
instead of a string lookup in a set:
   
   ```java
   // before
   
!usedTagValues.get(keyIndex).contains(candidate.clientTags.get(tagKeys.get(keyIndex)))
   // after
   !usedTagValues[keyIndex][candidate.valueIds[keyIndex]]
   ```
   
   This check runs for every candidate on every pick, so it's the hot path. We 
only ever compared tags for equality, and ids are equal exactly when the 
strings are, so the behavior is the same.



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