Skip to content
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

[WIP] Support customer exception #1411

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public FailoverCluster(ConsumerBootstrap consumerBootstrap) {
public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException {
String methodName = request.getMethodName();
int retries = consumerConfig.getMethodRetries(methodName);
// 用户定义的异常
List<Class<? extends Throwable>> customerExceptions = consumerConfig.getCustomerExceptions();
int time = 0;
// 异常日志
SofaRpcException throwable = null;
Expand Down Expand Up @@ -95,7 +97,14 @@ public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException {
throw e;
}
}
} catch (Exception e) { // 其它异常不重试
} catch (Exception e) {
// 执行用户自定义异常重试
for (Class<? extends Throwable> exceptionClass : customerExceptions) {
if (exceptionClass.isInstance(e)) {
time++;
}
}
// 其它异常不重试
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR,
"Failed to call " + request.getInterfaceName() + "." + request.getMethodName()
+ " on remote server: " + providerInfo + ", cause by unknown exception: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ public class RpcOptions {
* 默认失败重试次数
*/
public static final String CONSUMER_RETRIES = "consumer.retries";
/**
* 用户自定义异常集合
*/
public static final String CONSUMER_EXCEPTIONS = "consumer.exceptions";
/**
* 默认是否异步
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@

import static com.alipay.sofa.rpc.common.RpcConfigs.getBooleanValue;
import static com.alipay.sofa.rpc.common.RpcConfigs.getIntValue;
import static com.alipay.sofa.rpc.common.RpcConfigs.getListValue;
import static com.alipay.sofa.rpc.common.RpcConfigs.getStringValue;
import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_EXCEPTIONS;
import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_REJECTED_EXECUTION_POLICY;
import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_ADDRESS_HOLDER;
import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_ADDRESS_WAIT;
Expand Down Expand Up @@ -225,6 +227,11 @@ public class ConsumerConfig<T> extends AbstractInterfaceConfig<T, ConsumerConfig
*/
protected int retries = getIntValue(CONSUMER_RETRIES);

/**
* 用户自定义异常集合
*/
protected List<String> customerExceptions = getListValue(CONSUMER_EXCEPTIONS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming customerExceptions to consumerExceptions to maintain naming consistency with the static import CONSUMER_EXCEPTIONS.

- protected List<String> customerExceptions = getListValue(CONSUMER_EXCEPTIONS);
+ protected List<String> consumerExceptions = getListValue(CONSUMER_EXCEPTIONS);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
protected List<String> customerExceptions = getListValue(CONSUMER_EXCEPTIONS);
protected List<String> consumerExceptions = getListValue(CONSUMER_EXCEPTIONS);


/**
* 接口下每方法的最大可并行执行请求数,配置-1关闭并发过滤器,等于0表示开启过滤但是不限制
*/
Expand Down Expand Up @@ -442,6 +449,14 @@ public ConsumerConfig<T> setRetries(int retries) {
return this;
}

public List<String> getCustomerExceptions() {
return customerExceptions;
}

public void setCustomerExceptions(List<String> customerExceptions) {
this.customerExceptions = customerExceptions;
}
Comment on lines +452 to +458
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getter and setter methods for customerExceptions are correctly implemented. However, consider renaming them to align with the suggested field name change to consumerExceptions.

- public List<String> getCustomerExceptions() {
+ public List<String> getConsumerExceptions() {
      return consumerExceptions;
  }

- public void setCustomerExceptions(List<String> customerExceptions) {
+ public void setConsumerExceptions(List<String> consumerExceptions) {
      this.consumerExceptions = consumerExceptions;
  }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public List<String> getCustomerExceptions() {
return customerExceptions;
}
public void setCustomerExceptions(List<String> customerExceptions) {
this.customerExceptions = customerExceptions;
}
public List<String> getConsumerExceptions() {
return consumerExceptions;
}
public void setConsumerExceptions(List<String> consumerExceptions) {
this.consumerExceptions = consumerExceptions;
}


/**
* Gets connection holder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ PS:大家也看到了,本JSON文档是支持注释的,而标准JSON是不支
"consumer.loadBalancer": "auto",
//默认失败重试次数
"consumer.retries": 0,
// 用户自定义异常集合, 默认为空
"consumer.exceptions": [],
//接口下每方法的最大可并行执行请求数,配置-1关闭并发过滤器,等于0表示开启过滤但是不限制
"consumer.concurrents": 0,
// 默认是否异步
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ public class RpcErrorType {
* 客户端未定义异常
*/
public static final int CLIENT_UNDECLARED_ERROR = 299;

/**
* 用户自定义异常
*/
public static final int CUSTOMER_DESIGN_ERROR = 310;
}
Loading