spring-cloud/spring-cloud-commons

Q. Is there any reason why does LoadBalancerCacheManager Bean excluded from autowire candidates?

kworkbee opened this issue · 3 comments

Hi, I'm planning to implement a new feature so that LoadBalancer can switch to another zone if all instances within a particular zone fail to respond.

For acheiving this, the new ServiceInstanceListSupplier needs cache, which is why I wanted to inject LoadBalancerCacheManager.

I checked that the Bean is currently excluded from Autowire Candidates, and I wonder if there is a reason why it was written like this.

I wonder if it is okay to exclude that option to achieve what I intend, or if not, what other alternatives can be used.

I'm not sure what you mean by it being excluded from Autowire Candidates. The caching should work out of the box. If you're experiencing any issues with it, please provide a minimal, complete, verifiable example that reproduces the issue.

@OlgaMaciaszek

In LoadBalancerCacheAutoConfiguration, LoadBalancerCacheManager beans seem to be registered excluded from the autowire candidates.

        @Configuration(proxyBeanMethods = false)
	@ConditionalOnClass({ Caffeine.class, CaffeineCacheManager.class })
	protected static class CaffeineLoadBalancerCacheManagerConfiguration {

                // Here
		@Bean(autowireCandidate = false)
		@ConditionalOnMissingBean
		LoadBalancerCacheManager caffeineLoadBalancerCacheManager(LoadBalancerCacheProperties cacheProperties) {
			return new CaffeineBasedLoadBalancerCacheManager(cacheProperties);
		}

	}

	@Configuration(proxyBeanMethods = false)
	@Conditional(OnCaffeineCacheMissingCondition.class)
	@ConditionalOnClass(ConcurrentMapWithTimedEviction.class)
	protected static class DefaultLoadBalancerCacheManagerConfiguration {

                // Here
		@Bean(autowireCandidate = false)
		@ConditionalOnMissingBean
		LoadBalancerCacheManager defaultLoadBalancerCacheManager(LoadBalancerCacheProperties cacheProperties) {
			return new DefaultLoadBalancerCacheManager(cacheProperties);
		}

	}

Because of this configuration, the newly registered Bean cannot be injected with LoadBalancerCacheManager.

        @Bean
	@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "zone-failover-awareness")
        // LoadBalancerCacheManager cannot be injected
	LoadBalancerCacheDataManager zoneFailoverAwarenessLoadBalancerCacheManager(LoadBalancerCacheManager cacheManager) {
		return new ZoneFailoverAwarenessLoadBalancerCacheDataManager(cacheManager);
	}

If I include beans excluded from the autowire candidates again, the existing tests below will fail.

@Test
void defaultLoadBalancerCacheShouldNotOverrideCacheTypeSetting() {
ApplicationContextRunner contextRunner = noCaffeineRunner().withUserConfiguration(TestConfiguration.class)
.withPropertyValues("spring.cache.type=none");
contextRunner.run(context -> {
assertThat(context.getBeansOfType(CacheManager.class)).hasSize(2);
assertThat(context.getBean("defaultLoadBalancerCacheManager"))
.isInstanceOf(DefaultLoadBalancerCacheManager.class);
assertThat(context.getBeansOfType(CacheManager.class).get("cacheManager"))
.isInstanceOf(NoOpCacheManager.class);
});
}
@Test
void defaultLoadBalancerCacheShouldNotOverrideExistingCacheManager() {
ApplicationContextRunner contextRunner = noCaffeineRunner().withUserConfiguration(TestConfiguration.class);
contextRunner.run(context -> {
assertThat(context.getBeansOfType(CacheManager.class)).hasSize(2);
assertThat(context.getBean("cacheManager")).isInstanceOf(ConcurrentMapCacheManager.class);
assertThat(((CacheManager) context.getBean("cacheManager")).getCacheNames()).isEmpty();
assertThat(((CacheManager) context.getBean("defaultLoadBalancerCacheManager")).getCacheNames()).hasSize(1);
assertThat(((CacheManager) context.getBean("defaultLoadBalancerCacheManager")).getCacheNames())
.contains("CachingServiceInstanceListSupplierCache");
});
}
@Test
void shouldNotInstantiateDefaultLoadBalancerCacheIfDisabled() {
ApplicationContextRunner contextRunner = noCaffeineRunner()
.withPropertyValues("spring.cloud.loadbalancer.cache.enabled=false")
.withUserConfiguration(TestConfiguration.class);
contextRunner.run(context -> {
assertThat(context.getBeansOfType(CacheManager.class)).hasSize(1);
assertThat(context.getBean("cacheManager")).isInstanceOf(ConcurrentMapCacheManager.class);
assertThat(((CacheManager) context.getBean("cacheManager")).getCacheNames()).isEmpty();
});
}

These tests exactly show why this is done - so that the LoadBalancerCache does not override cache managers configured by users or taken up from other autoconfigurations.