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


##########
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:
   Thanks @ethbak, good catch. Some context on why I originally didn't cancel: 
I designed this against the executors that Lucene's own merge schedulers 
provide, and those never queue. `ConcurrentMergeScheduler`'s `CachedExecutor` 
either hands a command to a thread through a `SynchronousQueue` or runs it on 
the caller, and the default `MergeScheduler` always runs it on the caller. So 
`tryInvoke` either declined or returned a future whose task already had a 
thread of its own. Waiting on it could never mean waiting for a thread to free 
up.
   
   You're right that the codec's `mergeExec` breaks that assumption, as can a 
custom `getIntraMergeExecutor`. `mergeExec` also takes precedence over the 
intra-merge executor, so a busy fixed-size pool there queues every worker. On 
`main`, `invokeAll` copes by having the caller run any task that has not 
started, so without cancelling, this PR would have regressed that configuration.
   
   The revision I just pushed cancels the way you suggest: `tryInvoke` now 
returns a `TaskExecutor.Task`, and once the calling thread runs out of batches, 
it cancels every recruited future before collecting. Unstarted ones complete 
immediately, so it only waits for workers that another thread actually started. 
`testConcurrentMergeBuilderDoesNotWaitForQueuedWorkers` covers this with a 
one-thread `mergeExec` whose thread is blocked.
   
   Separately, `mergeExec` sits outside `ConcurrentMergeScheduler`'s thread 
accounting, which makes me wonder whether it still earns its place next to the 
intra-merge executor. That seems worth its own issue rather than this PR.
   
   Since #16794, this loop is a shared `runInBatches` that every phase uses, 
your repair levels included, so I'd appreciate a look at 
`repairDisconnectedNodes`. Each level still finishes before the next one 
starts, and the old task cap, `min(workers, ceilDiv(total, 
REPAIR_BATCH_SIZE))`, becomes "never recruit more workers than there are 
batches left".
   



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