Skip to content

Commit 394da9c

Browse files
committed
Introduce multiple datasources
This change enables multiple datasources in Polaris, with possibility of **runtime** activation of the desired datasource. This is achieved by: 1) Introducing a new enabler property: `polaris.persistence.relational.jdbc.datasource`, which points to a named datasource to activate. 2) Declaring, **at build time**, one named datasource for each JDBC driver shipped with the application. 3) Introducing a new `DataSourceActivator` config interceptor that will, at runtime, change the `active` setting of the target datasource to `true`, and all others to `false` (`active` is a runtime setting, contrary to `db-kind`). This change also makes the server ship with the H2 driver by default, since that's the pre-condition for an H2 datasource to be selected at runtime. This change will further enable **as future improvements**: - Using to JDBC + H2 by default for server images, while still allowing users to switch to PostgreSQL *without having to rebuild Polaris*. - Enabling the persistence to "plug" into not one, but many datasources, e.g. a specific datasource for main persistence, and a separate datasource for metrics. This was outlined in #3960 – but the machinery to make that happen was missing.
1 parent bec4a7e commit 394da9c

51 files changed

Lines changed: 773 additions & 191 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

helm/polaris/templates/configmap.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ data:
5151
5252
{{- /* Persistence */ -}}
5353
{{- $_ = set $map "polaris.persistence.type" .Values.persistence.type -}}
54+
{{- if eq .Values.persistence.type "relational-jdbc" -}}
55+
{{- $_ = set $map "polaris.persistence.relational.jdbc.datasource" .Values.persistence.relationalJdbc.datasource -}}
56+
{{- end -}}
5457
{{- if eq .Values.persistence.type "nosql" -}}
5558
{{- $_ = set $map "polaris.persistence.nosql.backend" .Values.persistence.nosql.backend -}}
5659
{{- $_ = set $map "quarkus.mongodb.database" .Values.persistence.nosql.database -}}

helm/polaris/templates/deployment.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ spec:
9595
{{- include "polaris.secretToEnv" (list .Values.storage.secret "awsAccessKeyId" "polaris.storage.aws.access-key") | indent 12 -}}
9696
{{- include "polaris.secretToEnv" (list .Values.storage.secret "awsSecretAccessKey" "polaris.storage.aws.secret-key") | indent 12 -}}
9797
{{- include "polaris.secretToEnv" (list .Values.storage.secret "gcpToken" "polaris.storage.gcp.token") | indent 12 -}}
98-
{{- include "polaris.secretToEnv" (list .Values.persistence.relationalJdbc.secret "username" "quarkus.datasource.username") | indent 12 -}}
99-
{{- include "polaris.secretToEnv" (list .Values.persistence.relationalJdbc.secret "password" "quarkus.datasource.password") | indent 12 -}}
100-
{{- include "polaris.secretToEnv" (list .Values.persistence.relationalJdbc.secret "jdbcUrl" "quarkus.datasource.jdbc.url") | indent 12 -}}
98+
{{- include "polaris.secretToEnv" (list .Values.persistence.relationalJdbc.secret "username" (printf "quarkus.datasource.%s.username" .Values.persistence.relationalJdbc.datasource)) | indent 12 -}}
99+
{{- include "polaris.secretToEnv" (list .Values.persistence.relationalJdbc.secret "password" (printf "quarkus.datasource.%s.password" .Values.persistence.relationalJdbc.datasource)) | indent 12 -}}
100+
{{- include "polaris.secretToEnv" (list .Values.persistence.relationalJdbc.secret "jdbcUrl" (printf "quarkus.datasource.%s.jdbc.url" .Values.persistence.relationalJdbc.datasource)) | indent 12 -}}
101101
{{- include "polaris.secretToEnv" (list .Values.persistence.nosql.secret "connectionString" "quarkus.mongodb.connection-string") | indent 12 -}}
102102
{{- include "polaris.secretToEnv" (list .Values.oidc.client.secret "key" "quarkus.oidc.credentials.secret") | indent 12 -}}
103103
{{- if .Values.extraEnv -}}

