Repository navigation
feat: add async cursor prefetch mechanism #20897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
clintropolis
merged 3 commits into
apache:master
from
clintropolis:cursor-factory-prefetch
Dec 1, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
processing/src/main/java/org/apache/druid/segment/CursorPrefetch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.segment; | ||
|
|
||
| import org.apache.druid.common.asyncresource.SettableAsyncResource; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Handle on a best-effort warm-up started by {@link CursorFactory#prefetch(CursorBuildSpec)}, which does actions such | ||
| * as downloading column files and deserializing column holders. | ||
| * | ||
| * <h3>Lifecycle</h3> | ||
| * <ul> | ||
| * <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> | ||
| * <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> | ||
| * <li>Closing the handle before it is ready cancels the warm-up, pending downloads are canceled and the pins | ||
| * released, and {@link #get()} then throws | ||
| * {@link org.apache.druid.common.asyncresource.AsyncResourceCanceledException}.</li> | ||
| * <li>Close is not idempotent; close exactly once.</li> | ||
| * </ul> | ||
| */ | ||
| public class CursorPrefetch extends SettableAsyncResource<CursorPrefetch.Stats> | ||
| { | ||
| /** | ||
| * Stats about the prefetch plan. | ||
| */ | ||
| public record Stats(List<String> bundles, int plannedRuns, long plannedBytes) | ||
| { | ||
| public static final Stats NONE = new Stats(List.of(), 0, 0L); | ||
| } | ||
|
|
||
| /** | ||
| * @param canceler optional callback invoked from {@link #close()} when the handle is closed before the warm-up has | ||
| * completed for producers that support cancellation. | ||
| */ | ||
| public CursorPrefetch(@Nullable Runnable canceler) | ||
| { | ||
| super(false); | ||
| if (canceler != null) { | ||
| setCanceler(canceler); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An already-complete prefetch that did nothing and pins nothing, for sources that are always fully resident. | ||
| */ | ||
| public static CursorPrefetch none() | ||
| { | ||
| final CursorPrefetch prefetch = new CursorPrefetch(null); | ||
| prefetch.set(Stats.NONE, null); | ||
| return prefetch; | ||
| } | ||
|
|
||
| /** | ||
| * An already-complete prefetch whose warm-up could not be started, so it pins nothing; {@link #get()} throws | ||
| * {@code failure}. | ||
| */ | ||
| public static CursorPrefetch failed(Throwable failure) | ||
| { | ||
| final CursorPrefetch prefetch = new CursorPrefetch(null); | ||
| prefetch.setException(failure); | ||
| return prefetch; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)