Skip to content
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

Datasource "health-exclude" property should apply to reactive pools too #20273

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.agroal.deployment;

import static io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT;

import java.sql.Driver;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -35,7 +33,6 @@
import io.quarkus.datasource.deployment.spi.DefaultDataSourceDbKindBuildItem;
import io.quarkus.datasource.runtime.DataSourceBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesExcludedFromHealthChecks;
import io.quarkus.datasource.runtime.DataSourcesRuntimeConfig;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
Expand Down Expand Up @@ -336,15 +333,8 @@ private String resolveDriver(String dataSourceName, String dbKind,
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
HealthBuildItem addHealthCheck(Capabilities capabilities, DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig,
AgroalRecorder recorder, BuildProducer<SyntheticBeanBuildItem> syntheticBeans) {
HealthBuildItem addHealthCheck(Capabilities capabilities, DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig) {
if (capabilities.isPresent(Capability.SMALLRYE_HEALTH)) {
syntheticBeans.produce(SyntheticBeanBuildItem.configure(DataSourcesExcludedFromHealthChecks.class)
.scope(Singleton.class)
.unremovable()
.supplier(recorder.dataSourcesExcludedFromHealthChecks(dataSourcesBuildTimeConfig))
.done());
return new HealthBuildItem("io.quarkus.agroal.runtime.health.DataSourceHealthCheck",
dataSourcesBuildTimeConfig.healthEnabled);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package io.quarkus.agroal.runtime;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import io.agroal.api.AgroalDataSource;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.datasource.runtime.DataSourceBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesExcludedFromHealthChecks;
import io.quarkus.datasource.runtime.DataSourcesRuntimeConfig;
import io.quarkus.runtime.annotations.Recorder;

Expand All @@ -35,24 +28,4 @@ public AgroalDataSource get() {
}
};
}

public Supplier<DataSourcesExcludedFromHealthChecks> dataSourcesExcludedFromHealthChecks(
DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig) {
return new Supplier<DataSourcesExcludedFromHealthChecks>() {
@Override
public DataSourcesExcludedFromHealthChecks get() {
List<String> excludedNames = new ArrayList<>();
if (dataSourcesBuildTimeConfig.defaultDataSource.healthExclude) {
excludedNames.add(DataSourceUtil.DEFAULT_DATASOURCE_NAME);
}
for (Map.Entry<String, DataSourceBuildTimeConfig> dataSource : dataSourcesBuildTimeConfig.namedDataSources
.entrySet()) {
if (dataSource.getValue().healthExclude) {
excludedNames.add(dataSource.getKey());
}
}
return new DataSourcesExcludedFromHealthChecks(excludedNames);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -32,7 +31,7 @@ protected void init() {
Set<Bean<?>> beans = Arc.container().beanManager().getBeans(DataSource.class);
DataSourcesExcludedFromHealthChecks excluded = Arc.container().instance(DataSourcesExcludedFromHealthChecks.class)
.get();
List<String> excludedNames = excluded.getExcludedNames();
Set<String> excludedNames = excluded.getExcludedNames();
for (Bean<?> bean : beans) {
if (bean.getName() == null) {
if (!excludedNames.contains(DataSourceUtil.DEFAULT_DATASOURCE_NAME)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkus.datasource.deployment;

import static io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT;

import javax.inject.Singleton;

import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesExcludedFromHealthChecks;
import io.quarkus.datasource.runtime.DataSourcesExcludedFromHealthChecksRecorder;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Record;

public class DataSourcesExcludedFromHealthChecksProcessor {

@BuildStep
@Record(STATIC_INIT)
void produceBean(
Capabilities capabilities,
DataSourcesExcludedFromHealthChecksRecorder recorder,
DataSourcesBuildTimeConfig config,
BuildProducer<SyntheticBeanBuildItem> syntheticBeans) {
if (capabilities.isPresent(Capability.SMALLRYE_HEALTH)) {
syntheticBeans.produce(SyntheticBeanBuildItem.configure(DataSourcesExcludedFromHealthChecks.class)
.scope(Singleton.class)
.unremovable()
.runtimeValue(recorder.configureDataSourcesExcludedFromHealthChecks(config))
.done());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.quarkus.datasource.runtime;

import java.util.List;
import java.util.Set;

public class DataSourcesExcludedFromHealthChecks {

private List<String> excludedNames;
private final Set<String> excludedNames;

public DataSourcesExcludedFromHealthChecks(List<String> names) {
this.excludedNames = names;
public DataSourcesExcludedFromHealthChecks(Set<String> excludedNames) {
this.excludedNames = excludedNames;
}

public List<String> getExcludedNames() {
public Set<String> getExcludedNames() {
return excludedNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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.datasource.common.runtime.DataSourceUtil;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class DataSourcesExcludedFromHealthChecksRecorder {

public RuntimeValue<DataSourcesExcludedFromHealthChecks> configureDataSourcesExcludedFromHealthChecks(
DataSourcesBuildTimeConfig config) {
Stream.Builder<String> builder = Stream.builder();
if (config.defaultDataSource.healthExclude) {
builder.add(DataSourceUtil.DEFAULT_DATASOURCE_NAME);
}
for (Map.Entry<String, DataSourceBuildTimeConfig> dataSource : config.namedDataSources.entrySet()) {
if (dataSource.getValue().healthExclude) {
builder.add(dataSource.getKey());
}
}
Set<String> excludedNames = builder.build().collect(toUnmodifiableSet());
return new RuntimeValue<>(new DataSourcesExcludedFromHealthChecks(excludedNames));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.quarkus.datasource.runtime.DataSourceBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesRuntimeConfig;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand Down Expand Up @@ -93,11 +95,16 @@ void registerServiceBinding(BuildProducer<DefaultDataSourceDbKindBuildItem> dbKi
*/
@BuildStep
void addHealthCheck(
Capabilities capabilities,
BuildProducer<HealthBuildItem> healthChecks,
DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig,
DataSourcesReactiveBuildTimeConfig dataSourcesReactiveBuildTimeConfig,
List<DefaultDataSourceDbKindBuildItem> defaultDataSourceDbKindBuildItems,
CurateOutcomeBuildItem curateOutcomeBuildItem) {
if (!capabilities.isPresent(Capability.SMALLRYE_HEALTH)) {
return;
}

if (!hasPools(dataSourcesBuildTimeConfig, dataSourcesReactiveBuildTimeConfig, defaultDataSourceDbKindBuildItems,
curateOutcomeBuildItem)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
Expand All @@ -16,8 +17,10 @@
import org.eclipse.microprofile.health.Readiness;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.datasource.runtime.DataSourcesExcludedFromHealthChecks;
import io.quarkus.reactive.datasource.ReactiveDataSource;
import io.vertx.mutiny.db2client.DB2Pool;

Expand All @@ -33,8 +36,14 @@ class ReactiveDB2DataSourcesHealthCheck implements HealthCheck {

@PostConstruct
protected void init() {
for (InstanceHandle<DB2Pool> handle : Arc.container().select(DB2Pool.class, Any.Literal.INSTANCE).handles()) {
db2Pools.put(getDB2PoolName(handle.getBean()), handle.get());
ArcContainer container = Arc.container();
DataSourcesExcludedFromHealthChecks excluded = container.instance(DataSourcesExcludedFromHealthChecks.class).get();
Set<String> excludedNames = excluded.getExcludedNames();
for (InstanceHandle<DB2Pool> handle : container.select(DB2Pool.class, Any.Literal.INSTANCE).handles()) {
String db2PoolName = getDB2PoolName(handle.getBean());
if (!excludedNames.contains(db2PoolName)) {
db2Pools.put(db2PoolName, handle.get());
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions extensions/reactive-mssql-client/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health-deployment</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.quarkus.datasource.runtime.DataSourceBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesRuntimeConfig;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand Down Expand Up @@ -90,11 +92,16 @@ void registerServiceBinding(BuildProducer<DefaultDataSourceDbKindBuildItem> dbKi
*/
@BuildStep
void addHealthCheck(
Capabilities capabilities,
BuildProducer<HealthBuildItem> healthChecks,
DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig,
DataSourcesReactiveBuildTimeConfig dataSourcesReactiveBuildTimeConfig,
List<DefaultDataSourceDbKindBuildItem> defaultDataSourceDbKindBuildItems,
CurateOutcomeBuildItem curateOutcomeBuildItem) {
if (!capabilities.isPresent(Capability.SMALLRYE_HEALTH)) {
return;
}

if (!hasPools(dataSourcesBuildTimeConfig, dataSourcesReactiveBuildTimeConfig, defaultDataSourceDbKindBuildItems,
curateOutcomeBuildItem)) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.reactive.mssql.client;

import org.hamcrest.CoreMatchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class DataSourceHealthCheckExclusionTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class))
.withConfigurationResource("application-datasources-with-health.properties");

@Test
public void testDataSourceHealthCheckExclusion() {
RestAssured.when().get("/q/health/ready")
.then()
.body("status", CoreMatchers.equalTo("UP"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
quarkus.datasource.db-kind=mssql
quarkus.datasource.username=sa
quarkus.datasource.password=A_Str0ng_Required_Password
quarkus.datasource.reactive.url=${reactive-mssql.url}
# this data source is broken, but will be excluded from the health check, so the overall check should pass
quarkus.datasource."broken".db-kind=mssql
quarkus.datasource."broken".username=BROKEN
quarkus.datasource."broken".password=A_Str0ng_Required_Password
quarkus.datasource."broken".reactive.url=${reactive-mssql.url}
quarkus.datasource."broken".health-exclude=true
quarkus.datasource.health.enabled=true
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.quarkus.reactive.mssql.client.runtime.health;

import java.util.Set;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;

import org.eclipse.microprofile.health.Readiness;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.datasource.runtime.DataSourcesExcludedFromHealthChecks;
import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceHealthCheck;
import io.vertx.mssqlclient.MSSQLPool;

Expand All @@ -21,8 +25,14 @@ public ReactiveMSSQLDataSourcesHealthCheck() {

@PostConstruct
protected void init() {
for (InstanceHandle<MSSQLPool> handle : Arc.container().select(MSSQLPool.class, Any.Literal.INSTANCE).handles()) {
addPool(getPoolName(handle.getBean()), handle.get());
ArcContainer container = Arc.container();
DataSourcesExcludedFromHealthChecks excluded = container.instance(DataSourcesExcludedFromHealthChecks.class).get();
Set<String> excludedNames = excluded.getExcludedNames();
for (InstanceHandle<MSSQLPool> handle : container.select(MSSQLPool.class, Any.Literal.INSTANCE).handles()) {
String poolName = getPoolName(handle.getBean());
if (!excludedNames.contains(poolName)) {
addPool(poolName, handle.get());
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions extensions/reactive-mysql-client/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health-deployment</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.quarkus.datasource.runtime.DataSourceBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
import io.quarkus.datasource.runtime.DataSourcesRuntimeConfig;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand Down Expand Up @@ -93,11 +95,16 @@ void registerServiceBinding(BuildProducer<DefaultDataSourceDbKindBuildItem> dbKi
*/
@BuildStep
void addHealthCheck(
Capabilities capabilities,
BuildProducer<HealthBuildItem> healthChecks,
DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig,
DataSourcesReactiveBuildTimeConfig dataSourcesReactiveBuildTimeConfig,
List<DefaultDataSourceDbKindBuildItem> defaultDataSourceDbKindBuildItems,
CurateOutcomeBuildItem curateOutcomeBuildItem) {
if (!capabilities.isPresent(Capability.SMALLRYE_HEALTH)) {
return;
}

if (!hasPools(dataSourcesBuildTimeConfig, dataSourcesReactiveBuildTimeConfig, defaultDataSourceDbKindBuildItems,
curateOutcomeBuildItem)) {
return;
Expand Down
Loading