Repository navigation
Keep the config and catalog watch task schedulers local - #1056
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
ConsulConfigAutoConfiguration and ConsulCatalogWatchAutoConfiguration registered their single-threaded ThreadPoolTaskScheduler as TaskScheduler beans. Those beans leaked into the rest of the application: - Spring Boot's TaskSchedulingAutoConfiguration backs off when a TaskScheduler bean exists, or the context ends up with several TaskScheduler beans so that injecting a TaskScheduler by type fails with a NoUniqueBeanDefinitionException. - When an application defines its own TaskScheduler under another name, @scheduled methods can no longer pick a scheduler and fall back to a local single-threaded one. - Replacing the scheduler as documented required bean definition overriding, which Spring Boot disables by default. Register the schedulers wrapped in the Spring Cloud Commons TaskSchedulerWrapper instead, as Spring Cloud Kubernetes and Spring Cloud Vault already do. The wrapper initializes and destroys the scheduler with the context, but it is not a TaskScheduler, so it no longer affects the application's scheduling. Both beans keep their names and back off when the application defines a bean with the same name, so they can be replaced without bean overriding. A plain TaskScheduler bean with that name is still used, so existing customizations keep working. The thread name prefixes stay configWatchTaskScheduler- and catalogWatchTaskScheduler-. The docs describe how to provide a custom TaskSchedulerWrapper, and the discovery docs now reference the constant on the right class, ConsulCatalogWatchAutoConfiguration. Fixes spring-cloudgh-723 Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The config watch and the catalog watch each register their
ThreadPoolTaskScheduleras aTaskSchedulerbean (configWatchTaskSchedulerandcatalogWatchTaskScheduler). These beans are visible to the whole application, so they affect its scheduling:TaskSchedulingAutoConfigurationbacks off when aTaskSchedulerbean exists. If it doesn't back off, the context has severalTaskSchedulerbeans and injecting aTaskSchedulerby type fails with aNoUniqueBeanDefinitionException.@Scheduledmethods can no longer pick a scheduler. They fall back to a local single-threaded scheduler, as described in Make catalogWatchTaskScheduler/configWatchTaskScheduler more sophisticated #723.Solution
As suggested in #723, the schedulers are now registered wrapped in the Spring Cloud Commons
TaskSchedulerWrapper, the same way Spring Cloud Kubernetes and Spring Cloud Vault do it.The wrapper starts and shuts down the scheduler with the context. It is not a
TaskScheduler, so the application's own scheduler and Spring Boot's scheduler are left alone.The beans keep their names (
ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAMEandConsulCatalogWatchAutoConfiguration.CATALOG_WATCH_TASK_SCHEDULER_NAME). They back off when the application defines a bean with the same name, so they can be replaced without bean overriding:A plain
TaskSchedulerbean with that name is still used, so existing customizations keep working.The thread names stay the same (
configWatchTaskScheduler-NandcatalogWatchTaskScheduler-N).The config and discovery docs explain the new behaviour and show how to provide a custom scheduler. The discovery docs also pointed at
ConsulDiscoveryClientConfiguration.CATALOG_WATCH_TASK_SCHEDULER_NAME, which doesn't exist. They now point atConsulCatalogWatchAutoConfiguration.Testing
ConsulConfigAutoConfigurationTestsandConsulCatalogWatchAutoConfigurationTestsare new. Each one covers:TaskSchedulerbean, is used by the watch and keeps its thread name prefixTaskSchedulerbean stays the onlyTaskScheduler, and@Scheduledmethods run on ittaskScheduleris still auto-configuredTaskSchedulerWrapperwith the bean name is usedTaskSchedulerwith the bean name is still usedOn
main, all 10 tests except the two shutdown tests fail.I also ran an end-to-end check against a real Consul 1.22 agent, using temporary
@SpringBootTestapps with@EnableScheduling(not part of this PR):taskScheduleris the onlyTaskSchedulerbean.@Scheduledmethods run onscheduling-1, a KV change triggers theRefreshEventonconfigWatchTaskScheduler-1, andHeartbeatEvents are published oncatalogWatchTaskScheduler-1.main, the same apps fail withNoUniqueBeanDefinitionException: ... expected single matching bean but found 2: taskScheduler,configWatchTaskScheduler, and the discovery app has bothtaskSchedulerandcatalogWatchTaskScheduler.ConfigWatchTests,ConsulCatalogWatchTests, Checkstyle and spring-javaformat pass.Fixes #723