Skip to content
Closed
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 @@ -7,6 +7,7 @@
import io.opensharing.asset.table.delta.DeltaKernel;
import io.opensharing.asset.table.delta.DeltaTableMetadataReader;
import io.opensharing.asset.table.delta.DeltaTableQueryReader;
import io.opensharing.asset.table.delta.SourceTableException;
import io.opensharing.auth.AuthContext;
import io.opensharing.auth.UserContext;
import io.opensharing.catalog.AssetLocation;
Expand All @@ -33,6 +34,7 @@
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -124,8 +126,12 @@ public long version(
String startingTimestamp) {
ShareEntity share = protocolShares.requireGranted(recipient, shareName);
Instant timestamp = parseTimestamp("startingTimestamp", startingTimestamp);
return kernel.getVersion(
findTable(share, schemaAlias, tableAlias).catalogTable(), timestamp, owner(share));
ResolvedAsset table = findTable(share, schemaAlias, tableAlias).catalogTable();
return readingTable(
share,
schemaAlias,
tableAlias,
() -> kernel.getVersion(table, timestamp, owner(share)));
}

/**
Expand All @@ -144,7 +150,11 @@ public DeltaTableMetadataReader.Result metadata(
ShareEntity share = protocolShares.requireGranted(recipient, shareName);
Instant asOf = parseTimestamp("timestamp", timestamp);
ResolvedAsset table = findTable(share, schemaAlias, tableAlias).catalogTable();
return metadataReader.read(table, version, asOf, owner(share), capabilities);
return readingTable(
share,
schemaAlias,
tableAlias,
() -> metadataReader.read(table, version, asOf, owner(share), capabilities));
}

/**
Expand Down Expand Up @@ -207,7 +217,11 @@ public void query(
findTable(share, schemaAlias, tableAlias).catalogTable(), schemaAlias, tableAlias);
QueryTableRequest body = request == null ? QueryTableRequest.EMPTY : request;
if (body.startingVersion() != null || body.endingVersion() != null) {
queryChanges(body, catalogTable, share, options, sink);
readingTable(
share,
schemaAlias,
tableAlias,
() -> queryChanges(body, catalogTable, share, options, sink));
return;
}
boolean historical = body.version() != null || body.timestamp() != null;
Expand All @@ -223,17 +237,23 @@ public void query(
if (body.predicateHints() != null && !body.predicateHints().isEmpty()) {
log.debug("Ignoring deprecated predicateHints {}", body.predicateHints());
}
queryReader.read(
catalogTable,
owner(share),
options,
sink,
body.version(),
parseTimestamp("timestamp", body.timestamp()),
body.refreshToken(),
includeRefreshToken,
body.jsonPredicateHints(),
limitHint(body));
Instant timestamp = parseTimestamp("timestamp", body.timestamp());
readingTable(
share,
schemaAlias,
tableAlias,
() ->
queryReader.read(
catalogTable,
owner(share),
options,
sink,
body.version(),
timestamp,
body.refreshToken(),
includeRefreshToken,
body.jsonPredicateHints(),
limitHint(body)));
}

/**
Expand All @@ -254,17 +274,49 @@ public void changes(
ResolvedAsset catalogTable =
requireUrlAccess(
findTable(share, schemaAlias, tableAlias).catalogTable(), schemaAlias, tableAlias);
queryReader.readChangeDataFeed(
catalogTable,
owner(share),
options,
sink,
request.startingVersion(),
parseTimestamp("startingTimestamp", request.startingTimestamp()),
request.endingVersion(),
parseTimestamp("endingTimestamp", request.endingTimestamp()),
Boolean.TRUE.equals(request.includeHistoricalMetadata()),
Boolean.TRUE.equals(request.includeHistoricalProtocol()));
Instant startingTimestamp = parseTimestamp("startingTimestamp", request.startingTimestamp());
Instant endingTimestamp = parseTimestamp("endingTimestamp", request.endingTimestamp());
readingTable(
share,
schemaAlias,
tableAlias,
() ->
queryReader.readChangeDataFeed(
catalogTable,
owner(share),
options,
sink,
request.startingVersion(),
startingTimestamp,
request.endingVersion(),
endingTimestamp,
Boolean.TRUE.equals(request.includeHistoricalMetadata()),
Boolean.TRUE.equals(request.includeHistoricalProtocol())));
}

/**
* Reads the table, answering a failure of the source table itself in the table's name as the
* share gives it, since the recipient knows no other.
*/
private <T> T readingTable(
ShareEntity share, String schemaAlias, String tableAlias, Supplier<T> read) {
try {
return read.get();
} catch (SourceTableException failed) {
throw failed.forRecipient(share.getName() + "." + schemaAlias + "." + tableAlias);
}
}

private void readingTable(
ShareEntity share, String schemaAlias, String tableAlias, Runnable read) {
readingTable(
share,
schemaAlias,
tableAlias,
() -> {
read.run();
return null;
});
}

