Skip to content

Keep the config and catalog watch task schedulers local - #1056

Open
officialasishkumar wants to merge 1 commit into
spring-cloud:mainfrom
officialasishkumar:feat/723-local-watch-task-schedulers
Open

officialasishkumar wants to merge 1 commit into
spring-cloud:mainfrom
officialasishkumar:feat/723-local-watch-task-schedulers

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

Problem

The config watch and the catalog watch each register their ThreadPoolTaskScheduler as a TaskScheduler bean (configWatchTaskScheduler and catalogWatchTaskScheduler). These beans are visible to the whole application, so they affect its scheduling:

  • Spring Boot's TaskSchedulingAutoConfiguration backs off when a TaskScheduler bean exists. If it doesn't back off, the context has several TaskScheduler beans and injecting a TaskScheduler by type fails with a NoUniqueBeanDefinitionException.
  • If an application defines its own scheduler under another name, @Scheduled methods can no longer pick a scheduler. They fall back to a local single-threaded scheduler, as described in Make catalogWatchTaskScheduler/configWatchTaskScheduler more sophisticated #723.
  • The documented way to replace the scheduler only works with bean definition overriding enabled, which Spring Boot turns off by default.

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_NAME and ConsulCatalogWatchAutoConfiguration.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:

    @Bean(ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME)
    public TaskSchedulerWrapper<TaskScheduler> configWatchTaskScheduler() {
    	ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    	taskScheduler.setPoolSize(2);
    	return new TaskSchedulerWrapper<>(taskScheduler);
    }
  • A plain TaskScheduler bean with that name is still used, so existing customizations keep working.

  • The thread names stay the same (configWatchTaskScheduler-N and catalogWatchTaskScheduler-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 at ConsulCatalogWatchAutoConfiguration.

Testing

  • ConsulConfigAutoConfigurationTests and ConsulCatalogWatchAutoConfigurationTests are new. Each one covers:

    • the default scheduler is wrapped, is not a TaskScheduler bean, is used by the watch and keeps its thread name prefix
    • the wrapped scheduler shuts down when the context closes
    • an application TaskScheduler bean stays the only TaskScheduler, and @Scheduled methods run on it
    • Spring Boot's taskScheduler is still auto-configured
    • a custom TaskSchedulerWrapper with the bean name is used
    • a plain TaskScheduler with the bean name is still used

    On 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 @SpringBootTest apps with @EnableScheduling (not part of this PR):

    • With this change, taskScheduler is the only TaskScheduler bean. @Scheduled methods run on scheduling-1, a KV change triggers the RefreshEvent on configWatchTaskScheduler-1, and HeartbeatEvents are published on catalogWatchTaskScheduler-1.
    • On main, the same apps fail with NoUniqueBeanDefinitionException: ... expected single matching bean but found 2: taskScheduler,configWatchTaskScheduler, and the discovery app has both taskScheduler and catalogWatchTaskScheduler.
  • ConfigWatchTests, ConsulCatalogWatchTests, Checkstyle and spring-javaformat pass.

Fixes #723

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make catalogWatchTaskScheduler/configWatchTaskScheduler more sophisticated

2 participants