Skip to content
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

[Kernel] Added Domain Metadata support to Delta Kernel #3835

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
94260ef
Added Domain Metadata support to Delta Kernel
qiyuandong-db Oct 31, 2024
7612613
Lazily load domain metadata during log replay.
qiyuandong-db Oct 31, 2024
2ac3985
Use an iterator to wrap the data action iterator for DM duplicate det…
qiyuandong-db Oct 31, 2024
807c896
Update kernel/kernel-api/src/main/java/io/delta/kernel/internal/Delta…
qiyuandong-db Oct 31, 2024
84d4ae6
Improve error message
qiyuandong-db Oct 31, 2024
509bc27
Rename a unit test
qiyuandong-db Oct 31, 2024
83f3b1e
Don't allow duplicate DMs when reading actions from the winning txn d…
qiyuandong-db Nov 1, 2024
7d7032e
Update error messages in the tests
qiyuandong-db Nov 1, 2024
dadc5a6
Fix typos in comments
qiyuandong-db Nov 3, 2024
0c188c7
Add an integration test with spark
qiyuandong-db Nov 4, 2024
7948fdf
Fix the JavadocGenerationFailed error in the Delta Kernel CI job.
qiyuandong-db Nov 4, 2024
6195e7f
Resolve PR comments.
qiyuandong-db Nov 5, 2024
fd06f6d
Update util method extractDomainMetadataMap.
qiyuandong-db Nov 5, 2024
3e30a41
Remove blank lines.
qiyuandong-db Nov 5, 2024
cec85cf
Address PR comments
qiyuandong-db Nov 6, 2024
8edcc9a
Address PR comments
qiyuandong-db Nov 7, 2024
baca1c5
Move domain metadata actions out of dataActions
qiyuandong-db Nov 7, 2024
7e0a172
Fix javafmt
qiyuandong-db Nov 7, 2024
127de8c
Use a set to check for unsupported writer features
qiyuandong-db Nov 10, 2024
c5f3672
Resolve PR comments
qiyuandong-db Nov 11, 2024
b2d6546
Move golden table to kernel tests.
qiyuandong-db Nov 11, 2024
cd7ddeb
Use getTestResourceFilePath in test to get golden table path.
qiyuandong-db Nov 11, 2024
68c77d8
Resolve git comments
qiyuandong-db Nov 12, 2024
5afec36
Move resolveDomainMetadataConflict into handleDomainMetadata
qiyuandong-db Nov 12, 2024
e358878
Move addDomainMetadata from TransactionImpl to TransactionBuilderImpl
qiyuandong-db Nov 12, 2024
4f56a2f
Use SUPPORTED_WRITER_FEATURES in validateWriteSupportedTable
qiyuandong-db Nov 12, 2024
189ec75
Remove the duplicate check when reading winning txn's DM. Change extr…
qiyuandong-db Nov 13, 2024
a4e3104
Fix nit
qiyuandong-db Nov 13, 2024
b0e4a65
Rename populateDomainMetadataMap
qiyuandong-db Nov 13, 2024
f89199c
Remove unused imports
qiyuandong-db Nov 13, 2024
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 @@ -18,6 +18,7 @@
import static java.lang.String.format;

import io.delta.kernel.exceptions.*;
import io.delta.kernel.internal.actions.DomainMetadata;
import io.delta.kernel.types.DataType;
import io.delta.kernel.types.StructType;
import io.delta.kernel.utils.DataFileStatus;
Expand Down Expand Up @@ -274,6 +275,34 @@ public static KernelException invalidConfigurationValueException(
return new InvalidConfigurationValueException(key, value, helpMessage);
}

public static KernelException domainMetadataUnsupported() {
String message =
"Found DomainMetadata action(s) but table feature 'domainMetadata' "
+ "is not supported on this table.";
return new KernelException(message);
}

