|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.plugin.iceberg.catalog.file; |
| 15 | + |
| 16 | +import io.trino.Session; |
| 17 | +import io.trino.filesystem.local.LocalFileSystemFactory; |
| 18 | +import io.trino.metastore.HiveMetastore; |
| 19 | +import io.trino.metastore.PrincipalPrivileges; |
| 20 | +import io.trino.metastore.Table; |
| 21 | +import io.trino.plugin.hive.NodeVersion; |
| 22 | +import io.trino.plugin.hive.metastore.HiveMetastoreConfig; |
| 23 | +import io.trino.plugin.hive.metastore.file.FileHiveMetastore; |
| 24 | +import io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig; |
| 25 | +import io.trino.plugin.iceberg.TestingIcebergPlugin; |
| 26 | +import io.trino.spi.connector.SchemaNotFoundException; |
| 27 | +import io.trino.testing.AbstractTestQueryFramework; |
| 28 | +import io.trino.testing.DistributedQueryRunner; |
| 29 | +import org.junit.jupiter.api.AfterAll; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.TestInstance; |
| 32 | +import org.junit.jupiter.api.parallel.Execution; |
| 33 | + |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.util.Optional; |
| 37 | +import java.util.concurrent.atomic.AtomicReference; |
| 38 | + |
| 39 | +import static com.google.common.io.MoreFiles.deleteRecursively; |
| 40 | +import static com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE; |
| 41 | +import static io.trino.testing.TestingNames.randomNameSuffix; |
| 42 | +import static io.trino.testing.TestingSession.testSessionBuilder; |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 45 | +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; |
| 46 | +import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD; |
| 47 | + |
| 48 | +@TestInstance(PER_CLASS) |
| 49 | +@Execution(SAME_THREAD) |
| 50 | +public class TestIcebergFileMetastoreCreateTableFailure |
| 51 | + extends AbstractTestQueryFramework |
| 52 | +{ |
| 53 | + private static final String ICEBERG_CATALOG = "iceberg"; |
| 54 | + private static final String SCHEMA_NAME = "test_schema"; |
| 55 | + |
| 56 | + private Path dataDirectory; |
| 57 | + private HiveMetastore metastore; |
| 58 | + private final AtomicReference<RuntimeException> testException = new AtomicReference<>(); |
| 59 | + |
| 60 | + @Override |
| 61 | + protected DistributedQueryRunner createQueryRunner() |
| 62 | + throws Exception |
| 63 | + { |
| 64 | + this.dataDirectory = Files.createTempDirectory("test_iceberg_create_table_failure"); |
| 65 | + // Using FileHiveMetastore as approximation of HMS |
| 66 | + this.metastore = new FileHiveMetastore( |
| 67 | + new NodeVersion("testversion"), |
| 68 | + new LocalFileSystemFactory(Path.of(dataDirectory.toString())), |
| 69 | + new HiveMetastoreConfig().isHideDeltaLakeTables(), |
| 70 | + new FileHiveMetastoreConfig() |
| 71 | + .setCatalogDirectory("local://")) |
| 72 | + { |
| 73 | + @Override |
| 74 | + public synchronized void createTable(Table table, PrincipalPrivileges principalPrivileges) |
| 75 | + { |
| 76 | + if (testException.get() != null) { |
| 77 | + throw testException.get(); |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + Session session = testSessionBuilder() |
| 83 | + .setCatalog(ICEBERG_CATALOG) |
| 84 | + .setSchema(SCHEMA_NAME) |
| 85 | + .build(); |
| 86 | + |
| 87 | + DistributedQueryRunner queryRunner = DistributedQueryRunner.builder(session).build(); |
| 88 | + queryRunner.installPlugin(new TestingIcebergPlugin(Path.of(dataDirectory.toString()), Optional.of(new TestingIcebergFileMetastoreCatalogModule(metastore)))); |
| 89 | + queryRunner.createCatalog(ICEBERG_CATALOG, "iceberg"); |
| 90 | + queryRunner.execute("CREATE SCHEMA " + SCHEMA_NAME); |
| 91 | + |
| 92 | + return queryRunner; |
| 93 | + } |
| 94 | + |
| 95 | + @AfterAll |
| 96 | + public void cleanup() |
| 97 | + throws Exception |
| 98 | + { |
| 99 | + if (metastore != null) { |
| 100 | + metastore.dropDatabase(SCHEMA_NAME, true); |
| 101 | + } |
| 102 | + if (dataDirectory != null) { |
| 103 | + deleteRecursively(dataDirectory, ALLOW_INSECURE); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testCreateTableFailureMetadataCleanedUp() |
| 109 | + { |
| 110 | + testException.set(new SchemaNotFoundException("simulated_test_schema")); |
| 111 | + String tableName = "test_create_failure_" + randomNameSuffix(); |
| 112 | + String tableLocation = "local:///" + tableName; |
| 113 | + String createTableSql = "CREATE TABLE " + tableName + " (a varchar) WITH (location = '" + tableLocation + "')"; |
| 114 | + assertThatThrownBy(() -> getQueryRunner().execute(createTableSql)) |
| 115 | + .hasMessageContaining("Schema simulated_test_schema not found"); |
| 116 | + |
| 117 | + Path metadataDirectory = dataDirectory.resolve(tableName, "metadata"); |
| 118 | + assertThat(metadataDirectory).as("Metadata file should not exist").isEmptyDirectory(); |
| 119 | + |
| 120 | + // it should be possible to create a table with the same name after the failure |
| 121 | + testException.set(null); |
| 122 | + getQueryRunner().execute(createTableSql); |
| 123 | + assertThat(metadataDirectory).as("Metadata file should not exist").isNotEmptyDirectory(); |
| 124 | + } |
| 125 | +} |
0 commit comments