5555import org .slf4j .LoggerFactory ;
5656
5757/**
58- * The implementation of Configuration interface for configuring the {@link PolarisMetaStoreManager}
58+ * The implementation of Configuration interface for configuring the
59+ * {@link PolarisMetaStoreManager}
5960 * using a JDBC backed by SQL metastore. TODO: refactor - <a
6061 * href="https://github.com/apache/polaris/pull/1287/files#r2047487588">...</a>
6162 */
@@ -70,11 +71,15 @@ public class JdbcMetaStoreManagerFactory implements MetaStoreManagerFactory {
7071 final Map <String , Supplier <BasePersistence >> sessionSupplierMap = new HashMap <>();
7172 protected final PolarisDiagnostics diagServices = new PolarisDefaultDiagServiceImpl ();
7273
73- @ Inject PolarisStorageIntegrationProvider storageIntegrationProvider ;
74- @ Inject Instance <DataSource > dataSource ;
75- @ Inject RelationalJdbcConfiguration relationalJdbcConfiguration ;
74+ @ Inject
75+ PolarisStorageIntegrationProvider storageIntegrationProvider ;
76+ @ Inject
77+ Instance <DataSourceResolver > dataSourceResolver ;
78+ @ Inject
79+ RelationalJdbcConfiguration relationalJdbcConfiguration ;
7680
77- protected JdbcMetaStoreManagerFactory () {}
81+ protected JdbcMetaStoreManagerFactory () {
82+ }
7883
7984 protected PrincipalSecretsGenerator secretsGenerator (
8085 String realmId , @ Nullable RootCredentialsSet rootCredentialsSet ) {
@@ -98,21 +103,24 @@ private void initializeForRealm(
98103 String realmId = realmContext .getRealmIdentifier ();
99104 sessionSupplierMap .put (
100105 realmId ,
101- () ->
102- new JdbcBasePersistenceImpl (
103- datasourceOperations ,
104- secretsGenerator (realmId , rootCredentialsSet ),
105- storageIntegrationProvider ,
106- realmId ));
106+ () -> new JdbcBasePersistenceImpl (
107+ datasourceOperations , // TODO: We need a way to pass the right data source for the actual operation
108+ // (metrics vs main),
109+ // but for now JdbcBasePersistenceImpl does everything. So we pass the main
110+ // DatasourceOperations.
111+ secretsGenerator (realmId , rootCredentialsSet ),
112+ storageIntegrationProvider ,
113+ realmId ));
107114
108115 PolarisMetaStoreManager metaStoreManager = createNewMetaStoreManager ();
109116 metaStoreManagerMap .put (realmId , metaStoreManager );
110117 }
111118
112- public DatasourceOperations getDatasourceOperations () {
119+ public DatasourceOperations getDatasourceOperations (String realmId , String storeType ) {
113120 DatasourceOperations databaseOperations ;
114121 try {
115- databaseOperations = new DatasourceOperations (dataSource .get (), relationalJdbcConfiguration );
122+ DataSource resolvedDs = dataSourceResolver .get ().resolve (realmId , storeType );
123+ databaseOperations = new DatasourceOperations (resolvedDs , relationalJdbcConfiguration );
116124 } catch (SQLException sqlException ) {
117125 throw new RuntimeException (sqlException );
118126 }
@@ -124,12 +132,11 @@ public synchronized Map<String, PrincipalSecretsResult> bootstrapRealms(
124132 Iterable <String > realms , RootCredentialsSet rootCredentialsSet ) {
125133 SchemaOptions schemaOptions = ImmutableSchemaOptions .builder ().build ();
126134
127- BootstrapOptions bootstrapOptions =
128- ImmutableBootstrapOptions .builder ()
129- .realms (realms )
130- .rootCredentialsSet (rootCredentialsSet )
131- .schemaOptions (schemaOptions )
132- .build ();
135+ BootstrapOptions bootstrapOptions = ImmutableBootstrapOptions .builder ()
136+ .realms (realms )
137+ .rootCredentialsSet (rootCredentialsSet )
138+ .schemaOptions (schemaOptions )
139+ .build ();
133140
134141 return bootstrapRealms (bootstrapOptions );
135142 }
@@ -142,7 +149,7 @@ public synchronized Map<String, PrincipalSecretsResult> bootstrapRealms(
142149 for (String realm : bootstrapOptions .realms ()) {
143150 RealmContext realmContext = () -> realm ;
144151 if (!metaStoreManagerMap .containsKey (realm )) {
145- DatasourceOperations datasourceOperations = getDatasourceOperations ();
152+ DatasourceOperations datasourceOperations = getDatasourceOperations (realm , DataSourceResolver . STORE_TYPE_MAIN );
146153 try {
147154 // Run the set-up script to create the tables.
148155 datasourceOperations .executeScript (
@@ -155,8 +162,7 @@ public synchronized Map<String, PrincipalSecretsResult> bootstrapRealms(
155162 }
156163 initializeForRealm (
157164 datasourceOperations , realmContext , bootstrapOptions .rootCredentialsSet ());
158- PrincipalSecretsResult secretsResult =
159- bootstrapServiceAndCreatePolarisPrincipalForRealm (realmContext );
165+ PrincipalSecretsResult secretsResult = bootstrapServiceAndCreatePolarisPrincipalForRealm (realmContext );
160166 results .put (realm , secretsResult );
161167 }
162168 }
@@ -188,7 +194,8 @@ public Map<String, BaseResult> purgeRealms(Iterable<String> realms) {
188194 public synchronized PolarisMetaStoreManager getOrCreateMetaStoreManager (
189195 RealmContext realmContext ) {
190196 if (!metaStoreManagerMap .containsKey (realmContext .getRealmIdentifier ())) {
191- DatasourceOperations datasourceOperations = getDatasourceOperations ();
197+ DatasourceOperations datasourceOperations = getDatasourceOperations (realmContext .getRealmIdentifier (),
198+ DataSourceResolver .STORE_TYPE_MAIN );
192199 initializeForRealm (datasourceOperations , realmContext , null );
193200 checkPolarisServiceBootstrappedForRealm (realmContext );
194201 }
@@ -198,7 +205,8 @@ public synchronized PolarisMetaStoreManager getOrCreateMetaStoreManager(
198205 @ Override
199206 public synchronized BasePersistence getOrCreateSession (RealmContext realmContext ) {
200207 if (!sessionSupplierMap .containsKey (realmContext .getRealmIdentifier ())) {
201- DatasourceOperations datasourceOperations = getDatasourceOperations ();
208+ DatasourceOperations datasourceOperations = getDatasourceOperations (realmContext .getRealmIdentifier (),
209+ DataSourceResolver .STORE_TYPE_MAIN );
202210 initializeForRealm (datasourceOperations , realmContext , null );
203211 }
204212 checkPolarisServiceBootstrappedForRealm (realmContext );
@@ -219,33 +227,30 @@ public synchronized EntityCache getOrCreateEntityCache(
219227 }
220228
221229 /**
222- * This method bootstraps service for a given realm: i.e. creates all the needed entities in the
230+ * This method bootstraps service for a given realm: i.e. creates all the needed
231+ * entities in the
223232 * metastore and creates a root service principal.
224233 */
225234 private PrincipalSecretsResult bootstrapServiceAndCreatePolarisPrincipalForRealm (
226235 RealmContext realmContext ) {
227- // While bootstrapping we need to act as a fake privileged context since the real
236+ // While bootstrapping we need to act as a fake privileged context since the
237+ // real
228238 // CallContext may not have been resolved yet.
229- PolarisMetaStoreManager metaStoreManager =
230- metaStoreManagerMap .get (realmContext .getRealmIdentifier ());
239+ PolarisMetaStoreManager metaStoreManager = metaStoreManagerMap .get (realmContext .getRealmIdentifier ());
231240 BasePersistence metaStore = sessionSupplierMap .get (realmContext .getRealmIdentifier ()).get ();
232- PolarisCallContext polarisContext =
233- new PolarisCallContext (realmContext , metaStore , diagServices );
241+ PolarisCallContext polarisContext = new PolarisCallContext (realmContext , metaStore , diagServices );
234242
235- Optional <PrincipalEntity > preliminaryRootPrincipal =
236- metaStoreManager .findRootPrincipal (polarisContext );
243+ Optional <PrincipalEntity > preliminaryRootPrincipal = metaStoreManager .findRootPrincipal (polarisContext );
237244 if (preliminaryRootPrincipal .isPresent ()) {
238- String overrideMessage =
239- "It appears this metastore manager has already been bootstrapped. "
240- + "To continue bootstrapping, please first purge the metastore with the `purge` command." ;
245+ String overrideMessage = "It appears this metastore manager has already been bootstrapped. "
246+ + "To continue bootstrapping, please first purge the metastore with the `purge` command." ;
241247 LOGGER .error ("\n \n {} \n \n " , overrideMessage );
242248 throw new IllegalArgumentException (overrideMessage );
243249 }
244250
245251 metaStoreManager .bootstrapPolarisService (polarisContext );
246252
247- PrincipalEntity rootPrincipal =
248- metaStoreManager .findRootPrincipal (polarisContext ).orElseThrow ();
253+ PrincipalEntity rootPrincipal = metaStoreManager .findRootPrincipal (polarisContext ).orElseThrow ();
249254 return metaStoreManager .loadPrincipalSecrets (
250255 polarisContext ,
251256 rootPrincipal
@@ -254,18 +259,20 @@ private PrincipalSecretsResult bootstrapServiceAndCreatePolarisPrincipalForRealm
254259 }
255260
256261 /**
257- * In this method we check if Service was bootstrapped for a given realm, i.e. that all the
258- * entities were created (root principal, root principal role, etc) If service was not
259- * bootstrapped we are throwing IllegalStateException exception That will cause service to crash
260- * and force user to run Bootstrap command and initialize MetaStore and create all the required
262+ * In this method we check if Service was bootstrapped for a given realm, i.e.
263+ * that all the
264+ * entities were created (root principal, root principal role, etc) If service
265+ * was not
266+ * bootstrapped we are throwing IllegalStateException exception That will cause
267+ * service to crash
268+ * and force user to run Bootstrap command and initialize MetaStore and create
269+ * all the required
261270 * entities
262271 */
263272 private void checkPolarisServiceBootstrappedForRealm (RealmContext realmContext ) {
264- PolarisMetaStoreManager metaStoreManager =
265- metaStoreManagerMap .get (realmContext .getRealmIdentifier ());
273+ PolarisMetaStoreManager metaStoreManager = metaStoreManagerMap .get (realmContext .getRealmIdentifier ());
266274 BasePersistence metaStore = sessionSupplierMap .get (realmContext .getRealmIdentifier ()).get ();
267- PolarisCallContext polarisContext =
268- new PolarisCallContext (realmContext , metaStore , diagServices );
275+ PolarisCallContext polarisContext = new PolarisCallContext (realmContext , metaStore , diagServices );
269276
270277 Optional <PrincipalEntity > rootPrincipal = metaStoreManager .findRootPrincipal (polarisContext );
271278 if (rootPrincipal .isEmpty ()) {
0 commit comments