Skip to content

Commit 1b33804

Browse files
committed
fixup
1 parent e44fede commit 1b33804

4 files changed

Lines changed: 251 additions & 8 deletions

File tree

‎amoro-format-lance/pom.xml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@
114114
<groupId>org.apache.iceberg</groupId>
115115
<artifactId>iceberg-aliyun</artifactId>
116116
</dependency>
117+
118+
<!-- HMSMockServer and the derby schema used by the hive3 namespace tests -->
119+
<dependency>
120+
<groupId>org.apache.amoro</groupId>
121+
<artifactId>amoro-common</artifactId>
122+
<version>${project.version}</version>
123+
<type>test-jar</type>
124+
<scope>test</scope>
125+
</dependency>
126+
<dependency>
127+
<groupId>org.apache.derby</groupId>
128+
<artifactId>derby</artifactId>
129+
<scope>test</scope>
130+
</dependency>
131+
<!-- arrow-memory-netty brings netty-buffer 4.1.129 while hive3 pulls netty-common
132+
4.1.119; align them on the test classpath only. -->
133+
<dependency>
134+
<groupId>io.netty</groupId>
135+
<artifactId>netty-common</artifactId>
136+
<version>${netty.version}</version>
137+
<scope>test</scope>
138+
</dependency>
117139
</dependencies>
118140

119141
</project>

‎amoro-format-lance/src/main/java/org/apache/amoro/formats/lance/AbstractLanceCatalog.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ public void createDatabase(String database) {
229229
@Override
230230
public void dropDatabase(String database) {
231231
validateDatabase(database);
232-
namespace.dropNamespace(new DropNamespaceRequest().id(namespaceId(database)));
232+
try {
233+
namespace.dropNamespace(new DropNamespaceRequest().id(namespaceId(database)));
234+
} catch (InvalidInputException e) {
235+
throw new IllegalStateException("Failed to drop database: " + database, e);
236+
}
233237
}
234238
}

‎amoro-format-lance/src/main/java/org/apache/amoro/formats/lance/LanceHiveCatalog.java‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@
3636
import org.lance.namespace.hive3.Hive3Namespace;
3737
import org.lance.namespace.hive3.Hive3NamespaceConfig;
3838
import org.lance.namespace.model.DeregisterTableRequest;
39+
import org.lance.namespace.model.DeregisterTableResponse;
3940
import org.lance.namespace.model.DescribeTableRequest;
4041
import org.lance.namespace.model.DescribeTableResponse;
41-
import org.lance.namespace.model.DropTableRequest;
4242

4343
import java.util.HashMap;
4444
import java.util.Locale;
4545
import java.util.Map;
4646

4747
/** Lance catalog backed by a Hive metastore; metadata is delegated to {@link Hive3Namespace}. */
4848
public class LanceHiveCatalog extends AbstractLanceCatalog {
49-
5049
private static final String HIVE_METASTORE_URIS = "hive.metastore.uris";
5150

5251
/** Escape hatch for storage options Amoro does not model, e.g. MinIO's aws_allow_http. */
@@ -73,13 +72,21 @@ protected AmoroTable<?> openTable(String database, String table) {
7372
response.getProperties());
7473
}
7574

76-
/** Purge deletes the table data; otherwise only the metastore entry is deregistered. */
7775
@Override
7876
protected void dropInNamespace(String database, String table, boolean purge) {
79-
if (purge) {
80-
namespace.dropTable(new DropTableRequest().id(tableId(database, table)));
81-
} else {
82-
namespace.deregisterTable(new DeregisterTableRequest().id(tableId(database, table)));
77+
// delete the table metadata.
78+
DeregisterTableResponse response =
79+
namespace.deregisterTable(new DeregisterTableRequest().id(tableId(database, table)));
80+
if (!purge) {
81+
return;
82+
}
83+
84+
// delete the table data.
85+
String location = response.getLocation();
86+
try {
87+
Dataset.drop(location, storageOptions(catalogProperties, location));
88+
} catch (Exception e) {
89+
throw new IllegalStateException("Failed to purge Lance data at " + location, e);
8390
}
8491
}
8592

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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 Schema SCHEMA =
63+
new Schema(
64+
Collections.singletonList(
65+
new Field("id", FieldType.nullable(new ArrowType.Int(32, true)), null)));
66+
67+
private static final String S3_ENDPOINT = "http://127.0.0.1:1";
68+
private static HMSMockServer hms;
69+
private static RootAllocator allocator;
70+
private static LanceHiveCatalog catalog;
71+
private String database;
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

Comments
 (0)