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
22 changes: 22 additions & 0 deletions fe/fe-connector/fe-connector-lance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ under the License.
<artifactId>commons-lang3</artifactId>
</dependency>

<!-- The table-access cache (LanceNamespaceClient) expires each entry after its own time, which
Caffeine's Expiry expresses. The plugin carries its own copy, as the other connector plugins do,
rather than whichever version the FE classpath happens to hold; pinned to 2.9.3, the version they
bundle. -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
<exclusions>
<!-- Annotation-only jars. Guava, which lance-core brings, needs newer ones, and these older
ones would win by being nearer in the dependency tree. -->
<exclusion>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
/**
* Everything a user writes in {@code CREATE CATALOG} for a Lance catalog, bound and checked.
*
* <p>The keys and their rules are the ones Lance catalogs have had since 4.1, unchanged: images written by
* those versions persist the raw property map, and this class must accept it as it is. Settings that are
* <p>The keys and their rules are the ones Lance catalogs have had since 4.1 (the table-access cache TTL since
* 4.2), unchanged: images written by those versions persist the raw property map, and this class must accept it
* as it is. Settings that are
* one-per-FE rather than one-per-catalog live in the plugin's {@code lance.conf} ({@link LanceConf}).
*
* <p>{@link #of(Map)} touches neither storage nor any {@code org.lance.*} type: it runs at CREATE, again on
Expand All @@ -62,6 +63,12 @@ public final class LanceCatalogProperties {
public static final String ROOT_DATABASE = "lance.namespace.root_database";
public static final String DEFAULT_DELIMITER = "$";
public static final String DEFAULT_ROOT_DATABASE = "default";
/**
* How long a table's dataset URI and storage options, as the namespace describes them, are reused before the
* table is described again; 0 describes it on every read. See {@link LanceNamespaceClient}.
*/
public static final String TABLE_ACCESS_CACHE_TTL_SECONDS = "lance.table_access_cache_ttl_seconds";
public static final int DEFAULT_TABLE_ACCESS_CACHE_TTL_SECONDS = 60;

/** Every REST-only key starts with this; a filesystem catalog rejects all of them. */
public static final String REST_PREFIX = "lance.rest.";
Expand Down Expand Up @@ -116,6 +123,11 @@ public final class LanceCatalogProperties {
description = "The Doris database name representing the catalog's root namespace. Default: default.")
private String rootDatabase = DEFAULT_ROOT_DATABASE;

@ConnectorProperty(names = {TABLE_ACCESS_CACHE_TTL_SECONDS}, required = false,
description = "Maximum lifetime of cached table URIs and access options in seconds. "
+ "Default: 60. Set to 0 to describe the table on every read.")
private int tableAccessCacheTtlSeconds = DEFAULT_TABLE_ACCESS_CACHE_TTL_SECONDS;

@ConnectorProperty(names = {REST_URI}, required = false,
description = "The HTTP or HTTPS endpoint of the Lance REST namespace service.")
private String restUri;
Expand Down Expand Up @@ -171,6 +183,10 @@ public static LanceCatalogProperties of(Map<String, String> properties) {
}