helm/polaris/tests/configmap_test.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ tests:
122122

123123
- it: should configure relational-jdbc persistence
124124
set:
125-
persistence: { type: "relational-jdbc", relationalJdbc: { secret: { name: "polaris-persistence" } } }
125+
persistence: { type: "relational-jdbc", relationalJdbc: { datasource: "postgresql", secret: { name: "polaris-persistence" } } }
126126
asserts:
127127
- matchRegex: { path: 'data["application.properties"]', pattern: "polaris.persistence.type=relational-jdbc" }
128+
- matchRegex: { path: 'data["application.properties"]', pattern: "polaris.persistence.relational.jdbc.datasource=postgresql" }
128129

129130
- it: should configure nosql persistence with default values
130131
set:

helm/polaris/tests/deployment_test.yaml

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,28 +1470,58 @@ tests:
14701470
- it: should set relational-jdbc persistence environment variables
14711471
template: deployment.yaml
14721472
set:
1473-
persistence: { type: "relational-jdbc", relationalJdbc: { secret: { name: "polaris-persistence", username: "username", password: "password", jdbcUrl: "jdbcUrl" } } }
1473+
persistence: { type: "relational-jdbc", relationalJdbc: { datasource: "postgresql", secret: { name: "polaris-persistence", username: "username", password: "password", jdbcUrl: "jdbcUrl" } } }
14741474
asserts:
14751475
- contains:
14761476
path: spec.template.spec.containers[0].env
14771477
content:
1478-
name: quarkus.datasource.username
1478+
name: quarkus.datasource.postgresql.username
14791479
valueFrom:
14801480
secretKeyRef:
14811481
name: polaris-persistence
14821482
key: username
14831483
- contains:
14841484
path: spec.template.spec.containers[0].env
14851485
content:
1486-
name: quarkus.datasource.password
1486+
name: quarkus.datasource.postgresql.password
14871487
valueFrom:
14881488
secretKeyRef:
14891489
name: polaris-persistence
14901490
key: password
14911491
- contains:
14921492
path: spec.template.spec.containers[0].env
14931493
content:
1494-
name: quarkus.datasource.jdbc.url
1494+
name: quarkus.datasource.postgresql.jdbc.url
1495+
valueFrom:
1496+
secretKeyRef:
1497+
name: polaris-persistence
1498+
key: jdbcUrl
1499+
1500+
- it: should set relational-jdbc persistence environment variables for custom datasource name
1501+
template: deployment.yaml
1502+
set:
1503+
persistence: { type: "relational-jdbc", relationalJdbc: { datasource: "mydb", secret: { name: "polaris-persistence", username: "username", password: "password", jdbcUrl: "jdbcUrl" } } }
1504+
asserts:
1505+
- contains:
1506+
path: spec.template.spec.containers[0].env
1507+
content:
1508+
name: quarkus.datasource.mydb.username
1509+
valueFrom:
1510+
secretKeyRef:
1511+
name: polaris-persistence
1512+
key: username
1513+
- contains:
1514+
path: spec.template.spec.containers[0].env
1515+
content:
1516+
name: quarkus.datasource.mydb.password
1517+
valueFrom:
1518+
secretKeyRef:
1519+
name: polaris-persistence
1520+
key: password
1521+
- contains:
1522+
path: spec.template.spec.containers[0].env
1523+
content:
1524+
name: quarkus.datasource.mydb.jdbc.url
14951525
valueFrom:
14961526
secretKeyRef:
14971527
name: polaris-persistence

