Class ExternalSourceDrainUtils

java.lang.Object
org.elasticsearch.xpack.esql.datasources.ExternalSourceDrainUtils

public final class ExternalSourceDrainUtils extends Object
Utility for draining pages from a CloseableIterator into an AsyncExternalSourceBuffer with non-blocking backpressure.

Runs synchronously while the buffer has space (hot path), yields the thread when the buffer is full, and resumes via the provided Executor when space is freed (cold path). No timeout is needed — cancellation propagates via AsyncExternalSourceBuffer.finish(boolean) setting noMoreInputs, which causes AsyncExternalSourceBuffer.waitForSpace() to return an already-completed listener so the drain loop exits promptly.

  • Method Details

    • drainPagesAsync

      public static void drainPagesAsync(CloseableIterator<Page> pages, AsyncExternalSourceBuffer buffer, Executor executor, ActionListener<Void> listener)
      Drains pages from iterator into buffer asynchronously. Runs synchronously while the buffer has space; yields the thread when the buffer is full and resumes via executor when space is freed. Completion (success or failure) is reported via the listener.

      Iterator ownership: This method does NOT close the iterator. The caller must close it regardless of outcome (e.g. via ActionListener.runAfter(org.elasticsearch.action.ActionListener<Response>, java.lang.Runnable)).

      Executor contract: The executor must be a real thread-pool executor (e.g. generic), never DIRECT_EXECUTOR_SERVICE. Continuations resume on this executor to avoid running producer I/O on the Driver thread. The executor captures and restores thread context at submission time, so no explicit context-preserving wrapper is needed.

      Cancellation: No timeout. Cancellation comes from buffer.finish(true) setting noMoreInputs, which causes waitForSpace() to return an already-completed listener.