-
Notifications
You must be signed in to change notification settings - Fork 508
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 1 commit
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,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.persistence.relational.jdbc; | ||
|
|
||
| import java.util.Set; | ||
| import javax.sql.DataSource; | ||
|
|
||
| /** | ||
| * 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 { | ||
|
|
||
| String STORE_TYPE_MAIN = "main"; | ||
|
Subham-KRLX marked this conversation as resolved.
Outdated
|
||
| String STORE_TYPE_METRICS = "metrics"; | ||
| String STORE_TYPE_EVENTS = "events"; | ||
|
|
||
| /** | ||
| * Resolves the DataSource for a given realm and store type. | ||
| * | ||
| * @param realmId the realm identifier | ||
| * @param storeType the type of store (e.g., main, metrics, events) | ||
| * @return the resolved DataSource | ||
| */ | ||
| DataSource resolve(String realmId, String storeType); | ||
|
|
||
| /** | ||
| * Returns all unique DataSources managed by this resolver. | ||
| * This is useful for global operations like schema initialization against all | ||
| * data sources. | ||
| * | ||
| * @return a set of all DataSources | ||
| */ | ||
| Set<DataSource> getAllUniqueDataSources(); | ||
|
Subham-KRLX marked this conversation as resolved.
Outdated
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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 jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.inject.Instance; | ||
| import jakarta.inject.Inject; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import javax.sql.DataSource; | ||
| 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. | ||
| * | ||
| * <p> | ||
| * To enable per-realm or per-store datasource routing, this class can be | ||
| * extended or replaced | ||
| * with a custom implementation that resolves named datasources based on | ||
| * configuration. | ||
| */ | ||
| @ApplicationScoped | ||
|
Subham-KRLX marked this conversation as resolved.
|
||
| public class DefaultDataSourceResolver implements DataSourceResolver { | ||
|
dimas-b marked this conversation as resolved.
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 not sure I understand why this class was placed in
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. Actually the question of where to place implementations of this interface is a tricky one. When designing a true multi-datasource implementation, it will need the Quarkus Agroal extension, and therefore, would likely have to live in It would look like this: @ApplicationScoped
@Identifier("per-realm")
public class PerRealmDataSourceResolver implements DataSourceResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDataSourceResolver.class);
@Override
public DataSource resolve(RealmContext realmContext, StoreType storeType) {
String dataSourceName = findDataSourceName(realmContext, storeType);
LOGGER.debug(
"Using DataSource '{}' for realm '{}' and store '{}'",
dataSourceName,
realmContext.getRealmIdentifier(),
storeType);
return AgroalDataSourceUtil.dataSourceIfActive(dataSourceName)
.orElseThrow(
() ->
new IllegalStateException(
"DataSource '" + dataSourceName + "' is not active or does not exist"));
}
private String findDataSourceName(RealmContext realmContext, StoreType storeType) {
...
}
}But the problem is that I think this needs some more thinking.
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. How about we add a new module like The idea is for
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. If we use Downstream alternatives will have the the option of reusing
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. Do we have a compelling reason not to turn
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. Only "general principles" kind of reason 🙂 I tend to think it valuable to isolate runtime / CDI concerns from the basic code / logic of a particular component like JDBC persistence. In that regard, ideally, I believe the NoSQL Persistence impl and the OPA Authorizer follow similar principles (perhaps varying in degree, but similar in essence). I also proposed a similar refactoring for the Admin tool in #3947. I do not really know whether this matters for downstream Polaris users right now 🤔 🤷 In my personal experience, I find that it is much easier to include another module into a build than struggle with excluding intruding dependencies 😅 I know some concerns were raised about module proliferation in Polaris, but from my POV, Gradle is able to deal with a large number of modules very well, so I personally do not see it as an issue.
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. Looking at the evolution of this PR (and this class specifically), I think it's probably fine to move CDI/quarkus-related code into the |
||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDataSourceResolver.class); | ||
|
|
||
| private final Instance<DataSource> defaultDataSource; | ||
|
Subham-KRLX marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Inject | ||
| public DefaultDataSourceResolver(Instance<DataSource> defaultDataSource) { | ||
| this.defaultDataSource = defaultDataSource; | ||
| } | ||
|
|
||
| @Override | ||
| public DataSource resolve(String realmId, String storeType) { | ||
| LOGGER.debug("Using default DataSource for realm '{}' and store '{}'", realmId, storeType); | ||
| return defaultDataSource.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<DataSource> getAllUniqueDataSources() { | ||
| Set<DataSource> dataSources = new HashSet<>(); | ||
| dataSources.add(defaultDataSource.get()); | ||
| return dataSources; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.