Skip to content
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

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

Closed
kworkbee opened this issue Feb 12, 2023 · 3 comments
Assignees
Labels

Comments

@kworkbee
Copy link

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.

@OlgaMaciaszek
Copy link
Collaborator

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.

@kworkbee
Copy link
Author

kworkbee commented Feb 20, 2023

@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();
});
}

@OlgaMaciaszek
Copy link
Collaborator

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.

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

No branches or pull requests

3 participants