Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ default AsyncCursorHolder makeCursorHolderAsync(CursorBuildSpec spec)
);
}

/**
* Optional preloading for a given {@link CursorBuildSpec}, which starts doing the loading that a later
* {@link #makeCursorHolderAsync(CursorBuildSpec)} would otherwise do on demand. See {@link CursorPrefetch} for the
* lifecycle of the returned handle, in particular that it must stay open for the warmed-up parts to stay resident.
* <p>
* A warm-up that cannot be started, for instance because the segment cache has no room for what it would load, is
* reported through the returned handle rather than thrown. This method throws only for a spec that the factory
* cannot serve at all (which {@link #makeCursorHolderAsync(CursorBuildSpec)} would reject the same way).
*/
default CursorPrefetch prefetch(CursorBuildSpec spec)
{
return CursorPrefetch.none();
}

/**
* Returns the {@link RowSignature} of the data available from this cursor factory. For mutable segments, even though
* the signature may evolve over time, any particular object returned by this method is an immutable snapshot.
Expand Down
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>
Comment on lines +35 to +38

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)

* <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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,19 @@ public FilteredCursorFactory(CursorFactory delegate, @Nullable DimFilter filter)
@Override
public CursorHolder makeCursorHolder(CursorBuildSpec spec)
{
if (filter == null) {
return delegate.makeCursorHolder(spec);
} else {
return delegate.makeCursorHolder(specWithFilter(spec));
}
return delegate.makeCursorHolder(delegateSpec(spec));
}

@Override
public AsyncCursorHolder makeCursorHolderAsync(CursorBuildSpec spec)
{
if (filter == null) {
return delegate.makeCursorHolderAsync(spec);
} else {
return delegate.makeCursorHolderAsync(specWithFilter(spec));
}
return delegate.makeCursorHolderAsync(delegateSpec(spec));
}

@Override
public CursorPrefetch prefetch(CursorBuildSpec spec)
{
return delegate.prefetch(delegateSpec(spec));
}

@Override
Expand All @@ -71,8 +69,11 @@ public ColumnCapabilities getColumnCapabilities(String column)
return delegate.getColumnCapabilities(column);
}

private CursorBuildSpec specWithFilter(final CursorBuildSpec spec)
private CursorBuildSpec delegateSpec(final CursorBuildSpec spec)
{
if (filter == null) {
return spec;
}
return CursorBuildSpec.builder(spec).andBaseFilter(Filters.toFilter(filter)).build();
}
}
Loading
Loading