Skip to content

feat: add async cursor prefetch mechanism - #20897

Merged
clintropolis merged 3 commits into
apache:masterfrom
clintropolis:cursor-factory-prefetch
Dec 1, 2026
Merged

clintropolis merged 3 commits into
apache:masterfrom
clintropolis:cursor-factory-prefetch

Conversation

@clintropolis

@clintropolis clintropolis commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Description

This PR adds CursorFactory#prefetch which starts the loading that a later makeCursorHolderAsync(spec) would otherwise do on demand for partial loading V10 segments. This is a building block for load-ahead prefetch in MSQ leaf stages (which will come in a follow-up).

Comment on lines +1218 to +1226
@SuppressWarnings("unused")
ListenableFuture<?> unused = gatedExec.submit(() -> {
try {
gate.await();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues found in this review. The prefetch lifecycle and shared async cursor machinery preserve bundle ownership across success, failure, and cancellation, while wrapper factories pass the same transformed specifications used by async cursor construction.

Reviewed 18 of 18 changed files.


This is an automated review by Codex GPT-5.6-Luna(max)

@FrankChen021 FrankChen021 added the Approval Recommended Used when AI completes a review and reports no findings label Nov 13, 2026
@capistrant
capistrant self-requested a review November 16, 2026 17:39

@capistrant capistrant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly have suggested some minor javadoc tweaks (non-blocking opinions) plus some general questions about the contract of CursorPrefetch. Biggest potentially blocking concern has an inline comment in the partial cursor factory prefetch method on handling of some synchronous failures and whether it breaks the stated prefetch contract. Overall tho, seems cool

Comment on lines +74 to +77
/**
* The spec passed to the delegate: the caller's spec with {@link #policy} applied. Used by every delegating method
* here.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit suggestion

Suggested change
/**
* The spec passed to the delegate: the caller's spec with {@link #policy} applied. Used by every delegating method
* here.
*/
/**
* The caller's spec with {@link #policy} applied.
*/

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this wasn't adding much so just removed

Comment on lines +120 to +124
/**
* The spec passed to the base cursor factory: the caller's spec rewritten for the unnest input column, with only
* the base-table half of its filters (see {@link #computeFilterSplit}). Shared by every holder path, which also need
* {@code filterSplit} for the post-unnest half.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit suggestion

Suggested change
/**
* The spec passed to the base cursor factory: the caller's spec rewritten for the unnest input column, with only
* the base-table half of its filters (see {@link #computeFilterSplit}). Shared by every holder path, which also need
* {@code filterSplit} for the post-unnest half.
*/
/**
* The spec passed to the base cursor factory: the caller's spec rewritten for the unnest input column, with only
* the base-table half of its filters (see {@link #computeFilterSplit}).
*/

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just removed this since this one is also just delegating to another method

Comment on lines +112 to +114
/**
* The spec passed to the base cursor factory for {@code spec}, computing the filter split itself.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd remove this as it feels self explanatory if the reader just looks at the called delegateSpec

Comment on lines +123 to +125
/**
* The spec passed to the base cursor factory for {@code spec}; see {@link #planCursor}.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd consider dropping this as unneeded

Comment on lines +82 to +84
* <b>Prefetch.</b> {@link #prefetch} shares the async path's machinery but, instead of a cursor holder, produces a
* {@link CursorPrefetch} that owns the bundle holds. A later {@link #makeCursorHolderAsync} for the same spec (made
* while the prefetch is still open) plans no downloads as everything is resident.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the intended contract around starting a cursor holder build when a prefetch is not done, if any? If such a thing does happen, wouldn't the holder build plan downloads for whatever is not pre-fetched yet? I suppose it wouldn't actually be downloaded because it would realize it is resident thanks to prefetch before kicking off its own download.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefetch isn't required, the callers that use partial loads still must use CursorFactory#makeCursorHolderAsync so any part not present will just load on demand as part of that contract. Prefetch just makes all of the things resident in advance so that the later async step can be immediate.

if (plan.bundles().isEmpty()) {
return CursorPrefetch.none();
}
final PendingDownloads pending = acquireHoldsAndSubmitRuns(plan.bundles());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can throw synchronously, correct? Does it doing so violate the best effort contract of CursorPrefetch that states:

<li>The handle is ready ({@link #isReady()}) once the warm-up is complete, whether it succeeded or failed;
 *   {@link #get()} returns {@link Stats} on success and throws the failure otherwise.</li>

Might we want to try/catch here and create a failed prefetch?

The planCurstorPrefetch above also could fail to plan. that seems potentially different in that it may be un-recoverable at cursor build time if it can't even plan now. so maybe it throwing eagerly is ok as long as it is handled nicely?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't really matter much with how the branch i had on top of this was wiring it up, but i went ahead and changed it to be more consistent

Comment on lines +35 to +38
* <li>Keep the handle open for as long as the warmed-up parts should stay resident. Closing it after it is ready
* releases the holds that pin them. The cursor build that benefits from the warm-up takes its own pins on the
* calling thread, so close the handle only once that {@code makeCursorHolderAsync} call has returned, or the parts
* may be evicted in between and loaded twice.</li>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are callers going to be fully responsible for responsible use of Prefetch handles, then? No real objection here, other than it just feels like an area to keep close eye on when creating followups that use this prefetch mechanism

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, it is optional, but callers using it will need to close the handles to release the holds (since we do holds on the prefetch so that other prefetch don't evict what we just loaded if space is tight, the backoff from #20904 is also important to make stuff chill once the things in this patch are wired up)

Assertions.assertTrue(base.prefetchCanceled.get(), "closing the prefetch before it's ready cancels the base");
}
catch (Throwable t) {
prefetch.close();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if the final assertion fails this will clobber the assertion fail reason with an Already Closed exception

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Fix failed-prefetch cleanup before merging. Failed range loads and partial setup failures can leave already-submitted deep-storage tasks running after the cursor or prefetch has failed, consuming loader capacity unnecessarily.

Reviewed 18 of 18 changed files; findings in 1 file.

Validation: git diff --check c7913041e5de0d7f9ea4b4ed19eaa127d75cfda1..5283291895807f6bc336c48320533b3683075347 passed.

Severity Findings
P0 0
P1 0
P2 2
P3 0
Total 2

This is an automated review by Codex GPT-5.6-Luna(max)

After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.

private static void failHolder(List<BundleHoldRelease> holdReleases, AsyncCursorHolder asyncHolder, Throwable t)
private static void fail(List<BundleHoldRelease> holdReleases, SettableAsyncResource<?> target, Throwable t)
{
holdReleases.forEach(BundleHoldRelease::requestRelease);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Failed range load leaves sibling downloads running

Finding: When one range download fails, AsyncResources.collect marks the aggregate ready with the error but intentionally leaves sibling resources open. This failure path only requests hold release and completes the target; once the target is ready, its canceler is cleared, so closing the failed CursorPrefetch or AsyncCursorHolder cannot cancel sibling tasks. Already-running downloads can continue consuming deep-storage and loader capacity after the operation has failed.

Suggestion: Close or cancel the collected download resource in the shared failure path, and add a multi-run failure test that verifies sibling tasks are canceled.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't really new in this PR though moving the code around probably flagged it, but I fixed it anyway

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at the current head: fail(PendingDownloads, ...) now calls PendingDownloads.unwind() before completing the target, and unwind() closes the collected download resource so sibling AsyncResources are canceled before the failed handle loses its canceler. The added multi-run failure coverage also checks sibling cancellation and exactly-once hold release. This follow-up is resolved.

// submitDownload shutdown rejection). A run download submitted before the failure may already be in flight, so
// release every bundle hold through the handshake (requestRelease defers to the last in-flight body) rather
// than dropping it directly mid-mapFile().
throw CloseableUtils.closeAndWrapInCatch(t, () -> holdReleases.forEach(BundleHoldRelease::requestRelease));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Partial setup failure leaves submitted tasks uncanceled

Finding: If a later bundle acquisition or submitDownload call fails after earlier range tasks were submitted, the runDownloads list is scoped to the try block and discarded. The catch requests hold release only, while prefetch returns a ready failed handle with no canceler and the async cursor path throws, leaving no owner that can close the previously returned task resources.

Suggestion: Retain submitted run resources in the setup failure path and close them before releasing the acquired holds; cover partial-submission rejection in a test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not new, but also fixed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at the current head: runDownloads remains available in the setup catch path, and every submitted resource is closed before the acquired bundle holds are released. The added partial-submission rejection coverage checks both prefetch and async cursor setup. This follow-up is resolved.

@FrankChen021 FrankChen021 removed the Approval Recommended Used when AI completes a review and reports no findings label Nov 19, 2026

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues remain at the reviewed head. Both prior P2s are resolved: failed downloads now cancel siblings, and partial setup failures cancel submitted tasks before releasing holds.

Reviewed 18 of 18 changed files. Current-head review is sufficient for normal recommendation rules; follow-up focus was the incremental cleanup change and its tests, reconciled with the full diff and surrounding async-resource lifecycle.


This is an automated review by Codex GPT-5.6-Luna(max)

@FrankChen021 FrankChen021 added the Approval Recommended Used when AI completes a review and reports no findings label Nov 20, 2026
@clintropolis
clintropolis merged commit 348775b into apache:master Dec 1, 2026
28 checks passed
@clintropolis
clintropolis deleted the cursor-factory-prefetch branch December 1, 2026 01:37
@github-actions github-actions Bot added this to the 40.0.0 milestone Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approval Recommended Used when AI completes a review and reports no findings Area - Segment Format and Ser/De

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants