flyrain commented on code in PR #4054:
URL: https://github.com/apache/polaris/pull/4054#discussion_r3023687529


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetaStoreManagerFactory.java:
##########
@@ -178,75 +177,87 @@ public synchronized Map<String, PrincipalSecretsResult> 
bootstrapRealms(
           throw new RuntimeException(
               String.format("Error executing sql script: %s", e.getMessage()), 
e);
         }
-        initializeForRealm(
-            datasourceOperations, realmContext, 
bootstrapOptions.rootCredentialsSet());
+        // Cache the effective schema version for this realm
+        schemaVersionCache.put(realm, effectiveSchemaVersion);
 
-        PolarisMetaStoreManager metaStoreManager =
-            metaStoreManagerMap.get(realmContext.getRealmIdentifier());
-        BasePersistence metaStore = 
sessionSupplierMap.get(realmContext.getRealmIdentifier()).get();
+        PolarisMetaStoreManager metaStoreManager = createNewMetaStoreManager();
+        BasePersistence metaStore = createSession(realm, 
bootstrapOptions.rootCredentialsSet());
         PolarisCallContext polarisContext = new 
PolarisCallContext(realmContext, metaStore);
 
         PrincipalSecretsResult secretsResult =
             createPolarisPrincipalForRealm(metaStoreManager, polarisContext);
         results.put(realm, secretsResult);
+        verifiedRealms.add(realm);
       }
     }
 
     return Map.copyOf(results);
   }
 
   @Override
-  public Map<String, BaseResult> purgeRealms(Iterable<String> realms) {
+  public synchronized Map<String, BaseResult> purgeRealms(Iterable<String> 
realms) {
     Map<String, BaseResult> results = new HashMap<>();
 
     for (String realm : realms) {
       RealmContext realmContext = () -> realm;
-      PolarisMetaStoreManager metaStoreManager = 
getOrCreateMetaStoreManager(realmContext);
-      BasePersistence session = getOrCreateSession(realmContext);
+      PolarisMetaStoreManager metaStoreManager = createNewMetaStoreManager();
+      BasePersistence session = createSession(realm, null);
 
       PolarisCallContext callContext = new PolarisCallContext(realmContext, 
session);
+
+      // Verify the realm is bootstrapped before purging — a non-bootstrapped 
realm
+      // has no root principal, so purging it is a no-op that should be 
reported as failure.
+      Optional<PrincipalEntity> rootPrincipal = 
metaStoreManager.findRootPrincipal(callContext);
+      if (rootPrincipal.isEmpty()) {
+        results.put(
+            realm, new BaseResult(BaseResult.ReturnStatus.ENTITY_NOT_FOUND, 
"Not bootstrapped"));
+        continue;
+      }
+
       BaseResult result = metaStoreManager.purge(callContext);
       results.put(realm, result);
 
-      sessionSupplierMap.remove(realm);
-      metaStoreManagerMap.remove(realm);
+      // Evict all cached state for this realm
+      entityCacheMap.remove(realm);
+      schemaVersionCache.remove(realm);
+      verifiedRealms.remove(realm);
     }
 
     return Map.copyOf(results);
   }
 
   @Override
-  public synchronized PolarisMetaStoreManager getOrCreateMetaStoreManager(
-      RealmContext realmContext) {
-    if (!metaStoreManagerMap.containsKey(realmContext.getRealmIdentifier())) {
-      DatasourceOperations datasourceOperations = getDatasourceOperations();
-      initializeForRealm(datasourceOperations, realmContext, null);
-      checkPolarisServiceBootstrappedForRealm(realmContext);
-    }
-    return metaStoreManagerMap.get(realmContext.getRealmIdentifier());
+  public PolarisMetaStoreManager getOrCreateMetaStoreManager(RealmContext 
realmContext) {
+    // Stateless — create a fresh instance on every call, no caching needed
+    return createNewMetaStoreManager();

Review Comment:
   Question: why not use the one in the entity cache? The caller is request 
scoped, 
https://github.com/apache/polaris/blob/8cbfb268c780c976acd59536c60d4ae06089c46f/runtime/service/src/main/java/org/apache/polaris/service/config/ServiceProducers.java#L236,
 I guess the point of caching is to reuse it here. Am I missing something?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to