World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#6566] improvement(core): Add the cache mechanism for metalake and use cache to load `in-use` information. (#6569)
Mini Yu153da2a22a0a
diff --git a/core/src/main/java/org/apache/gravitino/GravitinoEnv.java b/core/src/main/java/org/apache/gravitino/GravitinoEnv.javaindex 57f04a0cfb..05eb4d6822 100644--- a/core/src/main/java/org/apache/gravitino/GravitinoEnv.java+++ b/core/src/main/java/org/apache/gravitino/GravitinoEnv.java@@ -94,6 +94,8 @@ public class GravitinoEnv {private CatalogManager catalogManager;+ private MetalakeManager metalakeManager;+private SchemaDispatcher schemaDispatcher;private TableDispatcher tableDispatcher;@@ -390,6 +392,10 @@ public class GravitinoEnv {eventListenerManager.stop();}+ if (metalakeManager != null) {+ metalakeManager.close();+ }+LOG.info("Gravitino Environment is shut down.");}@@ -414,10 +420,13 @@ public class GravitinoEnv {// create and initialize a random id generatorthis.idGenerator = new RandomIdGenerator();+ // Tree lock+ this.lockManager = new LockManager(config);+// Create and initialize metalake related modules, the operation chain is:// MetalakeEventDispatcher -> MetalakeNormalizeDispatcher -> MetalakeHookDispatcher ->// MetalakeManager- MetalakeDispatcher metalakeManager = new MetalakeManager(entityStore, idGenerator);+ this.metalakeManager = new MetalakeManager(entityStore, idGenerator);MetalakeHookDispatcher metalakeHookDispatcher = new MetalakeHookDispatcher(metalakeManager);MetalakeNormalizeDispatcher metalakeNormalizeDispatcher =new MetalakeNormalizeDispatcher(metalakeHookDispatcher);@@ -498,9 +507,6 @@ public class GravitinoEnv {this.auxServiceManager = new AuxiliaryServiceManager();this.auxServiceManager.serviceInit(config);- // Tree lock- this.lockManager = new LockManager(config);-// Create and initialize Tag related modulesthis.tagDispatcher = new TagEventDispatcher(eventBus, new TagManager(idGenerator, entityStore));}diff --git a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.javaindex 7c1722c1ce..8ff2e9f624 100644--- a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java+++ b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java@@ -262,7 +262,7 @@ public class CatalogManager implements CatalogDispatcher, Closeable {private final Config config;- @VisibleForTesting final Cache<NameIdentifier, CatalogWrapper> catalogCache;+ @VisibleForTesting static Cache<NameIdentifier, CatalogWrapper> catalogCache;private final EntityStore store;@@ -281,7 +281,7 @@ public class CatalogManager implements CatalogDispatcher, Closeable {this.idGenerator = idGenerator;long cacheEvictionIntervalInMs = config.get(Configs.CATALOG_CACHE_EVICTION_INTERVAL_MS);- this.catalogCache =+ catalogCache =Caffeine.newBuilder().expireAfterAccess(cacheEvictionIntervalInMs, TimeUnit.MILLISECONDS).removalListener(@@ -848,8 +848,13 @@ public class CatalogManager implements CatalogDispatcher, Closeable {private static boolean getCatalogInUseValue(EntityStore store, NameIdentifier catalogIdent) {try {- CatalogEntity catalogEntity =- store.get(catalogIdent, EntityType.CATALOG, CatalogEntity.class);+ CatalogWrapper wrapper = catalogCache.getIfPresent(catalogIdent);+ CatalogEntity catalogEntity;+ if (wrapper != null) {+ catalogEntity = wrapper.catalog.entity();+ } else {+ catalogEntity = store.get(catalogIdent, EntityType.CATALOG, CatalogEntity.class);+ }return (boolean)BASIC_CATALOG_PROPERTIES_METADATA.getOrDefault(catalogEntity.getProperties(), PROPERTY_IN_USE);diff --git a/core/src/main/java/org/apache/gravitino/metalake/MetalakeManager.java b/core/src/main/java/org/apache/gravitino/metalake/MetalakeManager.javaindex c1b464f436..cbe1fa6cb0 100644--- a/core/src/main/java/org/apache/gravitino/metalake/MetalakeManager.java+++ b/core/src/main/java/org/apache/gravitino/metalake/MetalakeManager.java@@ -20,13 +20,21 @@ package org.apache.gravitino.metalake;import static org.apache.gravitino.Metalake.PROPERTY_IN_USE;+import com.github.benmanes.caffeine.cache.Cache;+import com.github.benmanes.caffeine.cache.Caffeine;+import com.github.benmanes.caffeine.cache.Scheduler;+import com.google.common.annotations.VisibleForTesting;import com.google.common.collect.Maps;+import com.google.common.util.concurrent.ThreadFactoryBuilder;+import java.io.Closeable;import java.io.IOException;import java.time.Instant;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;+import java.util.concurrent.ScheduledThreadPoolExecutor;+import java.util.concurrent.TimeUnit;import org.apache.gravitino.Entity.EntityType;import org.apache.gravitino.EntityAlreadyExistsException;import org.apache.gravitino.EntityStore;@@ -54,7 +62,7 @@ import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** Manages Metalakes within the Apache Gravitino system. */-public class MetalakeManager implements MetalakeDispatcher {+public class MetalakeManager implements MetalakeDispatcher, Closeable {private static final String METALAKE_DOES_NOT_EXIST_MSG = "Metalake %s does not exist";@@ -64,6 +72,28 @@ public class MetalakeManager implements MetalakeDispatcher {private final IdGenerator idGenerator;+ // Currently, there will be only one MetalakeManager instance in the system. In this case+ // we can clear or close the cache when the instance is destroyed.+ @VisibleForTesting+ static final Cache<NameIdentifier, BaseMetalake> METALAKE_CACHE =+ Caffeine.newBuilder()+ .expireAfterAccess(24, TimeUnit.HOURS)+ .removalListener((k, v, c) -> LOG.info("Closing metalake {}.", k))+ .scheduler(+ Scheduler.forScheduledExecutorService(+ new ScheduledThreadPoolExecutor(+ 1,+ new ThreadFactoryBuilder()+ .setDaemon(true)+ .setNameFormat("metalake-cleaner-%d")+ .build())))+ .build();++ @Override+ public void close() {+ METALAKE_CACHE.invalidateAll();+ }+/*** Constructs a MetalakeManager instance.*@@ -73,6 +103,13 @@ public class MetalakeManager implements MetalakeDispatcher {public MetalakeManager(EntityStore store, IdGenerator idGenerator) {this.store = store;this.idGenerator = idGenerator;++ // pre-load all metalakes and put them into cache, this is useful when user load schema/table+ // directly without list/get metalake first.+ BaseMetalake[] metalakes = listMetalakes();+ for (BaseMetalake metalake : metalakes) {+ METALAKE_CACHE.put(metalake.nameIdentifier(), metalake);+ }}/**@@ -103,10 +140,12 @@ public class MetalakeManager implements MetalakeDispatcher {public static boolean metalakeInUse(EntityStore store, NameIdentifier ident)throws NoSuchMetalakeException {try {- BaseMetalake metalake = store.get(ident, EntityType.METALAKE, BaseMetalake.class);+ BaseMetalake metalake = METALAKE_CACHE.getIfPresent(ident);+ if (metalake == null) {+ metalake = store.get(ident, EntityType.METALAKE, BaseMetalake.class);+ }return (boolean)metalake.propertiesMetadata().getOrDefault(metalake.properties(), PROPERTY_IN_USE);-} catch (NoSuchEntityException e) {LOG.warn("Metalake {} does not exist", ident, e);throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, ident);@@ -149,20 +188,25 @@ public class MetalakeManager implements MetalakeDispatcher {*/@Overridepublic BaseMetalake loadMetalake(NameIdentifier ident) throws NoSuchMetalakeException {- try {- BaseMetalake baseMetalake =- TreeLockUtils.doWithTreeLock(- ident,- LockType.READ,- () -> store.get(ident, EntityType.METALAKE, BaseMetalake.class));- return newMetalakeWithResolvedProperties(baseMetalake);- } catch (NoSuchEntityException e) {- LOG.warn("Metalake {} does not exist", ident, e);- throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, ident);- } catch (IOException ioe) {- LOG.error("Loading Metalake {} failed due to storage issues", ident, ioe);- throw new RuntimeException(ioe);- }+ return TreeLockUtils.doWithTreeLock(+ ident,+ LockType.READ,+ () ->+ METALAKE_CACHE.get(+ ident,+ k -> {+ try {+ BaseMetalake baseMetalake =+ store.get(ident, EntityType.METALAKE, BaseMetalake.class);+ return newMetalakeWithResolvedProperties(baseMetalake);+ } catch (NoSuchEntityException e) {+ LOG.warn("Metalake {} does not exist", ident, e);+ throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, ident);+ } catch (IOException ioe) {+ LOG.error("Loading Metalake {} failed due to storage issues", ident, ioe);+ throw new RuntimeException(ioe);+ }+ }));}private BaseMetalake newMetalakeWithResolvedProperties(BaseMetalake metalakeEntity) {@@ -222,6 +266,7 @@ public class MetalakeManager implements MetalakeDispatcher {() -> {try {store.put(metalake, false /* overwritten */);+ METALAKE_CACHE.put(ident, newMetalakeWithResolvedProperties(metalake));return metalake;} catch (EntityAlreadyExistsException | AlreadyExistsException e) {LOG.warn("Metalake {} already exists", ident, e);@@ -253,22 +298,25 @@ public class MetalakeManager implements MetalakeDispatcher {throw new MetalakeNotInUseException("Metalake %s is not in use, please enable it first", ident);}-- return store.update(- ident,- BaseMetalake.class,- EntityType.METALAKE,- metalake -> {- BaseMetalake.Builder builder = newMetalakeBuilder(metalake);- Map<String, String> newProps =- metalake.properties() == null- ? Maps.newHashMap()- : Maps.newHashMap(metalake.properties());- builder = updateEntity(builder, newProps, changes);-- return builder.build();- });-+ METALAKE_CACHE.invalidate(ident);+ BaseMetalake baseMetalake =+ store.update(+ ident,+ BaseMetalake.class,+ EntityType.METALAKE,+ metalake -> {+ BaseMetalake.Builder builder = newMetalakeBuilder(metalake);+ Map<String, String> newProps =+ metalake.properties() == null+ ? Maps.newHashMap()+ : Maps.newHashMap(metalake.properties());+ builder = updateEntity(builder, newProps, changes);++ return builder.build();+ });+ METALAKE_CACHE.put(+ baseMetalake.nameIdentifier(), newMetalakeWithResolvedProperties(baseMetalake));+ return baseMetalake;} catch (NoSuchEntityException ne) {LOG.warn("Metalake {} does not exist", ident, ne);throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, ident);@@ -305,6 +353,8 @@ public class MetalakeManager implements MetalakeDispatcher {"Metalake %s is in use, please disable it first or use force option", ident);}+ METALAKE_CACHE.invalidate(ident);+List<CatalogEntity> catalogEntities =store.list(Namespace.of(ident.name()), CatalogEntity.class, EntityType.CATALOG);if (!catalogEntities.isEmpty() && !force) {@@ -331,22 +381,25 @@ public class MetalakeManager implements MetalakeDispatcher {try {boolean inUse = metalakeInUse(store, ident);if (!inUse) {- store.update(- ident,- BaseMetalake.class,- EntityType.METALAKE,- metalake -> {- BaseMetalake.Builder builder = newMetalakeBuilder(metalake);-- Map<String, String> newProps =- metalake.properties() == null- ? Maps.newHashMap()- : Maps.newHashMap(metalake.properties());- newProps.put(PROPERTY_IN_USE, "true");- builder.withProperties(newProps);-- return builder.build();- });+ METALAKE_CACHE.invalidate(ident);+ BaseMetalake baseMetalake =+ store.update(+ ident,+ BaseMetalake.class,+ EntityType.METALAKE,+ metalake -> {+ BaseMetalake.Builder builder = newMetalakeBuilder(metalake);++ Map<String, String> newProps =+ metalake.properties() == null+ ? Maps.newHashMap()+ : Maps.newHashMap(metalake.properties());+ newProps.put(PROPERTY_IN_USE, "true");+ builder.withProperties(newProps);++ return builder.build();+ });+ METALAKE_CACHE.put(ident, newMetalakeWithResolvedProperties(baseMetalake));}return null;@@ -365,22 +418,25 @@ public class MetalakeManager implements MetalakeDispatcher {try {boolean inUse = metalakeInUse(store, ident);if (inUse) {- store.update(- ident,- BaseMetalake.class,- EntityType.METALAKE,- metalake -> {- BaseMetalake.Builder builder = newMetalakeBuilder(metalake);-- Map<String, String> newProps =- metalake.properties() == null- ? Maps.newHashMap()- : Maps.newHashMap(metalake.properties());- newProps.put(PROPERTY_IN_USE, "false");- builder.withProperties(newProps);-- return builder.build();- });+ METALAKE_CACHE.invalidate(ident);+ BaseMetalake baseMetalake =+ store.update(+ ident,+ BaseMetalake.class,+ EntityType.METALAKE,+ metalake -> {+ BaseMetalake.Builder builder = newMetalakeBuilder(metalake);++ Map<String, String> newProps =+ metalake.properties() == null+ ? Maps.newHashMap()+ : Maps.newHashMap(metalake.properties());+ newProps.put(PROPERTY_IN_USE, "false");+ builder.withProperties(newProps);++ return builder.build();+ });+ METALAKE_CACHE.put(ident, newMetalakeWithResolvedProperties(baseMetalake));}return null;} catch (IOException e) {diff --git a/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java b/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.javaindex d11bff572f..009c797894 100644--- a/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java+++ b/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java@@ -247,7 +247,7 @@ public class TestCatalogManager {testProperties(props, testCatalog.properties());Assertions.assertEquals(Catalog.Type.RELATIONAL, testCatalog.type());- Assertions.assertNotNull(catalogManager.catalogCache.getIfPresent(ident));+ Assertions.assertNotNull(CatalogManager.catalogCache.getIfPresent(ident));// test before creationNameIdentifier ident2 = NameIdentifier.of("metalake1", "test1");@@ -265,7 +265,7 @@ public class TestCatalogManager {catalogManager.createCatalog(ident2, Catalog.Type.RELATIONAL, provider, "comment", props));Assertions.assertTrue(exception1.getMessage().contains("Metalake metalake1 does not exist"));- Assertions.assertNull(catalogManager.catalogCache.getIfPresent(ident2));+ Assertions.assertNull(CatalogManager.catalogCache.getIfPresent(ident2));// test before creationAssertions.assertThrows(@@ -285,7 +285,7 @@ public class TestCatalogManager {exception2.getMessage().contains("Catalog metalake.test1 already exists"));// Test if the catalog is already cached- CatalogManager.CatalogWrapper cached = catalogManager.catalogCache.getIfPresent(ident);+ CatalogManager.CatalogWrapper cached = CatalogManager.catalogCache.getIfPresent(ident);Assertions.assertNotNull(cached);// Test failed creation@@ -300,7 +300,7 @@ public class TestCatalogManager {Assertions.assertTrue(exception3.getMessage().contains("Properties are reserved and cannot be set"),exception3.getMessage());- Assertions.assertNull(catalogManager.catalogCache.getIfPresent(failedIdent));+ Assertions.assertNull(CatalogManager.catalogCache.getIfPresent(failedIdent));// Test failed for the second timeThrowable exception4 =Assertions.assertThrows(@@ -311,7 +311,7 @@ public class TestCatalogManager {Assertions.assertTrue(exception4.getMessage().contains("Properties are reserved and cannot be set"),exception4.getMessage());- Assertions.assertNull(catalogManager.catalogCache.getIfPresent(failedIdent));+ Assertions.assertNull(CatalogManager.catalogCache.getIfPresent(failedIdent));}@Test@@ -394,7 +394,7 @@ public class TestCatalogManager {exception.getMessage().contains("Catalog metalake.test22 does not exist"));// Load operation will cache the catalog- Assertions.assertNotNull(catalogManager.catalogCache.getIfPresent(ident));+ Assertions.assertNotNull(CatalogManager.catalogCache.getIfPresent(ident));}@Test@@ -440,8 +440,8 @@ public class TestCatalogManager {exception.getMessage().contains("Catalog metalake.test33 does not exist"));// Alter operation will update the cache- Assertions.assertNull(catalogManager.catalogCache.getIfPresent(ident));- Assertions.assertNotNull(catalogManager.catalogCache.getIfPresent(ident1));+ Assertions.assertNull(CatalogManager.catalogCache.getIfPresent(ident));+ Assertions.assertNotNull(CatalogManager.catalogCache.getIfPresent(ident1));}@Test@@ -469,7 +469,7 @@ public class TestCatalogManager {Assertions.assertFalse(dropped1);// Drop operation will update the cache- Assertions.assertNull(catalogManager.catalogCache.getIfPresent(ident));+ Assertions.assertNull(CatalogManager.catalogCache.getIfPresent(ident));}@Testdiff --git a/core/src/test/java/org/apache/gravitino/metalake/TestMetalakeManager.java b/core/src/test/java/org/apache/gravitino/metalake/TestMetalakeManager.javaindex cc2b9fd0d0..4765ddc716 100644--- a/core/src/test/java/org/apache/gravitino/metalake/TestMetalakeManager.java+++ b/core/src/test/java/org/apache/gravitino/metalake/TestMetalakeManager.java@@ -18,6 +18,12 @@*/package org.apache.gravitino.metalake;+import static org.apache.gravitino.Configs.TREE_LOCK_CLEAN_INTERVAL;+import static org.apache.gravitino.Configs.TREE_LOCK_MAX_NODE_IN_MEMORY;+import static org.apache.gravitino.Configs.TREE_LOCK_MIN_NODE_IN_MEMORY;+import static org.mockito.Mockito.doReturn;++import com.github.benmanes.caffeine.cache.Cache;import com.google.common.collect.ImmutableMap;import com.google.common.collect.Sets;import java.io.IOException;@@ -25,6 +31,7 @@ import java.util.Map;import java.util.Set;import org.apache.gravitino.Config;import org.apache.gravitino.EntityStore;+import org.apache.gravitino.GravitinoEnv;import org.apache.gravitino.MetalakeChange;import org.apache.gravitino.NameIdentifier;import org.apache.gravitino.StringIdentifier;@@ -32,6 +39,7 @@ import org.apache.gravitino.UserPrincipal;import org.apache.gravitino.auth.AuthConstants;import org.apache.gravitino.exceptions.MetalakeAlreadyExistsException;import org.apache.gravitino.exceptions.NoSuchMetalakeException;+import org.apache.gravitino.lock.LockManager;import org.apache.gravitino.meta.BaseMetalake;import org.apache.gravitino.storage.RandomIdGenerator;import org.apache.gravitino.storage.memory.TestMemoryEntityStore;@@ -40,6 +48,8 @@ import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;+import org.mockito.Mockito;+import org.testcontainers.shaded.org.apache.commons.lang3.reflect.FieldUtils;public class TestMetalakeManager {@@ -50,12 +60,17 @@ public class TestMetalakeManager {private static Config config;@BeforeAll- public static void setUp() {- config = new Config(false) {};+ public static void setUp() throws IllegalAccessException {+ config = Mockito.mock(Config.class);++ doReturn(100000L).when(config).get(TREE_LOCK_MAX_NODE_IN_MEMORY);+ doReturn(1000L).when(config).get(TREE_LOCK_MIN_NODE_IN_MEMORY);+ doReturn(36000L).when(config).get(TREE_LOCK_CLEAN_INTERVAL);entityStore = new TestMemoryEntityStore.InMemoryEntityStore();entityStore.initialize(config);+ FieldUtils.writeField(GravitinoEnv.getInstance(), "lockManager", new LockManager(config), true);metalakeManager = new MetalakeManager(entityStore, new RandomIdGenerator());}@@ -195,6 +210,65 @@ public class TestMetalakeManager {Assertions.assertFalse(dropped1, "metalake should be non-existent");}+ @Test+ public void testMetalakeCache() {+ NameIdentifier ident = NameIdentifier.of("test51");+ Map<String, String> props = ImmutableMap.of("key1", "value1");+ BaseMetalake metalake = metalakeManager.createMetalake(ident, "comment", props);+ Assertions.assertEquals("test51", metalake.name());+ Assertions.assertEquals("comment", metalake.comment());++ Cache<NameIdentifier, BaseMetalake> cache = MetalakeManager.METALAKE_CACHE;++ BaseMetalake baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);+ Assertions.assertEquals("test51", baseMetalake.name());+ Assertions.assertEquals("comment", baseMetalake.comment());++ metalakeManager.disableMetalake(ident);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);+ metalakeManager.dropMetalake(ident);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNull(baseMetalake);++ metalakeManager.createMetalake(ident, "comment", props);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);++ metalakeManager.disableMetalake(ident);+ metalakeManager.dropMetalake(ident);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNull(baseMetalake);++ metalakeManager.createMetalake(ident, "comment", props);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);+ metalakeManager.disableMetalake(ident);+ metalakeManager.dropMetalake(ident);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNull(baseMetalake);++ metalakeManager.createMetalake(ident, "comment", props);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);+ metalakeManager.disableMetalake(ident);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);+ Assertions.assertEquals("false", baseMetalake.properties().get("in-use"));+ metalakeManager.enableMetalake(ident);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);+ Assertions.assertEquals("true", baseMetalake.properties().get("in-use"));++ metalakeManager.loadMetalake(ident);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);+ metalakeManager.disableMetalake(ident);+ baseMetalake = cache.getIfPresent(ident);+ Assertions.assertNotNull(baseMetalake);+ }+private void testProperties(Map<String, String> expectedProps, Map<String, String> testProps) {expectedProps.forEach((k, v) -> {diff --git a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/BaseIT.java b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/BaseIT.javaindex f8cbe508f8..c600e537bd 100644--- a/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/BaseIT.java+++ b/integration-test-common/src/test/java/org/apache/gravitino/integration/test/util/BaseIT.java@@ -369,6 +369,7 @@ public class BaseIT {client.close();}customConfigs.clear();+LOG.info("Tearing down Gravitino Server");}