Skip to content
Merged
Prev Previous commit
Next Next commit
Refactor and remove deprecations, fix checkstyle.
  • Loading branch information
OlgaMaciaszek committed Sep 22, 2020
commit 8cb08d57cab638543f9f237f50b8ab40621be391
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
*/
public class InterceptorRetryPolicy implements RetryPolicy {

private HttpRequest request;
private final HttpRequest request;

private LoadBalancedRetryPolicy policy;
private final LoadBalancedRetryPolicy policy;

private ServiceInstanceChooser serviceInstanceChooser;
private final ServiceInstanceChooser serviceInstanceChooser;

private String serviceName;
private final String serviceName;

/**
* Creates a new retry policy.
Expand All @@ -56,21 +56,20 @@ public boolean canRetry(RetryContext context) {
LoadBalancedRetryContext lbContext = (LoadBalancedRetryContext) context;
if (lbContext.getRetryCount() == 0 && lbContext.getServiceInstance() == null) {
// We haven't even tried to make the request yet so return true so we do
lbContext.setServiceInstance(
this.serviceInstanceChooser.choose(this.serviceName));
lbContext.setServiceInstance(serviceInstanceChooser.choose(serviceName));
return true;
}
return this.policy.canRetryNextServer(lbContext);
return policy.canRetryNextServer(lbContext);
}

@Override
public RetryContext open(RetryContext parent) {
return new LoadBalancedRetryContext(parent, this.request);
return new LoadBalancedRetryContext(parent, request);
}

@Override
public void close(RetryContext context) {
this.policy.close((LoadBalancedRetryContext) context);
policy.close((LoadBalancedRetryContext) context);
}

@Override
Expand All @@ -80,7 +79,7 @@ public void registerThrowable(RetryContext context, Throwable throwable) {
// increases the retry count
lbContext.registerThrowable(throwable);
// let the policy know about the exception as well
this.policy.registerThrowable(lbContext, throwable);
policy.registerThrowable(lbContext, throwable);
}

@Override
Expand All @@ -94,25 +93,25 @@ public boolean equals(Object o) {

InterceptorRetryPolicy that = (InterceptorRetryPolicy) o;

if (!this.request.equals(that.request)) {
if (!request.equals(that.request)) {
return false;
}
if (!this.policy.equals(that.policy)) {
if (!policy.equals(that.policy)) {
return false;
}
if (!this.serviceInstanceChooser.equals(that.serviceInstanceChooser)) {
if (!serviceInstanceChooser.equals(that.serviceInstanceChooser)) {
return false;
}
return this.serviceName.equals(that.serviceName);
return serviceName.equals(that.serviceName);

}

@Override
public int hashCode() {
int result = this.request.hashCode();
result = 31 * result + this.policy.hashCode();
result = 31 * result + this.serviceInstanceChooser.hashCode();
result = 31 * result + this.serviceName.hashCode();
int result = request.hashCode();
result = 31 * result + policy.hashCode();
result = 31 * result + serviceInstanceChooser.hashCode();
result = 31 * result + serviceName.hashCode();
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class LoadBalancerRetryProperties {
private boolean enabled = true;

/**
* Indicates retries should be attempted on operations other than {@link HttpMethod#GET}.
* Indicates retries should be attempted on operations other than
* {@link HttpMethod#GET}.
*/
private boolean retryOnAllOperations = false;

Expand All @@ -42,8 +43,8 @@ public class LoadBalancerRetryProperties {
private int maxRetriesOnSameServiceInstance = 0;

/**
* Number of retries to be executed on the next <code>ServiceInstance</code>.
* A <code>ServiceInstance</code> is chosen before each retry call.
* Number of retries to be executed on the next <code>ServiceInstance</code>. A
* <code>ServiceInstance</code> is chosen before each retry call.
*/
private int maxRetriesOnNextServiceInstance = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
*/
public class RetryLoadBalancerInterceptor implements ClientHttpRequestInterceptor {

private LoadBalancerClient loadBalancer;
private final LoadBalancerClient loadBalancer;

private LoadBalancerRetryProperties lbProperties;
private final LoadBalancerRetryProperties lbProperties;

private LoadBalancerRequestFactory requestFactory;
private final LoadBalancerRequestFactory requestFactory;

private LoadBalancedRetryFactory lbRetryFactory;
private final LoadBalancedRetryFactory lbRetryFactory;

public RetryLoadBalancerInterceptor(LoadBalancerClient loadBalancer,
LoadBalancerRetryProperties lbProperties,
Expand All @@ -65,8 +65,8 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
final String serviceName = originalUri.getHost();
Assert.state(serviceName != null,
"Request URI does not contain a valid hostname: " + originalUri);
final LoadBalancedRetryPolicy retryPolicy = this.lbRetryFactory
.createRetryPolicy(serviceName, this.loadBalancer);
final LoadBalancedRetryPolicy retryPolicy = lbRetryFactory
.createRetryPolicy(serviceName, loadBalancer);
RetryTemplate template = createRetryTemplate(serviceName, request, retryPolicy);
return template.execute(context -> {
ServiceInstance serviceInstance = null;
Expand All @@ -75,11 +75,11 @@ public ClientHttpResponse intercept(final HttpRequest request, final byte[] body
serviceInstance = lbContext.getServiceInstance();
}
if (serviceInstance == null) {
serviceInstance = this.loadBalancer.choose(serviceName);
serviceInstance = loadBalancer.choose(serviceName);
}
ClientHttpResponse response = RetryLoadBalancerInterceptor.this.loadBalancer
.execute(serviceName, serviceInstance,
this.requestFactory.createRequest(request, body, execution));
ClientHttpResponse response = loadBalancer.execute(serviceName,
serviceInstance,
requestFactory.createRequest(request, body, execution));
int statusCode = response.getRawStatusCode();
if (retryPolicy != null && retryPolicy.retryableStatusCode(statusCode)) {
byte[] bodyCopy = StreamUtils.copyToByteArray(response.getBody());
Expand All @@ -103,19 +103,17 @@ protected ClientHttpResponse createResponse(ClientHttpResponse response,
private RetryTemplate createRetryTemplate(String serviceName, HttpRequest request,
LoadBalancedRetryPolicy retryPolicy) {
RetryTemplate template = new RetryTemplate();
BackOffPolicy backOffPolicy = this.lbRetryFactory
.createBackOffPolicy(serviceName);
BackOffPolicy backOffPolicy = lbRetryFactory.createBackOffPolicy(serviceName);
template.setBackOffPolicy(
backOffPolicy == null ? new NoBackOffPolicy() : backOffPolicy);
template.setThrowLastExceptionOnExhausted(true);
RetryListener[] retryListeners = this.lbRetryFactory
.createRetryListeners(serviceName);
RetryListener[] retryListeners = lbRetryFactory.createRetryListeners(serviceName);
if (retryListeners != null && retryListeners.length != 0) {
template.setListeners(retryListeners);
}
template.setRetryPolicy(!this.lbProperties.isEnabled() || retryPolicy == null
template.setRetryPolicy(!lbProperties.isEnabled() || retryPolicy == null
? new NeverRetryPolicy() : new InterceptorRetryPolicy(request,
retryPolicy, this.loadBalancer, serviceName));
retryPolicy, loadBalancer, serviceName));
return template;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

import java.time.Duration;
import java.util.Map;
import java.util.Set;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedCaseInsensitiveMap;

/**
Expand Down Expand Up @@ -86,4 +83,5 @@ public void setInterval(Duration interval) {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpRequest;
import org.springframework.retry.RetryContext;

import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -49,90 +49,89 @@ public class InterceptorRetryPolicyTest {

@Before
public void setup() {
this.request = mock(HttpRequest.class);
this.policy = mock(LoadBalancedRetryPolicy.class);
this.serviceInstanceChooser = mock(ServiceInstanceChooser.class);
this.serviceName = "foo";
request = mock(HttpRequest.class);
policy = mock(LoadBalancedRetryPolicy.class);
serviceInstanceChooser = mock(ServiceInstanceChooser.class);
serviceName = "foo";
}

@After
public void teardown() {
this.request = null;
this.policy = null;
this.serviceInstanceChooser = null;
this.serviceName = null;
request = null;
policy = null;
serviceInstanceChooser = null;
serviceName = null;
}

@Test
public void canRetryBeforeExecution() throws Exception {
public void canRetryBeforeExecution() {
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(
this.request, this.policy, this.serviceInstanceChooser, this.serviceName);
request, policy, serviceInstanceChooser, serviceName);
LoadBalancedRetryContext context = mock(LoadBalancedRetryContext.class);
when(context.getRetryCount()).thenReturn(0);
ServiceInstance serviceInstance = mock(ServiceInstance.class);
when(this.serviceInstanceChooser.choose(eq(this.serviceName)))
.thenReturn(serviceInstance);
when(serviceInstanceChooser.choose(eq(serviceName))).thenReturn(serviceInstance);
then(interceptorRetryPolicy.canRetry(context)).isTrue();
verify(context, times(1)).setServiceInstance(eq(serviceInstance));

}

@Test
public void canRetryNextServer() throws Exception {
public void canRetryNextServer() {
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(
this.request, this.policy, this.serviceInstanceChooser, this.serviceName);
request, policy, serviceInstanceChooser, serviceName);
LoadBalancedRetryContext context = mock(LoadBalancedRetryContext.class);
when(context.getRetryCount()).thenReturn(1);
when(this.policy.canRetryNextServer(eq(context))).thenReturn(true);
when(policy.canRetryNextServer(eq(context))).thenReturn(true);
then(interceptorRetryPolicy.canRetry(context)).isTrue();
}

@Test
public void cannotRetry() throws Exception {
public void cannotRetry() {
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(
this.request, this.policy, this.serviceInstanceChooser, this.serviceName);
request, policy, serviceInstanceChooser, serviceName);
LoadBalancedRetryContext context = mock(LoadBalancedRetryContext.class);
when(context.getRetryCount()).thenReturn(1);
then(interceptorRetryPolicy.canRetry(context)).isFalse();
}

@Test
public void open() throws Exception {
public void open() {
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(
this.request, this.policy, this.serviceInstanceChooser, this.serviceName);
request, policy, serviceInstanceChooser, serviceName);
RetryContext context = interceptorRetryPolicy.open(null);
then(context).isInstanceOf(LoadBalancedRetryContext.class);
}

@Test
public void close() throws Exception {
public void close() {
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(
this.request, this.policy, this.serviceInstanceChooser, this.serviceName);
request, policy, serviceInstanceChooser, serviceName);
LoadBalancedRetryContext context = mock(LoadBalancedRetryContext.class);
interceptorRetryPolicy.close(context);
verify(this.policy, times(1)).close(eq(context));
verify(policy, times(1)).close(eq(context));
}

@Test
public void registerThrowable() throws Exception {
public void registerThrowable() {
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(
this.request, this.policy, this.serviceInstanceChooser, this.serviceName);
request, policy, serviceInstanceChooser, serviceName);
LoadBalancedRetryContext context = mock(LoadBalancedRetryContext.class);
Throwable thrown = new Exception();
interceptorRetryPolicy.registerThrowable(context, thrown);
verify(context, times(1)).registerThrowable(eq(thrown));
verify(this.policy, times(1)).registerThrowable(eq(context), eq(thrown));
verify(policy, times(1)).registerThrowable(eq(context), eq(thrown));
}

@Test
public void equals() throws Exception {
public void equals() {
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(
this.request, this.policy, this.serviceInstanceChooser, this.serviceName);
request, policy, serviceInstanceChooser, serviceName);
then(interceptorRetryPolicy.equals(null)).isFalse();
then(interceptorRetryPolicy.equals(new Object())).isFalse();
then(interceptorRetryPolicy.equals(interceptorRetryPolicy)).isTrue();
then(interceptorRetryPolicy.equals(new InterceptorRetryPolicy(this.request,
this.policy, this.serviceInstanceChooser, this.serviceName))).isTrue();
then(interceptorRetryPolicy.equals(new InterceptorRetryPolicy(request, policy,
serviceInstanceChooser, serviceName))).isTrue();
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
/*
* 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.loadbalancer.blocking.retry;

import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRetryProperties;
import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser;
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerProperties;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;

/**
* An implementation of {@link LoadBalancedRetryFactory} for {@link BlockingLoadBalancerClient}.
* An implementation of {@link LoadBalancedRetryFactory} for
* {@link BlockingLoadBalancerClient}.
*
* @author Olga Maciaszek-Sharma
* @since 2..2.6
*/
public class BlockingLoadBalancedRetryFactory implements LoadBalancedRetryFactory {

Expand All @@ -21,7 +38,10 @@ public BlockingLoadBalancedRetryFactory(LoadBalancerRetryProperties retryPropert
}

@Override
public LoadBalancedRetryPolicy createRetryPolicy(String serviceId, ServiceInstanceChooser serviceInstanceChooser) {
return new BlockingLoadBalancedRetryPolicy(serviceId, serviceInstanceChooser, retryProperties);
public LoadBalancedRetryPolicy createRetryPolicy(String serviceId,
ServiceInstanceChooser serviceInstanceChooser) {
return new BlockingLoadBalancedRetryPolicy(serviceId, serviceInstanceChooser,
retryProperties);
}

}
Loading