-
Notifications
You must be signed in to change notification settings - Fork 3k
Add Support for Kafka Connect commit validation #14506
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
33 changes: 33 additions & 0 deletions
33
core/src/main/java/org/apache/iceberg/SupportsCommitValidation.java
This file contains hidden or 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,33 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.util.function.BiConsumer; | ||
|
|
||
| public interface SupportsCommitValidation { | ||
| CommitValidator NON_VALIDATING = (base, metadata) -> {}; | ||
|
|
||
| @FunctionalInterface | ||
| interface CommitValidator extends BiConsumer<TableMetadata, TableMetadata> { | ||
| @Override | ||
| void accept(TableMetadata snapshot, TableMetadata snapshot2); | ||
| } | ||
|
|
||
| void commit(CommitValidator validator); | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -38,9 +38,13 @@ | |
| import org.apache.iceberg.ContentFile; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.DeleteFile; | ||
| import org.apache.iceberg.PendingUpdate; | ||
| import org.apache.iceberg.RowDelta; | ||
| import org.apache.iceberg.Snapshot; | ||
| import org.apache.iceberg.SupportsCommitValidation; | ||
| import org.apache.iceberg.SupportsCommitValidation.CommitValidator; | ||
| import org.apache.iceberg.Table; | ||
| import org.apache.iceberg.TableMetadata; | ||
| import org.apache.iceberg.catalog.Catalog; | ||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
| import org.apache.iceberg.connect.IcebergSinkConfig; | ||
|
|
@@ -50,6 +54,7 @@ | |
| import org.apache.iceberg.connect.events.Event; | ||
| import org.apache.iceberg.connect.events.StartCommit; | ||
| import org.apache.iceberg.connect.events.TableReference; | ||
| import org.apache.iceberg.exceptions.CommitFailedException; | ||
| import org.apache.iceberg.exceptions.NoSuchTableException; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
|
|
@@ -251,7 +256,7 @@ private void commitToTable( | |
| appendOp.set(VALID_THROUGH_TS_SNAPSHOT_PROP, validThroughTs.toString()); | ||
| } | ||
| dataFiles.forEach(appendOp::appendFile); | ||
| appendOp.commit(); | ||
| validateAndCommit(appendOp, branch, committedOffsets); | ||
| } else { | ||
| RowDelta deltaOp = table.newRowDelta(); | ||
| if (branch != null) { | ||
|
|
@@ -264,7 +269,7 @@ private void commitToTable( | |
| } | ||
| dataFiles.forEach(deltaOp::addRows); | ||
| deleteFiles.forEach(deltaOp::addDeletes); | ||
| deltaOp.commit(); | ||
| validateAndCommit(deltaOp, branch, committedOffsets); | ||
| } | ||
|
|
||
| Long snapshotId = latestSnapshot(table, branch).snapshotId(); | ||
|
|
@@ -284,6 +289,29 @@ private void commitToTable( | |
| } | ||
| } | ||
|
|
||
| private void validateAndCommit( | ||
| PendingUpdate<?> pendingUpdate, String branch, Map<Integer, Long> expectedOffsets) { | ||
| CommitValidator validator = | ||
| (base, metadata) -> { | ||
| Map<Integer, Long> lastCommittedOffsets = lastCommittedOffsetsForTable(base, branch); | ||
|
|
||
| if (expectedOffsets == null || expectedOffsets.isEmpty()) { | ||
| return; // there are no stored offsets, so assume we're starting with new offsets | ||
| } | ||
|
|
||
| if (!expectedOffsets.equals(lastCommittedOffsets)) { | ||
| throw new CommitFailedException( | ||
| "Latest offsets do not match expected offsets for this commit."); | ||
| } | ||
| }; | ||
|
|
||
| if (pendingUpdate instanceof SupportsCommitValidation) { | ||
| ((SupportsCommitValidation) pendingUpdate).commit(validator); | ||
| } else { | ||
| pendingUpdate.commit(); | ||
| } | ||
| } | ||
|
|
||
| private <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { | ||
| Map<Object, Boolean> seen = Maps.newConcurrentMap(); | ||
| return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; | ||
|
|
@@ -296,6 +324,13 @@ private Snapshot latestSnapshot(Table table, String branch) { | |
| return table.snapshot(branch); | ||
| } | ||
|
|
||
| private Snapshot latestSnapshot(TableMetadata metadata, String branch) { | ||
| if (branch == null) { | ||
| return metadata.currentSnapshot(); | ||
| } | ||
| return metadata.snapshot(metadata.ref(branch).snapshotId()); | ||
| } | ||
|
Comment on lines
+327
to
+332
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. probably we can move this to |
||
|
|
||
| private Map<Integer, Long> lastCommittedOffsetsForTable(Table table, String branch) { | ||
| Snapshot snapshot = latestSnapshot(table, branch); | ||
| while (snapshot != null) { | ||
|
|
@@ -315,6 +350,25 @@ private Map<Integer, Long> lastCommittedOffsetsForTable(Table table, String bran | |
| return ImmutableMap.of(); | ||
| } | ||
|
|
||
| private Map<Integer, Long> lastCommittedOffsetsForTable(TableMetadata metadata, String branch) { | ||
| Snapshot snapshot = latestSnapshot(metadata, branch); | ||
| while (snapshot != null) { | ||
|
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. [optional] we can get an iterable of ancestors from the SnapshotUtils |
||
| Map<String, String> summary = snapshot.summary(); | ||
| String value = summary.get(snapshotOffsetsProp); | ||
| if (value != null) { | ||
| TypeReference<Map<Integer, Long>> typeRef = new TypeReference<Map<Integer, Long>>() {}; | ||
| try { | ||
| return MAPPER.readValue(value, typeRef); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
| Long parentSnapshotId = snapshot.parentId(); | ||
| snapshot = parentSnapshotId != null ? metadata.snapshot(parentSnapshotId) : null; | ||
| } | ||
| return ImmutableMap.of(); | ||
| } | ||
|
|
||
| void terminate() { | ||
| this.terminated = true; | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[doubt] wouldn't this be lastCommittedOffset ?
if expectedOffset is null and lastCommittedOffset is non empty then we should fail ?
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 debated this, but I'm not sure what the expected behavior should be in that case. It seems likely that we want to error in that case as well, but the scenario around it is less clear to me.