World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#14272] fix(lance): refuse catalog-level DropNamespace on non-lakehouse catalogs (#14273)
Nevin Zheng2ce6bdbb4680
diff --git a/lance/lance-common/src/main/java/org/apache/gravitino/lance/common/ops/gravitino/GravitinoLanceNameSpaceOperations.java b/lance/lance-common/src/main/java/org/apache/gravitino/lance/common/ops/gravitino/GravitinoLanceNameSpaceOperations.javaindex 2ca476ed9b..17b9b198b0 100644--- a/lance/lance-common/src/main/java/org/apache/gravitino/lance/common/ops/gravitino/GravitinoLanceNameSpaceOperations.java+++ b/lance/lance-common/src/main/java/org/apache/gravitino/lance/common/ops/gravitino/GravitinoLanceNameSpaceOperations.java@@ -361,20 +361,23 @@ public class GravitinoLanceNameSpaceOperations implements LanceNamespaceOperatioprivate DropNamespaceResponse dropCatalog(String catalogName, DropMode mode, DropBehavior behavior) {+ // Only a lakehouse catalog is a Lance namespace. Check the scope before the drop, as describe+ // and exists do, so that a Lance request can never drop (or start dropping) another catalog.+ Catalog catalog;+ try {+ catalog = namespaceWrapper.loadCatalog(catalogName);+ } catch (NoSuchCatalogException e) {+ return catalogNotFound(catalogName, mode);+ }+ namespaceWrapper.validateLakehouseCatalog(catalogName, catalog);+try {boolean dropped = namespaceWrapper.dropCatalog(catalogName, behavior == DropBehavior.CASCADE);if (dropped) {return new DropNamespaceResponse();- } else {- // Catalog did not exist- if (mode == DropMode.FAIL) {- throw new NamespaceNotFoundException(- "Catalog not found: " + catalogName,- CommonUtil.formatCurrentStackTrace(),- catalogName);- }- return new DropNamespaceResponse(); // SKIP mode}+ // Catalog did not exist+ return catalogNotFound(catalogName, mode);} catch (NonEmptyCatalogException e) {throw new InvalidInputException(String.format("Catalog %s is not empty", catalogName),@@ -393,6 +396,14 @@ public class GravitinoLanceNameSpaceOperations implements LanceNamespaceOperatio}}+ private DropNamespaceResponse catalogNotFound(String catalogName, DropMode mode) {+ if (mode == DropMode.FAIL) {+ throw new NamespaceNotFoundException(+ "Catalog not found: " + catalogName, CommonUtil.formatCurrentStackTrace(), catalogName);+ }+ return new DropNamespaceResponse(); // SKIP mode+ }+private DropNamespaceResponse dropSchema(String catalogName, String schemaName, DropMode mode, DropBehavior behavior) {try {diff --git a/lance/lance-common/src/main/java/org/apache/gravitino/lance/common/ops/gravitino/GravitinoLanceNamespaceWrapper.java b/lance/lance-common/src/main/java/org/apache/gravitino/lance/common/ops/gravitino/GravitinoLanceNamespaceWrapper.javaindex d6880ce320..b36ac200ef 100644--- a/lance/lance-common/src/main/java/org/apache/gravitino/lance/common/ops/gravitino/GravitinoLanceNamespaceWrapper.java+++ b/lance/lance-common/src/main/java/org/apache/gravitino/lance/common/ops/gravitino/GravitinoLanceNamespaceWrapper.java@@ -205,13 +205,25 @@ public class GravitinoLanceNamespaceWrapper extends NamespaceWrapper {throw new NamespaceNotFoundException("Catalog not found: " + catalogName, CommonUtil.formatCurrentStackTrace(), catalogName);}+ validateLakehouseCatalog(catalogName, catalog);+ return catalog;+ }++ /**+ * Rejects a catalog that is not a lakehouse catalog. Lance only exposes lakehouse catalogs as+ * namespaces, so any other catalog is reported as a missing namespace.+ *+ * @param catalogName the catalog name used in the error+ * @param catalog the loaded catalog+ * @throws NamespaceNotFoundException if the catalog is not a lakehouse catalog+ */+ void validateLakehouseCatalog(String catalogName, Catalog catalog) {if (!isLakehouseCatalog(catalog)) {throw new NamespaceNotFoundException("Catalog is not a lakehouse catalog: " + catalogName,CommonUtil.formatCurrentStackTrace(),catalogName);}- return catalog;}String[] listSchemas(Catalog catalog) throws NoSuchCatalogException {diff --git a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/common/ops/gravitino/TestGravitinoLanceNameSpaceOperations.java b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/common/ops/gravitino/TestGravitinoLanceNameSpaceOperations.javaindex 51629e4478..078049bb04 100644--- a/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/common/ops/gravitino/TestGravitinoLanceNameSpaceOperations.java+++ b/lance/lance-rest-server/src/test/java/org/apache/gravitino/lance/common/ops/gravitino/TestGravitinoLanceNameSpaceOperations.java@@ -18,19 +18,32 @@*/package org.apache.gravitino.lance.common.ops.gravitino;+import static org.mockito.ArgumentMatchers.anyBoolean;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;import java.util.regex.Pattern;import org.apache.gravitino.Catalog;+import org.apache.gravitino.exceptions.CatalogInUseException;+import org.apache.gravitino.exceptions.NoSuchCatalogException;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;+import org.junit.jupiter.params.provider.NullSource;+import org.junit.jupiter.params.provider.ValueSource;+import org.lance.namespace.errors.InvalidInputException;import org.lance.namespace.errors.NamespaceNotFoundException;+import org.lance.namespace.model.DropNamespaceResponse;import org.lance.namespace.model.ListNamespacesResponse;import org.mockito.Mockito;class TestGravitinoLanceNameSpaceOperations {private static final String DELIMITER = Pattern.quote(".");+ private static final String LAKEHOUSE_PROVIDER = "lakehouse-generic";@Testvoid testListNamespacesOnSchemaReturnsEmptyList() {@@ -91,4 +104,157 @@ class TestGravitinoLanceNameSpaceOperations {() -> operations.listNamespaces("bogus_catalog", DELIMITER, null, null));Assertions.assertEquals("Catalog not found: bogus_catalog", exception.getMessage());}++ @ParameterizedTest+ @NullSource+ @ValueSource(strings = {"restrict", "cascade"})+ void testDropCatalogRefusesNonLakehouseCatalog(String behavior) {+ // A Hive catalog is invisible to Lance list, describe and exists, so Lance must not drop it.+ // The default (null) behavior is RESTRICT, which would otherwise reach the catalog hook's+ // privilege cleanup before the drop is refused as "in use".+ GravitinoLanceNamespaceWrapper.CatalogOperator operator = mockOperator();+ Catalog catalog = mockCatalog(Catalog.Type.RELATIONAL, "hive");+ when(operator.loadCatalog("sales")).thenReturn(catalog);+ GravitinoLanceNameSpaceOperations operations = newOperations(operator);++ NamespaceNotFoundException exception =+ Assertions.assertThrows(+ NamespaceNotFoundException.class,+ () -> operations.dropNamespace("sales", DELIMITER, "fail", behavior));++ Assertions.assertEquals("Catalog is not a lakehouse catalog: sales", exception.getMessage());+ Assertions.assertEquals("sales", exception.getInstance());+ verify(operator, never()).dropCatalog(anyString(), anyBoolean());+ }++ @ParameterizedTest+ @CsvSource({+ "RELATIONAL, lakehouse-iceberg",+ "RELATIONAL, jdbc-mysql",+ "FILESET, fileset",+ "FILESET, lakehouse-generic",+ "MODEL, lakehouse-generic"+ })+ void testDropCatalogRefusesEveryNonLakehouseKind(Catalog.Type type, String provider) {+ // Both the type and the provider must match. The right provider on the wrong type is refused.+ GravitinoLanceNamespaceWrapper.CatalogOperator operator = mockOperator();+ Catalog catalog = mockCatalog(type, provider);+ when(operator.loadCatalog("other")).thenReturn(catalog);+ GravitinoLanceNameSpaceOperations operations = newOperations(operator);++ Assertions.assertThrows(+ NamespaceNotFoundException.class,+ () -> operations.dropNamespace("other", DELIMITER, "fail", "cascade"));+ verify(operator, never()).dropCatalog(anyString(), anyBoolean());+ }++ @Test+ void testDropCatalogRefusesNonLakehouseCatalogInSkipMode() {+ // SKIP only tolerates a missing catalog. An existing non-lakehouse catalog is still refused,+ // as it is for a schema-level drop under a non-lakehouse catalog.+ GravitinoLanceNamespaceWrapper.CatalogOperator operator = mockOperator();+ Catalog catalog = mockCatalog(Catalog.Type.RELATIONAL, "hive");+ when(operator.loadCatalog("sales")).thenReturn(catalog);+ GravitinoLanceNameSpaceOperations operations = newOperations(operator);++ Assertions.assertThrows(+ NamespaceNotFoundException.class,+ () -> operations.dropNamespace("sales", DELIMITER, "skip", "cascade"));+ verify(operator, never()).dropCatalog(anyString(), anyBoolean());+ }++ @Test+ void testDropCatalogErrorMatchesDescribeForNonLakehouseCatalog() {+ GravitinoLanceNamespaceWrapper.CatalogOperator operator = mockOperator();+ Catalog catalog = mockCatalog(Catalog.Type.RELATIONAL, "hive");+ when(operator.loadCatalog("sales")).thenReturn(catalog);+ GravitinoLanceNameSpaceOperations operations = newOperations(operator);++ NamespaceNotFoundException describeException =+ Assertions.assertThrows(+ NamespaceNotFoundException.class,+ () -> operations.describeNamespace("sales", DELIMITER));+ Exception dropException =+ Assertions.assertThrows(+ Exception.class, () -> operations.dropNamespace("sales", DELIMITER, null, null));++ Assertions.assertEquals(describeException.getClass(), dropException.getClass());+ Assertions.assertEquals(describeException.getMessage(), dropException.getMessage());+ Assertions.assertEquals(+ describeException.getInstance(),+ ((NamespaceNotFoundException) dropException).getInstance());+ }++ @ParameterizedTest+ @CsvSource({"restrict, false", "cascade, true"})+ void testDropLakehouseCatalogStillDrops(String behavior, boolean expectedForce) {+ GravitinoLanceNamespaceWrapper.CatalogOperator operator = mockOperator();+ Catalog catalog = mockCatalog(Catalog.Type.RELATIONAL, LAKEHOUSE_PROVIDER);+ when(operator.loadCatalog("lance_cat")).thenReturn(catalog);+ when(operator.dropCatalog("lance_cat", expectedForce)).thenReturn(true);+ GravitinoLanceNameSpaceOperations operations = newOperations(operator);++ DropNamespaceResponse response =+ operations.dropNamespace("lance_cat", DELIMITER, "fail", behavior);++ Assertions.assertNotNull(response);+ verify(operator).dropCatalog("lance_cat", expectedForce);+ }++ @Test+ void testDropLakehouseCatalogInUseKeepsExistingError() {+ GravitinoLanceNamespaceWrapper.CatalogOperator operator = mockOperator();+ Catalog catalog = mockCatalog(Catalog.Type.RELATIONAL, LAKEHOUSE_PROVIDER);+ when(operator.loadCatalog("lance_cat")).thenReturn(catalog);+ when(operator.dropCatalog("lance_cat", false))+ .thenThrow(new CatalogInUseException("Catalog %s is in use", "lance_cat"));+ GravitinoLanceNameSpaceOperations operations = newOperations(operator);++ InvalidInputException exception =+ Assertions.assertThrows(+ InvalidInputException.class,+ () -> operations.dropNamespace("lance_cat", DELIMITER, null, null));+ Assertions.assertEquals("Catalog lance_cat is in use", exception.getMessage());+ }++ @Test+ void testDropMissingCatalogKeepsExistingBehavior() {+ GravitinoLanceNamespaceWrapper.CatalogOperator operator = mockOperator();+ when(operator.loadCatalog("bogus"))+ .thenThrow(new NoSuchCatalogException("Catalog %s does not exist", "bogus"));+ when(operator.dropCatalog(anyString(), anyBoolean())).thenReturn(false);+ GravitinoLanceNameSpaceOperations operations = newOperations(operator);++ NamespaceNotFoundException exception =+ Assertions.assertThrows(+ NamespaceNotFoundException.class,+ () -> operations.dropNamespace("bogus", DELIMITER, "fail", "restrict"));+ Assertions.assertEquals("Catalog not found: bogus", exception.getMessage());+ Assertions.assertEquals("bogus", exception.getInstance());++ Assertions.assertNotNull(operations.dropNamespace("bogus", DELIMITER, "skip", "cascade"));+ }++ private static GravitinoLanceNamespaceWrapper.CatalogOperator mockOperator() {+ // By default the backing drop succeeds, as it would for any catalog the caller owns.+ GravitinoLanceNamespaceWrapper.CatalogOperator operator =+ Mockito.mock(GravitinoLanceNamespaceWrapper.CatalogOperator.class);+ when(operator.dropCatalog(anyString(), anyBoolean())).thenReturn(true);+ return operator;+ }++ private static GravitinoLanceNameSpaceOperations newOperations(+ GravitinoLanceNamespaceWrapper.CatalogOperator operator) {+ // A real wrapper, so the scope check under test is the production one.+ GravitinoLanceNamespaceWrapper wrapper = new GravitinoLanceNamespaceWrapper();+ wrapper.setCatalogOperator(operator);+ return new GravitinoLanceNameSpaceOperations(wrapper);+ }++ private static Catalog mockCatalog(Catalog.Type type, String provider) {+ Catalog catalog = Mockito.mock(Catalog.class);+ when(catalog.type()).thenReturn(type);+ when(catalog.provider()).thenReturn(provider);+ return catalog;+ }}