World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#1664] fix(core): Fix the issue that GC collector will remove tx mark mistakely (#1672)
Qi Yua6bbd2536dfc
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/FunctionUtils.java b/core/src/main/java/com/datastrato/gravitino/storage/FunctionUtils.javaindex e93aea831f..363b1ce8e9 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/FunctionUtils.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/FunctionUtils.java@@ -49,6 +49,9 @@ public class FunctionUtils {public static <R, E extends Exception> R executeInTransaction(Executable<R, E> executable, TransactionalKvBackend transactionalKvBackend)throws E, IOException {+ if (transactionalKvBackend.inTransaction()) {+ return executable.execute();+ }transactionalKvBackend.begin();try {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.javaindex f3e7f975e2..e42dbd257f 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@@ -80,7 +80,7 @@ public class KvEntityStore implements EntityStore {@VisibleForTesting StorageLayoutVersion storageLayoutVersion;private TransactionIdGenerator txIdGenerator;- private KvGarbageCollector kvGarbageCollector;+ @VisibleForTesting KvGarbageCollector kvGarbageCollector;private TransactionalKvBackend transactionalKvBackend;@Overridediff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvGarbageCollector.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvGarbageCollector.javaindex 1e178aedf0..30bbf504e2 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvGarbageCollector.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvGarbageCollector.java@@ -70,8 +70,7 @@ public final class KvGarbageCollector implements Closeable {long dateTimeLineMinute = config.get(KV_DELETE_AFTER_TIME) / 1000 / 60;// We will collect garbage every 10 minutes at least. If the dateTimeLineMinute is larger than- // 100- // minutes, we would collect garbage every dateTimeLineMinute/10 minutes.+ // 100 minutes, we would collect garbage every dateTimeLineMinute/10 minutes.long frequency = Math.max(dateTimeLineMinute / 10, 10);garbageCollectorPool.scheduleAtFixedRate(this::collectAndClean, 5, frequency, TimeUnit.MINUTES);}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionalKvBackend.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionalKvBackend.javaindex 3f2b363723..c9b3724a30 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionalKvBackend.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionalKvBackend.java@@ -28,4 +28,11 @@ public interface TransactionalKvBackend extends KvBackend {/** Close the current transaction. */void closeTransaction();++ /**+ * Check whether the backend is in transaction in the current thread.+ *+ * @return true if the backend is in transaction in the current thread+ */+ boolean inTransaction();}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionalKvBackendImpl.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionalKvBackendImpl.javaindex ab00262fcf..1687af82f8 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionalKvBackendImpl.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionalKvBackendImpl.java@@ -110,6 +110,7 @@ public class TransactionalKvBackendImpl implements TransactionalKvBackend {} finally {putPairs.get().clear();originalKeys.get().clear();+ txId.remove();}}@@ -121,6 +122,18 @@ public class TransactionalKvBackendImpl implements TransactionalKvBackend {}}+ @Override+ public void closeTransaction() {+ putPairs.get().clear();+ originalKeys.get().clear();+ txId.remove();+ }++ @Override+ public boolean inTransaction() {+ return txId.get() != null;+ }+@Overridepublic void initialize(Config config) throws IOException {}@@ -243,12 +256,6 @@ public class TransactionalKvBackendImpl implements TransactionalKvBackend {@Overridepublic void close() throws IOException {}- @Override- public void closeTransaction() {- putPairs.get().clear();- originalKeys.get().clear();- }-public static byte[] getRealValue(byte[] rawValue) {byte[] firstType = ArrayUtils.subarray(rawValue, 0, LENGTH_OF_VALUE_STATUS);ValueStatusEnum statusEnum = ValueStatusEnum.fromCode(firstType[0]);diff --git a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvEntityStorage.java b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvEntityStorage.javaindex f45bc620a6..ecf1fa08bc 100644--- a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvEntityStorage.java+++ b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestKvEntityStorage.java@@ -1022,4 +1022,156 @@ public class TestKvEntityStorage {(AuditInfo) e.auditInfo()));}}++ @Test+ void testRemoveWithGCCollector() throws IOException, InterruptedException {+ Config config = Mockito.mock(Config.class);+ File baseDir = new File(System.getProperty("java.io.tmpdir"));+ File file = Files.createTempDirectory(baseDir.toPath(), "test").toFile();+ file.deleteOnExit();+ Mockito.when(config.get(ENTITY_STORE)).thenReturn("kv");+ Mockito.when(config.get(ENTITY_KV_STORE)).thenReturn(DEFAULT_ENTITY_KV_STORE);+ Mockito.when(config.get(Configs.ENTITY_SERDE)).thenReturn("proto");+ Mockito.when(config.get(ENTRY_KV_ROCKSDB_BACKEND_PATH)).thenReturn(file.getAbsolutePath());+ Mockito.when(config.get(STORE_TRANSACTION_MAX_SKEW_TIME)).thenReturn(1000L);+ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(20 * 60 * 1000L);++ try (EntityStore store = EntityStoreFactory.createEntityStore(config)) {+ store.initialize(config);++ if (!(store instanceof KvEntityStore)) {+ return;+ }+ KvEntityStore kvEntityStore = (KvEntityStore) store;++ store.setSerDe(EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));+ AuditInfo auditInfo =+ new AuditInfo.Builder().withCreator("creator").withCreateTime(Instant.now()).build();++ BaseMetalake metalake1 = createBaseMakeLake("metalake1", auditInfo);+ BaseMetalake metalake2 = createBaseMakeLake("metalake2", auditInfo);+ BaseMetalake metalake3 = createBaseMakeLake("metalake3", auditInfo);++ store.put(metalake1);+ store.put(metalake2);+ store.put(metalake3);++ store.delete(NameIdentifier.of("metalake1"), EntityType.METALAKE);+ store.delete(NameIdentifier.of("metalake2"), EntityType.METALAKE);+ store.delete(NameIdentifier.of("metalake3"), EntityType.METALAKE);++ store.put(metalake1);+ store.put(metalake2);+ store.put(metalake3);++ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);+ Thread.sleep(1500);++ kvEntityStore.kvGarbageCollector.collectAndClean();++ Assertions.assertDoesNotThrow(+ () -> store.get(NameIdentifier.of("metalake1"), EntityType.METALAKE, BaseMetalake.class));+ Assertions.assertDoesNotThrow(+ () -> store.get(NameIdentifier.of("metalake2"), EntityType.METALAKE, BaseMetalake.class));+ Assertions.assertDoesNotThrow(+ () -> store.get(NameIdentifier.of("metalake3"), EntityType.METALAKE, BaseMetalake.class));++ // Test catalog+ CatalogEntity catalog1 = createCatalog(Namespace.of("metalake1"), "catalog1", auditInfo);+ CatalogEntity catalog2 = createCatalog(Namespace.of("metalake1"), "catalog2", auditInfo);++ store.put(catalog1);+ store.put(catalog2);++ store.delete(NameIdentifier.of("metalake1", "catalog1"), EntityType.CATALOG);+ store.delete(NameIdentifier.of("metalake1", "catalog2"), EntityType.CATALOG);++ store.put(catalog1);+ store.put(catalog2);++ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);+ Thread.sleep(1500);++ kvEntityStore.kvGarbageCollector.collectAndClean();++ Assertions.assertDoesNotThrow(+ () ->+ store.get(+ NameIdentifier.of("metalake1", "catalog1"),+ EntityType.CATALOG,+ CatalogEntity.class));+ Assertions.assertDoesNotThrow(+ () ->+ store.get(+ NameIdentifier.of("metalake1", "catalog2"),+ EntityType.CATALOG,+ CatalogEntity.class));++ // Test schema+ SchemaEntity schema1 =+ createSchemaEntity(Namespace.of("metalake1", "catalog2"), "schema1", auditInfo);+ SchemaEntity schema2 =+ createSchemaEntity(Namespace.of("metalake1", "catalog2"), "schema2", auditInfo);++ store.put(schema1);+ store.put(schema2);++ store.delete(NameIdentifier.of("metalake1", "catalog2", "schema1"), EntityType.SCHEMA);+ store.delete(NameIdentifier.of("metalake1", "catalog2", "schema2"), EntityType.SCHEMA);++ store.put(schema1);+ store.put(schema2);++ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);+ Thread.sleep(1500);+ kvEntityStore.kvGarbageCollector.collectAndClean();++ Assertions.assertDoesNotThrow(+ () ->+ store.get(+ NameIdentifier.of("metalake1", "catalog2", "schema1"),+ EntityType.SCHEMA,+ SchemaEntity.class));+ Assertions.assertDoesNotThrow(+ () ->+ store.get(+ NameIdentifier.of("metalake1", "catalog2", "schema2"),+ EntityType.SCHEMA,+ SchemaEntity.class));++ // Test table+ TableEntity table1 =+ createTableEntity(Namespace.of("metalake1", "catalog2", "schema2"), "table1", auditInfo);+ TableEntity table2 =+ createTableEntity(Namespace.of("metalake1", "catalog2", "schema2"), "table2", auditInfo);++ store.put(table1);+ store.put(table2);++ store.delete(+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"), EntityType.TABLE);+ store.delete(+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table2"), EntityType.TABLE);++ store.put(table1);+ store.put(table2);++ Mockito.when(config.get(KV_DELETE_AFTER_TIME)).thenReturn(1000L);+ Thread.sleep(1500);+ kvEntityStore.kvGarbageCollector.collectAndClean();++ Assertions.assertDoesNotThrow(+ () ->+ store.get(+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table1"),+ EntityType.TABLE,+ TableEntity.class));+ Assertions.assertDoesNotThrow(+ () ->+ store.get(+ NameIdentifier.of("metalake1", "catalog2", "schema2", "table2"),+ EntityType.TABLE,+ TableEntity.class));+ }+ }}