Skip to content

Provide health for an AbstractRoutingDataSource's resolved targets #25708

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

Closed
wants to merge 1 commit into from
Closed
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
Expand Up @@ -17,19 +17,19 @@
package org.springframework.boot.actuate.autoconfigure.jdbc;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.boot.actuate.health.CompositeHealthContributor;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.NamedContributor;
import org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -45,6 +45,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.util.Assert;

/**
* {@link EnableAutoConfiguration Auto-configuration} for
Expand All @@ -64,8 +65,7 @@
@ConditionalOnEnabledHealthIndicator("db")
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(DataSourceHealthIndicatorProperties.class)
public class DataSourceHealthContributorAutoConfiguration extends
CompositeHealthContributorConfiguration<AbstractHealthIndicator, DataSource> implements InitializingBean {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I broke out of the CHCC hierarchy as this stretched that contract a bit too much. We need the createIndicator to return HealthContributor rather than AbstractHealthIndicator to allow for the case where one of the datasource beans is actual the AbstractRoutingDataSource which in turn is composite contributor.

public class DataSourceHealthContributorAutoConfiguration implements InitializingBean {

private final Collection<DataSourcePoolMetadataProvider> metadataProviders;

Expand Down Expand Up @@ -94,10 +94,18 @@ public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources
return createContributor(dataSources);
}

@Override
protected AbstractHealthIndicator createIndicator(DataSource source) {
private HealthContributor createContributor(Map<String, DataSource> beans) {
Assert.notEmpty(beans, "Beans must not be empty");
if (beans.size() == 1) {
return createIndicator(beans.values().iterator().next());
}
return CompositeHealthContributor.fromMap(beans, this::createIndicator);
}

private HealthContributor createIndicator(DataSource source) {
if (source instanceof AbstractRoutingDataSource) {
return new RoutingDataSourceHealthIndicator();
AbstractRoutingDataSource routingDataSource = (AbstractRoutingDataSource) source;
return new RoutingDataSourceHealthIndicator(routingDataSource, this::createIndicator);
}
return new DataSourceHealthIndicator(source, getValidationQuery(source));
}
Expand All @@ -108,14 +116,29 @@ private String getValidationQuery(DataSource source) {
}

/**
* {@link HealthIndicator} used for {@link AbstractRoutingDataSource} beans where we
* can't actually query for the status.
* {@link CompositeHealthContributor} used for {@link AbstractRoutingDataSource} beans
* where the overall health is composed of a {@link DataSourceHealthIndicator} for
* each routed datasource.
*/
static class RoutingDataSourceHealthIndicator extends AbstractHealthIndicator {
static class RoutingDataSourceHealthIndicator implements CompositeHealthContributor {

private CompositeHealthContributor delegate;

RoutingDataSourceHealthIndicator(AbstractRoutingDataSource routingDataSource,
Function<DataSource, HealthContributor> indicatorFunction) {
Map<String, DataSource> routedDataSources = routingDataSource.getResolvedDataSources().entrySet().stream()
.collect(Collectors.toMap((e) -> e.getKey().toString(), Map.Entry::getValue));
this.delegate = CompositeHealthContributor.fromMap(routedDataSources, indicatorFunction);
}

@Override
public HealthContributor getContributor(String name) {
return this.delegate.getContributor(name);
}

@Override
protected void doHealthCheck(Builder builder) throws Exception {
builder.unknown().withDetail("routing", true);
public Iterator<NamedContributor<HealthContributor>> iterator() {
return this.delegate.iterator();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.boot.actuate.autoconfigure.jdbc;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.junit.jupiter.api.Test;
Expand All @@ -38,6 +41,7 @@
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -94,9 +98,16 @@ void runWithRoutingAndEmbeddedDataSourceShouldNotIncludeRoutingDataSourceWhenIgn
}

@Test
void runWithOnlyRoutingDataSourceShouldIncludeRoutingDataSource() {
this.contextRunner.withUserConfiguration(RoutingDataSourceConfig.class)
.run((context) -> assertThat(context).hasSingleBean(RoutingDataSourceHealthIndicator.class));
void runWithOnlyRoutingDataSourceShouldIncludeRoutingDataSourceWithComposedIndicators() {
this.contextRunner.withUserConfiguration(RoutingDataSourceConfig.class).run((context) -> {
assertThat(context).hasSingleBean(RoutingDataSourceHealthIndicator.class);
RoutingDataSourceHealthIndicator routingHealthContributor = context
.getBean(RoutingDataSourceHealthIndicator.class);
assertThat(routingHealthContributor.getContributor("one")).isInstanceOf(DataSourceHealthIndicator.class);
assertThat(routingHealthContributor.getContributor("two")).isInstanceOf(DataSourceHealthIndicator.class);
assertThat(routingHealthContributor.iterator()).toIterable().extracting("name")
.containsExactlyInAnyOrder("one", "two");
});
}

@Test
Expand Down Expand Up @@ -144,7 +155,12 @@ static class RoutingDataSourceConfig {

@Bean
AbstractRoutingDataSource routingDataSource() {
return mock(AbstractRoutingDataSource.class);
Map<Object, DataSource> dataSources = new HashMap<>();
dataSources.put("one", mock(DataSource.class));
dataSources.put("two", mock(DataSource.class));
AbstractRoutingDataSource routingDataSource = mock(AbstractRoutingDataSource.class);
given(routingDataSource.getResolvedDataSources()).willReturn(dataSources);
return routingDataSource;
}

}
Expand Down