-
Notifications
You must be signed in to change notification settings - Fork 715
feat: Multi-AZ Failover LoadBalancing #1212
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
Open
kworkbee
wants to merge
11
commits into
spring-cloud:main
Choose a base branch
from
kworkbee:loadbalancer/multi-az-failover
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7961e3c
test: Configures Multi-AZ Failover LoadBalancing Feature
kworkbee e4b63f5
feat: Configures Multi-AZ Failover LoadBalancing Feature
kworkbee 49d90ae
feat: Disable Multi-AZ Failover feature when cannot use Caching Feature
kworkbee 951ff0c
feat: New property spring.cloud.loadbalancer.secondary-zones
kworkbee fd5a363
style: New line
kworkbee 3028054
feat: Removes Optional / StringUtil
kworkbee 481305c
feat: Uses StringUtils.commaDelimitedListToStringArray instead
kworkbee 43b4d03
feat: Failed Instances only when connection failed or 5xx error will …
kworkbee 3581edf
chore: Rename Test method
kworkbee 0d6d1e4
test: Fixes Build Error
kworkbee f7e439e
Merge branch 'main' into loadbalancer/multi-az-failover
kworkbee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
84 changes: 84 additions & 0 deletions
84
.../springframework/cloud/loadbalancer/config/LoadBalancerCacheDataManagerConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.loadbalancer.config; | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.cache.caffeine.CaffeineCacheManager; | ||
import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager; | ||
import org.springframework.cloud.loadbalancer.core.LoadBalancerCacheDataManager; | ||
import org.springframework.cloud.loadbalancer.core.MultiAZFailoverLoadBalancerCacheDataManager; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.DependsOn; | ||
|
||
/** | ||
* An AutoConfiguration that provides a {@link LoadBalancerCacheDataManager} bean for | ||
* adding ServiceInstance into a cache. | ||
* | ||
* @author Jiwon Jeon | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@AutoConfigureAfter(LoadBalancerCacheAutoConfiguration.class) | ||
@AutoConfigureBefore(LoadBalancerLifecycleConfiguration.class) | ||
public class LoadBalancerCacheDataManagerConfiguration { | ||
|
||
@Bean | ||
@DependsOn("caffeineLoadBalancerCacheManager") | ||
@ConditionalOnClass({ Caffeine.class, CaffeineCacheManager.class }) | ||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "multi-az-failover") | ||
LoadBalancerCacheDataManager multiAZFailoverCaffeineLoadBalancerCacheDataManager(ApplicationContext context) { | ||
return new MultiAZFailoverLoadBalancerCacheDataManager( | ||
context.getBean("caffeineLoadBalancerCacheManager", LoadBalancerCacheManager.class)); | ||
} | ||
|
||
@Bean | ||
@DependsOn("defaultLoadBalancerCacheManager") | ||
@Conditional(OnCaffeineCacheMissingCondition.class) | ||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "multi-az-failover") | ||
LoadBalancerCacheDataManager multiAZFailoverDefaultLoadBalancerCacheDataManager(ApplicationContext context) { | ||
return new MultiAZFailoverLoadBalancerCacheDataManager( | ||
context.getBean("defaultLoadBalancerCacheManager", LoadBalancerCacheManager.class)); | ||
} | ||
|
||
static final class OnCaffeineCacheMissingCondition extends AnyNestedCondition { | ||
|
||
private OnCaffeineCacheMissingCondition() { | ||
super(ConfigurationPhase.REGISTER_BEAN); | ||
} | ||
|
||
@ConditionalOnMissingClass("com.github.benmanes.caffeine.cache.Caffeine") | ||
static class CaffeineClassMissing { | ||
|
||
} | ||
|
||
@ConditionalOnMissingClass("org.springframework.cache.caffeine.CaffeineCacheManager") | ||
static class CaffeineCacheManagerClassMissing { | ||
|
||
} | ||
|
||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...ava/org/springframework/cloud/loadbalancer/config/LoadBalancerLifecycleConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.loadbalancer.config; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancerLifecycle; | ||
import org.springframework.cloud.loadbalancer.core.LoadBalancerCacheDataManager; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* An AutoConfiguration that provides {@link LoadBalancerLifecycle} bean defining | ||
* behaviors before and after load-balancing. | ||
* | ||
* @author Jiwon Jeon | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@AutoConfigureAfter({ LoadBalancerCacheAutoConfiguration.class, LoadBalancerCacheDataManagerConfiguration.class }) | ||
public class LoadBalancerLifecycleConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnBean(LoadBalancerCacheDataManager.class) | ||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "multi-az-failover") | ||
LoadBalancerLifecycle multiAZFailoverLoadBalancerLifecycle(LoadBalancerCacheDataManager cacheDataManager) { | ||
return new MultiAZFailoverLoadBalancerLifecycle(cacheDataManager); | ||
} | ||
|
||
} |
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
84 changes: 84 additions & 0 deletions
84
...a/org/springframework/cloud/loadbalancer/config/MultiAZFailoverLoadBalancerLifecycle.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.loadbalancer.config; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.CompletionContext; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancerLifecycle; | ||
import org.springframework.cloud.client.loadbalancer.Request; | ||
import org.springframework.cloud.client.loadbalancer.Response; | ||
import org.springframework.cloud.client.loadbalancer.ResponseData; | ||
import org.springframework.cloud.loadbalancer.core.LoadBalancerCacheDataManager; | ||
import org.springframework.web.reactive.result.view.RequestContext; | ||
|
||
/** | ||
* A LoadBalancerLifecycle implementation that defines behaviors before and after | ||
* load-balancing, considering Multi-AZ failover. | ||
* | ||
* @author Jiwon Jeon | ||
*/ | ||
public class MultiAZFailoverLoadBalancerLifecycle | ||
implements LoadBalancerLifecycle<RequestContext, ResponseData, ServiceInstance> { | ||
|
||
private static final Log LOG = LogFactory.getLog(MultiAZFailoverLoadBalancerLifecycle.class); | ||
|
||
private final LoadBalancerCacheDataManager cacheDataManager; | ||
|
||
public MultiAZFailoverLoadBalancerLifecycle(LoadBalancerCacheDataManager cacheDataManager) { | ||
this.cacheDataManager = cacheDataManager; | ||
} | ||
|
||
@Override | ||
public boolean supports(Class requestContextClass, Class responseClass, Class serverTypeClass) { | ||
return RequestContext.class.isAssignableFrom(requestContextClass) | ||
&& ResponseData.class.isAssignableFrom(responseClass) | ||
&& ServiceInstance.class.isAssignableFrom(serverTypeClass); | ||
} | ||
|
||
@Override | ||
public void onStart(Request<RequestContext> request) { | ||
} | ||
|
||
@Override | ||
public void onStartRequest(Request<RequestContext> request, Response<ServiceInstance> lbResponse) { | ||
} | ||
|
||
@Override | ||
public void onComplete(CompletionContext<ResponseData, ServiceInstance, RequestContext> completionContext) { | ||
final CompletionContext.Status status = completionContext.status(); | ||
final ServiceInstance instance = completionContext.getLoadBalancerResponse().getServer(); | ||
|
||
if (CompletionContext.Status.DISCARD.equals(status)) { | ||
if (LOG.isWarnEnabled()) { | ||
LOG.warn("Any instance selected. Cache will be evicted..."); | ||
} | ||
|
||
cacheDataManager.clear(); | ||
} | ||
else if (CompletionContext.Status.FAILED.equals(status)) { | ||
if (LOG.isErrorEnabled()) { | ||
LOG.error(String.format("Requesting to [%s] failed", instance)); | ||
} | ||
|
||
cacheDataManager.putInstance(instance); | ||
} | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...c/main/java/org/springframework/cloud/loadbalancer/core/LoadBalancerCacheDataManager.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.loadbalancer.core; | ||
|
||
import org.springframework.cache.Cache; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager; | ||
|
||
/** | ||
* An interface allows adding ServiceInstance into cache through | ||
* {@link LoadBalancerCacheManager}. | ||
* | ||
* @author Jiwon Jeon | ||
*/ | ||
public interface LoadBalancerCacheDataManager { | ||
|
||
Cache getCache(); | ||
|
||
void clear(); | ||
|
||
<T> T getInstance(String key, Class<T> classType); | ||
|
||
void putInstance(ServiceInstance instance); | ||
|
||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.