FrankChen021 commented on a change in pull request #11257:
URL: https://github.com/apache/druid/pull/11257#discussion_r649194445



##########
File path: 
server/src/main/java/org/apache/druid/server/coordinator/ReservoirSegmentSampler.java
##########
@@ -31,6 +32,42 @@
 
   private static final EmittingLogger log = new 
EmittingLogger(ReservoirSegmentSampler.class);
 
+  static List<BalancerSegmentHolder> getRandomBalancerSegmentHolders(
+      final List<ServerHolder> serverHolders,
+      Set<String> broadcastDatasources,
+      int k
+  )
+  {
+    List<BalancerSegmentHolder> holders = new ArrayList<>(k);
+    int numSoFar = 0;
+
+    for (ServerHolder server : serverHolders) {
+      if (!server.getServer().getType().isSegmentReplicationTarget()) {
+        // if the server only handles broadcast segments (which don't need to 
be rebalanced), we have nothing to do
+        continue;
+      }
+
+      for (DataSegment segment : server.getServer().iterateAllSegments()) {
+        if (broadcastDatasources.contains(segment.getDataSource())) {
+          // we don't need to rebalance segments that were assigned via 
broadcast rules
+          continue;
+        }
+
+        if (numSoFar < k) {
+          holders.add(new BalancerSegmentHolder(server.getServer(), segment));
+          numSoFar++;
+          continue;
+        }
+        int randNum = ThreadLocalRandom.current().nextInt(numSoFar + 1);
+        if (randNum < k) {
+          holders.set(randNum, new BalancerSegmentHolder(server.getServer(), 
segment));
+        }

Review comment:
       Random picking here is a standard implementation of a reservoir sampling 
algorithm. It makes all segments have the same probability to be selected. The 
original code also uses this trick, so no need to worry :-)




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

For queries about this service, please contact Infrastructure at:
[email protected]



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

Reply via email to