public static KernelException duplicateDomainMetadataAction(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this error is thrown when a commit (already on the filesystem contains duplicate entries for the same metadata). In this case the table is corrupted and we should use InvalidTableException

Copy link
Author

@qiyuandong-db qiyuandong-db Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is thrown in two cases:

  • When we detect duplicate domain metadata entries in a winning commit file during conflict resolution, which corresponds to what you mentioned (read path).
  • When validating domain metadata actions to be committed in Transaction.commit() (write path).

As discussed in another thread, I think we can possibly remove the duplicate check in DM conflict resolving, and in this case this error is only thrown in Transaction.commit().

String domain, DomainMetadata action1, DomainMetadata action2) {
String message =
String.format(
"Multiple actions detected for domain '%s' in single transaction: '%s' and '%s'. "
+ "Only one action per domain is allowed.",
domain, action1.toString(), action2.toString());
return new KernelException(message);
}

public static ConcurrentWriteException concurrentDomainMetadataAction(
DomainMetadata domainMetadataAttempt, DomainMetadata winningDomainMetadata) {
String message =
String.format(
"A concurrent writer added a domainMetadata action for the same domain: %s. "
+ "No domain-specific conflict resolution is available for this domain. "
+ "Attempted domainMetadata: %s. Winning domainMetadata: %s",
domainMetadataAttempt.getDomain(), domainMetadataAttempt, winningDomainMetadata);
return new ConcurrentWriteException(message);
}

/* ------------------------ HELPER METHODS ----------------------------- */
private static String formatTimestamp(long millisSinceEpochUTC) {
return new Timestamp(millisSinceEpochUTC).toInstant().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.delta.kernel.engine.CommitCoordinatorClientHandler;
import io.delta.kernel.engine.Engine;
import io.delta.kernel.internal.actions.CommitInfo;
import io.delta.kernel.internal.actions.DomainMetadata;
import io.delta.kernel.internal.actions.Metadata;
import io.delta.kernel.internal.actions.Protocol;
import io.delta.kernel.internal.fs.Path;
Expand All @@ -31,6 +32,7 @@
import io.delta.kernel.internal.snapshot.LogSegment;
import io.delta.kernel.internal.snapshot.TableCommitCoordinatorClientHandler;
import io.delta.kernel.types.StructType;
import java.util.Map;
import java.util.Optional;

/** Implementation of {@link Snapshot}. */
Expand Down Expand Up @@ -83,6 +85,17 @@ public Protocol getProtocol() {
return protocol;
}

/**
* Get the domain metadata map from the log replay, which lazily loads and replays a history of
* domain metadata actions, resolving them to produce the current state of the domain metadata.
*
* @return A map where the keys are domain names and the values are {@link DomainMetadata}
* objects.
*/
public Map<String, DomainMetadata> getDomainMetadataMap() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docs (see getLatestTransactionVersion for an example)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is used just in tests? Do we see a need for it when writing to the table?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a doc for this.

Currently, it is only used in tests. I imagine we will need this when writing to the table in the future.

For example, in Row Tracking, we’ll need to get its domain metadata from the snapshot to retrieve the previous HighWatermark. We need this to assign fresh Row IDs to any AddFile actions within dataActions that don’t yet have row IDs before committing.

return logReplay.getDomainMetadataMap();
}

public CreateCheckpointIterator getCreateCheckpointIterator(Engine engine) {
long minFileRetentionTimestampMillis =
System.currentTimeMillis() - TOMBSTONE_RETENTION.fromMetadata(metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class TableFeatures {
add("columnMapping");
add("typeWidening-preview");
add("typeWidening");
add("domainMetadata");
}
});

Expand All @@ -57,6 +58,12 @@ public class TableFeatures {
}
});

/** The feature name for domain metadata. */
public static final String DOMAIN_METADATA_FEATURE_NAME = "domainMetadata";

/** The minimum writer version required to support domain metadata. */
public static final int DOMAIN_METADATA_MIN_WRITER_VERSION_REQUIRED = 7;