helm/polaris/values.schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,10 @@
10011001
"relationalJdbc": {
10021002
"type": "object",
10031003
"properties": {
1004+
"datasource": {
1005+
"description": "The name of the Quarkus named datasource to use for relational persistence. Must match one of the built-in named datasources (h2, postgresql), or a custom datasource configured via extraEnv / extraConfig (in this case, you must use a custom Polaris server image including the custom datasource). Polaris will activate this datasource automatically.",
1006+
"type": "string"
1007+
},
10041008
"secret": {
10051009
"type": "object",
10061010
"properties": {

helm/polaris/values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,12 @@ persistence:
922922
type: in-memory # relational-jdbc, nosql
923923
# The configuration for the relational-jdbc persistence manager.
924924
relationalJdbc:
925+
# -- The name of the Quarkus named datasource to use for relational persistence.
926+
# Must match one of the built-in named datasources (h2, postgresql), or a custom datasource
927+
# configured via extraEnv / extraConfig (in this case, you must use a custom Polaris server
928+
# image including the custom datasource). Polaris will activate this datasource automatically.
929+
# @section -- Persistence
930+
datasource: postgresql
925931
# The secret name to pull the database connection properties from.
926932
secret:
927933
# -- The secret name to pull database connection properties from

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
import io.smallrye.common.annotation.Identifier;
2424
import jakarta.enterprise.context.ApplicationScoped;
25-
import jakarta.enterprise.inject.Instance;
26-
import jakarta.enterprise.inject.Produces;
2725
import jakarta.inject.Inject;
2826
import java.sql.SQLException;
2927
import java.time.Clock;
@@ -32,7 +30,6 @@
3230
import java.util.Optional;
3331
import java.util.Set;
3432
import java.util.concurrent.ConcurrentHashMap;
35-
import javax.sql.DataSource;
3633
import org.apache.polaris.core.PolarisCallContext;
3734
import org.apache.polaris.core.PolarisDiagnostics;
3835
import org.apache.polaris.core.config.BehaviorChangeConfiguration;
@@ -93,13 +90,6 @@ public class JdbcMetaStoreManagerFactory implements MetaStoreManagerFactory {
9390

9491
protected JdbcMetaStoreManagerFactory() {}
9592

96-
@Produces
97-
@ApplicationScoped
98-
static DatasourceOperations produceDatasourceOperations(
99-
Instance<DataSource> dataSource, RelationalJdbcConfiguration relationalJdbcConfiguration) {
100-
return new DatasourceOperations(dataSource.get(), relationalJdbcConfiguration);
101-
}
102-
10393
protected PrincipalSecretsGenerator secretsGenerator(
10494
String realmId, @Nullable RootCredentialsSet rootCredentialsSet) {
10595
if (rootCredentialsSet != null) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@
2121
import java.util.Optional;
2222

2323
public interface RelationalJdbcConfiguration {
24-
// max retries before giving up
24+
/** The maximum number of retries before giving up the operation. */
2525
Optional<Integer> maxRetries();
2626

27-
// max retry duration
27+
/** The maximum retry duration in milliseconds. */
2828
Optional<Long> maxDurationInMs();
2929

30-
// initial delay
30+
/** The initial retry delay. */
3131
Optional<Long> initialDelayInMs();
3232

3333
/**
3434
* Explicitly configured database type. If not specified, the database type will be inferred from
3535
* the JDBC connection metadata. Supported values: "postgresql", "cockroachdb", "h2"
3636
*/
3737
Optional<String> databaseType();
38+
39+
/** The datasource name to use. Required. */
40+
String dataSource();
3841
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ProductionReadinessCheck checkRelationalJdbc(
4040
return ProductionReadinessCheck.of(
4141
ProductionReadinessCheck.Error.of(
4242
"The current persistence (jdbc:h2) is intended for tests only.",
43-
"quarkus.datasource.jdbc.url"));
43+
"polaris.persistence.relational.jdbc.datasource"));
4444
}
4545
return ProductionReadinessCheck.OK;
4646
}

persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/JdbcGrantRecordsIdempotencyTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,10 @@ public Optional<Long> initialDelayInMs() {
154154
public Optional<String> databaseType() {
155155
return Optional.of("h2");
156156
}
157+
158+
@Override
159+
public String dataSource() {
160+
return "h2";
161+
}
157162
}
158163
}

0 commit comments

Comments
 (0)