This is an automated email from the ASF dual-hosted git repository.

Jackie-Jiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new 9880297d879 Reduce hash collisions in replica-group instance lookup 
(#19915)
9880297d879 is described below

commit 9880297d879fb2b0d21d454ec326d499beac468d
Author: P_K <[email protected]>
AuthorDate: Tue Nov 24 04:55:39 2026 +0530

    Reduce hash collisions in replica-group instance lookup (#19915)
---
 .../instanceselector/SegmentGroupsBuilder.java     |  37 ++++++-
 .../SegmentGroupsBuilderHashStrategyTest.java      | 116 +++++++++++++++++++++
 2 files changed, 151 insertions(+), 2 deletions(-)

diff --git 
a/pinot-broker/src/main/java/org/apache/pinot/broker/routing/instanceselector/SegmentGroupsBuilder.java
 
b/pinot-broker/src/main/java/org/apache/pinot/broker/routing/instanceselector/SegmentGroupsBuilder.java
index 54a4b74a297..2c5b11d0a9a 100644
--- 
a/pinot-broker/src/main/java/org/apache/pinot/broker/routing/instanceselector/SegmentGroupsBuilder.java
+++ 
b/pinot-broker/src/main/java/org/apache/pinot/broker/routing/instanceselector/SegmentGroupsBuilder.java
@@ -19,13 +19,15 @@
 package org.apache.pinot.broker.routing.instanceselector;
 
 import com.google.common.annotations.VisibleForTesting;
+import it.unimi.dsi.fastutil.Hash;
+import it.unimi.dsi.fastutil.HashCommon;
 import it.unimi.dsi.fastutil.ints.IntArrayList;
 import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
 import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
 import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
 import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -58,6 +60,36 @@ import org.slf4j.LoggerFactory;
 /// Not thread safe. Only used by the rebuild thread, for the duration of one 
rebuild.
 final class SegmentGroupsBuilder {
   private static final Logger LOGGER = 
LoggerFactory.getLogger(SegmentGroupsBuilder.class);
+
+  // Preserve Set equality regardless of instance iteration order.
+  // Mixed hashes reduce collisions without copying or sorting keys.
+  // Backing assignment maps must remain unchanged during the rebuild.
+  @VisibleForTesting
+  static final Hash.Strategy<Set<String>> INSTANCE_SET_STRATEGY = new 
Hash.Strategy<>() {
+
+    @Override
+    public int hashCode(Set<String> instances) {
+      if (instances == null) {
+        return 0;
+      }
+
+      int first = 0;
+      int second = 0;
+      for (String instance : instances) {
+        int hash = instance.hashCode();
+        first += HashCommon.mix(hash);
+        second += HashCommon.mix(hash ^ 0x9e3779b9);
+      }
+
+      return HashCommon.mix(first ^ Integer.rotateLeft(second, 16) ^ 
instances.size());
+    }
+
+    @Override
+    public boolean equals(Set<String> left, Set<String> right) {
+      return Objects.equals(left, right);
+    }
+  };
+
   private static final int UNKNOWN_POOL = Integer.MIN_VALUE;
   private static final int NO_GROUP = -1;
 
@@ -227,7 +259,8 @@ final class SegmentGroupsBuilder {
   /// Returns the instances unavailable for any old segment, keyed by the 
instances in the ideal state of the segment:
   /// the instances that are not online for routing in both the ideal state 
and the external view.
   private Map<Set<String>, Set<String>> 
computeUnavailableInstancesMap(List<InstanceStateMaps> instanceStateMapsList) {
-    Map<Set<String>, Set<String>> unavailableInstancesMap = new HashMap<>();
+    Object2ObjectOpenCustomHashMap<Set<String>, Set<String>> 
unavailableInstancesMap =
+        new Object2ObjectOpenCustomHashMap<>(INSTANCE_SET_STRATEGY);
     for (InstanceStateMaps instanceStateMaps : instanceStateMapsList) {
       if (instanceStateMaps._numOldSegments == 0) {
         continue;
diff --git 
a/pinot-broker/src/test/java/org/apache/pinot/broker/routing/instanceselector/SegmentGroupsBuilderHashStrategyTest.java
 
b/pinot-broker/src/test/java/org/apache/pinot/broker/routing/instanceselector/SegmentGroupsBuilderHashStrategyTest.java
new file mode 100644
index 00000000000..1c1d947b165
--- /dev/null
+++ 
b/pinot-broker/src/test/java/org/apache/pinot/broker/routing/instanceselector/SegmentGroupsBuilderHashStrategyTest.java
@@ -0,0 +1,116 @@
+/**
+ * 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.pinot.broker.routing.instanceselector;
+
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertTrue;
+
+
+public class SegmentGroupsBuilderHashStrategyTest {
+
+  @Test
+  public void testEqualSetsWithDifferentIterationOrders() {
+    Set<String> first = new LinkedHashSet<>(List.of("server1", "server2", 
"server3"));
+    Set<String> reordered = new LinkedHashSet<>(List.of("server3", "server1", 
"server2"));
+
+    assertEquals(first, reordered);
+    assertEquals(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.hashCode(first),
+        SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.hashCode(reordered));
+
+    Map<Set<String>, String> map = new 
Object2ObjectOpenCustomHashMap<>(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY);
+
+    map.put(first, "unavailable");
+    assertEquals(map.get(reordered), "unavailable");
+  }
+
+  @Test
+  public void testReducedCollisionsForDifferentInstanceSets() {
+    Set<String> first = Set.of("a", "d");
+    Set<String> second = Set.of("b", "c");
+
+    assertEquals(first.hashCode(), second.hashCode());
+    assertNotEquals(first, second);
+
+    assertNotEquals(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.hashCode(first),
+        SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.hashCode(second));
+
+    Map<Set<String>, String> map = new 
Object2ObjectOpenCustomHashMap<>(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY);
+
+    map.put(first, "first");
+    map.put(second, "second");
+
+    assertEquals(map.size(), 2);
+    assertEquals(map.get(first), "first");
+    assertEquals(map.get(second), "second");
+  }
+
+  @Test
+  public void testStrategyNullAndSetEqualityContract() {
+    Set<String> first = Set.of("server1", "server2");
+    Set<String> reordered = new LinkedHashSet<>(List.of("server2", "server1"));
+
+    assertEquals(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.hashCode(null), 0);
+    assertTrue(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.equals(null, null));
+    assertFalse(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.equals(first, 
null));
+    assertFalse(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.equals(null, 
first));
+    assertTrue(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.equals(first, 
reordered));
+    assertFalse(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.equals(first, 
Set.of("server1", "server3")));
+  }
+
+  @Test
+  public void testHashDistributionForNumericServerNames() {
+    List<String> servers = new ArrayList<>(200);
+    for (int i = 0; i < 200; i++) {
+      servers.add("Server_pinot-server-" + i + 
".pinot-server-headless.ns.svc.cluster.local_8098");
+    }
+
+    Set<Integer> originalHashes = new HashSet<>();
+    Set<Integer> strategyHashes = new HashSet<>();
+    int groups = 0;
+
+    outer:
+    for (int i = 0; i < servers.size() - 2; i++) {
+      for (int j = i + 1; j < servers.size() - 1; j++) {
+        for (int k = j + 1; k < servers.size(); k++) {
+          Set<String> instances = Set.of(servers.get(i), servers.get(j), 
servers.get(k));
+          originalHashes.add(instances.hashCode());
+          
strategyHashes.add(SegmentGroupsBuilder.INSTANCE_SET_STRATEGY.hashCode(instances));
+          if (++groups == 100_000) {
+            break outer;
+          }
+        }
+      }
+    }
+
+    assertEquals(groups, 100_000);
+    assertTrue(originalHashes.size() < 5_000, "Unexpected legacy collision 
distribution");
+    assertTrue(strategyHashes.size() >= 99_000, "Insufficient hash diversity: 
" + strategyHashes.size());
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to