Skip to content

Commit b2bde79

Browse files
committed
Support custom retryable exceptions via @RetryableException and SofaRpcException(#1362)
1 parent 2a11a24 commit b2bde79

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

core-impl/client/src/main/java/com/alipay/sofa/rpc/client/FailoverCluster.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException {
6767
providerInfo = select(request, invokedProviderInfos);
6868
SofaResponse response = filterChain(providerInfo, request);
6969
if (response != null) {
70+
// 检查是否是用户自定义的可重试异常
71+
Object appResponse = response.getAppResponse();
72+
if (appResponse instanceof SofaRpcException) {
73+
if (((SofaRpcException) appResponse).getErrorType() == RpcErrorType.CUSTOMER_RETRY_ERROR) {
74+
time++;
75+
throwable = (SofaRpcException) appResponse;
76+
continue;
77+
}
78+
}
7079
if (throwable != null) {
7180
if (LOGGER.isWarnEnabled(consumerConfig.getAppName())) {
7281
LOGGER.warnWithApp(consumerConfig.getAppName(),

core/api/src/main/java/com/alipay/sofa/rpc/filter/ProviderInvoker.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.alipay.sofa.rpc.filter;
1818

1919
import com.alipay.sofa.rpc.common.RpcConstants;
20+
import com.alipay.sofa.rpc.common.annotation.RetryableException;
2021
import com.alipay.sofa.rpc.config.ProviderConfig;
2122
import com.alipay.sofa.rpc.context.RpcInternalContext;
2223
import com.alipay.sofa.rpc.context.RpcInvokeContext;
@@ -113,7 +114,12 @@ public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
113114
// sofaResponse.setErrorMsg(e.getMessage());
114115
} catch (InvocationTargetException e) { // 业务代码抛出异常
115116
cutCause(e.getCause());
116-
sofaResponse.setAppResponse(e.getCause());
117+
// 查看该异常是否被用户声明为可重试异常
118+
if (e.getCause().getClass().isAnnotationPresent(RetryableException.class)) {
119+
sofaResponse.setAppResponse(new SofaRpcException(RpcErrorType.CUSTOMER_RETRY_ERROR, e.getCause()));
120+
} else {
121+
sofaResponse.setAppResponse(e.getCause());
122+
}
117123
} finally {
118124
// R8: Record business processing execution time
119125
if (RpcInternalContext.isAttachmentEnable()) {

core/exception/src/main/java/com/alipay/sofa/rpc/core/exception/RpcErrorType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ public class RpcErrorType {
107107
* 客户端未定义异常
108108
*/
109109
public static final int CLIENT_UNDECLARED_ERROR = 299;
110+
111+
/**
112+
* 用户自定义可重试异常
113+
*/
114+
public static final int CUSTOMER_RETRY_ERROR = 300;
110115
}

0 commit comments

Comments
 (0)