////////////////////
// Helper Methods //
////////////////////
Expand Down Expand Up @@ -93,7 +100,7 @@ public static void validateReadSupportedTable(
* <li>protocol writer version 1.
* <li>protocol writer version 2 only with appendOnly feature enabled.
* <li>protocol writer version 7 with {@code appendOnly}, {@code inCommitTimestamp}, {@code
* columnMapping}, {@code typeWidening} feature enabled.
* columnMapping}, {@code typeWidening}, {@code domainMetadata} feature enabled.
* </ul>
*
* @param protocol Table protocol
Expand Down Expand Up @@ -125,20 +132,8 @@ public static void validateWriteSupportedTable(
throw unsupportedWriterProtocol(tablePath, minWriterVersion);
case 7:
for (String writerFeature : protocol.getWriterFeatures()) {
switch (writerFeature) {
// Only supported writer features as of today in Kernel
case "appendOnly":
break;
case "inCommitTimestamp":
break;
case "columnMapping":
break;
case "typeWidening-preview":
break;
case "typeWidening":
break;
default:
throw unsupportedWriterFeature(tablePath, writerFeature);
if (!SUPPORTED_WRITER_FEATURES.contains(writerFeature)) {
throw unsupportedWriterFeature(tablePath, writerFeature);
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class TransactionBuilderImpl implements TransactionBuilder {
private Optional<List<String>> partitionColumns = Optional.empty();
private Optional<SetTransaction> setTxnOpt = Optional.empty();
private Optional<Map<String, String>> tableProperties = Optional.empty();
private List<DomainMetadata> domainMetadatas = new ArrayList<>();

public TransactionBuilderImpl(TableImpl table, String engineInfo, Operation operation) {
this.table = table;
Expand Down Expand Up @@ -93,6 +94,16 @@ public TransactionBuilder withTableProperties(Engine engine, Map<String, String>
return this;
}

/**
* Internal API to set the domain metadata for the transaction. Visible for testing.
*
* @param domainMetadatas List of domain metadata to be added to the transaction.
*/
public TransactionBuilder withDomainMetadatas(List<DomainMetadata> domainMetadatas) {
this.domainMetadatas = new ArrayList<>(domainMetadatas);
return this;
}

@Override
public Transaction build(Engine engine) {
SnapshotImpl snapshot;
Expand Down Expand Up @@ -156,7 +167,8 @@ public Transaction build(Engine engine) {
setTxnOpt,
shouldUpdateMetadata,
shouldUpdateProtocol,
table.getClock());
table.getClock(),
domainMetadatas);
}

/** Validate the given parameters for the transaction. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
import io.delta.kernel.internal.fs.Path;
import io.delta.kernel.internal.replay.ConflictChecker;
import io.delta.kernel.internal.replay.ConflictChecker.TransactionRebaseState;
import io.delta.kernel.internal.util.Clock;
import io.delta.kernel.internal.util.ColumnMapping;
import io.delta.kernel.internal.util.FileNames;
import io.delta.kernel.internal.util.InCommitTimestampUtils;
import io.delta.kernel.internal.util.VectorUtils;
import io.delta.kernel.internal.util.*;
import io.delta.kernel.types.StructType;
import io.delta.kernel.utils.CloseableIterable;
import io.delta.kernel.utils.CloseableIterator;
Expand Down Expand Up @@ -73,6 +69,7 @@ public class TransactionImpl implements Transaction {
private final Optional<SetTransaction> setTxnOpt;
private final boolean shouldUpdateProtocol;
private final Clock clock;
private final List<DomainMetadata> domainMetadatas;
private Metadata metadata;
private boolean shouldUpdateMetadata;

Expand All @@ -90,7 +87,8 @@ public TransactionImpl(
Optional<SetTransaction> setTxnOpt,
boolean shouldUpdateMetadata,
boolean shouldUpdateProtocol,
Clock clock) {
Clock clock,
List<DomainMetadata> domainMetadatas) {
this.isNewTable = isNewTable;
this.dataPath = dataPath;
this.logPath = logPath;
Expand All @@ -103,6 +101,7 @@ public TransactionImpl(
this.shouldUpdateMetadata = shouldUpdateMetadata;
this.shouldUpdateProtocol = shouldUpdateProtocol;
this.clock = clock;
this.domainMetadatas = domainMetadatas;
}

@Override
Expand Down Expand Up @@ -221,6 +220,12 @@ private TransactionCommitResult doCommit(
}
setTxnOpt.ifPresent(setTxn -> metadataActions.add(createTxnSingleAction(setTxn.toRow())));

// Check for duplicate domain metadata and if the protocol supports
DomainMetadataUtils.validateDomainMetadatas(domainMetadatas, protocol);

domainMetadatas.forEach(
dm -> metadataActions.add(createDomainMetadataSingleAction(dm.toRow())));

try (CloseableIterator<Row> stageDataIter = dataActions.iterator()) {
// Create a new CloseableIterator that will return the metadata actions followed by the
// data actions.
Expand Down Expand Up @@ -269,6 +274,10 @@ public Optional<SetTransaction> getSetTxnOpt() {
return setTxnOpt;
}

public List<DomainMetadata> getDomainMetadatas() {
return domainMetadatas;
}

/**
* Generates a timestamp which is greater than the commit timestamp of the readSnapshot. This can
* result in an additional file read and that this will only happen if ICT is enabled.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (2024) The Delta Lake Project Authors.
*
* Licensed 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 io.delta.kernel.internal.actions;

import static io.delta.kernel.internal.util.InternalUtils.requireNonNull;
import static java.util.Objects.requireNonNull;

import io.delta.kernel.data.ColumnVector;
import io.delta.kernel.data.Row;
import io.delta.kernel.internal.data.GenericRow;
import io.delta.kernel.types.BooleanType;
import io.delta.kernel.types.StringType;
import io.delta.kernel.types.StructType;
import java.util.HashMap;
import java.util.Map;

/** Delta log action representing an `DomainMetadata` action */
public class DomainMetadata {
/** Full schema of the {@link DomainMetadata} action in the Delta Log. */
public static final StructType FULL_SCHEMA =
new StructType()
.add("domain", StringType.STRING, false /* nullable */)
.add("configuration", StringType.STRING, false /* nullable */)
.add("removed", BooleanType.BOOLEAN, false /* nullable */);

public static DomainMetadata fromColumnVector(ColumnVector vector, int rowId) {
if (vector.isNullAt(rowId)) {
return null;
}
return new DomainMetadata(
requireNonNull(vector.getChild(0), rowId, "domain").getString(rowId),
requireNonNull(vector.getChild(1), rowId, "configuration").getString(rowId),
requireNonNull(vector.getChild(2), rowId, "removed").getBoolean(rowId));
}

public static DomainMetadata fromRow(Row row) {
qiyuandong-db marked this conversation as resolved.
Show resolved Hide resolved
if (row == null) {
return null;
}
assert (row.getSchema().equals(FULL_SCHEMA));
return new DomainMetadata(
requireNonNull(row, 0, "domain").getString(0),
requireNonNull(row, 1, "configuration").getString(1),
requireNonNull(row, 2, "removed").getBoolean(2));
}

private final String domain;
private final String configuration;
private final boolean removed;

/**
* The domain metadata action contains a configuration string for a named metadata domain. Two
* overlapping transactions conflict if they both contain a domain metadata action for the same
* metadata domain. Per-domain conflict resolution logic can be implemented.
*
* @param domain A string used to identify a specific domain.
* @param configuration A string containing configuration for the metadata domain.
* @param removed If it is true it serves as a tombstone to logically delete a {@link
* DomainMetadata} action.
*/
public DomainMetadata(String domain, String configuration, boolean removed) {
scottsand-db marked this conversation as resolved.
Show resolved Hide resolved
this.domain = requireNonNull(domain, "domain is null");
this.configuration = requireNonNull(configuration, "configuration is null");
this.removed = removed;
}

public String getDomain() {
return domain;
}

public String getConfiguration() {
return configuration;
}

public boolean isRemoved() {
return removed;
}

/**
* Encode as a {@link Row} object with the schema {@link DomainMetadata#FULL_SCHEMA}.
*
* @return {@link Row} object with the schema {@link DomainMetadata#FULL_SCHEMA}
*/
public Row toRow() {
Map<Integer, Object> domainMetadataMap = new HashMap<>();
domainMetadataMap.put(0, domain);
domainMetadataMap.put(1, configuration);
domainMetadataMap.put(2, removed);

return new GenericRow(DomainMetadata.FULL_SCHEMA, domainMetadataMap);
}

@Override
public String toString() {
qiyuandong-db marked this conversation as resolved.
Show resolved Hide resolved
return String.format(
"DomainMetadata{domain='%s', configuration='%s', removed='%s'}",
domain, configuration, removed);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
DomainMetadata that = (DomainMetadata) obj;
return removed == that.removed
&& domain.equals(that.domain)
&& configuration.equals(that.configuration);
}

@Override
public int hashCode() {
return java.util.Objects.hash(domain, configuration, removed);
}
}
Loading
Loading