World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#12504] fix(core): Fix flaky TestCatalogManager.testCatalogCacheRemoveListener (#12514)
Qi Yu933401bda9da
diff --git a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.javaindex b6090c9671..c5a205fb24 100644--- a/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java+++ b/core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java@@ -53,6 +53,7 @@ import java.util.Properties;import java.util.ServiceLoader;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;@@ -367,7 +368,9 @@ public class CatalogManager implements CatalogDispatcher, Closeable {private final SecretManager secretManager;- private final List<Consumer<NameIdentifier>> removalListeners = Lists.newArrayList();+ // Copy-on-write: listeners may be registered while the cache's removal listener (running on a+ // cache executor thread) is iterating this list.+ private final List<Consumer<NameIdentifier>> removalListeners = new CopyOnWriteArrayList<>();private final ConcurrentHashMap<NameIdentifier, AtomicInteger> localMutationCounts =new ConcurrentHashMap<>();diff --git a/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java b/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.javaindex 23b9140a36..b6caa8cbd9 100644--- a/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java+++ b/core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java@@ -145,6 +145,9 @@ public class TestCatalogManager {void reset() throws IOException {((InMemoryEntityStore) entityStore).clear();entityStore.put(metalakeEntity, true);+ // The shared CatalogManager is created once in @BeforeAll, so its cache would otherwise keep+ // entries created by previously executed test methods and make tests order-dependent.+ catalogManager.getCatalogCache().invalidateAll();}@AfterAll@@ -1443,37 +1446,49 @@ public class TestCatalogManager {}@Test- public void testCatalogCacheRemoveListener() {+ public void testCatalogCacheRemoveListener() throws IOException {NameIdentifier ident = NameIdentifier.of(metalake, "catalog");Map<String, String> props =ImmutableMap.of(PROPERTY_KEY1, "value1", PROPERTY_KEY2, "value2", PROPERTY_KEY5_PREFIX + "1", "value3");- // Create a catalog- catalogManager.createCatalog(ident, Catalog.Type.RELATIONAL, provider, "comment", props);+ // Use a dedicated CatalogManager (and entity store) instead of the shared static one: the+ // shared instance keeps cache entries and cache removal listeners registered by other test+ // methods, which would make the assertions below depend on the test execution order.+ EntityStore store = new InMemoryEntityStore();+ store.initialize(config);+ store.put(metalakeEntity, true);- // Load the catalog to add it to the cache- catalogManager.loadCatalog(ident);- Assertions.assertNotNull(catalogManager.getCatalogCache().getIfPresent(ident));+ try (CatalogManager manager =+ new CatalogManager(config, store, new RandomIdGenerator(), new SecretManager(config))) {+ // Create a catalog+ manager.createCatalog(ident, Catalog.Type.RELATIONAL, provider, "comment", props);++ // Load the catalog to add it to the cache+ manager.loadCatalog(ident);+ Assertions.assertNotNull(manager.getCatalogCache().getIfPresent(ident));++ // Add a listener to track removed catalogs+ Set<NameIdentifier> removedCatalogs = Sets.newConcurrentHashSet();+ manager.addCatalogCacheRemoveListener(removedCatalogs::add);- // Add a listener to track removed catalogs- Set<NameIdentifier> removedCatalogs = Sets.newConcurrentHashSet();- catalogManager.addCatalogCacheRemoveListener(removedCatalogs::add);-- // Invalidate the cache to trigger the removal listener- catalogManager.getCatalogCache().invalidate(ident);-- // Wait for the async eviction to complete- await()- .atMost(Duration.ofSeconds(5))- .untilAsserted(- () -> {- Assertions.assertTrue(- removedCatalogs.contains(ident),- "Listener should be notified of catalog removal");- Assertions.assertEquals(- 1, removedCatalogs.size(), "Only one catalog should be removed");- });+ // Invalidate the cache to trigger the removal listener+ manager.getCatalogCache().invalidate(ident);++ // Wait for the async eviction to complete+ await()+ .atMost(Duration.ofSeconds(5))+ .untilAsserted(+ () -> {+ Assertions.assertTrue(+ removedCatalogs.contains(ident),+ "Listener should be notified of catalog removal");+ Assertions.assertEquals(+ 1, removedCatalogs.size(), "Only one catalog should be removed");+ });+ } finally {+ store.close();+ }}private void testProperties(Map<String, String> expectedProps, Map<String, String> testProps) {