leventov commented on a change in pull request #7174: Change reservoir sampling 
of segments to spliterator approach
URL: https://github.com/apache/incubator-druid/pull/7174#discussion_r263592712
 
 

 ##########
 File path: 
server/src/main/java/org/apache/druid/server/coordinator/ReservoirSegmentSampler.java
 ##########
 @@ -19,36 +19,93 @@
 
 package org.apache.druid.server.coordinator;
 
+import org.apache.druid.client.ImmutableDruidServer;
 import org.apache.druid.timeline.DataSegment;
 
+import java.util.Collection;
 import java.util.List;
+import java.util.Spliterator;
 import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
 
 final class ReservoirSegmentSampler
 {
+  private static final int SPLITERATOR_SIZE_THRESHOLD = 25;
 
   static BalancerSegmentHolder getRandomBalancerSegmentHolder(final 
List<ServerHolder> serverHolders)
   {
-    ServerHolder fromServerHolder = null;
-    DataSegment proposalSegment = null;
-    int numSoFar = 0;
-
-    for (ServerHolder server : serverHolders) {
-      for (DataSegment segment : server.getServer().getSegments()) {
-        int randNum = ThreadLocalRandom.current().nextInt(numSoFar + 1);
-        // w.p. 1 / (numSoFar+1), swap out the server and segment
-        if (randNum == numSoFar) {
-          fromServerHolder = server;
-          proposalSegment = segment;
+    ServerHolder fromServerHolder = reservoirSampleServer(serverHolders);
+    if (fromServerHolder == null) {
+      return null;
+    }
+
+    DataSegment proposalSegment;
+    Collection<DataSegment> segments = 
fromServerHolder.getServer().getSegments();
 
 Review comment:
   getSegments() returns a lazy collection (see `ImmutableDruidServer` code) 
that is not really suitable for spliteration. A random choice in 
`ImmutableDruidServer` is somewhat complicated by the fact that data is spread 
there across data sources, so you may need to do reservoir choice of a data 
source first, in the same way as you do reservoir choice of a server.
   
   So there need to be two abstract, independently testable utilities:
    - `<T> T reservoirChooseSized(Collection<T> elements, ToIntFunction<T> 
getSize)`
    - `<K, V> Map.Entry<K, V> chooseRandomEntry(HashMap<K, V> m)`
   
   But even if you do this, you cannot choose from an `ImmutableMap` in 
`ImmutableDruidDataSource` efficiently, because on the old Guava version that 
we use (16.0.1) `ImmutableMap` doesn't support efficient splitting.
   
   It means that if you want to persist to bring this whole idea of optimized 
picking to live, you need to change the algorithm all the way from 
`DruidCoordinatorBalancer` and `BalancerStrategy.pickSegmentToMove()` and 
provide it with a list of `DruidServer` objects (borrowed from 
`DruidCoordinator` object) rather than `ImmutableDruidServer`, and eventually 
pick from `DruidDataSource.idToSegmentMap` (a `ConcurrentHashMap`).
   
   This may be a daunting task. On the other hand, it may be drawn irrelevent 
by the refactoring of `DruidCoordinatorBalancer` that I plan for #7159. 
Therefore I suggest to put this effort on hold until after #7159 is fixed. Then 
it will be clearer if the problem is still relevant.

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


With regards,
Apache Git Services

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

Reply via email to