/** Returns {@code catalogTable} once it is known to support the {@code url} access mode. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.opensharing.asset.table.delta;

import io.delta.kernel.Snapshot;
import io.delta.kernel.Table;
import io.delta.kernel.defaults.engine.DefaultEngine;
import io.delta.kernel.engine.Engine;
import io.delta.kernel.exceptions.InvalidTableException;
import io.delta.kernel.exceptions.KernelEngineException;
import io.delta.kernel.exceptions.KernelException;
import io.delta.kernel.exceptions.TableNotFoundException;
import io.delta.kernel.exceptions.UnsupportedProtocolVersionException;
import io.delta.kernel.exceptions.UnsupportedTableFeatureException;
import io.delta.kernel.internal.TableImpl;
import io.opensharing.asset.table.StorageLocations;
import io.opensharing.auth.AuthContext;
Expand All @@ -19,9 +22,16 @@
import io.opensharing.catalog.TableProperties;
import io.opensharing.exception.CatalogException;
import io.opensharing.http.ApiException;
import java.io.FileNotFoundException;
import java.nio.file.NoSuchFileException;
import java.time.Duration;
import java.time.Instant;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,6 +40,15 @@ public class DeltaKernel {

private static final Logger log = LoggerFactory.getLogger(DeltaKernel.class);
static final Duration CREDENTIAL_TTL = Duration.ofMinutes(5);
private static final String CONTACT_PROVIDER = "Please contact your data provider.";
private static final String SERVER_CANNOT_READ =
"This server cannot read them. " + CONTACT_PROVIDER;
private static final int MAX_CAUSE_DEPTH = 10;
/** A location with a scheme, such as one of the table's files under another spelling. */
private static final Pattern URI_LIKE =
Pattern.compile("[A-Za-z][A-Za-z0-9+.-]*:/{1,3}[^\\s,;'\")\\]]*[^\\s,;'\")\\]:.]");
/** What a stripped table path is replaced with in a message the recipient gets. */
private static final String PATH_PLACEHOLDER = "the table";

private final CatalogConnector catalog;

