World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#13278] fix(core): clean up missing schemas on explicit cascading drops (#13279)
Qi Yudc2545f03594
diff --git a/core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java b/core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.javaindex 01da4659e4..e2fdd1fdf3 100644--- a/core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java+++ b/core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java@@ -572,13 +572,12 @@ public class SchemaOperationDispatcher extends OperationDispatcher implements Screturn droppedFromCatalog;}- // A false result is ambiguous: the external schema may have been renamed or dropped out- // of band. Preserve the registration because deleting it after a rename would lose- // Gravitino-only metadata. A true out-of-band drop can therefore leave a stale- // registration that requires separate cleanup.- if (droppedFromCatalog) {+ // A non-cascading drop preserves a missing registration because the source schema+ // may have been renamed. An explicit cascading drop also removes stale metadata.+ boolean droppedFromStore = false;+ if (droppedFromCatalog || cascade) {try {- store.delete(ident, SCHEMA, true);+ droppedFromStore = store.delete(ident, SCHEMA, true);} catch (NoSuchEntityException e) {LOG.warn("The schema to be dropped does not exist in the store: {}", ident, e);} catch (Exception e) {@@ -595,10 +594,10 @@ public class SchemaOperationDispatcher extends OperationDispatcher implements SccatalogIdent,c -> c.doWithSchemaOps(s -> s.schemaExists(schemaIdent)),RuntimeException.class));- if (droppedFromCatalog) {+ if (droppedFromCatalog || droppedFromStore) {secretManager.deleteSecretsFromProperties(schemaProperties);}- return droppedFromCatalog;+ return droppedFromCatalog || droppedFromStore;});}diff --git a/core/src/test/java/org/apache/gravitino/catalog/TestSchemaOperationDispatcher.java b/core/src/test/java/org/apache/gravitino/catalog/TestSchemaOperationDispatcher.javaindex a631e8fbbb..a04c549a4c 100644--- a/core/src/test/java/org/apache/gravitino/catalog/TestSchemaOperationDispatcher.java+++ b/core/src/test/java/org/apache/gravitino/catalog/TestSchemaOperationDispatcher.java@@ -400,6 +400,57 @@ public class TestSchemaOperationDispatcher extends TestOperationDispatcher {Assertions.assertTrue(entityStore.exists(schemaIdent, SCHEMA));}+ @Test+ void testDropSchemaRemovedFromSourceReportsMetadataCleanup() throws Exception {+ reset(entityStore);+ NameIdentifier ident = NameIdentifier.of(metalake, catalog, "externally_dropped_schema");+ dispatcher.createSchema(ident, "comment", ImmutableMap.of("k1", "v1", "k2", "v2"));+ boolean droppedFromSource =+ catalogManager.doWithCatalogWrapper(+ NameIdentifier.of(metalake, catalog),+ wrapper -> wrapper.doWithSchemaOps(ops -> ops.dropSchema(ident, true)));+ Assertions.assertTrue(droppedFromSource);+ Assertions.assertTrue(entityStore.exists(ident, SCHEMA));++ Assertions.assertTrue(dispatcher.dropSchema(ident, true));+ Assertions.assertFalse(entityStore.exists(ident, SCHEMA));+ Assertions.assertFalse(dispatcher.dropSchema(ident, true));+ }++ @Test+ void testCascadingDropOfMissingSchemaDeletesStoredSecrets() throws Exception {+ reset(entityStore);+ try (SecretManager secrets = memorySecretManager()) {+ SchemaOperationDispatcher d =+ new SchemaOperationDispatcher(catalogManager, entityStore, idGenerator, secrets);+ NameIdentifier ident = NameIdentifier.of(metalake, catalog, "missing_schema_secret");+ d.createSchema(+ ident,+ "comment",+ ImmutableMap.of("k1", "v1"),+ Map.of("k2", new SecretBinding("memory", "s3cr3t")),+ Map.of());+ SchemaEntity entity = entityStore.get(ident, SCHEMA, SchemaEntity.class);+ SecretUrn urn =+ SecretUrn.buildWriteThrough(+ "memory",+ Map.of(+ SecretConstants.ATTR_ENTITY_TYPE, "schema",+ SecretConstants.ATTR_ENTITY_ID, String.valueOf(entity.id()),+ SecretConstants.ATTR_PROPERTY_KEY, "k2"));+ boolean sourceDropped =+ catalogManager.doWithCatalogWrapper(+ NameIdentifier.of(metalake, catalog),+ wrapper -> wrapper.doWithSchemaOps(ops -> ops.dropSchema(ident, true)));+ Assertions.assertTrue(sourceDropped);+ Assertions.assertFalse(d.dropSchema(ident, false));+ Assertions.assertEquals("s3cr3t", secrets.readSecret(urn));+ Assertions.assertTrue(d.dropSchema(ident, true));+ Assertions.assertFalse(entityStore.exists(ident, SCHEMA));+ Assertions.assertThrows(IllegalArgumentException.class, () -> secrets.readSecret(urn));+ }+ }+@Testpublic void testDropHierarchicalSchemaCleansUpOrphanedAncestors() throws IOException {// Clear any spy stubs leaked from other tests sharing the static entityStore.