-
Notifications
You must be signed in to change notification settings - Fork 3k
support adding extra commit metadata with SQL in Spark #4956
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
807cbd1
thread local
CodingCat df42e42
temp
CodingCat 54cea12
test
CodingCat 22cc18a
formatting
CodingCat 8ee624c
test
CodingCat dcb888e
move to spark-core
CodingCat bce1188
address comments
CodingCat 0191a77
address comments
CodingCat aadf104
parameterize exception classes
CodingCat c11ee4f
stylistic fix
CodingCat 3e0c364
bit
CodingCat a5f5512
remove unused imports
CodingCat a3d7445
remove file
CodingCat e1cd056
3.0 and 3.1
CodingCat 9b94da4
fix building issues
CodingCat 023de70
redo some changes
CodingCat c89a49c
fix tests
CodingCat cd4147f
addr comments
CodingCat 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
61 changes: 61 additions & 0 deletions
61
spark/v3.0/spark/src/main/java/org/apache/iceberg/spark/CommitMetadata.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,61 @@ | ||
| /* | ||
| * 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.spark; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.Callable; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.util.ExceptionUtil; | ||
|
|
||
| /** | ||
| * utility class to accept thread local commit properties | ||
| */ | ||
| public class CommitMetadata { | ||
|
|
||
| private CommitMetadata() { | ||
|
|
||
| } | ||
|
|
||
| private static final ThreadLocal<Map<String, String>> COMMIT_PROPERTIES = ThreadLocal.withInitial(ImmutableMap::of); | ||
|
|
||
| /** | ||
| * running the code wrapped as a caller, and any snapshot committed within the callable object will be attached with | ||
| * the metadata defined in properties | ||
| * @param properties extra commit metadata to attach to the snapshot committed within callable | ||
| * @param callable the code to be executed | ||
| * @param exClass the expected type of exception which would be thrown from callable | ||
| */ | ||
| public static <R, E extends Exception> R withCommitProperties( | ||
| Map<String, String> properties, Callable<R> callable, Class<E> exClass) throws E { | ||
| COMMIT_PROPERTIES.set(properties); | ||
| try { | ||
| return callable.call(); | ||
| } catch (Throwable e) { | ||
| ExceptionUtil.castAndThrow(e, exClass); | ||
| return null; | ||
| } finally { | ||
| COMMIT_PROPERTIES.set(ImmutableMap.of()); | ||
| } | ||
| } | ||
|
|
||
| public static Map<String, String> commitProperties() { | ||
| return COMMIT_PROPERTIES.get(); | ||
| } | ||
| } |
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
61 changes: 61 additions & 0 deletions
61
spark/v3.1/spark/src/main/java/org/apache/iceberg/spark/CommitMetadata.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,61 @@ | ||
| /* | ||
| * 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.spark; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.Callable; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.util.ExceptionUtil; | ||
|
|
||
| /** | ||
| * utility class to accept thread local commit properties | ||
| */ | ||
| public class CommitMetadata { | ||
|
|
||
| private CommitMetadata() { | ||
|
|
||
| } | ||
|
|
||
| private static final ThreadLocal<Map<String, String>> COMMIT_PROPERTIES = ThreadLocal.withInitial(ImmutableMap::of); | ||
|
|
||
| /** | ||
| * running the code wrapped as a caller, and any snapshot committed within the callable object will be attached with | ||
| * the metadata defined in properties | ||
| * @param properties extra commit metadata to attach to the snapshot committed within callable | ||
| * @param callable the code to be executed | ||
| * @param exClass the expected type of exception which would be thrown from callable | ||
| */ | ||
| public static <R, E extends Exception> R withCommitProperties( | ||
| Map<String, String> properties, Callable<R> callable, Class<E> exClass) throws E { | ||
| COMMIT_PROPERTIES.set(properties); | ||
| try { | ||
| return callable.call(); | ||
| } catch (Throwable e) { | ||
| ExceptionUtil.castAndThrow(e, exClass); | ||
| return null; | ||
| } finally { | ||
| COMMIT_PROPERTIES.set(ImmutableMap.of()); | ||
| } | ||
| } | ||
|
|
||
| public static Map<String, String> commitProperties() { | ||
| return COMMIT_PROPERTIES.get(); | ||
| } | ||
| } |
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
61 changes: 61 additions & 0 deletions
61
spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/CommitMetadata.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,61 @@ | ||
| /* | ||
| * 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.spark; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.Callable; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.util.ExceptionUtil; | ||
|
|
||
| /** | ||
| * utility class to accept thread local commit properties | ||
| */ | ||
| public class CommitMetadata { | ||
CodingCat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private CommitMetadata() { | ||
|
|
||
| } | ||
|
|
||
| private static final ThreadLocal<Map<String, String>> COMMIT_PROPERTIES = ThreadLocal.withInitial(ImmutableMap::of); | ||
|
|
||
| /** | ||
| * running the code wrapped as a caller, and any snapshot committed within the callable object will be attached with | ||
| * the metadata defined in properties | ||
| * @param properties extra commit metadata to attach to the snapshot committed within callable | ||
| * @param callable the code to be executed | ||
| * @param exClass the expected type of exception which would be thrown from callable | ||
| */ | ||
| public static <R, E extends Exception> R withCommitProperties( | ||
| Map<String, String> properties, Callable<R> callable, Class<E> exClass) throws E { | ||
| COMMIT_PROPERTIES.set(properties); | ||
| try { | ||
| return callable.call(); | ||
| } catch (Throwable e) { | ||
| ExceptionUtil.castAndThrow(e, exClass); | ||
| return null; | ||
| } finally { | ||
| COMMIT_PROPERTIES.set(ImmutableMap.of()); | ||
| } | ||
| } | ||
|
|
||
| public static Map<String, String> commitProperties() { | ||
| return COMMIT_PROPERTIES.get(); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Does this class need to be
public? Could it be made package-private?I have concerns around the usage of
ThreadLocalfor things that most cases don't need to be thread local. I don't want to give users too much room to hurt themselves because they don't consider thatCommitMetadatais only threadlocal and then their writes not working properly in the common case of writes without user-side multithreading (e.g. it gets set in one thread somewhere, but another thread is used for commit).EDIT - Since this takes a
Callable, it's less of a concern. I would still name it in a way that's a bit more reflective of the thread local nature (especially if we wanted aCommitMetadataclass one day that doens't require a callable and is persistent). That and I always prefer things be package-private if possible.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.
sure, changed to CallerWithCommitMetadata....but...eh...not sure if it is better or worse....
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.
Yes, this does need to be public because it is a way for Iceberg users to pass metadata.