Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#14835] fix(core): Keep a disabled catalog not in use after a refused drop (#14836)

Yuhui411c1d4e1cb7
diff --git a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
index e8118d646b..27b8d8fc65 100644
--- a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
+++ b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java
@@ -1451,22 +1451,24 @@ public class CatalogManager implements CatalogDispatcher, Closeable {
}
secretManager.deleteSecretsFromProperties(catalogProperties);
}
- catalogCache.invalidate(ident);
return deleted;
} catch (NoSuchMetalakeException | NoSuchCatalogException ignored) {
return false;
} catch (NoSuchEntityException ignored) {
// Another server deleted the catalog after it was loaded above, so a later store read
- // such as listing its schemas no longer finds it. The drop stays idempotent, but the
- // wrapper cached by loadCatalogAndWrap has to be discarded. store.delete itself never
- // reaches here: it maps a missing entity to false on its own.
- catalogCache.invalidate(ident);
+ // such as listing its schemas no longer finds it. The drop stays idempotent.
+ // store.delete itself never reaches here: it maps a missing entity to false on its own.
return false;
} catch (GravitinoRuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
+ } finally {
+ // Discard the wrapper on every outcome. Operations initialized during the drop skip the
+ // catalog in-use check, so a refused or failed drop must not leave them cached for
+ // later calls on a disabled catalog.
+ catalogCache.invalidate(ident);
}
});
}
diff --git a/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java b/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java
index 1b9f3b7ad5..903e7a5395 100644
--- a/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java
+++ b/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java
@@ -87,6 +87,7 @@ import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchEntityException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.apache.gravitino.exceptions.NonEmptyCatalogException;
import org.apache.gravitino.exceptions.OptimisticLockException;
import org.apache.gravitino.lock.LockManager;
import org.apache.gravitino.lock.LockType;
@@ -1942,6 +1943,53 @@ public class TestCatalogManager {
Assertions.assertTrue(entityStore.exists(ident, EntityType.CATALOG));
}
+ @Test
+ void testRefusedDropKeepsDisabledCatalogNotInUse() throws Exception {
+ InMemoryEntityStore store = new InMemoryEntityStore();
+ store.initialize(config);
+ store.put(metalakeEntity, true);
+ CatalogManager manager =
+ new CatalogManager(config, store, new RandomIdGenerator(), new SecretManager(config));
+
+ NameIdentifier ident = NameIdentifier.of("metalake", "refused_drop");
+ Map<String, String> props =
+ ImmutableMap.of(
+ "provider",
+ "test",
+ PROPERTY_KEY1,
+ "value1",
+ PROPERTY_KEY2,
+ "value2",
+ PROPERTY_KEY5_PREFIX + "1",
+ "value3",
+ BaseCatalog.CATALOG_OPERATION_IMPL,
+ SingleSchemaCatalogOperations.class.getName());
+ manager.createCatalog(ident, Catalog.Type.RELATIONAL, provider, "comment", props);
+ store.put(
+ SchemaEntity.builder()
+ .withId(RandomIdGenerator.INSTANCE.nextId())
+ .withName(SingleSchemaCatalogOperations.SCHEMA_NAME)
+ .withNamespace(Namespace.of("metalake", "refused_drop"))
+ .withAuditInfo(
+ AuditInfo.builder()
+ .withCreator(PrincipalUtils.getCurrentPrincipal().getName())
+ .withCreateTime(Instant.now())
+ .build())
+ .build());
+
+ manager.disableCatalog(ident);
+ Assertions.assertThrows(
+ NonEmptyCatalogException.class, () -> manager.dropCatalog(ident, false));
+
+ // The operations built while checking schemas for the refused drop must not be reused by
+ // later calls on the disabled catalog.
+ Namespace schemaNs = Namespace.of("metalake", "refused_drop");
+ Assertions.assertThrows(
+ CatalogNotInUseException.class,
+ () -> manager.loadCatalogAndWrap(ident).doWithSchemaOps(ops -> ops.listSchemas(schemaNs)));
+ manager.close();
+ }
+
@Test
public void testForceDropCatalog() throws Exception {
NameIdentifier ident = NameIdentifier.of("metalake", "test41");
@@ -2810,4 +2858,23 @@ public class TestCatalogManager {
c.loadFromProperties(p);
return new SecretManager(c);
}
+
+ /** Catalog operations whose backend always holds one user-created schema. */
+ public static class SingleSchemaCatalogOperations extends TestCatalogOperations {
+ static final String SCHEMA_NAME = "existing_schema";
+
+ public SingleSchemaCatalogOperations() {
+ super(ImmutableMap.of());
+ }
+
+ @Override
+ public NameIdentifier[] listSchemas(Namespace namespace) {
+ return new NameIdentifier[] {NameIdentifier.of(namespace, SCHEMA_NAME)};
+ }
+
+ @Override
+ public Schema loadSchema(NameIdentifier ident) {
+ return Mockito.mock(Schema.class);
+ }
+ }
}