private void validateCommonProperties() {
if (tableAccessCacheTtlSeconds < 0) {
throw new IllegalArgumentException("Property '" + TABLE_ACCESS_CACHE_TTL_SECONDS
+ "' must be non-negative");
}
if (namespaceDelimiter.isEmpty() || namespaceDelimiter.indexOf('\\') >= 0) {
throw new IllegalArgumentException("Property '" + NAMESPACE_DELIMITER
+ "' cannot be empty or contain the escape character '\\'");
Expand Down Expand Up @@ -398,6 +414,11 @@ public List<String> getParentNamespace() {
return parentNamespace;
}

/** See {@link #TABLE_ACCESS_CACHE_TTL_SECONDS}; never negative. */
public int getTableAccessCacheTtlSeconds() {
return tableAccessCacheTtlSeconds;
}

/**
* The catalog's properties as written, unmodifiable. It contains credentials: pass it only to code
* that keeps it internal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public final class LanceConf {

/**
* Java Arrow memory, in bytes, that one catalog may use while it reads dataset metadata. Must be
* positive.
* positive. It bounds each generation of the catalog's resources ({@link LanceCatalogClient}); after a
* REFRESH CATALOG, each older generation still serving a read keeps its own limit in use until its last read
* ends. The Lance Sessions' caches are native memory and not counted.
*/
public static final String CONF_ARROW_MEMORY_LIMIT_BYTES = "lance_catalog_arrow_memory_limit_bytes";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ public final class LanceConnector implements Connector {
private final LanceCatalogClient client;

public LanceConnector(Map<String, String> properties, ConnectorContext context) {
this.props = LanceCatalogProperties.of(properties);
this(LanceCatalogProperties.of(properties), context);
}

private LanceConnector(LanceCatalogProperties props, ConnectorContext context) {
this(props, context, new LanceCatalogClient(props,
() -> context.getStorageContext().getStorageProperties(),
() -> LanceConf.arrowMemoryLimitBytes(context)));
}

/** Over a catalog client built by the caller, which tests give recording resources. */
LanceConnector(LanceCatalogProperties props, ConnectorContext context, LanceCatalogClient client) {
this.props = props;
this.context = context;
this.client = new LanceCatalogClient(props, () -> context.getStorageContext().getStorageProperties(),
() -> LanceConf.arrowMemoryLimitBytes(context));
this.client = client;
}

@Override
Expand Down Expand Up @@ -106,6 +116,34 @@ public ConnectorTestResult testConnection(ConnectorSession session) {
}
}

/**
* REFRESH CATALOG: the next call opens a new namespace and a cold Lance Session, which reads afresh a
* dataset replaced at the same URI. The engine also lands here on replay when it cannot name the database.
*/
@Override
public void invalidateAll() {
client.refresh();
}

/**
* REFRESH DATABASE: every table is described again on its next read, and the Session keeps its cached
* metadata. That is safe while a dataset lives: a committed version never changes, and a read of the newest
* version still finds the newest one. A dataset deleted and written again at the same URI starts its
* versions over, though, and for one with V1 manifest names Lance keys a version's index list by the version
* alone, so the Session can serve the deleted dataset's indexes for the new one: that case needs REFRESH
* CATALOG ({@link #invalidateAll}).
*/
@Override
public void invalidateDb(String dbName) {
client.invalidateTableAccess();
}

/** REFRESH TABLE: as {@link #invalidateDb}, for every table, since a described table access is cheap. */
@Override
public void invalidateTable(String dbName, String tableName) {
client.invalidateTableAccess();
}

@Override
public void close() {
client.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,12 @@ public ConnectorTableHandle applySnapshot(ConnectorSession session, ConnectorTab
// ========= internals =========

private LanceTableMetadata metadata(ConnectorSession session, LanceTableHandle handle) {
// Looked up before the memo's loader runs: a loader may not use the statement scope itself.
LanceMetadataMetrics metrics = LanceMetadataMetrics.forTable(session, handle.getDbName(),
handle.getTableName());
return LanceStatementScope.sharedMetadata(session, handle,
() -> client.loadTableMetadata(handle.getDbName(), handle.getTableName(), handle.getVersion()));
() -> client.loadTableMetadata(handle.getDbName(), handle.getTableName(), handle.getVersion(),
metrics));
}

private LanceTableMetadata metadataAtOrBefore(ConnectorSession session, LanceTableHandle handle,
Expand All @@ -232,7 +236,8 @@ private LanceTableMetadata metadataAtOrBefore(ConnectorSession session, LanceTab
+ "', expected " + LanceSnapshotResolver.TIMESTAMP_FORMATS);
}
LanceTableMetadata metadata = client.loadTableMetadataAtOrBefore(handle.getDbName(),
handle.getTableName(), timestamp.getAsLong(), requestedText);
handle.getTableName(), timestamp.getAsLong(), requestedText,
LanceMetadataMetrics.forTable(session, handle.getDbName(), handle.getTableName()));
// Remembered under the version it resolved to, which is what the pinned reads that follow ask for.
return LanceStatementScope.sharedMetadata(session, handle.withVersion(metadata.getVersion()),
() -> metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.doris.connector.lance;

import org.apache.doris.connector.lance.LanceMetadataMetrics.Stage;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lance.Dataset;
Expand All @@ -32,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.function.Supplier;

/**
* The top-level field ids and the user indexes of one fixed dataset version.
Expand Down Expand Up @@ -63,11 +66,23 @@ static LanceIndexMetadata withUnavailableFieldIds(List<LanceIndexSegmentInfo> in
return new LanceIndexMetadata(false, Collections.emptyMap(), indexSegments);
}

/** Reads the field ids and the index segments of the caller's open snapshot. */
static LanceIndexMetadata read(Dataset dataset) {
Map<String, Integer> lanceFieldIds = loadTopLevelFieldIds(dataset);
/** Reads the field ids and the index segments of the caller's open snapshot, timing each into {@code timer}. */
static LanceIndexMetadata read(Dataset dataset, LanceMetadataMetrics.Timer timer) {
return read(dataset::getLanceSchema, () -> LanceDatasetIndexDiscovery.describeUserIndexes(dataset),
dataset.version(), timer);
}

/**
* {@link #read(Dataset, LanceMetadataMetrics.Timer)} over the two SDK reads it makes: the Lance schema, and the
* validated user indexes.
*/
static LanceIndexMetadata read(Supplier<LanceSchema> lanceSchema, Supplier<List<IndexDescription>> userIndexes,
long version, LanceMetadataMetrics.Timer timer) {
Map<String, Integer> lanceFieldIds = timer.measure(Stage.FIELD_IDS,
() -> loadTopLevelFieldIds(lanceSchema, version));
// Keep index validation even when the known schema conversion error disables field-ID mapping.
List<LanceIndexSegmentInfo> indexSegments = loadIndexSegments(dataset);
List<LanceIndexSegmentInfo> indexSegments = timer.measure(Stage.INDEXES,
() -> loadIndexSegments(userIndexes.get()));
return lanceFieldIds == null
? withUnavailableFieldIds(indexSegments)
: new LanceIndexMetadata(lanceFieldIds, indexSegments);
Expand All @@ -87,10 +102,10 @@ OptionalInt getLanceFieldId(String fieldName) {
return fieldId == null ? OptionalInt.empty() : OptionalInt.of(fieldId);
}

private static Map<String, Integer> loadTopLevelFieldIds(Dataset dataset) {
private static Map<String, Integer> loadTopLevelFieldIds(Supplier<LanceSchema> lanceSchema, long version) {
LanceSchema schema;
try {
schema = dataset.getLanceSchema();
schema = lanceSchema.get();
} catch (IllegalArgumentException e) {
if (!"ArrowSchema conversion error".equals(e.getMessage())) {
throw e;
Expand All @@ -101,7 +116,7 @@ private static Map<String, Integer> loadTopLevelFieldIds(Dataset dataset) {
// the SDK call: invalid ids and duplicate names below must remain errors.
LOG.warn("Lance SDK schema conversion failed at dataset version {}; "
+ "disabling FE scalar index segment planning for this snapshot: {}",
dataset.version(), e.getMessage());
version, e.getMessage());
return null;
}
Map<String, Integer> result = new LinkedHashMap<>();
Expand All @@ -118,9 +133,9 @@ private static Map<String, Integer> loadTopLevelFieldIds(Dataset dataset) {
return result;
}

private static List<LanceIndexSegmentInfo> loadIndexSegments(Dataset dataset) {
private static List<LanceIndexSegmentInfo> loadIndexSegments(List<IndexDescription> userIndexes) {
List<LanceIndexSegmentInfo> result = new ArrayList<>();
for (IndexDescription description : LanceDatasetIndexDiscovery.describeUserIndexes(dataset)) {
for (IndexDescription description : userIndexes) {
for (Index segment : description.getSegments()) {
if (segment.indexType() == null) {
continue;
Expand Down
Loading
Loading