-
Notifications
You must be signed in to change notification settings - Fork 3k
AWS: Glue catalog lock interface #1823
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 all commits
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 |
|---|---|---|
|
|
@@ -57,16 +57,20 @@ class GlueTableOperations extends BaseMetastoreTableOperations { | |
| private final String databaseName; | ||
| private final String tableName; | ||
| private final String fullTableName; | ||
| private final String commitLockEntityId; | ||
| private final FileIO fileIO; | ||
| private final LockManager lockManager; | ||
|
|
||
| GlueTableOperations(GlueClient glue, String catalogName, AwsProperties awsProperties, | ||
| GlueTableOperations(GlueClient glue, LockManager lockManager, String catalogName, AwsProperties awsProperties, | ||
| FileIO fileIO, TableIdentifier tableIdentifier) { | ||
| this.glue = glue; | ||
| this.awsProperties = awsProperties; | ||
| this.databaseName = IcebergToGlueConverter.getDatabaseName(tableIdentifier); | ||
| this.tableName = IcebergToGlueConverter.getTableName(tableIdentifier); | ||
| this.fullTableName = String.format("%s.%s.%s", catalogName, databaseName, tableName); | ||
| this.commitLockEntityId = String.format("%s.%s", databaseName, tableName); | ||
| this.fileIO = fileIO; | ||
| this.lockManager = lockManager; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -100,10 +104,11 @@ protected void doRefresh() { | |
| protected void doCommit(TableMetadata base, TableMetadata metadata) { | ||
| String newMetadataLocation = writeNewMetadata(metadata, currentVersion() + 1); | ||
| boolean exceptionThrown = true; | ||
| Table glueTable = getGlueTable(); | ||
| checkMetadataLocation(glueTable, base); | ||
| Map<String, String> properties = prepareProperties(glueTable, newMetadataLocation); | ||
| try { | ||
| lock(newMetadataLocation); | ||
|
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. Because this locks in the I think the solution is to not throw exceptions in release. I'll comment on that below. |
||
| Table glueTable = getGlueTable(); | ||
| checkMetadataLocation(glueTable, base); | ||
| Map<String, String> properties = prepareProperties(glueTable, newMetadataLocation); | ||
| persistGlueTable(glueTable, properties); | ||
| exceptionThrown = false; | ||
| } catch (ConcurrentModificationException e) { | ||
|
|
@@ -114,9 +119,14 @@ protected void doCommit(TableMetadata base, TableMetadata metadata) { | |
| } catch (SdkException e) { | ||
| throw new CommitFailedException(e, "Cannot commit %s because unexpected exception contacting AWS", tableName()); | ||
| } finally { | ||
| if (exceptionThrown) { | ||
| io().deleteFile(newMetadataLocation); | ||
| } | ||
| cleanupMetadataAndUnlock(exceptionThrown, newMetadataLocation); | ||
| } | ||
| } | ||
|
|
||
| private void lock(String newMetadataLocation) { | ||
| if (!lockManager.acquire(commitLockEntityId, newMetadataLocation)) { | ||
| throw new IllegalStateException(String.format("Fail to acquire lock %s to commit new metadata at %s", | ||
| commitLockEntityId, newMetadataLocation)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -180,4 +190,18 @@ private void persistGlueTable(Table glueTable, Map<String, String> parameters) { | |
| .build()); | ||
| } | ||
| } | ||
|
|
||
| private void cleanupMetadataAndUnlock(boolean exceptionThrown, String metadataLocation) { | ||
| try { | ||
| if (exceptionThrown) { | ||
| // if anything went wrong, clean up the uncommitted metadata file | ||
| io().deleteFile(metadataLocation); | ||
| } | ||
| } catch (RuntimeException e) { | ||
| LOG.error("Fail to cleanup metadata file at {}", metadataLocation, e); | ||
| throw e; | ||
| } finally { | ||
| lockManager.release(commitLockEntityId, metadataLocation); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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.aws.glue; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * An interface for locking, used to ensure Glue catalog commit isolation. | ||
| */ | ||
| interface LockManager extends AutoCloseable { | ||
|
|
||
| /** | ||
| * Try to acquire a lock | ||
| * @param entityId ID of the entity to lock | ||
| * @param ownerId ID of the owner if the lock | ||
| * @return if the lock for the entity is acquired by the owner | ||
| */ | ||
| boolean acquire(String entityId, String ownerId); | ||
|
|
||
| /** | ||
| * Release a lock | ||
| * @param entityId ID of the entity to lock | ||
| * @param ownerId ID of the owner if the lock | ||
| * @return if the lock for the entity of the owner is released | ||
|
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. This isn't quite correct. It should return true if the lock was held and released, and false otherwise. This should also note that the contract requires not throwing exceptions from this method. |
||
| */ | ||
| boolean release(String entityId, String ownerId); | ||
|
|
||
| /** | ||
| * Initialize lock manager from catalog properties. | ||
| * @param properties catalog properties | ||
| */ | ||
| void initialize(Map<String, String> properties); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.