-
Notifications
You must be signed in to change notification settings - Fork 826
[JAV-193] loadbalance retry supports to custom Retry Policy #198
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 | ||
* | ||
* http://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 io.servicecomb.loadbalance; | ||
|
||
import java.net.ConnectException; | ||
import java.net.SocketException; | ||
import java.net.SocketTimeoutException; | ||
import java.util.List; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.netflix.client.RetryHandler; | ||
import com.netflix.client.Utils; | ||
|
||
public class LoadBalanceRetryHandler implements RetryHandler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 代码提交不完整?看代码内容,只是实现了一个RetryHandler替换Default。需求的内容是让用户可以定制RetryHandler。 现在实现后,用户仍然无法定制RetryHandler,或者获取到RetryHandler,使用setRetryOnSameExceptions来定制。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当前PR实现是支持“业务期望某些业务异常,也要支持retryOnSame”,用户定制RetryHandler的需求在当前PR没有涉及 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议添加新的JIRA来实现客户定制RetryHandler机制。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 支持“业务期望某些业务异常,也要支持retryOnSame”, 即使只是支持这个场景,也实现的不对,开发者无法获取到LoadBalanceRetryHandler的引用,自然无法对retryOnSame进行任何定制。 这个建议提供integrated demo,在demo的springmvc/pojo项目中补充测试用例。 |
||
|
||
private final static LoadBalanceRetryHandler INSTANCE = new LoadBalanceRetryHandler(); | ||
|
||
private int retrySameServer; | ||
private int retryNextServer; | ||
private boolean retryEnabled; | ||
|
||
public static LoadBalanceRetryHandler getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
private List<Class<? extends Throwable>> retryOnSameExceptions = Lists | ||
.newArrayList(new Class[]{SocketTimeoutException.class, ConnectException.class}); | ||
|
||
private List<Class<? extends Throwable>> circuitRelated = Lists | ||
.newArrayList(new Class[]{SocketException.class, SocketTimeoutException.class}); | ||
|
||
private LoadBalanceRetryHandler() { | ||
this.retrySameServer = 0; | ||
this.retryNextServer = 0; | ||
this.retryEnabled = true; | ||
} | ||
|
||
public void setRetryRuntimeParams(int retrySameServer, int retryNextServer, boolean retryEnabled) { | ||
this.retrySameServer = retrySameServer; | ||
this.retryNextServer = retryNextServer; | ||
this.retryEnabled = retryEnabled; | ||
} | ||
|
||
public void addRetryOnSameExceptions(List<Class<? extends Throwable>> retryOnSameExceptions) { | ||
if (retryOnSameExceptions != null && retryOnSameExceptions.size() != 0) { | ||
this.retryOnSameExceptions.addAll(retryOnSameExceptions); | ||
} | ||
} | ||
|
||
public List<Class<? extends Throwable>> getRetryOnSameExceptions() { | ||
return retryOnSameExceptions; | ||
} | ||
|
||
public boolean removeRetryOnSameException(Class<? extends Throwable> exception) { | ||
return retryOnSameExceptions.remove(exception); | ||
} | ||
|
||
@Override | ||
public boolean isRetriableException(Throwable e, boolean sameServer) { | ||
return retryEnabled && (!sameServer || Utils.isPresentAsCause(e, retryOnSameExceptions)); | ||
} | ||
|
||
@Override | ||
public boolean isCircuitTrippingException(Throwable e) { | ||
return Utils.isPresentAsCause(e, circuitRelated); | ||
} | ||
|
||
@Override | ||
public int getMaxRetriesOnSameServer() { | ||
return retrySameServer; | ||
} | ||
|
||
@Override | ||
public int getMaxRetriesOnNextServer() { | ||
return retryNextServer; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 | ||
* | ||
* http://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 io.servicecomb.loadbalance; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License header file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
import java.net.ConnectException; | ||
import java.net.SocketException; | ||
import java.net.SocketTimeoutException; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.core.IsCollectionContaining.hasItem; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import io.servicecomb.swagger.invocation.exception.InvocationException; | ||
|
||
public class TestLoadBalanceRetryHandler { | ||
|
||
private final class DummyException extends RuntimeException { | ||
|
||
DummyException(String message, Throwable e) { | ||
super(message, e); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRetryOnSameOperations() { | ||
List<Class<? extends Throwable>> appendExceptions = Lists | ||
.newArrayList(new Class[]{InvocationException.class, IllegalAccessException.class}); | ||
|
||
LoadBalanceRetryHandler.getInstance().setRetryRuntimeParams(1, 2, true); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance().getMaxRetriesOnSameServer(), is(1)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance().getMaxRetriesOnNextServer(), is(2)); | ||
|
||
LoadBalanceRetryHandler.getInstance().addRetryOnSameExceptions(appendExceptions); | ||
List<Class<? extends Throwable>> retryOnSameExceptions = LoadBalanceRetryHandler.getInstance() | ||
.getRetryOnSameExceptions(); | ||
Assert.assertThat(retryOnSameExceptions, hasItem(InvocationException.class)); | ||
Assert.assertThat(retryOnSameExceptions, hasItem(IllegalAccessException.class)); | ||
|
||
Assert.assertThat(retryOnSameExceptions.size(), is(4)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance().removeRetryOnSameException(InvocationException.class), | ||
is(true)); | ||
Assert | ||
.assertThat(LoadBalanceRetryHandler.getInstance().removeRetryOnSameException(SocketException.class), is(false)); | ||
Assert.assertThat(retryOnSameExceptions.size(), is(3)); | ||
Assert.assertThat(retryOnSameExceptions, | ||
contains(SocketTimeoutException.class, ConnectException.class, IllegalAccessException.class)); | ||
} | ||
|
||
@Test | ||
public void testIsRetriableException() { | ||
LoadBalanceRetryHandler.getInstance().setRetryRuntimeParams(1, 2, true); | ||
Assert | ||
.assertThat(LoadBalanceRetryHandler.getInstance().isRetriableException(new SocketException(), false), is(true)); | ||
Assert | ||
.assertThat(LoadBalanceRetryHandler.getInstance().isRetriableException(new SocketException(), true), is(false)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance().isRetriableException(new SocketTimeoutException(), true), | ||
is(true)); | ||
Assert | ||
.assertThat(LoadBalanceRetryHandler.getInstance().isRetriableException(new ConnectException(), true), is(true)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance() | ||
.isRetriableException(new DummyException("connect failed", new ConnectException()), true), is(true)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance() | ||
.isRetriableException(new DummyException("socket timeout", new SocketTimeoutException()), true), is(true)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance() | ||
.isRetriableException(new DummyException("illegal access", new IllegalAccessException()), true), is(false)); | ||
|
||
LoadBalanceRetryHandler.getInstance().setRetryRuntimeParams(1, 2, false); | ||
Assert | ||
.assertThat(LoadBalanceRetryHandler.getInstance().isRetriableException(new SocketException(), true), is(false)); | ||
|
||
} | ||
|
||
@Test | ||
public void testIsCircuitTrippingException() { | ||
LoadBalanceRetryHandler.getInstance().setRetryRuntimeParams(1, 2, true); | ||
|
||
Assert | ||
.assertThat(LoadBalanceRetryHandler.getInstance().isCircuitTrippingException(new SocketException()), is(true)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance().isCircuitTrippingException(new SocketTimeoutException()), | ||
is(true)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance().isCircuitTrippingException(new IllegalAccessException()), | ||
is(false)); | ||
|
||
Assert.assertThat(LoadBalanceRetryHandler.getInstance() | ||
.isCircuitTrippingException(new DummyException("socket exception", new SocketException())), is(true)); | ||
Assert.assertThat(LoadBalanceRetryHandler.getInstance() | ||
.isCircuitTrippingException(new DummyException("socket timeout", new SocketTimeoutException())), is(true)); | ||
Assert.assertThat( | ||
LoadBalanceRetryHandler.getInstance() | ||
.isCircuitTrippingException(new DummyException("illegal access", new IllegalAccessException())), is(false)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the license header here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done