Skip to content

Commit

Permalink
WIP reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Dec 13, 2023
1 parent 7c330bf commit 9a0db8b
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ public boolean isDataSourceCreated(String dataSourceName) {
return dataSources.containsKey(dataSourceName);
}

public boolean isDataSourceActive(String dataSourceName) {
return dataSources.containsKey(dataSourceName);
}

public Set<String> getActiveDataSourceNames() {
return dataSources.keySet();
}

public AgroalDataSource getDataSource(String dataSourceName) {
return dataSources.computeIfAbsent(dataSourceName, new Function<String, AgroalDataSource>() {
@Override
Expand Down Expand Up @@ -169,7 +161,7 @@ public AgroalDataSource doCreateDataSource(String dataSourceName, boolean failIf
.dataSources().get(dataSourceName).jdbc();
DataSourceRuntimeConfig dataSourceRuntimeConfig = dataSourcesRuntimeConfig.dataSources().get(dataSourceName);

if (!dataSourceRuntimeConfig.active()) {
if (!dataSourceSupport.getInactiveNames().contains(dataSourceName)) {
if (failIfInactive) {
throw new ConfigurationException(String.format(Locale.ROOT,
"Datasource '%s' was deactivated through configuration properties."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.quarkus.agroal.runtime.DataSources;
import io.quarkus.arc.Arc;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.datasource.runtime.DataSourcesHealthSupport;
import io.quarkus.datasource.runtime.DataSourcesSupport;

@Readiness
@ApplicationScoped
Expand All @@ -33,10 +33,10 @@ public class DataSourceHealthCheck implements HealthCheck {

@PostConstruct
protected void init() {
DataSourcesHealthSupport support = Arc.container().instance(DataSourcesHealthSupport.class)
DataSourcesSupport support = Arc.container().instance(DataSourcesSupport.class)
.get();
Set<String> names = support.getConfiguredNames();
Set<String> excludedNames = support.getExcludedNames();
Set<String> excludedNames = support.getHealthCheckExcludedNames();
Set<String> activeNames = dataSources.getActiveDataSourceNames();
for (String name : names) {
if (excludedNames.contains(name) || !activeNames.contains(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesHealthSupport;
import io.quarkus.datasource.runtime.DataSourcesHealthSupportRecorder;
import io.quarkus.datasource.runtime.DataSourcesSupport;
import io.quarkus.datasource.runtime.DataSourcesRecorder;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.annotations.BuildProducer;
Expand All @@ -20,11 +20,11 @@ public class DataSourcesExcludedFromHealthChecksProcessor {
@Record(STATIC_INIT)
void produceBean(
Capabilities capabilities,
DataSourcesHealthSupportRecorder recorder,
DataSourcesRecorder recorder,
DataSourcesBuildTimeConfig config,
BuildProducer<SyntheticBeanBuildItem> syntheticBeans) {
if (capabilities.isPresent(Capability.SMALLRYE_HEALTH)) {
syntheticBeans.produce(SyntheticBeanBuildItem.configure(DataSourcesHealthSupport.class)
syntheticBeans.produce(SyntheticBeanBuildItem.configure(DataSourcesSupport.class)
.scope(Singleton.class)
.unremovable()
.runtimeValue(recorder.configureDataSourcesHealthSupport(config))
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.datasource.runtime;

import static java.util.stream.Collectors.toUnmodifiableSet;

import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class DataSourcesRecorder {

public RuntimeValue<DataSourcesSupport> configureDataSourcesHealthSupport(
DataSourcesBuildTimeConfig config) {
Stream.Builder<String> configured = Stream.builder();
Stream.Builder<String> excludedForHealthChecks = Stream.builder();
for (Map.Entry<String, DataSourceBuildTimeConfig> dataSource : config.dataSources().entrySet()) {
// TODO this is wrong, as the default datasource could be configured without db-kind being set:
// it's inferred automatically for the default datasource when possible.
// We probably fail to register health checks for default datasources
// that don't have db-kind set right now; this needs to be tested and fixed.
if (dataSource.getValue().dbKind().isPresent()) {
configured.add(dataSource.getKey());
}
if (dataSource.getValue().healthExclude()) {
excludedForHealthChecks.add(dataSource.getKey());
}
}
Set<String> names = configured.build().collect(toUnmodifiableSet());
Set<String> excludedNames = excludedForHealthChecks.build().collect(toUnmodifiableSet());
return new RuntimeValue<>(new DataSourcesSupport(names, excludedNames));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.datasource.runtime;

import java.util.Set;

/**
* Helper class that holds the names of all configured data sources,
* along with the names of those that are inactive or excluded from health checks.
* <p>
* This is used by any feature that needs runtime access to data sources,
* e.g. Flyway/Liquibase or health check implementation classes.
*/
public class DataSourcesSupport {

private final Set<String> configuredNames;
private final Set<String> healthCheckExcludedNames;
private Set<String> inactiveNames;

public DataSourcesSupport(Set<String> configuredNames, Set<String> healthCheckExcludedNames) {
this.configuredNames = configuredNames;
this.healthCheckExcludedNames = healthCheckExcludedNames;
}


// TODO careful when using this, as it might (incorrectly) not include the default datasource.
// See TODO in code that calls the constructor of this class.
public Set<String> getConfiguredNames() {
return configuredNames;
}

public Set<String> getInactiveNames() {
return inactiveNames;
}

public Set<String> getHealthCheckExcludedNames() {
return healthCheckExcludedNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.datasource.runtime.DataSourcesHealthSupport;
import io.quarkus.datasource.runtime.DataSourcesSupport;
import io.quarkus.reactive.datasource.ReactiveDataSource;
import io.vertx.mutiny.db2client.DB2Pool;

Expand All @@ -37,8 +37,8 @@ class ReactiveDB2DataSourcesHealthCheck implements HealthCheck {
@PostConstruct
protected void init() {
ArcContainer container = Arc.container();
DataSourcesHealthSupport excluded = container.instance(DataSourcesHealthSupport.class).get();
Set<String> excludedNames = excluded.getExcludedNames();
DataSourcesSupport excluded = container.instance(DataSourcesSupport.class).get();
Set<String> excludedNames = excluded.getHealthCheckExcludedNames();
for (InstanceHandle<DB2Pool> handle : container.select(DB2Pool.class, Any.Literal.INSTANCE).handles()) {
String db2PoolName = getDB2PoolName(handle.getBean());
if (!excludedNames.contains(db2PoolName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.datasource.runtime.DataSourcesHealthSupport;
import io.quarkus.datasource.runtime.DataSourcesSupport;
import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceHealthCheck;
import io.vertx.mssqlclient.MSSQLPool;

Expand All @@ -26,8 +26,8 @@ public ReactiveMSSQLDataSourcesHealthCheck() {
@PostConstruct
protected void init() {
ArcContainer container = Arc.container();
DataSourcesHealthSupport excluded = container.instance(DataSourcesHealthSupport.class).get();
Set<String> excludedNames = excluded.getExcludedNames();
DataSourcesSupport excluded = container.instance(DataSourcesSupport.class).get();
Set<String> excludedNames = excluded.getHealthCheckExcludedNames();
for (InstanceHandle<MSSQLPool> handle : container.select(MSSQLPool.class, Any.Literal.INSTANCE).handles()) {
String poolName = getPoolName(handle.getBean());
if (!excludedNames.contains(poolName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.datasource.runtime.DataSourcesHealthSupport;
import io.quarkus.datasource.runtime.DataSourcesSupport;
import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceHealthCheck;
import io.vertx.mysqlclient.MySQLPool;

Expand All @@ -26,8 +26,8 @@ public ReactiveMySQLDataSourcesHealthCheck() {
@PostConstruct
protected void init() {
ArcContainer container = Arc.container();
DataSourcesHealthSupport excluded = container.instance(DataSourcesHealthSupport.class).get();
Set<String> excludedNames = excluded.getExcludedNames();
DataSourcesSupport excluded = container.instance(DataSourcesSupport.class).get();
Set<String> excludedNames = excluded.getHealthCheckExcludedNames();
for (InstanceHandle<MySQLPool> handle : container.select(MySQLPool.class, Any.Literal.INSTANCE).handles()) {
String poolName = getPoolName(handle.getBean());
if (!excludedNames.contains(poolName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.datasource.runtime.DataSourcesHealthSupport;
import io.quarkus.datasource.runtime.DataSourcesSupport;
import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceHealthCheck;
import io.vertx.oracleclient.OraclePool;

Expand All @@ -26,8 +26,8 @@ public ReactiveOracleDataSourcesHealthCheck() {
@PostConstruct
protected void init() {
ArcContainer container = Arc.container();
DataSourcesHealthSupport excluded = container.instance(DataSourcesHealthSupport.class).get();
Set<String> excludedNames = excluded.getExcludedNames();
DataSourcesSupport excluded = container.instance(DataSourcesSupport.class).get();
Set<String> excludedNames = excluded.getHealthCheckExcludedNames();
for (InstanceHandle<OraclePool> handle : container.select(OraclePool.class, Any.Literal.INSTANCE).handles()) {
String poolName = getPoolName(handle.getBean());
if (!excludedNames.contains(poolName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.datasource.runtime.DataSourcesHealthSupport;
import io.quarkus.datasource.runtime.DataSourcesSupport;
import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceHealthCheck;
import io.vertx.pgclient.PgPool;

Expand All @@ -26,8 +26,8 @@ public ReactivePgDataSourcesHealthCheck() {
@PostConstruct
protected void init() {
ArcContainer container = Arc.container();
DataSourcesHealthSupport excluded = container.instance(DataSourcesHealthSupport.class).get();
Set<String> excludedNames = excluded.getExcludedNames();
DataSourcesSupport excluded = container.instance(DataSourcesSupport.class).get();
Set<String> excludedNames = excluded.getHealthCheckExcludedNames();
for (InstanceHandle<PgPool> handle : container.select(PgPool.class, Any.Literal.INSTANCE).handles()) {
String poolName = getPoolName(handle.getBean());
if (!excludedNames.contains(poolName)) {
Expand Down

0 comments on commit 9a0db8b

Please sign in to comment.