|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.amoro.formats.lance; |
| 20 | + |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import org.apache.amoro.hive.HMSMockServer; |
| 26 | +import org.apache.amoro.table.TableMetaStore; |
| 27 | +import org.apache.arrow.memory.RootAllocator; |
| 28 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 29 | +import org.apache.arrow.vector.types.pojo.Field; |
| 30 | +import org.apache.arrow.vector.types.pojo.FieldType; |
| 31 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 32 | +import org.apache.commons.io.FileUtils; |
| 33 | +import org.apache.hadoop.hive.conf.HiveConf; |
| 34 | +import org.apache.hadoop.hive.metastore.api.SerDeInfo; |
| 35 | +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; |
| 36 | +import org.apache.hadoop.hive.metastore.api.Table; |
| 37 | +import org.apache.iceberg.aws.s3.S3FileIOProperties; |
| 38 | +import org.junit.jupiter.api.AfterAll; |
| 39 | +import org.junit.jupiter.api.AfterEach; |
| 40 | +import org.junit.jupiter.api.Assertions; |
| 41 | +import org.junit.jupiter.api.BeforeAll; |
| 42 | +import org.junit.jupiter.api.BeforeEach; |
| 43 | +import org.junit.jupiter.api.Test; |
| 44 | +import org.lance.Dataset; |
| 45 | +import org.lance.namespace.LanceNamespace; |
| 46 | +import org.lance.namespace.hive3.Hive3NamespaceConfig; |
| 47 | +import org.lance.namespace.model.DeclareTableRequest; |
| 48 | +import org.lance.namespace.model.DeclareTableResponse; |
| 49 | +import org.lance.namespace.model.DeregisterTableRequest; |
| 50 | +import org.lance.namespace.model.DeregisterTableResponse; |
| 51 | + |
| 52 | +import java.io.File; |
| 53 | +import java.net.URI; |
| 54 | +import java.util.Arrays; |
| 55 | +import java.util.Collections; |
| 56 | +import java.util.HashMap; |
| 57 | +import java.util.Map; |
| 58 | +import java.util.UUID; |
| 59 | + |
| 60 | +public class TestLanceHiveCatalog { |
| 61 | + |
| 62 | + private static final String S3_ENDPOINT = "http://127.0.0.1:1"; |
| 63 | + private static HMSMockServer hms; |
| 64 | + private static RootAllocator allocator; |
| 65 | + private static LanceHiveCatalog catalog; |
| 66 | + private String database; |
| 67 | + |
| 68 | + private static final Schema SCHEMA = |
| 69 | + new Schema( |
| 70 | + Collections.singletonList( |
| 71 | + new Field("id", FieldType.nullable(new ArrowType.Int(32, true)), null))); |
| 72 | + |
| 73 | + /** Registers the table in HMS and writes a Lance dataset at the returned location. */ |
| 74 | + private String createTable(String table) { |
| 75 | + DeclareTableResponse response = |
| 76 | + catalog.namespace.declareTable( |
| 77 | + new DeclareTableRequest() |
| 78 | + .id(Arrays.asList(Hive3NamespaceConfig.CATALOG_NAME, database, table))); |
| 79 | + String location = response.getLocation(); |
| 80 | + Dataset.write().allocator(allocator).uri(location).schema(SCHEMA).execute().close(); |
| 81 | + return location; |
| 82 | + } |
| 83 | + |
| 84 | + private static boolean dataExists(String location) { |
| 85 | + return new File(URI.create(location)).exists(); |
| 86 | + } |
| 87 | + |
| 88 | + @BeforeAll |
| 89 | + public static void startMetastore() { |
| 90 | + hms = new HMSMockServer(new File("target/hive_warehouse")); |
| 91 | + hms.start(); |
| 92 | + allocator = new RootAllocator(Long.MAX_VALUE); |
| 93 | + Map<String, String> properties = new HashMap<>(); |
| 94 | + properties.put("warehouse", hms.getWareHouseLocation()); |
| 95 | + properties.put("uri", hms.hiveConf().get(HiveConf.ConfVars.METASTOREURIS.varname)); |
| 96 | + properties.put(S3FileIOProperties.ENDPOINT, S3_ENDPOINT); |
| 97 | + properties.put(S3FileIOProperties.ACCESS_KEY_ID, "test-key"); |
| 98 | + properties.put(S3FileIOProperties.SECRET_ACCESS_KEY, "test-secret"); |
| 99 | + catalog = new LanceHiveCatalog("lance_hive", properties, TableMetaStore.EMPTY); |
| 100 | + } |
| 101 | + |
| 102 | + @BeforeEach |
| 103 | + public void createDatabase() { |
| 104 | + database = "db_" + UUID.randomUUID().toString().replace("-", ""); |
| 105 | + catalog.createDatabase(database); |
| 106 | + } |
| 107 | + |
| 108 | + @AfterEach |
| 109 | + public void cleanUp() throws Exception { |
| 110 | + hms.getClient().dropDatabase(database, true, true, true); |
| 111 | + FileUtils.deleteQuietly(new File(hms.getDatabasePath(database))); |
| 112 | + } |
| 113 | + |
| 114 | + @AfterAll |
| 115 | + public static void stopMetastore() { |
| 116 | + if (allocator != null) { |
| 117 | + allocator.close(); |
| 118 | + } |
| 119 | + hms.stop(); |
| 120 | + FileUtils.deleteQuietly(new File("target/hive_warehouse")); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testDropTable() { |
| 125 | + // 1、purge=true removes both the metastore entry and the Lance dataset directory. |
| 126 | + String location = createTable("t1"); |
| 127 | + Assertions.assertTrue(catalog.dropTable(database, "t1", true)); |
| 128 | + Assertions.assertFalse(catalog.tableExists(database, "t1")); |
| 129 | + Assertions.assertFalse(dataExists(location)); |
| 130 | + // A second drop finds no metastore entry and reports false instead of failing. |
| 131 | + Assertions.assertFalse(catalog.dropTable(database, "t1", true)); |
| 132 | + |
| 133 | + // 2、purge=false only deregisters the metastore entry, so the table data must still be there. |
| 134 | + location = createTable("t1"); |
| 135 | + Assertions.assertTrue(catalog.dropTable(database, "t1", false)); |
| 136 | + Assertions.assertFalse(catalog.tableExists(database, "t1")); |
| 137 | + Assertions.assertTrue(dataExists(location)); |
| 138 | + } |
| 139 | + |
| 140 | + /** A failed data deletion must be reported with the location left behind. */ |
| 141 | + @Test |
| 142 | + public void testPurgeFailureIsReported() throws Exception { |
| 143 | + String location = createTable("t2"); |
| 144 | + // Replace the dataset directory with a file so Dataset.drop fails deterministically. |
| 145 | + FileUtils.deleteDirectory(new File(URI.create(location))); |
| 146 | + FileUtils.writeStringToFile(new File(URI.create(location)), "not a dataset", "UTF-8"); |
| 147 | + |
| 148 | + Exception error = |
| 149 | + Assertions.assertThrows(Exception.class, () -> catalog.dropTable(database, "t2", true)); |
| 150 | + // The entry is already dropped, so the message must carry the location to clean up. |
| 151 | + Assertions.assertTrue(error.getMessage().contains(location), error.getMessage()); |
| 152 | + Assertions.assertFalse(catalog.tableExists(database, "t2")); |
| 153 | + } |
| 154 | + |
| 155 | + /** Purge must reach the catalog-only endpoint instead of an empty option map. */ |
| 156 | + @Test |
| 157 | + public void testPurgeUsesCatalogStorageOptions() throws Exception { |
| 158 | + LanceNamespace stub = mock(LanceNamespace.class); |
| 159 | + when(stub.deregisterTable(any(DeregisterTableRequest.class))) |
| 160 | + .thenReturn(new DeregisterTableResponse().location("s3://bucket/t.lance")); |
| 161 | + java.lang.reflect.Field field = AbstractLanceCatalog.class.getDeclaredField("namespace"); |
| 162 | + field.setAccessible(true); |
| 163 | + Object original = field.get(catalog); |
| 164 | + field.set(catalog, stub); |
| 165 | + try { |
| 166 | + Exception error = |
| 167 | + Assertions.assertThrows( |
| 168 | + IllegalStateException.class, () -> catalog.dropTable(database, "t1", true)); |
| 169 | + // Only the catalog-configured endpoint can put this host into the S3 error. |
| 170 | + Assertions.assertTrue( |
| 171 | + error.getCause().getMessage().contains(S3_ENDPOINT), error.getCause().getMessage()); |
| 172 | + } finally { |
| 173 | + field.set(catalog, original); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testDropDatabase() { |
| 179 | + // drop empty database |
| 180 | + catalog.dropDatabase(database); |
| 181 | + Assertions.assertFalse(catalog.databaseExists(database)); |
| 182 | + |
| 183 | + // drop no empty database. |
| 184 | + catalog.createDatabase(database); |
| 185 | + String location = createTable("t1"); |
| 186 | + Assertions.assertThrows(IllegalStateException.class, () -> catalog.dropDatabase(database)); |
| 187 | + // A rejected drop must leave the database, its table and the data untouched. |
| 188 | + Assertions.assertTrue(catalog.databaseExists(database)); |
| 189 | + Assertions.assertTrue(catalog.tableExists(database, "t1")); |
| 190 | + Assertions.assertTrue(dataExists(location)); |
| 191 | + } |
| 192 | + |
| 193 | + /** A non-Lance table in the same database must be left untouched by purge. */ |
| 194 | + @Test |
| 195 | + public void testDropNonLanceTableReturnsFalse() throws Exception { |
| 196 | + Table table = new Table(); |
| 197 | + table.setDbName(database); |
| 198 | + table.setTableName("hive_tbl"); |
| 199 | + table.setTableType("EXTERNAL_TABLE"); |
| 200 | + StorageDescriptor sd = new StorageDescriptor(); |
| 201 | + sd.setLocation(hms.getTablePath(database, "hive_tbl")); |
| 202 | + sd.setCols(Collections.emptyList()); |
| 203 | + sd.setSerdeInfo(new SerDeInfo()); |
| 204 | + table.setSd(sd); |
| 205 | + hms.getClient().createTable(table); |
| 206 | + |
| 207 | + Assertions.assertFalse(catalog.dropTable(database, "hive_tbl", true)); |
| 208 | + Assertions.assertTrue(hms.getClient().tableExists(database, "hive_tbl")); |
| 209 | + } |
| 210 | +} |
0 commit comments