-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Core: cleanExpiredMetadata cleans unreferenced encryption keys #18102
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
base: main
Are you sure you want to change the base?
Changes from all commits
66dbf73
b888588
5f84b4c
01568c1
a9e4148
f052872
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 |
|---|---|---|
|
|
@@ -38,10 +38,12 @@ | |
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.encryption.EncryptedKey; | ||
| import org.apache.iceberg.exceptions.CommitFailedException; | ||
| import org.apache.iceberg.exceptions.ValidationException; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
|
@@ -241,6 +243,11 @@ private TableMetadata internalApply() { | |
| reachableSpecs.add(base.defaultSpecId()); | ||
| Set<Integer> reachableSchemas = Sets.newConcurrentHashSet(); | ||
| reachableSchemas.add(base.currentSchemaId()); | ||
| Set<String> reachableKeyIds = | ||
| base.encryptionKeys().stream() | ||
| .map(EncryptedKey::encryptedById) | ||
| .filter(Objects::nonNull) | ||
| .collect(Collectors.toCollection(Sets::newConcurrentHashSet)); | ||
|
|
||
| boolean mayHaveExpiredSpecs = base.specs().size() > 1; | ||
|
|
||
|
|
@@ -255,6 +262,9 @@ private TableMetadata internalApply() { | |
| .forEach(reachableSpecs::add); | ||
| } | ||
| reachableSchemas.add(snapshot.schemaId()); | ||
| if (snapshot.keyId() != null) { | ||
| reachableKeyIds.add(snapshot.keyId()); | ||
| } | ||
| }); | ||
|
|
||
| Set<Integer> specsToRemove = | ||
|
|
@@ -270,6 +280,13 @@ private TableMetadata internalApply() { | |
| .filter(schemaId -> !reachableSchemas.contains(schemaId)) | ||
| .collect(Collectors.toSet()); | ||
| updatedMetaBuilder.removeSchemas(schemasToRemove); | ||
|
|
||
| Set<String> encryptionKeysToRemove = | ||
| base.encryptionKeys().stream() | ||
| .map(EncryptedKey::keyId) | ||
| .filter(keyId -> !reachableKeyIds.contains(keyId)) | ||
| .collect(Collectors.toSet()); | ||
| encryptionKeysToRemove.forEach(updatedMetaBuilder::removeEncryptionKey); | ||
|
Comment on lines
+283
to
+289
Contributor
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. In a similar line to: #12670 I see 2 arguments to introduce/make RemoveEncryptionKey -> RemoveEncryptionKeys. (Bulk). Performance: We've ran into performance issues server side when expiring 150k+ snapshots in a non-bulk way. Bulking the changes fixed it. Although I don't expect RemoveEncryptionKey to be as expensive of a call as RemoveSnapshot. So perhaps a premature optimisation. |
||
| } | ||
|
|
||
| return updatedMetaBuilder.build(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,13 @@ | |
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.CatalogProperties; | ||
| import org.apache.iceberg.ManifestListFile; | ||
| import org.apache.iceberg.Snapshot; | ||
| import org.apache.iceberg.TableMetadata; | ||
| import org.apache.iceberg.TableProperties; | ||
| import org.apache.iceberg.common.DynConstructors; | ||
| import org.apache.iceberg.io.OutputFile; | ||
|
|
@@ -181,6 +185,32 @@ public static Map<String, EncryptedKey> encryptionKeys(EncryptionManager em) { | |
| return sem.encryptionKeys(); | ||
| } | ||
|
|
||
| /** Adds referenced encryption keys and the current key encryption key to table metadata. */ | ||
| public static TableMetadata addEmKeysToMetadata( | ||
| TableMetadata metadata, EncryptionManager encryptionManager) { | ||
| if (!(encryptionManager instanceof StandardEncryptionManager standardEncryptionManager)) { | ||
| return metadata; | ||
| } | ||
|
|
||
| Set<String> referencedKeyIds = | ||
| Sets.union( | ||
| metadata.snapshots().stream() | ||
| .map(Snapshot::keyId) | ||
| .filter(Objects::nonNull) | ||
| .collect(Collectors.toUnmodifiableSet()), | ||
| metadata.encryptionKeys().stream() | ||
| .map(EncryptedKey::encryptedById) | ||
| .filter(Objects::nonNull) | ||
| .collect(Collectors.toUnmodifiableSet())); | ||
| String keyEncryptionKeyId = standardEncryptionManager.keyEncryptionKeyID(); | ||
| TableMetadata.Builder builder = TableMetadata.buildFrom(metadata); | ||
| standardEncryptionManager.encryptionKeys().values().stream() | ||
| .filter( | ||
| key -> referencedKeyIds.contains(key.keyId()) || keyEncryptionKeyId.equals(key.keyId())) | ||
| .forEach(builder::addEncryptionKey); | ||
| return builder.build(); | ||
| } | ||
|
Contributor
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. Need this in This method only adds the encryption keys that are still referenced within the given metadata. |
||
|
|
||
| /** | ||
| * Encrypts the key metadata for a manifest list. | ||
| * | ||
|
|
||
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.
For my own understanding. Couldn't figure out why encryptedById is nullable. In what cases do we expect this to be null?