You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polaris.apache.org by di...@apache.org on 2026/10/11 21:54:29 UTC

(polaris) branch main updated: Bound the catalog-role count taken when dropping a catalog (#5515)

This is an automated email from the ASF dual-hosted git repository.

dimas-b pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/polaris.git


The following commit(s) were added to refs/heads/main by this push:
     new 077dcb6e55 Bound the catalog-role count taken when dropping a catalog (#5515)
077dcb6e55 is described below

commit 077dcb6e5577f37e56b543f76f7710c2716a748c
Author: Ayush Saxena <ay...@apache.org>
AuthorDate: Mon Oct 12 03:24:24 2026 +0530

    Bound the catalog-role count taken when dropping a catalog (#5515)
    
    * Bound the catalog-role count taken when dropping a catalog
    
    dropEntity needs to know only whether a catalog holds more than one
    catalog role, and it asked listFullEntities for that with a page size of
    2. listFullEntities applies its page size to a post-fetch Java predicate,
    so it deliberately issues no LIMIT and selects every column: the query
    returned every catalog role in the catalog, both JSON property blobs
    included, to answer a yes/no question. Its javadoc already points the
    other way -- "If only the entity name/id/type is required, use
    listEntities instead".
    
    listEntities is bounded and projected, so the count now reads at most two
    rows of six columns. The single remaining role is then looked up by id to
    drop it, which also handles it having been dropped concurrently.
    
    * Review: add the null check before dropping the last catalog role
    
    Matches what the atomic implementation does, so the two do not diverge.
    
    * Review: move the catalog-role listing test to JDBC persistence
    
    Run the bounded-listing assertion against a real relational backend instead
    of the in-memory TreeMap store, so it covers the persistence that the change
    is meant to benefit. The test spies the persistence the fixture already
    built and re-points a call context at the spy, which keeps the realm and the
    bootstrapped schema intact.
    
    Drop the re-check before dropping the last catalog role in
    TransactionalMetaStoreManagerImpl. The entity there already comes back fully
    loaded from loadEntitiesInCurrentTxn, and the whole drop runs inside one
    serialized transaction, so the lookup re-read a value that cannot have
    changed and the null branch was unreachable. The lookup stays in
    AtomicOperationMetaStoreManager, where listEntities returns identities only
    and a full entity is needed to drop.
---
 ...toreManagerWithJdbcBasePersistenceImplTest.java | 47 ++++++++++++++++++++++
 .../AtomicOperationMetaStoreManager.java           | 18 ++++++---
 2 files changed, 60 insertions(+), 5 deletions(-)

diff --git a/persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/AtomicMetastoreManagerWithJdbcBasePersistenceImplTest.java b/persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/AtomicMetastoreManagerWithJdbcBasePersistenceImplTest.java
index c69ab6a5b9..fd26982ff2 100644
--- a/persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/AtomicMetastoreManagerWithJdbcBasePersistenceImplTest.java
+++ b/persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/AtomicMetastoreManagerWithJdbcBasePersistenceImplTest.java
@@ -38,11 +38,15 @@ import org.apache.polaris.core.entity.PolarisEntityConstants;
 import org.apache.polaris.core.entity.PolarisEntitySubType;
 import org.apache.polaris.core.entity.PolarisEntityType;
 import org.apache.polaris.core.persistence.AtomicOperationMetaStoreManager;
+import org.apache.polaris.core.persistence.BasePersistence;
 import org.apache.polaris.core.persistence.BasePolarisMetaStoreManagerTest;
 import org.apache.polaris.core.persistence.PolarisTestMetaStoreManager;
+import org.apache.polaris.core.persistence.pagination.PageToken;
 import org.assertj.core.api.Assertions;
 import org.h2.jdbcx.JdbcConnectionPool;
 import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
 
 public abstract class AtomicMetastoreManagerWithJdbcBasePersistenceImplTest
     extends BasePolarisMetaStoreManagerTest {
@@ -145,6 +149,49 @@ public abstract class AtomicMetastoreManagerWithJdbcBasePersistenceImplTest
         .contains(Optional.empty());
   }
 
+  @Test
+  void dropCatalogListsAtMostTwoCatalogRoleIdentities() {
+    var metaStoreManager = polarisTestMetaStoreManager.polarisMetaStoreManager();
+    var callContext = polarisTestMetaStoreManager.polarisCallContext();
+    PolarisBaseEntity catalog =
+        new PolarisBaseEntity(
+            PolarisEntityConstants.getNullId(),
+            metaStoreManager.generateNewEntityId(callContext).getId(),
+            PolarisEntityType.CATALOG,
+            PolarisEntitySubType.NULL_SUBTYPE,
+            PolarisEntityConstants.getRootEntityId(),
+            "counted_catalog_roles");
+    catalog = metaStoreManager.createCatalog(callContext, catalog, List.of()).getCatalog();
+
+    BasePersistence spiedPersistence = Mockito.spy(callContext.getMetaStore());
+    PolarisCallContext spiedCallContext =
+        new PolarisCallContext(callContext.getRealmContext(), spiedPersistence);
+
+    metaStoreManager.dropEntityIfExists(spiedCallContext, null, catalog, Map.of(), false);
+
+    Mockito.verify(spiedPersistence, Mockito.never())
+        .listFullEntities(
+            Mockito.any(),
+            Mockito.anyLong(),
+            Mockito.anyLong(),
+            Mockito.eq(PolarisEntityType.CATALOG_ROLE),
+            Mockito.any(),
+            Mockito.any(),
+            Mockito.any(),
+            Mockito.any());
+
+    ArgumentCaptor<PageToken> pageToken = ArgumentCaptor.forClass(PageToken.class);
+    Mockito.verify(spiedPersistence)
+        .listEntities(
+            Mockito.any(),
+            Mockito.anyLong(),
+            Mockito.anyLong(),
+            Mockito.eq(PolarisEntityType.CATALOG_ROLE),
+            Mockito.any(),
+            pageToken.capture());
+    Assertions.assertThat(pageToken.getValue().pageSize()).hasValue(2);
+  }
+
   private static PolarisBaseEntity buildLocationBasedNamespace(
       PolarisBaseEntity catalog, long entityId, String name, String baseLocation) {
     return new PolarisBaseEntity.Builder()
diff --git a/polaris-core/src/main/java/org/apache/polaris/core/persistence/AtomicOperationMetaStoreManager.java b/polaris-core/src/main/java/org/apache/polaris/core/persistence/AtomicOperationMetaStoreManager.java
index cf6e6e1217..4db61c03c8 100644
--- a/polaris-core/src/main/java/org/apache/polaris/core/persistence/AtomicOperationMetaStoreManager.java
+++ b/polaris-core/src/main/java/org/apache/polaris/core/persistence/AtomicOperationMetaStoreManager.java
@@ -1178,15 +1178,13 @@ public class AtomicOperationMetaStoreManager extends BaseMetaStoreManager {
       }
 
       // get the list of catalog roles, at most 2
-      List<PolarisBaseEntity> catalogRoles =
-          ms.listFullEntities(
+      List<EntityNameLookupRecord> catalogRoles =
+          ms.listEntities(
                   callCtx,
                   catalogId,
                   catalogId,
                   PolarisEntityType.CATALOG_ROLE,
                   PolarisEntitySubType.ANY_SUBTYPE,
-                  entity -> true,
-                  Function.identity(),
                   PageToken.fromLimit(2))
               .items();
 
@@ -1198,7 +1196,17 @@ public class AtomicOperationMetaStoreManager extends BaseMetaStoreManager {
       // if 1, drop the last catalog role. Should be the catalog admin role but don't validate this
       if (!catalogRoles.isEmpty()) {
         // drop the last catalog role in that catalog, should be the admin catalog role
-        this.dropEntity(callCtx, ms, catalogRoles.get(0));
+        EntityNameLookupRecord lastCatalogRole = catalogRoles.get(0);
+        PolarisBaseEntity catalogRoleToDrop =
+            ms.lookupEntity(
+                callCtx,
+                lastCatalogRole.getCatalogId(),
+                lastCatalogRole.getId(),
+                lastCatalogRole.getTypeCode());
+        // null means it was dropped concurrently, which leaves nothing to do
+        if (catalogRoleToDrop != null) {
+          this.dropEntity(callCtx, ms, catalogRoleToDrop);
+        }
       }
     } else if (refreshEntityToDrop.getType() == PolarisEntityType.NAMESPACE) {
       if (ms.hasChildren(