Skip to content

Commit 8fa6bf2

Browse files
authored
Materialize Realm ID for Session Supplier in JDBC (apache#1988)
It was discovered that the Session Supplier maps used in the MetaStoreManagerFactory implementations were passing in RealmContext objects to the supplier directly and then using the RealmContext objects to create BasePersistence implementation objects within the supplier. This supplier is cached on a per-realm basis in most MetaStoreManagerFactory implementations. RealmContext objects are request-scoped beans. As a result, if any work is being done outside the scope of the request, such as during a Task, any calls to getOrCreateSessionSupplier for creating a BasePersistence implementation will fail as the RealmContext object is no longer available. This PR will ensure for the JdbcMetaStoreManagerFactory that the Realm ID is materialized from the RealmContext and used inside the supplier so that the potentially deactivated RealmContext object does not need to be used in creating the BasePersistence object. Given that we are caching on a per-realm basis, this should not introduce any unforeseen behavior for the JdbcMetaStoreManagerFactory as the Realm ID must match exactly for the same supplier to be returned from the Session Supplier map.
1 parent 7f3b781 commit 8fa6bf2

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetaStoreManagerFactory.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ public class JdbcMetaStoreManagerFactory implements MetaStoreManagerFactory {
8383
protected JdbcMetaStoreManagerFactory() {}
8484

8585
protected PrincipalSecretsGenerator secretsGenerator(
86-
RealmContext realmContext, @Nullable RootCredentialsSet rootCredentialsSet) {
86+
String realmId, @Nullable RootCredentialsSet rootCredentialsSet) {
8787
if (rootCredentialsSet != null) {
88-
return PrincipalSecretsGenerator.bootstrap(
89-
realmContext.getRealmIdentifier(), rootCredentialsSet);
88+
return PrincipalSecretsGenerator.bootstrap(realmId, rootCredentialsSet);
9089
} else {
9190
return PrincipalSecretsGenerator.RANDOM_SECRETS;
9291
}
@@ -100,17 +99,20 @@ private void initializeForRealm(
10099
DatasourceOperations datasourceOperations,
101100
RealmContext realmContext,
102101
RootCredentialsSet rootCredentialsSet) {
102+
// Materialize realmId so that background tasks that don't have an active
103+
// RealmContext (request-scoped bean) can still create a JdbcBasePersistenceImpl
104+
String realmId = realmContext.getRealmIdentifier();
103105
sessionSupplierMap.put(
104-
realmContext.getRealmIdentifier(),
106+
realmId,
105107
() ->
106108
new JdbcBasePersistenceImpl(
107109
datasourceOperations,
108-
secretsGenerator(realmContext, rootCredentialsSet),
110+
secretsGenerator(realmId, rootCredentialsSet),
109111
storageIntegrationProvider,
110-
realmContext.getRealmIdentifier()));
112+
realmId));
111113

112114
PolarisMetaStoreManager metaStoreManager = createNewMetaStoreManager();
113-
metaStoreManagerMap.put(realmContext.getRealmIdentifier(), metaStoreManager);
115+
metaStoreManagerMap.put(realmId, metaStoreManager);
114116
}
115117

116118
public DatasourceOperations getDatasourceOperations() {

0 commit comments

Comments
 (0)