Skip to content

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

[#792] improvement(core): Optimize the deletion process of entities that have large sub-entities (#1132)

Qi Yub8f6efa8d375
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
index 6222c9d60c..0fafc0c174 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
@@ -282,20 +282,18 @@ public class KvEntityStore implements EntityStore {
ident, subEntities);
}
- for (byte[] prefix : subEntityPrefix) {
- transactionalKvBackend.deleteRange(
- new KvRange.KvRangeBuilder()
- .start(prefix)
- .startInclusive(true)
- .end(Bytes.increment(Bytes.wrap(prefix)).get())
- .build());
- }
+ // Remove id-name mapping;
+ unbindNameAndId(ident, entityType);
- deleteAuthorizationEntitiesIfNecessary(ident, entityType);
return transactionalKvBackend.delete(dataKey);
});
}
+ private void unbindNameAndId(NameIdentifier ident, EntityType entityType) throws IOException {
+ String identNameToIdKey = generateKeyForMapping(ident, entityType, nameMappingService);
+ nameMappingService.unbindNameAndId(identNameToIdKey);
+ }
+
@Override
public <R, E extends Exception> R executeInTransaction(Executable<R, E> executable)
throws E, IOException {
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
index 4df9a63f7d..e79ccf9cac 100644
--- a/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
+++ b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
@@ -23,6 +23,7 @@ import com.datastrato.gravitino.Catalog;
import com.datastrato.gravitino.Config;
import com.datastrato.gravitino.Configs;
import com.datastrato.gravitino.Entity;
+import com.datastrato.gravitino.Entity.EntityType;
import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.EntityStoreFactory;
import com.datastrato.gravitino.NameIdentifier;
@@ -1910,4 +1911,91 @@ public class TestEntityStorage {
Entity.EntityType.TABLE,
(e) -> e));
}
+
+ @ParameterizedTest
+ @MethodSource("storageProvider")
+ void testOptimizedDeleteForKv(String type) throws IOException {
+ if ("relational".equalsIgnoreCase(type)) {
+ return;
+ }
+
+ Config config = Mockito.mock(Config.class);
+ init(type, config);
+
+ AuditInfo auditInfo =
+ AuditInfo.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+
+ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {
+ store.initialize(config);
+ if (store instanceof RelationalEntityStore) {
+ prepareJdbcTable();
+ }
+
+ BaseMetalake metalake = createBaseMakeLake(1L, "metalake", auditInfo);
+ CatalogEntity catalog = createCatalog(1L, Namespace.of("metalake"), "catalog", auditInfo);
+ CatalogEntity catalogCopy =
+ createCatalog(2L, Namespace.of("metalake"), "catalogCopy", auditInfo);
+
+ SchemaEntity schemaEntity =
+ createSchemaEntity(1L, Namespace.of("metalake", "catalog"), "schema1", auditInfo);
+ SchemaEntity schemaEntity2 =
+ createSchemaEntity(2L, Namespace.of("metalake", "catalog"), "schema2", auditInfo);
+
+ TableEntity table =
+ createTableEntity(
+ 1L, Namespace.of("metalake", "catalog", "schema1"), "the same", auditInfo);
+ FilesetEntity filesetEntity =
+ createFilesetEntity(
+ 1L, Namespace.of("metalake", "catalog", "schema2"), "the same", auditInfo);
+
+ store.put(metalake);
+ store.put(catalog);
+ store.put(catalogCopy);
+ store.put(schemaEntity);
+ store.put(schemaEntity2);
+ store.put(table);
+ store.put(filesetEntity);
+
+ Assertions.assertDoesNotThrow(
+ () -> store.get(schemaEntity2.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
+ Assertions.assertDoesNotThrow(
+ () -> store.get(filesetEntity.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
+
+ // Test delete with the cascade or not
+ Assertions.assertThrows(
+ Exception.class, () -> store.delete(schemaEntity.nameIdentifier(), EntityType.SCHEMA));
+ Assertions.assertDoesNotThrow(
+ () -> store.delete(schemaEntity.nameIdentifier(), EntityType.SCHEMA, true));
+
+ Assertions.assertDoesNotThrow(
+ () -> store.get(filesetEntity.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
+
+ // Put the same schema back and see whether the deleted table exists or not
+ store.put(schemaEntity);
+ Assertions.assertDoesNotThrow(
+ () -> store.get(schemaEntity.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
+ Assertions.assertThrows(
+ Exception.class,
+ () -> store.get(table.nameIdentifier(), EntityType.TABLE, TableEntity.class));
+ Assertions.assertDoesNotThrow(
+ () -> store.get(filesetEntity.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
+
+ store.put(table);
+ Assertions.assertDoesNotThrow(
+ () -> store.get(schemaEntity.nameIdentifier(), EntityType.SCHEMA, SchemaEntity.class));
+ Assertions.assertDoesNotThrow(
+ () -> store.get(table.nameIdentifier(), EntityType.TABLE, TableEntity.class));
+ Assertions.assertDoesNotThrow(
+ () -> store.get(filesetEntity.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
+
+ store.delete(table.nameIdentifier(), EntityType.TABLE);
+ FilesetEntity filesetEntity1 =
+ createFilesetEntity(
+ 1L, Namespace.of("metalake", "catalog", "schema1"), "the same", auditInfo);
+ store.put(filesetEntity1);
+ Assertions.assertDoesNotThrow(
+ () ->
+ store.get(filesetEntity1.nameIdentifier(), EntityType.FILESET, FilesetEntity.class));
+ }
+ }
}