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


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/streams/assignor/TagTree.java:
##########
@@ -0,0 +1,423 @@
+/*
+ * 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.streams.assignor.IdenticalTagGroups.QueuedProcess;
+import 
org.apache.kafka.coordinator.group.streams.assignor.IdenticalTagGroups.TagGroup;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+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.PriorityQueue;
+
+/**
+ * A tree built from {@code rack.aware.assignment.tags} with every tag group 
of {@link IdenticalTagGroups} as a leaf.
+ * Given some tag keys as conditions, it tells whether a tag group with room 
has values for them that no holder of the
+ * task has yet, and returns the least-loaded process with room among such tag 
groups. It assumes that loads only grow
+ * and room only shrinks.
+ *
+ * @param <P> The assignor's process type.
+ */
+final class TagTree<P> {

Review Comment:
   Sure, I'll add a note to the class Javadoc.
   
   From the earlier JMH runs (1000 processes, 10k tasks, 2 standbys, full 
assignment): with 3 tag keys and 42 different combinations, the linear scan 
took ~13 ms. With 3 tag keys and 1000 different combinations (one per process, 
e.g. a host tag), it took 229 ms. The tree takes ~10 ms in both cases. The scan 
costs ~15–22 ns per tag group per standby, so it stays under 10 ms only up to 
about 20 tag groups.
   
   The tree has one level per tag key, and each node remembers the least-loaded 
process with room below it. For example, node c1 knows its least-loaded process 
is P2, node (c1, z1) knows P3, and node (c1, z2) knows P2. Each node also knows 
which tag values occur below it, so a pick can decide for a whole subtree at 
once. Say the active is in (c0, z0) and the standby needs a new cluster and a 
new zone. The tree never looks into c0 or (c1, z0), and takes P2 from (c1, z2) 
without looking at any process below (c1, z1) or (c1, z2).
   
   When a standby is placed, only the nodes on that process's path to the root 
can change, one per tag key. They are corrected lazily, when they reach the 
head of their parent's queue. So the cost of a pick depends mostly on the 
number of tag keys, not on the number of combinations, which is why 1000 
combinations are as fast as 42.



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