Skip to content

Commit

Permalink
Make RealmEntityManagerFactory thread-safe (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
adutra authored Nov 22, 2024
1 parent 4560245 commit 017ebbc
Showing 1 changed file with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
package org.apache.polaris.service.config;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
import org.apache.polaris.core.persistence.PolarisEntityManager;
Expand All @@ -32,7 +32,7 @@ public class RealmEntityManagerFactory {
private final MetaStoreManagerFactory metaStoreManagerFactory;

// Key: realmIdentifier
private final Map<String, PolarisEntityManager> cachedEntityManagers = new HashMap<>();
private final Map<String, PolarisEntityManager> cachedEntityManagers = new ConcurrentHashMap<>();

// Subclasses for test injection.
protected RealmEntityManagerFactory() {
Expand All @@ -47,17 +47,14 @@ public PolarisEntityManager getOrCreateEntityManager(RealmContext context) {
String realm = context.getRealmIdentifier();

LOGGER.debug("Looking up PolarisEntityManager for realm {}", realm);
PolarisEntityManager entityManagerInstance = cachedEntityManagers.get(realm);
if (entityManagerInstance == null) {
LOGGER.info("Initializing new PolarisEntityManager for realm {}", realm);

entityManagerInstance =
new PolarisEntityManager(
return cachedEntityManagers.computeIfAbsent(
realm,
r -> {
LOGGER.info("Initializing new PolarisEntityManager for realm {}", r);
return new PolarisEntityManager(
metaStoreManagerFactory.getOrCreateMetaStoreManager(context),
metaStoreManagerFactory.getOrCreateStorageCredentialCache(context));

cachedEntityManagers.put(realm, entityManagerInstance);
}
return entityManagerInstance;
});
}
}

0 comments on commit 017ebbc

Please sign in to comment.