-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Flink: Commit both data files and delete files to iceberg transaction. #1939
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 2 commits
14c028b
b5d2b09
c007165
1eae61a
0fdec67
4e769b2
0c6d008
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 |
|---|---|---|
|
|
@@ -19,30 +19,42 @@ | |
|
|
||
| package org.apache.iceberg.flink.sink; | ||
|
|
||
| import java.io.IOException; | ||
| import org.apache.flink.core.io.SimpleVersionedSerializer; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import org.apache.iceberg.ManifestFile; | ||
| import org.apache.iceberg.ManifestFiles; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
Contributor
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. Is this used?
Member
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. Em, this could be removed now. |
||
|
|
||
| class FlinkManifestSerializer implements SimpleVersionedSerializer<ManifestFile> { | ||
| private static final int VERSION_NUM = 1; | ||
| static final FlinkManifestSerializer INSTANCE = new FlinkManifestSerializer(); | ||
| class DeltaManifests implements Iterable<ManifestFile> { | ||
|
|
||
| @Override | ||
| public int getVersion() { | ||
| return VERSION_NUM; | ||
| private final ManifestFile dataManifest; | ||
| private final ManifestFile deleteManifest; | ||
|
|
||
| DeltaManifests(ManifestFile dataManifest, ManifestFile deleteManifest) { | ||
| this.dataManifest = dataManifest; | ||
| this.deleteManifest = deleteManifest; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] serialize(ManifestFile manifestFile) throws IOException { | ||
| Preconditions.checkNotNull(manifestFile, "ManifestFile to be serialized should not be null"); | ||
| ManifestFile dataManifest() { | ||
| return dataManifest; | ||
| } | ||
|
|
||
| return ManifestFiles.encode(manifestFile); | ||
| ManifestFile deleteManifest() { | ||
| return deleteManifest; | ||
| } | ||
|
|
||
| @NotNull | ||
| @Override | ||
| public ManifestFile deserialize(int version, byte[] serialized) throws IOException { | ||
| return ManifestFiles.decode(serialized); | ||
| public Iterator<ManifestFile> iterator() { | ||
|
Contributor
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. does this need to extend from Iterable? It seems only needed for using
Member
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. OK, Agreed we don't have to introduce the complex |
||
| List<ManifestFile> manifests = Lists.newArrayListWithCapacity(2); | ||
| if (dataManifest != null) { | ||
| manifests.add(dataManifest); | ||
| } | ||
|
|
||
| if (deleteManifest != null) { | ||
| manifests.add(deleteManifest); | ||
| } | ||
|
|
||
| return manifests.iterator(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.iceberg.flink.sink; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.DataInputStream; | ||
| import java.io.DataOutputStream; | ||
| import java.io.IOException; | ||
| import org.apache.flink.core.io.SimpleVersionedSerializer; | ||
| import org.apache.iceberg.ManifestFile; | ||
| import org.apache.iceberg.ManifestFiles; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
|
||
| class DeltaManifestsSerializer implements SimpleVersionedSerializer<DeltaManifests> { | ||
| private static final int VERSION_NUM = 1; | ||
| private static final byte[] EMPTY_BINARY = new byte[0]; | ||
|
|
||
| static final DeltaManifestsSerializer INSTANCE = new DeltaManifestsSerializer(); | ||
|
|
||
| @Override | ||
| public int getVersion() { | ||
| return VERSION_NUM; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] serialize(DeltaManifests deltaManifests) throws IOException { | ||
| Preconditions.checkNotNull(deltaManifests, "DeltaManifests to be serialized should not be null"); | ||
|
|
||
| ByteArrayOutputStream binaryOut = new ByteArrayOutputStream(); | ||
| DataOutputStream out = new DataOutputStream(binaryOut); | ||
|
|
||
| byte[] dataManifestBinary = EMPTY_BINARY; | ||
| if (deltaManifests.dataManifest() != null) { | ||
| dataManifestBinary = ManifestFiles.encode(deltaManifests.dataManifest()); | ||
| } | ||
|
|
||
| out.writeInt(dataManifestBinary.length); | ||
| out.write(dataManifestBinary); | ||
|
|
||
| byte[] deleteManifestBinary = EMPTY_BINARY; | ||
| if (deltaManifests.deleteManifest() != null) { | ||
| deleteManifestBinary = ManifestFiles.encode(deltaManifests.deleteManifest()); | ||
| } | ||
|
|
||
| out.writeInt(deleteManifestBinary.length); | ||
| out.write(deleteManifestBinary); | ||
|
|
||
| return binaryOut.toByteArray(); | ||
| } | ||
|
|
||
| @Override | ||
| public DeltaManifests deserialize(int version, byte[] serialized) throws IOException { | ||
| ByteArrayInputStream binaryIn = new ByteArrayInputStream(serialized); | ||
| DataInputStream in = new DataInputStream(binaryIn); | ||
|
|
||
| ManifestFile dataManifest = null; | ||
| int dataManifestSize = in.readInt(); | ||
| if (dataManifestSize > 0) { | ||
| byte[] dataManifestBinary = new byte[dataManifestSize]; | ||
| Preconditions.checkState(in.read(dataManifestBinary) == dataManifestSize); | ||
|
|
||
| dataManifest = ManifestFiles.decode(dataManifestBinary); | ||
| } | ||
|
|
||
| ManifestFile deleteManifest = null; | ||
| int deleteManifestSize = in.readInt(); | ||
| if (deleteManifestSize > 0) { | ||
| byte[] deleteManifestBinary = new byte[deleteManifestSize]; | ||
| Preconditions.checkState(in.read(deleteManifestBinary) == deleteManifestSize); | ||
|
|
||
| deleteManifest = ManifestFiles.decode(deleteManifestBinary); | ||
| } | ||
|
|
||
| return new DeltaManifests(dataManifest, deleteManifest); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,9 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.function.Supplier; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.DeleteFile; | ||
| import org.apache.iceberg.HasTableOperations; | ||
| import org.apache.iceberg.ManifestFile; | ||
| import org.apache.iceberg.ManifestFiles; | ||
|
|
@@ -32,18 +34,19 @@ | |
| import org.apache.iceberg.io.CloseableIterable; | ||
| import org.apache.iceberg.io.FileIO; | ||
| import org.apache.iceberg.io.OutputFile; | ||
| import org.apache.iceberg.io.WriteResult; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
|
|
||
| class FlinkManifestUtil { | ||
| private static final int ICEBERG_FORMAT_VERSION = 2; | ||
| private static final int FORMAT_V2 = 2; | ||
| private static final Long DUMMY_SNAPSHOT_ID = 0L; | ||
|
|
||
| private FlinkManifestUtil() { | ||
| } | ||
|
|
||
| static ManifestFile writeDataFiles(OutputFile outputFile, PartitionSpec spec, List<DataFile> dataFiles) | ||
| private static ManifestFile writeDataFiles(OutputFile outputFile, PartitionSpec spec, List<DataFile> dataFiles) | ||
| throws IOException { | ||
| ManifestWriter<DataFile> writer = ManifestFiles.write(ICEBERG_FORMAT_VERSION, spec, outputFile, DUMMY_SNAPSHOT_ID); | ||
| ManifestWriter<DataFile> writer = ManifestFiles.write(FORMAT_V2, spec, outputFile, DUMMY_SNAPSHOT_ID); | ||
|
|
||
| try (ManifestWriter<DataFile> closeableWriter = writer) { | ||
| closeableWriter.addAll(dataFiles); | ||
|
|
@@ -63,4 +66,53 @@ static ManifestOutputFileFactory createOutputFileFactory(Table table, String fli | |
| TableOperations ops = ((HasTableOperations) table).operations(); | ||
| return new ManifestOutputFileFactory(ops, table.io(), table.properties(), flinkJobId, subTaskId, attemptNumber); | ||
| } | ||
|
|
||
| static DeltaManifests writeCompletedFiles(WriteResult result, | ||
|
Contributor
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. just for my own education,
Contributor
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. We should serialize it and add it to the commit. This is the set of files that is referenced by any positional delete, which identifies deleted rows by file and row position. The commit will validate that all of the files still exist in the table. This isn't strictly needed for this use case because we know that the position deletes only refer to files that are created in this commit. Since the files are being added in the commit, it isn't possible for some other process to delete some of them from metadata. But it is still good to configure the commit properly in case this gets reused later.
Member
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. Thanks for the explanation, @rdblue . I think it's correct to validate the data files in
Member
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 unit test addressed the data files validation issue |
||
| Supplier<OutputFile> outputFileSupplier, | ||
| PartitionSpec spec) throws IOException { | ||
|
|
||
| ManifestFile dataManifest = null; | ||
| ManifestFile deleteManifest = null; | ||
|
|
||
| // Write the completed data files into a newly created data manifest file. | ||
| if (result.dataFiles() != null && result.dataFiles().length > 0) { | ||
| dataManifest = writeDataFiles(outputFileSupplier.get(), spec, Lists.newArrayList(result.dataFiles())); | ||
| } | ||
|
|
||
| // Write the completed delete files into a newly created delete manifest file. | ||
| if (result.deleteFiles() != null && result.deleteFiles().length > 0) { | ||
| OutputFile deleteManifestFile = outputFileSupplier.get(); | ||
|
|
||
| ManifestWriter<DeleteFile> deleteManifestWriter = ManifestFiles.writeDeleteManifest(FORMAT_V2, spec, | ||
| deleteManifestFile, DUMMY_SNAPSHOT_ID); | ||
| try (ManifestWriter<DeleteFile> writer = deleteManifestWriter) { | ||
| for (DeleteFile deleteFile : result.deleteFiles()) { | ||
| writer.add(deleteFile); | ||
| } | ||
| } | ||
|
|
||
| deleteManifest = deleteManifestWriter.toManifestFile(); | ||
| } | ||
|
|
||
| return new DeltaManifests(dataManifest, deleteManifest); | ||
|
Contributor
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. do we need to check if WriteResult is empty (no data and delete files)?
Member
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. We have a similar discussion here. Even if the
Member
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. About this question, I think we'd better to keep the dummy |
||
| } | ||
|
|
||
| static WriteResult readCompletedFiles(DeltaManifests deltaManifests, FileIO io) throws IOException { | ||
| WriteResult.Builder builder = WriteResult.builder(); | ||
|
|
||
| // Read the completed data files from persisted data manifest file. | ||
| if (deltaManifests.dataManifest() != null) { | ||
| builder.addDataFiles(readDataFiles(deltaManifests.dataManifest(), io)); | ||
| } | ||
|
|
||
| // Read the completed delete files from persisted delete manifests file. | ||
| if (deltaManifests.deleteManifest() != null) { | ||
| try (CloseableIterable<DeleteFile> deleteFiles = ManifestFiles | ||
| .readDeleteManifest(deltaManifests.deleteManifest(), io, null)) { | ||
| builder.addDeleteFiles(deleteFiles); | ||
| } | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
| } | ||
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.
Typically, we would follow the Java collection convention and use
addAll.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.
OK, rename it to
addAllsound great to me.