Class BatchSortedExchangeSource

java.lang.Object
org.elasticsearch.compute.operator.exchange.BatchSortedExchangeSource
All Implemented Interfaces:
Closeable, AutoCloseable, ExchangeSource

public class BatchSortedExchangeSource extends Object implements ExchangeSource, Closeable
Wraps an ExchangeSource and reorders BatchPages within each batch to ensure they are output in sequential order based on pageIndexInBatch.

Pages may arrive out of order (e.g., due to network ordering), but consumers expect pages within a batch to arrive in order (pageIndexInBatch = 0, 1, 2, ...). This class buffers out-of-order pages and releases them in the correct sequence.

Implements ExchangeSource so it can be used polymorphically wherever an ExchangeSource is expected.

  • Constructor Details

    • BatchSortedExchangeSource

      public BatchSortedExchangeSource(ExchangeSource delegate)
  • Method Details

    • pollPage

      public Page pollPage()
      Poll the next page in sorted order.

      If there are pages ready in the output queue, returns one immediately. Otherwise, polls from the delegate and sorts incoming pages.

      Specified by:
      pollPage in interface ExchangeSource
      Returns:
      the next page in order, or null if no pages are available
    • waitForReading

      public IsBlockedResult waitForReading()
      Returns an IsBlockedResult that resolves when a page is available.

      This method eagerly polls and sorts pages from the delegate until either:

      • An in-order page is available in the output queue
      • The delegate is blocked (returns the delegate's future)
      • The delegate is finished
      This prevents busy-spinning in the driver when pages arrive out of order.
      Specified by:
      waitForReading in interface ExchangeSource
      Returns:
      NOT_BLOCKED if a page is available or delegate is finished, otherwise a blocked result
    • waitUntilReady

      public IsBlockedResult waitUntilReady()
      Blocks until a page is ready in outputQueue OR delegate is finished. Unlike waitForReading(), this guarantees hasReadyPages() == true when returning NOT_BLOCKED (unless delegate is finished with no more pages).

      This prevents busy-spinning when pages arrive out of order.

      Returns:
      NOT_BLOCKED if a page is ready or delegate is finished, otherwise a blocked result
    • hasReadyPages

      public boolean hasReadyPages()
      Returns true if there are pages ready to be output (in correct order). This only counts pages in the output queue, not out-of-order pages buffered internally or pages in the delegate's buffer.
    • isFinished

      public boolean isFinished()
      Returns true if all pages have been consumed.
      Specified by:
      isFinished in interface ExchangeSource
    • bufferSize

      public int bufferSize()
      Returns the number of pages waiting in the output queue.
      Specified by:
      bufferSize in interface ExchangeSource
    • finish

      public void finish()
      Finish the underlying source.
      Specified by:
      finish in interface ExchangeSource
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • toString

      public String toString()
      Overrides:
      toString in class Object