-
Notifications
You must be signed in to change notification settings - Fork 448
Introduce DataSourceResolver for multi-datasource support in JDBC per… #3960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
136e381
207aa4c
e024bea
96e7ff0
9a07e6e
75e4096
fccb254
9c22635
1e00328
493c386
6311fbe
a36e745
118e046
dc8ffda
7eb2304
7ceaa69
9df42be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.persistence.relational.jdbc; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import org.apache.polaris.core.context.RealmContext; | ||
|
|
||
| /** | ||
| * Service to resolve the correct {@link DataSource} for a given realm and store | ||
| * type. This enables | ||
| * isolating different workloads (e.g., entity metadata vs metrics vs events) | ||
| * into different | ||
| * physical databases or connection pools. | ||
| */ | ||
| public interface DataSourceResolver { | ||
|
|
||
| /** The type of store representing the workload pattern. */ | ||
| enum StoreType { | ||
| METASTORE, | ||
| METRICS, | ||
| EVENTS | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the DataSource for a given realm and store type. | ||
| * | ||
| * @param realmContext the realm context | ||
| * @param storeType the type of store (e.g., main, metrics, events) | ||
| * @return the resolved DataSource | ||
| */ | ||
| DataSource resolve(RealmContext realmContext, StoreType storeType); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm supportive on realm, but we will need to think about the storage type.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @flyrain Could you clarify your concerns regarding StoreType? Are you suggesting a more extensible approach (e.g., String identifiers) or perhaps routing at a different layer? Happy to refine this to better fit the project's direction.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my POV if we are to introduce |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,10 +72,15 @@ public class JdbcMetaStoreManagerFactory implements MetaStoreManagerFactory { | |
| final Map<String, Supplier<BasePersistence>> sessionSupplierMap = new HashMap<>(); | ||
|
|
||
| @Inject Clock clock; | ||
|
|
||
| @Inject PolarisDiagnostics diagnostics; | ||
|
|
||
| @Inject PolarisStorageIntegrationProvider storageIntegrationProvider; | ||
| @Inject Instance<DataSource> dataSource; | ||
|
|
||
| @Inject Instance<DataSourceResolver> dataSourceResolver; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this injected as |
||
|
|
||
| @Inject RelationalJdbcConfiguration relationalJdbcConfiguration; | ||
|
|
||
| @Inject RealmConfig realmConfig; | ||
|
|
||
| protected JdbcMetaStoreManagerFactory() {} | ||
|
|
@@ -121,10 +126,12 @@ private void initializeForRealm( | |
| metaStoreManagerMap.put(realmId, metaStoreManager); | ||
| } | ||
|
|
||
| public DatasourceOperations getDatasourceOperations() { | ||
| public DatasourceOperations getDatasourceOperations( | ||
| RealmContext realmContext, DataSourceResolver.StoreType storeType) { | ||
| DatasourceOperations databaseOperations; | ||
| try { | ||
| databaseOperations = new DatasourceOperations(dataSource.get(), relationalJdbcConfiguration); | ||
| DataSource resolvedDs = dataSourceResolver.get().resolve(realmContext, storeType); | ||
| databaseOperations = new DatasourceOperations(resolvedDs, relationalJdbcConfiguration); | ||
| } catch (SQLException sqlException) { | ||
| throw new RuntimeException(sqlException); | ||
| } | ||
|
|
@@ -154,7 +161,8 @@ public synchronized Map<String, PrincipalSecretsResult> bootstrapRealms( | |
| for (String realm : bootstrapOptions.realms()) { | ||
| RealmContext realmContext = () -> realm; | ||
| if (!metaStoreManagerMap.containsKey(realm)) { | ||
| DatasourceOperations datasourceOperations = getDatasourceOperations(); | ||
| DatasourceOperations datasourceOperations = | ||
| getDatasourceOperations(realmContext, DataSourceResolver.StoreType.METASTORE); | ||
| int currentSchemaVersion = | ||
| JdbcBasePersistenceImpl.loadSchemaVersion(datasourceOperations, true); | ||
| int requestedSchemaVersion = JdbcBootstrapUtils.getRequestedSchemaVersion(bootstrapOptions); | ||
|
|
@@ -219,7 +227,8 @@ public Map<String, BaseResult> purgeRealms(Iterable<String> realms) { | |
| public synchronized PolarisMetaStoreManager getOrCreateMetaStoreManager( | ||
| RealmContext realmContext) { | ||
| if (!metaStoreManagerMap.containsKey(realmContext.getRealmIdentifier())) { | ||
| DatasourceOperations datasourceOperations = getDatasourceOperations(); | ||
| DatasourceOperations datasourceOperations = | ||
| getDatasourceOperations(realmContext, DataSourceResolver.StoreType.METASTORE); | ||
| initializeForRealm(datasourceOperations, realmContext, null); | ||
| checkPolarisServiceBootstrappedForRealm(realmContext); | ||
| } | ||
|
|
@@ -229,7 +238,8 @@ public synchronized PolarisMetaStoreManager getOrCreateMetaStoreManager( | |
| @Override | ||
| public synchronized BasePersistence getOrCreateSession(RealmContext realmContext) { | ||
| if (!sessionSupplierMap.containsKey(realmContext.getRealmIdentifier())) { | ||
| DatasourceOperations datasourceOperations = getDatasourceOperations(); | ||
| DatasourceOperations datasourceOperations = | ||
| getDatasourceOperations(realmContext, DataSourceResolver.StoreType.METASTORE); | ||
| initializeForRealm(datasourceOperations, realmContext, null); | ||
| } | ||
| checkPolarisServiceBootstrappedForRealm(realmContext); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.quarkus.common.config.jdbc; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
| import javax.sql.DataSource; | ||
| import org.apache.polaris.core.context.RealmContext; | ||
| import org.apache.polaris.persistence.relational.jdbc.DataSourceResolver; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Default implementation of {@link DataSourceResolver} that routes all realms and store types to a | ||
| * single default {@link DataSource}. This serves as both the production default and the base for | ||
| * multi-datasource extensions. | ||
| */ | ||
| @ApplicationScoped | ||
| public class DefaultDataSourceResolver implements DataSourceResolver { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDataSourceResolver.class); | ||
|
|
||
| private final DataSource defaultDataSource; | ||
|
|
||
| @Inject | ||
| public DefaultDataSourceResolver(DataSource defaultDataSource) { | ||
| this.defaultDataSource = defaultDataSource; | ||
| } | ||
|
|
||
| @Override | ||
| public DataSource resolve(RealmContext realmContext, StoreType storeType) { | ||
| LOGGER.debug( | ||
| "Using default DataSource for realm '{}' and store '{}'", | ||
| realmContext.getRealmIdentifier(), | ||
| storeType); | ||
| return defaultDataSource; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this contract is getting a bit ahead of the implementation. This PR only wires
METASTORE, but the SPI already publishesMETRICSandEVENTSas if those routing modes were part of the current supported surface. Would it make sense to keep the first step narrower and add newStoreTypevalues only once the corresponding paths are actually routed through this resolver?