ethbak commented on code in PR #16778:
URL: https://github.com/apache/lucene/pull/16778#discussion_r4670971730


##########
lucene/core/src/java/org/apache/lucene/util/hnsw/HnswConcurrentMergeBuilder.java:
##########
@@ -241,15 +242,45 @@ public OnHeapHnswGraph build(int maxOrd) throws 
IOException {
     if (plans.length > 0) {
       joinGraphs(plans, maxOrd, cumulativeWorkTimeNs);
     }
-    List<Callable<Void>> futures = new ArrayList<>();
-    for (int i = 0; i < workers.length; i++) {
+
+    // Reserve first worker for calling thread: only create Futures for 
remaining workers
+    List<Callable<Void>> tasks = new ArrayList<>(workers.length - 1);
+    for (int i = 1; i < workers.length; i++) {
       int finalI = i;
-      futures.add(() -> {
+      tasks.add(() -> {
         workers[finalI].run(maxOrd);
         return null;
       });
     }
-    taskExecutor.invokeAll(futures);
+
+    final List<Future<Void>> started = new ArrayList<>(workers.length);
+    try {
+      int start = workers[0].getStartPos(maxOrd);
+      int end;
+      while (start != -1) {
+        // Use this thread for the first worker
+        end = start + workers[0].batchSize;
+        if (end >= maxOrd) {
+          end = maxOrd; // No need to recruit more workers if this is the last 
batch
+        } else {
+          // Recruit other threads for workers that haven't started yet
+          while (started.size() < tasks.size()) {
+            var maybeFuture = 
taskExecutor.tryInvoke(tasks.get(started.size()));
+            if (maybeFuture.isEmpty()) {
+              break; // No room for more workers; break to process batch on 
this thread
+            }
+
+            started.add(maybeFuture.get());
+          }
+        }
+
+        workers[0].addVectors(start, end);
+        start = workers[0].getStartPos(maxOrd);
+      }
+    } finally {
+      TaskExecutor.collectResults(started);

Review Comment:
   `repairDisconnectedNodes` still looks good to me overall.
   
   Maybe some of your new tests 
(`testMergeRecruitsThreadThatSiblingMergeFrees`) could use a graph with deletes 
so it also checks the repair path? 
   
   Also I think our two log lines about number of workers could contradict 
confusingly. `repaired reused graph: ... with %d workers` counts with 
`workers.length` (how many workers exist) and `phase repair level N: ... %d/%d 
workers ran batches` prints `workersThatRan()`, which is how many actually ran. 



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


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

Reply via email to