dimas-b commented on code in PR #4054:
URL: https://github.com/apache/polaris/pull/4054#discussion_r3067029961


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetaStoreManagerFactory.java:
##########
@@ -178,75 +175,91 @@ 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(), true);
         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, true);
 
       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();
   }
 
   @Override
-  public synchronized BasePersistence getOrCreateSession(RealmContext 
realmContext) {
-    if (!sessionSupplierMap.containsKey(realmContext.getRealmIdentifier())) {
-      DatasourceOperations datasourceOperations = getDatasourceOperations();
-      initializeForRealm(datasourceOperations, realmContext, null);
+  public BasePersistence getOrCreateSession(RealmContext realmContext) {
+    String realmId = realmContext.getRealmIdentifier();
+    RealmConfig realmConfig = new RealmConfigImpl(realmConfigurationSource, 
realmContext);

Review Comment:
   I suppose this is related to the following comment from the main thread:
   
   > [...] It calls metaStoreManagerFactory.getOrCreateSession(realmContext), 
which in your refactored code reads from realmConfig - a @RequestScoped CDI 
bean. That throws ContextNotActiveException
   
   This means the `realmConfig` was from a different request scope. In other 
words, something was reused across different requests without proper context 
propagation.
   
   IMHO, this is not a problem in this factory, but in the caller.
   
   We need to review the caller and handle context propagation / activation 
properly. Cf. #4061
   
   @adutra : WDYT?
   



-- 
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