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

Provide health for an AbstractRoutingDataSource's resolved targets #25708

wants to merge 1 commit into from

Conversation

onobc
Copy link
Contributor

@onobc onobc commented Mar 17, 2021

Fixes gh-22824

Screen Shot 2021-03-16 at 9 32 53 PM

@@ -64,8 +71,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.

Function<DataSource, HealthContributor> indicatorFunction) {
Map<String, DataSource> routedDataSources = routingDataSource.getResolvedDataSources().entrySet().stream()
.collect(toMap((e) -> e.getKey().toString(), Map.Entry::getValue));
delegate = CompositeHealthContributor.fromMap(routedDataSources, indicatorFunction);
Copy link
Contributor Author

@onobc onobc Mar 17, 2021

Choose a reason for hiding this comment

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

We could have simply returned this CompositeHealthContributor.fromMap(routedDataSources, indicatorFunction) from here and not created a 1st class type for it. However, having the RoutingDataSourceHealthIndicator is helpful for testing as well as for future extensions to the indicator when/if needed.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 17, 2021
@onobc
Copy link
Contributor Author

onobc commented Mar 17, 2021

@wilkinsona not sure if this is what you had in mind - let me know where we need to adjust. I have a couple of points/questions/caveats to discuss as well.

Relies on metadataprovider

As it is written, it relies on the available metadata providers to generate the validation query. Will it ever be the case that one of the routed datasources is not of the types we know about? Is that even possible?

I have since figured out this is not an issue as described here

By default, Spring Boot provides metadata for all supported data sources; you can add additional DataSourcePoolMetadataProvider beans if your favorite data source isn’t supported out of the box. See DataSourcePoolMetadataProvidersConfiguration for examples.

Duplicate indicators

If a routed datasource is also a registered @Bean DataSource then it will have a regular DataSourceIndicator created as well as be represented in the RoutingDataSourceHealthIndicator. The IsolationLevelDataSourceRouter seems like an example where the routed datasources are most likely already registered DataSource beans.

  • Option 1 - do nothing
  • Option 2 - add boolean property to "deduplicate" (ignore the routing datasource when its already represented)

One edge case on option 2 is when one of the routed datasources is already covered but another is not. Would we ignore only the covered target datasource in the routing health indicator? That would be strange.

I am inclined to go w/ option 1. I am curious to hear others thoughts as well.

@philwebb philwebb changed the title Use target datasources to detrermine health for AbstractRoutingSource Provide health for an AbstractRoutingDataSource's resolved targets Mar 23, 2021
@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 23, 2021
@philwebb philwebb added this to the 2.5.x milestone Mar 23, 2021
@wilkinsona wilkinsona self-assigned this Apr 10, 2021
@wilkinsona wilkinsona modified the milestones: 2.5.x, 2.5.0-RC1 Apr 10, 2021
@wilkinsona
Copy link
Member

This looks great. Thank you, @Bono007.

I am inclined to go w/ option 1. I am curious to hear others thoughts as well.

Agreed. I think this is the better option.

Having the RoutingDataSourceHealthIndicator is helpful for testing as well as for future extensions to the indicator when/if needed

Agreed again. When looking at the main code I was tempted to get rid of it, but having looked at the tests I think it's useful to have the distinction. It's also a package-private implementation detail so we can refine in the future as needed without worrying about backwards compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide health for an AbstractRoutingDataSource's resolved targets
4 participants