-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Cleanup metadata file when commitNewTable fails for the Iceberg table
#14869
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
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
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
132 changes: 132 additions & 0 deletions
132
...java/io/trino/plugin/iceberg/catalog/file/TestIcebergFileMetastoreCreateTableFailure.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,132 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.iceberg.catalog.file; | ||
|
|
||
| import io.trino.Session; | ||
| import io.trino.plugin.hive.NodeVersion; | ||
| import io.trino.plugin.hive.metastore.HiveMetastore; | ||
| import io.trino.plugin.hive.metastore.HiveMetastoreConfig; | ||
| import io.trino.plugin.hive.metastore.PrincipalPrivileges; | ||
| import io.trino.plugin.hive.metastore.Table; | ||
| import io.trino.plugin.hive.metastore.file.FileHiveMetastore; | ||
| import io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig; | ||
| import io.trino.plugin.iceberg.TestingIcebergPlugin; | ||
| import io.trino.spi.connector.SchemaNotFoundException; | ||
| import io.trino.testing.AbstractTestQueryFramework; | ||
| import io.trino.testing.DistributedQueryRunner; | ||
| import org.testng.annotations.AfterClass; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static com.google.common.io.MoreFiles.deleteRecursively; | ||
| import static com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE; | ||
| import static com.google.inject.util.Modules.EMPTY_MODULE; | ||
| import static io.trino.plugin.hive.HiveTestUtils.HDFS_ENVIRONMENT; | ||
| import static io.trino.testing.TestingNames.randomNameSuffix; | ||
| import static io.trino.testing.TestingSession.testSessionBuilder; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| @Test(singleThreaded = true) // testException is a shared mutable state | ||
| public class TestIcebergFileMetastoreCreateTableFailure | ||
| extends AbstractTestQueryFramework | ||
| { | ||
| private static final String ICEBERG_CATALOG = "iceberg"; | ||
| private static final String SCHEMA_NAME = "test_schema"; | ||
|
|
||
| private Path dataDirectory; | ||
| private HiveMetastore metastore; | ||
| private final AtomicReference<RuntimeException> testException = new AtomicReference<>(); | ||
|
|
||
| @Override | ||
| protected DistributedQueryRunner createQueryRunner() | ||
| throws Exception | ||
| { | ||
| this.dataDirectory = Files.createTempDirectory("test_iceberg_create_table_failure"); | ||
| // Using FileHiveMetastore as approximation of HMS | ||
| this.metastore = new FileHiveMetastore( | ||
| new NodeVersion("testversion"), | ||
| HDFS_ENVIRONMENT, | ||
| new HiveMetastoreConfig().isHideDeltaLakeTables(), | ||
| new FileHiveMetastoreConfig() | ||
| .setCatalogDirectory(dataDirectory.toString())) | ||
| { | ||
| @Override | ||
| public synchronized void createTable(Table table, PrincipalPrivileges principalPrivileges) | ||
| { | ||
| throw testException.get(); | ||
| } | ||
| }; | ||
|
|
||
| Session session = testSessionBuilder() | ||
| .setCatalog(ICEBERG_CATALOG) | ||
| .setSchema(SCHEMA_NAME) | ||
| .build(); | ||
|
|
||
| DistributedQueryRunner queryRunner = DistributedQueryRunner.builder(session).build(); | ||
| queryRunner.installPlugin(new TestingIcebergPlugin(Optional.of(new TestingIcebergFileMetastoreCatalogModule(metastore)), Optional.empty(), EMPTY_MODULE)); | ||
| queryRunner.createCatalog(ICEBERG_CATALOG, "iceberg"); | ||
| queryRunner.execute("CREATE SCHEMA " + SCHEMA_NAME); | ||
|
|
||
| return queryRunner; | ||
| } | ||
|
|
||
| @AfterClass(alwaysRun = true) | ||
| public void cleanup() | ||
| throws Exception | ||
| { | ||
| if (metastore != null) { | ||
| metastore.dropDatabase(SCHEMA_NAME, true); | ||
| } | ||
| if (dataDirectory != null) { | ||
| deleteRecursively(dataDirectory, ALLOW_INSECURE); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateTableFailureMetadataCleanedUp() | ||
| { | ||
| String exceptionMessage = "Test-simulated metastore schema not found exception"; | ||
| testException.set(new SchemaNotFoundException("simulated_test_schema", exceptionMessage)); | ||
| testCreateTableFailure(exceptionMessage, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateTableFailureMetadataNotCleanedUp() | ||
| { | ||
| String exceptionMessage = "Test-simulated metastore runtime exception"; | ||
| testException.set(new RuntimeException(exceptionMessage)); | ||
| testCreateTableFailure(exceptionMessage, true); | ||
| } | ||
|
|
||
| protected void testCreateTableFailure(String expectedExceptionMessage, boolean shouldMetadataFileExist) | ||
| { | ||
| String tableName = "test_create_failure_" + randomNameSuffix(); | ||
| String tableLocation = Path.of(dataDirectory.toString(), tableName).toString(); | ||
| assertThatThrownBy(() -> getQueryRunner().execute("CREATE TABLE " + tableName + " (a varchar) WITH (location = '" + tableLocation + "')")) | ||
| .hasMessageContaining(expectedExceptionMessage); | ||
|
|
||
| Path metadataDirectory = Path.of(tableLocation, "metadata"); | ||
| if (shouldMetadataFileExist) { | ||
| assertThat(metadataDirectory).as("Metadata file should exist").isDirectoryContaining("glob:**.metadata.json"); | ||
| } | ||
| else { | ||
| assertThat(metadataDirectory).as("Metadata file should not exist").isEmptyDirectory(); | ||
| } | ||
| } | ||
| } |
186 changes: 186 additions & 0 deletions
186
...src/test/java/io/trino/plugin/iceberg/catalog/glue/TestIcebergGlueCreateTableFailure.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,186 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.iceberg.catalog.glue; | ||
|
|
||
| import com.amazonaws.services.glue.AWSGlueAsync; | ||
| import com.amazonaws.services.glue.model.InvalidInputException; | ||
| import com.amazonaws.services.glue.model.OperationTimeoutException; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import io.airlift.log.Logger; | ||
| import io.trino.Session; | ||
| import io.trino.filesystem.FileEntry; | ||
| import io.trino.filesystem.FileIterator; | ||
| import io.trino.filesystem.TrinoFileSystem; | ||
| import io.trino.filesystem.hdfs.HdfsFileSystemFactory; | ||
| import io.trino.plugin.hive.metastore.Database; | ||
| import io.trino.plugin.hive.metastore.glue.GlueHiveMetastore; | ||
| import io.trino.plugin.iceberg.TestingIcebergConnectorFactory; | ||
| import io.trino.spi.security.PrincipalType; | ||
| import io.trino.testing.AbstractTestQueryFramework; | ||
| import io.trino.testing.LocalQueryRunner; | ||
| import io.trino.testing.TestingConnectorSession; | ||
| import org.testng.annotations.AfterClass; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static com.google.common.io.MoreFiles.deleteRecursively; | ||
| import static com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE; | ||
| import static com.google.common.reflect.Reflection.newProxy; | ||
| import static com.google.inject.util.Modules.EMPTY_MODULE; | ||
| import static io.trino.plugin.hive.HiveTestUtils.HDFS_ENVIRONMENT; | ||
| import static io.trino.plugin.hive.metastore.glue.GlueHiveMetastore.createTestingGlueHiveMetastore; | ||
| import static io.trino.testing.TestingNames.randomNameSuffix; | ||
| import static io.trino.testing.TestingSession.testSessionBuilder; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /* | ||
| * The test currently uses AWS Default Credential Provider Chain, | ||
| * See https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html#credentials-default | ||
| * on ways to set your AWS credentials which will be needed to run this test. | ||
| */ | ||
| @Test(singleThreaded = true) // testException is a shared mutable state | ||
| public class TestIcebergGlueCreateTableFailure | ||
| extends AbstractTestQueryFramework | ||
| { | ||
| private static final Logger LOG = Logger.get(TestIcebergGlueCreateTableFailure.class); | ||
|
|
||
| private static final String ICEBERG_CATALOG = "iceberg"; | ||
|
|
||
| private final String schemaName = "test_iceberg_glue_" + randomNameSuffix(); | ||
|
|
||
| private Path dataDirectory; | ||
| private TrinoFileSystem fileSystem; | ||
| private GlueHiveMetastore glueHiveMetastore; | ||
| private final AtomicReference<RuntimeException> testException = new AtomicReference<>(); | ||
|
|
||
| @Override | ||
| protected LocalQueryRunner createQueryRunner() | ||
| throws Exception | ||
| { | ||
| Session session = testSessionBuilder() | ||
| .setCatalog(ICEBERG_CATALOG) | ||
| .setSchema(schemaName) | ||
| .build(); | ||
| LocalQueryRunner queryRunner = LocalQueryRunner.create(session); | ||
|
|
||
| AWSGlueAsyncAdapterProvider awsGlueAsyncAdapterProvider = delegate -> newProxy(AWSGlueAsync.class, (proxy, method, methodArgs) -> { | ||
| Object result; | ||
| if (method.getName().equals("createTable")) { | ||
| throw testException.get(); | ||
| } | ||
| try { | ||
| result = method.invoke(delegate, methodArgs); | ||
| } | ||
| catch (InvocationTargetException e) { | ||
| throw e.getCause(); | ||
| } | ||
| return result; | ||
| }); | ||
|
|
||
| queryRunner.createCatalog( | ||
| ICEBERG_CATALOG, | ||
| new TestingIcebergConnectorFactory(Optional.of(new TestingIcebergGlueCatalogModule(awsGlueAsyncAdapterProvider)), Optional.empty(), EMPTY_MODULE), | ||
| ImmutableMap.of()); | ||
|
|
||
| dataDirectory = Files.createTempDirectory("test_iceberg_create_table_failure"); | ||
| dataDirectory.toFile().deleteOnExit(); | ||
|
|
||
| glueHiveMetastore = createTestingGlueHiveMetastore(dataDirectory.toString()); | ||
| fileSystem = new HdfsFileSystemFactory(HDFS_ENVIRONMENT).create(TestingConnectorSession.SESSION); | ||
|
|
||
| Database database = Database.builder() | ||
| .setDatabaseName(schemaName) | ||
| .setOwnerName(Optional.of("public")) | ||
| .setOwnerType(Optional.of(PrincipalType.ROLE)) | ||
| .setLocation(Optional.of(dataDirectory.toString())) | ||
| .build(); | ||
| glueHiveMetastore.createDatabase(database); | ||
|
|
||
| return queryRunner; | ||
| } | ||
|
|
||
| @AfterClass(alwaysRun = true) | ||
| public void cleanup() | ||
| throws IOException | ||
| { | ||
| try { | ||
| if (glueHiveMetastore != null) { | ||
| glueHiveMetastore.dropDatabase(schemaName, false); | ||
| } | ||
| if (dataDirectory != null) { | ||
| deleteRecursively(dataDirectory, ALLOW_INSECURE); | ||
| } | ||
| } | ||
| catch (Exception e) { | ||
| LOG.error(e, "Failed to clean up Glue database: %s", schemaName); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateTableFailureMetadataCleanedUp() | ||
| throws Exception | ||
| { | ||
| final String exceptionMessage = "Test-simulated metastore invalid input exception"; | ||
| testException.set(new InvalidInputException(exceptionMessage)); | ||
| testCreateTableFailure(exceptionMessage, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateTableFailureMetadataNotCleanedUp() | ||
| throws Exception | ||
| { | ||
| final String exceptionMessage = "Test-simulated metastore operation timeout exception"; | ||
| testException.set(new OperationTimeoutException(exceptionMessage)); | ||
| testCreateTableFailure(exceptionMessage, true); | ||
| } | ||
|
|
||
| private void testCreateTableFailure(String expectedExceptionMessage, boolean shouldMetadataFileExist) | ||
| throws Exception | ||
| { | ||
| String tableName = "test_create_failure_" + randomNameSuffix(); | ||
| assertThatThrownBy(() -> getQueryRunner().execute("CREATE TABLE " + tableName + " (a_varchar) AS VALUES ('Trino')")) | ||
| .hasMessageContaining(expectedExceptionMessage); | ||
|
|
||
| assertMetadataLocation(tableName, shouldMetadataFileExist); | ||
| } | ||
|
|
||
| protected void assertMetadataLocation(String tableName, boolean shouldMetadataFileExist) | ||
| throws Exception | ||
| { | ||
| FileIterator fileIterator = fileSystem.listFiles(dataDirectory.toString()); | ||
| String tableLocationPrefix = Path.of(dataDirectory.toString(), tableName).toString(); | ||
| boolean metadataFileFound = false; | ||
| while (fileIterator.hasNext()) { | ||
| FileEntry fileEntry = fileIterator.next(); | ||
| String path = fileEntry.path(); | ||
| if (path.startsWith(tableLocationPrefix) && path.endsWith(".metadata.json")) { | ||
| metadataFileFound = true; | ||
| break; | ||
| } | ||
| } | ||
| if (shouldMetadataFileExist) { | ||
| assertThat(metadataFileFound).as("Metadata file should exist").isTrue(); | ||
| } | ||
| else { | ||
| assertThat(metadataFileFound).as("Metadata file should not exist").isFalse(); | ||
| } | ||
| } | ||
| } |
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.