-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Storage] Implement getCommits in UC Delta token-based client #6814
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
Changes from 3 commits
153b88a
473ef06
9b9b931
d6167e4
ee014c8
d246102
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
|
|
||
| import io.delta.storage.commit.Commit; | ||
| import io.delta.storage.commit.CommitFailedException; | ||
| import io.delta.storage.commit.CoordinatedCommitsUtils; | ||
| import io.delta.storage.commit.GetCommitsResponse; | ||
| import io.delta.storage.commit.TableIdentifier; | ||
| import io.delta.storage.commit.actions.AbstractMetadata; | ||
|
|
@@ -70,6 +71,8 @@ | |
| import java.util.function.Supplier; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileStatus; | ||
| import org.apache.hadoop.fs.Path; | ||
|
|
||
| /** | ||
| * A REST client implementation of {@link UCDeltaClient} that uses the UC Delta REST API for | ||
|
|
@@ -233,7 +236,7 @@ public void commit( | |
| throws IOException, CommitFailedException, UCCommitCoordinatorException { | ||
| ensureOpen(); | ||
| Objects.requireNonNull(tableId, "tableId must not be null"); | ||
| Objects.requireNonNull(tableIdentifier, "tableIdentifier must not be null"); | ||
| ResolvedTableName name = resolveThreePartName(tableIdentifier); | ||
|
|
||
| UpdateTableRequest request = new UpdateTableRequest(); | ||
| request.addRequirementsItem(new AssertTableUUID() | ||
|
|
@@ -266,14 +269,10 @@ public void commit( | |
| .protocol(toSDKDeltaProtocol(newProtocol.get()))); | ||
| } | ||
|
|
||
| String catalog = tableIdentifier.getNamespace()[0]; | ||
| String schema = tableIdentifier.getNamespace()[1]; | ||
| String table = tableIdentifier.getName(); | ||
|
|
||
| try { | ||
| deltaTablesApi.updateTable(catalog, schema, table, request); | ||
| deltaTablesApi.updateTable(name.catalog, name.schema, name.table, request); | ||
| } catch (ApiException e) { | ||
| handleUpdateTableException(e, catalog, schema, table); | ||
| handleUpdateTableException(e, name.catalog, name.schema, name.table); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -284,9 +283,82 @@ public GetCommitsResponse getCommits( | |
| TableIdentifier tableIdentifier, | ||
| Optional<Long> startVersion, | ||
| Optional<Long> endVersion) throws IOException, UCCommitCoordinatorException { | ||
| throw new UnsupportedOperationException( | ||
| "getCommits is not yet supported by UCDeltaTokenBasedRestClient. " + | ||
| "A separate PR will add this once the tableIdentifier mapping is available."); | ||
| ensureOpen(); | ||
| Objects.requireNonNull(tableId, "tableId must not be null"); | ||
| Objects.requireNonNull(tableUri, "tableUri must not be null"); | ||
| Objects.requireNonNull(startVersion, "startVersion must not be null"); | ||
| Objects.requireNonNull(endVersion, "endVersion must not be null"); | ||
|
|
||
| UUID expectedTableUuid = UUID.fromString(tableId); | ||
| ResolvedTableName name = resolveThreePartName(tableIdentifier); | ||
|
|
||
| // The UC loadTable endpoint does not support server-side filtering by version range, so | ||
| // we fetch the full unbackfilled commit window and filter client-side below. The server | ||
| // bounds the window size, so this list is not unbounded in practice. | ||
| LoadTableResponse response; | ||
| try { | ||
| response = deltaTablesApi.loadTable(name.catalog, name.schema, name.table); | ||
| } catch (ApiException e) { | ||
| if (e.getCode() == HTTP_NOT_FOUND) { | ||
| throw new NoSuchTableException( | ||
| String.format("Table %s not found in Unity Catalog", name.fullName), e); | ||
| } | ||
| throw new IOException( | ||
| String.format("Failed to load commits for table %s (HTTP %s): %s", | ||
| name.fullName, e.getCode(), e.getResponseBody()), | ||
| e); | ||
| } | ||
|
|
||
| TableMetadata metadata = response.getMetadata(); | ||
| UUID actualTableUuid = metadata != null ? metadata.getTableUuid() : null; | ||
| if (!expectedTableUuid.equals(actualTableUuid)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this will happen in the following case:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes — this is exactly the case the UUID check protects against. |
||
| throw new InvalidTargetTableException( | ||
| String.format( | ||
| "Table UUID mismatch for %s: expected %s but got %s", | ||
| name.fullName, | ||
| expectedTableUuid, | ||
| actualTableUuid)); | ||
| } | ||
|
|
||
| Path basePath = CoordinatedCommitsUtils.commitDirPath( | ||
| CoordinatedCommitsUtils.logDirPath(new Path(tableUri))); | ||
| List<Commit> commits = new ArrayList<>(); | ||
| if (response.getCommits() != null) { | ||
| for (DeltaCommit deltaCommit : response.getCommits()) { | ||
| long version = Objects.requireNonNull( | ||
| deltaCommit.getVersion(), "commit version must not be null"); | ||
| if (startVersion.isPresent() && version < startVersion.get()) { | ||
| continue; | ||
| } | ||
| if (endVersion.isPresent() && version > endVersion.get()) { | ||
| continue; | ||
| } | ||
| commits.add(fromDeltaCommit(deltaCommit, basePath)); | ||
| } | ||
| } | ||
|
|
||
| long latestTableVersion = response.getLatestTableVersion() != null | ||
| ? response.getLatestTableVersion() : -1L; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will it be the case if the table does not even have a single table version ? |
||
| return new GetCommitsResponse(commits, latestTableVersion); | ||
| } | ||
|
|
||
| /** Converts a UC SDK {@link DeltaCommit} to a Delta {@link Commit}. */ | ||
| private Commit fromDeltaCommit(DeltaCommit deltaCommit, Path basePath) { | ||
| FileStatus fileStatus = new FileStatus( | ||
| Objects.requireNonNull( | ||
| deltaCommit.getFileSize(), "commit fileSize must not be null"), | ||
| false /* isdir */, | ||
| 0 /* block_replication */, | ||
| 0 /* blocksize */, | ||
| Objects.requireNonNull( | ||
| deltaCommit.getFileModificationTimestamp(), | ||
| "commit fileModificationTimestamp must not be null"), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will it possible for this
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it's |
||
| new Path(basePath, Objects.requireNonNull( | ||
| deltaCommit.getFileName(), "commit fileName must not be null"))); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment for
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here — removed the |
||
| return new Commit( | ||
| Objects.requireNonNull(deltaCommit.getVersion(), "commit version must not be null"), | ||
| fileStatus, | ||
| Objects.requireNonNull(deltaCommit.getTimestamp(), "commit timestamp must not be null")); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those nullable checks may not necessary.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're right, removed all the |
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -339,38 +411,30 @@ public void close() throws IOException { | |
| @Override | ||
| public TableInfo loadTable(TableIdentifier tableIdentifier) throws IOException { | ||
| ensureOpen(); | ||
| Objects.requireNonNull(tableIdentifier, "tableIdentifier must not be null"); | ||
| String[] namespace = tableIdentifier.getNamespace(); | ||
| if (namespace == null || namespace.length != 2) { | ||
| throw new IllegalArgumentException( | ||
| "UC tableIdentifier must have a 2-component namespace [catalog, schema]; got " + | ||
| (namespace == null ? "null" : java.util.Arrays.toString(namespace))); | ||
| } | ||
| String catalog = namespace[0]; | ||
| String schema = namespace[1]; | ||
| String table = tableIdentifier.getName(); | ||
| ResolvedTableName name = resolveThreePartName(tableIdentifier); | ||
|
|
||
| try { | ||
| return toTableInfo( | ||
| deltaTablesApi.loadTable(catalog, schema, table), catalog, schema, table); | ||
| deltaTablesApi.loadTable(name.catalog, name.schema, name.table), | ||
| name.catalog, name.schema, name.table); | ||
| } catch (ApiException e) { | ||
| if (e.getCode() == HTTP_NOT_FOUND) { | ||
| throw new NoSuchTableException( | ||
| String.format("Table %s.%s.%s not found in Unity Catalog", catalog, schema, table), e); | ||
| String.format("Table %s not found in Unity Catalog", name.fullName), e); | ||
| } | ||
| // UC encodes non-Delta-format errors as HTTP 400 with error.type = | ||
| // "UnsupportedTableFormatException"; substring-match the body to avoid coupling to an | ||
| // ErrorResponse parser. | ||
| String body = e.getResponseBody(); | ||
| if (body != null && body.contains("UnsupportedTableFormatException")) { | ||
| throw new UnsupportedTableFormatException( | ||
| String.format("Table %s.%s.%s is not in Delta format; the Delta REST API cannot " | ||
| + "serve it. Body: %s", catalog, schema, table, body), | ||
| String.format("Table %s is not in Delta format; the Delta REST API cannot " | ||
| + "serve it. Body: %s", name.fullName, body), | ||
| e); | ||
| } | ||
| throw new IOException( | ||
| String.format("Failed to load table %s.%s.%s (HTTP %s): %s", | ||
| catalog, schema, table, e.getCode(), e.getResponseBody()), e); | ||
| String.format("Failed to load table %s (HTTP %s): %s", | ||
| name.fullName, e.getCode(), e.getResponseBody()), e); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -640,6 +704,25 @@ private void addMetadataUpdates( | |
| } | ||
| } | ||
|
|
||
| // =========================== | ||
| // Table Identifier Helpers | ||
| // =========================== | ||
|
|
||
| /** | ||
| * Validates that the given {@code tableIdentifier} is a Unity Catalog three-part | ||
| * (catalog.schema.table) name and returns its resolved parts. | ||
| */ | ||
| private static ResolvedTableName resolveThreePartName(TableIdentifier tableIdentifier) { | ||
| Objects.requireNonNull(tableIdentifier, "tableIdentifier must not be null"); | ||
| String[] namespace = tableIdentifier.getNamespace(); | ||
| if (namespace == null || namespace.length != 2) { | ||
| throw new IllegalArgumentException( | ||
| "UC tableIdentifier must have a 2-component namespace [catalog, schema]; got " + | ||
| (namespace == null ? "null" : java.util.Arrays.toString(namespace))); | ||
| } | ||
| return new ResolvedTableName(namespace[0], namespace[1], tableIdentifier.getName()); | ||
| } | ||
|
|
||
| // =========================== | ||
| // Exception Handling | ||
| // =========================== | ||
|
|
@@ -673,6 +756,21 @@ private void handleUpdateTableException( | |
| // Inner Classes | ||
| // =========================== | ||
|
|
||
| /** A Unity Catalog three-part table name resolved from a {@link TableIdentifier}. */ | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is intentional kept inner - its a private impl detail |
||
| private static final class ResolvedTableName { | ||
| final String catalog; | ||
| final String schema; | ||
| final String table; | ||
| final String fullName; | ||
|
|
||
| ResolvedTableName(String catalog, String schema, String table) { | ||
| this.catalog = catalog; | ||
| this.schema = schema; | ||
| this.table = table; | ||
| this.fullName = catalog + "." + schema + "." + table; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adapts a UC SDK {@link TableMetadata} to {@link AbstractMetadata}. | ||
| */ | ||
|
|
||
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.
wait, does all the
tableIdis a unique string that can be serialized to be stricklyUUID? I think OSS UC server should have the implication, but pls take a look for the delta uc spec, and see if it clearly says it's a string that can be deserialized to aUUID?Otherwise, if the table id is a
uniqueone, but it will not be able to deserialize to aUUID, then that's a serious bug.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.
I will more suggest to use a
String, rather than explicitly deserializing to a UUID instance.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.
okay, the spec clearly says it's
UUID. https://github.com/unitycatalog/unitycatalog/blob/main/api/delta-docs/Models/TableMetadata.mdBut I will still suggest to use a
String.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.
good catch, switched to String.
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.
It is fine to be a String here, only because the function signature says "String".
In this case,
aString.equals(anotherUUID.toString())is better thanUUID.fromString(...).equals(anotherUUID)