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 tag value ids instead of the tag strings** `RackAwareStandbyPicker` numbers the values of each key once in its constructor and only works on those ids afterwards. For example, with `rack.aware.assignment.tags=cluster,zone` and these process groups: | group | cluster | zone | value ids (cluster, zone) | |---|---|---|---| | G1 | c1 | z1 | [0, 0] | | G2 | c2 | z2 | [1, 1] | | G3 | c1 | z3 | [0, 2] | | G4 | c2 | (no zone tag) | [1, -1] | the ids are `cluster: c1=0, c2=1` and `zone: z1=0, z2=1, z3=2`, and the values used by the holders of a task become one `boolean` array per key: `usedTagValues = [boolean[2], boolean[3]]`. When the active task is on G2, `markUsed(G2)` sets `usedTagValues[0][1]` (c2) and `usedTagValues[1][1]` (z2). The check the picker runs most often, whether a candidate carries a value that no holder of the task carries yet, goes from two hash lookups to two array reads: It runs for every candidate on every filter pass of `pickCandidates`, and once per lower-priority key in `compareDiversity`, so it is the inner loop of the picker. `markUsed` also no longer calls the client-tags function and looks up every key for each holder. The strings were only ever compared for equality. Within a key, two ids are equal exactly when the strings are, so nothing is lost, and the string-to-id maps are dropped once the constructor is done. A missing tag gets -1 and, like `null` before, never counts as a new value. -- 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]