Expand All @@ -44,53 +63,134 @@ public DeltaKernel(CatalogConnector catalog) {
public long getVersion(ResolvedAsset table, Instant startingTimestamp, AuthContext auth) {
try (Session session = open(table, auth)) {
return versionAtOrAfter((TableImpl) session.table(), session.engine(), startingTimestamp);
} catch (KernelException | KernelEngineException failed) {
throw classifyRead(failed, table);
}
}

/**
* The table's latest version, or the earliest committed at or after {@code startingTimestamp}.
* A timestamp the log cannot answer is Kernel's report.
*/
static long versionAtOrAfter(TableImpl table, Engine engine, Instant startingTimestamp) {
Snapshot latest = latestSnapshot(table, engine);
if (startingTimestamp == null) {
return latest.getVersion();
return table.getLatestSnapshot(engine).getVersion();
Comment thread
littlegrasscao marked this conversation as resolved.
Resolved
}
if (startingTimestamp.toEpochMilli() > latest.getTimestamp(engine)) {
throw ApiException.invalidParameter(
"startingTimestamp is after the latest table version timestamp");
try {
Comment thread
littlegrasscao marked this conversation as resolved.
Resolved
return table.getVersionAtOrAfterTimestamp(engine, startingTimestamp.toEpochMilli());
} catch (IllegalArgumentException failed) {
throw classify(failed, table.getPath(engine), null);
}
return table.getVersionAtOrAfterTimestamp(engine, startingTimestamp.toEpochMilli());
}

/**
* The table's latest snapshot. A catalog Delta table without a log is a catalog error, and a
* protocol Kernel cannot read is the recipient's.
* Classifies a Kernel failure anywhere in reading {@code table}, of which the catalog's storage
* location is all the path known.
*/
static Snapshot latestSnapshot(Table table, Engine engine) {
try {
return table.getLatestSnapshot(engine);
} catch (TableNotFoundException missing) {
log.error("Delta table at '{}' has no log", table.getPath(engine));
throw new CatalogException("the table has no Delta log");
} catch (KernelException failed) {
if (unsupportedProtocol(failed)) {
throw unreadableTable(failed);
static RuntimeException classifyRead(RuntimeException failed, ResolvedAsset table) {
return classify(
failed, HadoopStorageConfiguration.kernelPath(table.location().storageLocation()), null);
}

/**
* The recipient's answer to a failure reading the table, classified once. A log or file that is
* missing or cannot be accessed, or a feature this server cannot read, is the shared source
* table's own state, which its data provider controls. A log whose content Kernel rejects is
* the table's state too, and Kernel says what is wrong. Any other Kernel report, such as a
* version or timestamp the table does not have, is the request's mistake. A failure of the
* storage layer itself is not the recipient's to hear about, and stays a server error. Kernel's
* messages name the table's storage path, which stays in the log.
*
* @param version the table version the failed read was for, if the request named one
*/
static RuntimeException classify(RuntimeException failed, String tablePath, Long version) {
if (failed instanceof TableNotFoundException || storageUnavailable(failed)) {
log.warn("Delta table at '{}' is unavailable: {}", tablePath, failed.toString());
return SourceTableException.unreadable(failed.toString());
}
if (failed instanceof KernelEngineException) {
// The storage layer failed for a reason of its own, which is a server error.
log.error("Delta table at '{}' failed in the storage layer", tablePath, failed);
return failed;
}
if (failed instanceof UnsupportedTableFeatureException unsupported) {
log.warn("Delta table at '{}': {}", tablePath, unsupported.getMessage());
return ApiException.unsupportedFeatures(
unsupported.getUnsupportedFeatures().stream()
.map(feature -> feature.toLowerCase(Locale.ROOT))
.toList(),
version,
SERVER_CANNOT_READ);
}
if (failed instanceof UnsupportedProtocolVersionException unsupported) {
log.warn("Delta table at '{}': {}", tablePath, unsupported.getMessage());
return ApiException.unsupportedFeatures(
List.of("reader version " + unsupported.getMinReaderVersion()),
version,
SERVER_CANNOT_READ);
}
if (failed instanceof InvalidTableException) {
return kernelError(failed, tablePath, " " + CONTACT_PROVIDER);
}
return kernelError(failed, tablePath, "");
Comment thread
littlegrasscao marked this conversation as resolved.
Resolved
}

/**
* Whether the table's files could not be found or read: Kernel reports that only as the storage
* layer's own failure, which may be the filesystem library's class of the same name.
*/
private static boolean storageUnavailable(Throwable failed) {
Throwable cause = failed;
for (int depth = 0; cause != null && depth < MAX_CAUSE_DEPTH; depth++) {
if (cause instanceof FileNotFoundException
|| cause instanceof NoSuchFileException
|| cause.getClass().getSimpleName().equals("AccessDeniedException")) {
Comment thread
littlegrasscao marked this conversation as resolved.
Resolved
return true;
}
throw failed;
cause = cause.getCause();
}
return false;
}

/**
* Whether Kernel cannot read the table's reader version or features, which it reports only by
* message.
* Kernel's report, in Kernel's own words and without the table's storage path, which stays in the
* log: every spelling of the path, and then any other location with a scheme, is removed from
* what the recipient gets. {@code suffix} is appended to the message.
*/
static boolean unsupportedProtocol(KernelException failed) {
String message = failed.getMessage();
return message != null && message.startsWith("Unsupported Delta ");
static ApiException kernelError(RuntimeException failed, String tablePath, String suffix) {
String message =
failed.getMessage() == null ? "Kernel rejected the request." : failed.getMessage();
log.info("Kernel rejected a request for '{}': {}", tablePath, message);
for (String form : pathForms(tablePath)) {
message = message.replaceAll(Pattern.quote(form) + "(/(?=[:\\s]|$))?", PATH_PLACEHOLDER);
Comment thread
littlegrasscao marked this conversation as resolved.
Resolved
}
message = URI_LIKE.matcher(message).replaceAll(PATH_PLACEHOLDER);
Comment thread
littlegrasscao marked this conversation as resolved.
Resolved
String lead = PATH_PLACEHOLDER + ": ";
if (message.startsWith(lead)) {
message = message.substring(lead.length());
}
return ApiException.badRequest(message + suffix);
Comment thread
littlegrasscao marked this conversation as resolved.
Resolved
}

/** Kernel's message names the table path, so it stays in the log. */
static ApiException unreadableTable(KernelException failed) {
log.warn("Kernel cannot read a shared table: {}", failed.getMessage());
return ApiException.invalidParameter(
"the table requires a Delta reader version or feature this server cannot read");
/**
* The spellings of a table path a Kernel message can carry: as given, a local path with and
* without its {@code file:} scheme, which Kernel normalizes to one slash, and with spaces escaped
* or unescaped, since a path and its URI form differ in all of those. Longer spellings come
* first, so replacing one never leaves a stub of another behind.
*/
private static List<String> pathForms(String tablePath) {
Set<String> paths = new HashSet<>(List.of(tablePath));
if (tablePath.startsWith("file:")) {
String local = tablePath.replaceFirst("^file:/*", "/");
paths.addAll(List.of(local, "file:" + local, "file://" + local));
}
Set<String> forms = new HashSet<>();
for (String path : paths) {
String base = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
forms.addAll(List.of(base, base.replace(" ", "%20"), base.replace("%20", " ")));
}
forms.remove("");
return forms.stream().sorted(Comparator.comparingInt(String::length).reversed()).toList();
}

/** Opens the table's root with read credentials the catalog vends to {@code auth}. */
Expand Down
Loading
Loading