-
Notifications
You must be signed in to change notification settings - Fork 715
Add Reactive retries for SC LoadBalancer #847
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
342cda8
Implement retry logic.
OlgaMaciaszek b0f4762
Fix retrying on next instance when RetryExhausted in same instance.
OlgaMaciaszek 85145dd
Fix retrying on next instance when RetryExhausted in same instance.
OlgaMaciaszek 2ec6268
Fix retrying on next instance when RetryExhausted in same instance.
OlgaMaciaszek 6536b6d
Move duplicated methods to utility class. Fix checkstyle.
OlgaMaciaszek 1b05fd1
Fix test.
OlgaMaciaszek 14b79e7
Add more tests.
OlgaMaciaszek 32bb645
Fix test.
OlgaMaciaszek 9de5b57
Add autoConfiguration.
OlgaMaciaszek 079e32e
Refactor and add javadocs.
OlgaMaciaszek f53e8b8
Add javadocs.
OlgaMaciaszek 47c0556
Use RetryAwareServiceInstanceListSupplier with reactive retries.
OlgaMaciaszek 767132c
Update properties.
OlgaMaciaszek 4e42230
Fix the docs.
OlgaMaciaszek 00dea77
Rename utility class.
OlgaMaciaszek 4cd60f7
Verify interactions in order.
OlgaMaciaszek 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
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
53 changes: 53 additions & 0 deletions
53
...a/org/springframework/cloud/client/loadbalancer/reactive/ExchangeFilterFunctionUtils.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,53 @@ | ||
/* | ||
* Copyright 2012-2020 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.client.loadbalancer.reactive; | ||
|
||
import java.net.URI; | ||
import java.util.Map; | ||
|
||
import org.springframework.web.reactive.function.client.ClientRequest; | ||
import org.springframework.web.reactive.function.client.ExchangeFilterFunction; | ||
|
||
/** | ||
* A utility class for load-balanced {@link ExchangeFilterFunction} instances. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
* @since 3.0.0 | ||
*/ | ||
public final class ExchangeFilterFunctionUtils { | ||
|
||
private ExchangeFilterFunctionUtils() { | ||
throw new IllegalStateException("Can't instantiate a utility class."); | ||
} | ||
|
||
static String getHint(String serviceId, Map<String, String> hints) { | ||
String defaultHint = hints.getOrDefault("default", "default"); | ||
String hintPropertyValue = hints.get(serviceId); | ||
return hintPropertyValue != null ? hintPropertyValue : defaultHint; | ||
} | ||
|
||
static ClientRequest buildClientRequest(ClientRequest request, URI uri) { | ||
return ClientRequest.create(request.method(), uri).headers(headers -> headers.addAll(request.headers())) | ||
.cookies(cookies -> cookies.addAll(request.cookies())) | ||
.attributes(attributes -> attributes.putAll(request.attributes())).body(request.body()).build(); | ||
} | ||
|
||
static String serviceInstanceUnavailableMessage(String serviceId) { | ||
return "LoadBalancer does not contain an instance for the service " + serviceId; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...pringframework/cloud/client/loadbalancer/reactive/LoadBalancedExchangeFilterFunction.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,29 @@ | ||
/* | ||
* Copyright 2012-2020 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.client.loadbalancer.reactive; | ||
|
||
import org.springframework.web.reactive.function.client.ExchangeFilterFunction; | ||
|
||
/** | ||
* A marker interface for load-balanced {@link ExchangeFilterFunction} instances. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
* @since 3.0.0 | ||
*/ | ||
public interface LoadBalancedExchangeFilterFunction extends ExchangeFilterFunction { | ||
|
||
} |
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
83 changes: 83 additions & 0 deletions
83
...java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerRetryContext.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,83 @@ | ||
/* | ||
* Copyright 2012-2020 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.client.loadbalancer.reactive; | ||
|
||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.reactive.function.client.ClientRequest; | ||
import org.springframework.web.reactive.function.client.ClientResponse; | ||
|
||
/** | ||
* Stores the data for a load-balanced call that is being retried. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
* @since 3.0.0 | ||
*/ | ||
class LoadBalancerRetryContext { | ||
|
||
private final ClientRequest request; | ||
|
||
private ClientResponse clientResponse; | ||
|
||
private Integer retriesSameServiceInstance = 0; | ||
|
||
private Integer retriesNextServiceInstance = 0; | ||
|
||
LoadBalancerRetryContext(ClientRequest request) { | ||
this.request = request; | ||
} | ||
|
||
ClientRequest getRequest() { | ||
return request; | ||
} | ||
|
||
ClientResponse getClientResponse() { | ||
return clientResponse; | ||
} | ||
|
||
void setClientResponse(ClientResponse clientResponse) { | ||
this.clientResponse = clientResponse; | ||
} | ||
|
||
Integer getRetriesSameServiceInstance() { | ||
return retriesSameServiceInstance; | ||
} | ||
|
||
void incrementRetriesSameServiceInstance() { | ||
retriesSameServiceInstance++; | ||
} | ||
|
||
void resetRetriesSameServiceInstance() { | ||
retriesSameServiceInstance = 0; | ||
} | ||
|
||
Integer getRetriesNextServiceInstance() { | ||
return retriesNextServiceInstance; | ||
} | ||
|
||
void incrementRetriesNextServiceInstance() { | ||
retriesNextServiceInstance++; | ||
} | ||
|
||
Integer getResponseStatusCode() { | ||
return clientResponse.statusCode().value(); | ||
} | ||
|
||
HttpMethod getRequestMethod() { | ||
return request.method(); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
.../java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerRetryPolicy.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,58 @@ | ||
/* | ||
* Copyright 2012-2020 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.client.loadbalancer.reactive; | ||
|
||
import org.springframework.http.HttpMethod; | ||
|
||
/** | ||
* Pluggable policy used to establish whether a given load-balanced call should be | ||
* retried. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
* @since 3.0.0 | ||
*/ | ||
public interface LoadBalancerRetryPolicy { | ||
|
||
/** | ||
* Return <code>true</code> to retry on the same service instance. | ||
* @param context the context for the retry operation | ||
* @return true to retry on the same service instance | ||
*/ | ||
boolean canRetrySameServiceInstance(LoadBalancerRetryContext context); | ||
|
||
/** | ||
* Return <code>true</code> to retry on the next service instance. | ||
* @param context the context for the retry operation | ||
* @return true to retry on the same service instance | ||
*/ | ||
boolean canRetryNextServiceInstance(LoadBalancerRetryContext context); | ||
|
||
/** | ||
* Return <code>true</code> to retry on the provided HTTP status code. | ||
* @param statusCode the HTTP status code | ||
* @return true to retry on the provided HTTP status code | ||
*/ | ||
boolean retryableStatusCode(int statusCode); | ||
|
||
/** | ||
* Return <code>true</code> to retry on the provided HTTP method. | ||
* @param method the HTTP request method | ||
* @return true to retry on the provided HTTP method | ||
*/ | ||
boolean canRetryOnMethod(HttpMethod method); | ||
|
||
} |
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.