-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Kernel] Load the protocol and metadata from the CRC files when avail…
…able (#4077) <!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md 2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 3. Be sure to keep the PR description updated to reflect all changes. 4. Please write your PR title to summarize what this PR proposes. 5. If possible, provide a concise example to reproduce the issue for a faster review. 6. If applicable, include the corresponding issue number in the PR title and link it in the body. --> #### Which Delta project/connector is this regarding? <!-- Please add the component selected below to the beginning of the pull request title For example: [Spark] Title of my pull request --> - [ ] Spark - [ ] Standalone - [ ] Flink - [X] Kernel - [ ] Other (fill in here) ## Description <!-- - Describe what this PR changes. - Describe why we need the change. If this PR resolves an issue be sure to include "Resolves #XXX" to correctly link and close the issue upon merge. --> CP crc loading code to master branch from "kernel-20250115-crc-optimization" PR created using `git cherry-pick 7d32f66` ## How was this patch tested? <!-- If tests were added, say they were added here. Please make sure to test the changes thoroughly including negative and positive cases if possible. If the changes were tested in any way other than unit tests, please clarify how you tested step by step (ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future). If the changes were not tested, please explain why. --> ## Does this PR introduce _any_ user-facing changes? <!-- If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible. If possible, please also clarify if this is a user-facing change compared to the released Delta Lake versions or within the unreleased branches such as master. If no, write 'No'. --> No --------- Co-authored-by: Venki Korukanti <[email protected]> Co-authored-by: Allison Portis <[email protected]>
- Loading branch information
1 parent
c01bb7c
commit 92a8a22
Showing
8 changed files
with
693 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
kernel/kernel-api/src/main/java/io/delta/kernel/internal/replay/CRCInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (2025) 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.replay; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import io.delta.kernel.data.ColumnarBatch; | ||
import io.delta.kernel.internal.actions.Metadata; | ||
import io.delta.kernel.internal.actions.Protocol; | ||
import io.delta.kernel.types.StructType; | ||
import java.util.Optional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class CRCInfo { | ||
private static final Logger logger = LoggerFactory.getLogger(CRCInfo.class); | ||
|
||
public static Optional<CRCInfo> fromColumnarBatch( | ||
long version, ColumnarBatch batch, int rowId, String crcFilePath) { | ||
Protocol protocol = Protocol.fromColumnVector(batch.getColumnVector(PROTOCOL_ORDINAL), rowId); | ||
Metadata metadata = Metadata.fromColumnVector(batch.getColumnVector(METADATA_ORDINAL), rowId); | ||
// protocol and metadata are nullable per fromColumnVector's implementation. | ||
if (protocol == null || metadata == null) { | ||
logger.warn("Invalid checksum file missing protocol and/or metadata: {}", crcFilePath); | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new CRCInfo(version, metadata, protocol)); | ||
} | ||
|
||
// We can add additional fields later | ||
public static final StructType FULL_SCHEMA = | ||
new StructType().add("protocol", Protocol.FULL_SCHEMA).add("metadata", Metadata.FULL_SCHEMA); | ||
|
||
private static final int PROTOCOL_ORDINAL = 0; | ||
private static final int METADATA_ORDINAL = 1; | ||
|
||
private final long version; | ||
private final Metadata metadata; | ||
private final Protocol protocol; | ||
|
||
protected CRCInfo(long version, Metadata metadata, Protocol protocol) { | ||
this.version = version; | ||
this.metadata = requireNonNull(metadata); | ||
this.protocol = requireNonNull(protocol); | ||
} | ||
|
||
/** The version of the Delta table that this CRCInfo represents. */ | ||
public long getVersion() { | ||
return version; | ||
} | ||
|
||
/** The {@link Metadata} stored in this CRCInfo. */ | ||
public Metadata getMetadata() { | ||
return metadata; | ||
} | ||
|
||
/** The {@link Protocol} stored in this CRCInfo. */ | ||
public Protocol getProtocol() { | ||
return protocol; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
kernel/kernel-api/src/main/java/io/delta/kernel/internal/replay/ChecksumReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright (2025) 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.replay; | ||
|
||
import static io.delta.kernel.internal.util.FileNames.*; | ||
import static io.delta.kernel.internal.util.Utils.singletonCloseableIterator; | ||
import static java.lang.Math.min; | ||
|
||
import io.delta.kernel.data.ColumnarBatch; | ||
import io.delta.kernel.engine.Engine; | ||
import io.delta.kernel.internal.fs.Path; | ||
import io.delta.kernel.internal.util.FileNames; | ||
import io.delta.kernel.utils.CloseableIterator; | ||
import io.delta.kernel.utils.FileStatus; | ||
import java.io.IOException; | ||
import java.util.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Utility method to load protocol and metadata from the Delta log checksum files. */ | ||
public class ChecksumReader { | ||
private static final Logger logger = LoggerFactory.getLogger(ChecksumReader.class); | ||
|
||
/** | ||
* Load the CRCInfo from the checksum file at the given version. If the checksum file is not found | ||
* at the given version, it will try to find the latest checksum file that is created at or after | ||
* the lower bound version. | ||
* | ||
* @param engine the engine to use for reading the checksum file | ||
* @param logPath the path to the Delta log | ||
* @param targetedVersion the target version to read the checksum file from | ||
* @param lowerBound the inclusive lower bound version to search for the checksum file | ||
* @return Optional {@link CRCInfo} containing the protocol and metadata, and the version of the | ||
* checksum file. If the checksum file is not found, it will return an empty | ||
*/ | ||
public static Optional<CRCInfo> getCRCInfo( | ||
Engine engine, Path logPath, long targetedVersion, long lowerBound) { | ||
// lower bound should always smaller than the targetedVersion. | ||
lowerBound = min(lowerBound, targetedVersion); | ||
logger.info("Loading CRC file for version {} with lower bound {}", targetedVersion, lowerBound); | ||
// First try to load the CRC at given version. If not found or failed to read then try to | ||
// find the latest CRC file that is created at or after the lower bound version. | ||
Path crcFilePath = checksumFile(logPath, targetedVersion); | ||
Optional<CRCInfo> crcInfoOpt = readChecksumFile(engine, crcFilePath); | ||
if (crcInfoOpt.isPresent() | ||
|| | ||
// we don't expect any more checksum files as it is the first version | ||
targetedVersion == 0 | ||
|| targetedVersion == lowerBound) { | ||
return crcInfoOpt; | ||
} | ||
logger.info( | ||
"CRC file for version {} not found, listing CRC files from version {}", | ||
targetedVersion, | ||
lowerBound); | ||
|
||
Path lowerBoundFilePath = checksumFile(logPath, lowerBound); | ||
try (CloseableIterator<FileStatus> crcFiles = | ||
engine.getFileSystemClient().listFrom(lowerBoundFilePath.toString())) { | ||
List<FileStatus> crcFilesList = | ||
crcFiles | ||
.filter(file -> isChecksumFile(file.getPath())) | ||
.takeWhile(file -> checksumVersion(new Path(file.getPath())) <= targetedVersion) | ||
.toInMemoryList(); | ||
|
||
// pick the last file which is the latest version that has the CRC file | ||
if (crcFilesList.isEmpty()) { | ||
logger.warn("No checksum files found in the range {} to {}", lowerBound, targetedVersion); | ||
return Optional.empty(); | ||
} | ||
|
||
FileStatus latestCRCFile = crcFilesList.get(crcFilesList.size() - 1); | ||
return readChecksumFile(engine, new Path(latestCRCFile.getPath())); | ||
} catch (IOException e) { | ||
logger.warn("Failed to list checksum files from {}", lowerBoundFilePath, e); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private static Optional<CRCInfo> readChecksumFile(Engine engine, Path filePath) { | ||
try (CloseableIterator<ColumnarBatch> iter = | ||
engine | ||
.getJsonHandler() | ||
.readJsonFiles( | ||
singletonCloseableIterator(FileStatus.of(filePath.toString())), | ||
CRCInfo.FULL_SCHEMA, | ||
Optional.empty())) { | ||
// We do this instead of iterating through the rows or using `getSingularRow` so we | ||
// can use the existing fromColumnVector methods in Protocol, Metadata, Format etc | ||
if (!iter.hasNext()) { | ||
logger.warn("Checksum file is empty: {}", filePath); | ||
return Optional.empty(); | ||
} | ||
|
||
ColumnarBatch batch = iter.next(); | ||
if (batch.getSize() != 1) { | ||
String msg = "Expected exactly one row in the checksum file {}, found {} rows"; | ||
logger.warn(msg, filePath, batch.getSize()); | ||
return Optional.empty(); | ||
} | ||
|
||
long crcVersion = FileNames.checksumVersion(filePath); | ||
|
||
return CRCInfo.fromColumnarBatch(crcVersion, batch, 0 /* rowId */, filePath.toString()); | ||
} catch (Exception e) { | ||
// This can happen when the version does not have a checksum file | ||
logger.warn("Failed to read checksum file {}", filePath, e); | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.