Skip to content

Commit

Permalink
Feat/error code limit (sofastack#859)
Browse files Browse the repository at this point in the history
* feat: use error code
  • Loading branch information
leizhiyuan authored Feb 20, 2020
1 parent 2c8ec8c commit 5bb37d9
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -753,11 +753,13 @@ public void notifyStateChangeToUnavailable() {
@Override
public void run() {
// 状态变化通知监听器
final Object proxyIns = consumerBootstrap.getProxyIns();
for (ConsumerStateListener listener : onprepear) {
try {
listener.onUnavailable(consumerBootstrap.getProxyIns());
listener.onUnavailable(proxyIns);
} catch (Exception e) {
LOGGER.error("Failed to notify consumer state listener when state change to unavailable");
LOGGER.error(LogCodes.getLog(LogCodes.ERROR_NOTIFY_CONSUMER_STATE_UN, proxyIns.getClass()
.getName()));
}
}
}
Expand All @@ -778,11 +780,13 @@ public void notifyStateChangeToAvailable() {
@Override
public void run() {
// 状态变化通知监听器
final Object proxyIns = consumerBootstrap.getProxyIns();
for (ConsumerStateListener listener : onprepear) {
try {
listener.onAvailable(consumerBootstrap.getProxyIns());
listener.onAvailable(proxyIns);
} catch (Exception e) {
LOGGER.error("Failed to notify consumer state listener when state change to available");
LOGGER.warn(LogCodes.getLog(LogCodes.WARN_NOTIFY_CONSUMER_STATE, proxyIns.getClass()
.getName()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.listener.ConsumerStateListener;
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.transport.ClientTransport;
Expand Down Expand Up @@ -301,12 +302,13 @@ public void notifyStateChangeToUnavailable() {
@Override
public void run() {
// 状态变化通知监听器
final Object proxyIns = consumerConfig.getConsumerBootstrap().getProxyIns();
for (ConsumerStateListener listener : onAvailable) {
try {
listener.onUnavailable(consumerConfig.getConsumerBootstrap().getProxyIns());
listener.onUnavailable(proxyIns);
} catch (Exception e) {
LOGGER.errorWithApp(consumerConfig.getAppName(),
"Failed to notify consumer state listener when state change to unavailable");
LogCodes.getLog(LogCodes.ERROR_NOTIFY_CONSUMER_STATE_UN, proxyIns.getClass().getName()));
}
}
}
Expand All @@ -327,12 +329,13 @@ public void notifyStateChangeToAvailable() {
@Override
public void run() {
// 状态变化通知监听器
final Object proxyIns = consumerConfig.getConsumerBootstrap().getProxyIns();
for (ConsumerStateListener listener : onAvailable) {
try {
listener.onAvailable(consumerConfig.getConsumerBootstrap().getProxyIns());
listener.onAvailable(proxyIns);
} catch (Exception e) {
LOGGER.warnWithApp(consumerConfig.getAppName(),
"Failed to notify consumer state listener when state change to available");
LogCodes.getLog(LogCodes.WARN_NOTIFY_CONSUMER_STATE, proxyIns.getClass().getName()));
}
}
}
Expand Down Expand Up @@ -392,9 +395,11 @@ public void updateProviders(ProviderGroup providerGroup) {
}
} catch (Exception e) {
if (LOGGER.isErrorEnabled(consumerConfig.getAppName())) {
LOGGER.errorWithApp(consumerConfig.getAppName(), "update " + consumerConfig.getInterfaceId() +
" provider (" + providerGroup
+ ") from list error:", e);
LOGGER
.errorWithApp(
consumerConfig.getAppName(),
LogCodes.getLog(LogCodes.ERROR_UPDATE_PROVIDERS, consumerConfig.getInterfaceId(), providerGroup),
e);
}
}
}
Expand Down Expand Up @@ -441,7 +446,8 @@ protected void addNode(List<ProviderInfo> providerInfoList) {
threads) + 1)) * connectTimeout + 500;
latch.await(totalTimeout, TimeUnit.MILLISECONDS); // 一直等到子线程都结束
} catch (InterruptedException e) {
LOGGER.errorWithApp(appName, "Exception when add provider", e);
LOGGER.errorWithApp(appName,
LogCodes.getLog(LogCodes.ERROR_UPDATE_PROVIDERS, consumerConfig.getInterfaceId(), ""), e);
} finally {
initPool.shutdown(); // 关闭线程池
}
Expand Down Expand Up @@ -514,9 +520,8 @@ public void removeNode(List<ProviderInfo> providerInfos) {
ClientTransportFactory.releaseTransport(transport, consumerConfig.getDisconnectTimeout());
}
} catch (Exception e) {
LOGGER.errorWithApp(appName, "Remove provider of " + consumerConfig.getInterfaceId() + ": " +
providerInfo
+ " from list error:", e);
LOGGER.errorWithApp(appName,
LogCodes.getLog(LogCodes.ERROR_DELETE_PROVIDERS, consumerConfig.getInterfaceId(), providerInfo), e);
}
}
}
Expand Down Expand Up @@ -855,7 +860,7 @@ public void run() {
try {
doReconnect();
} catch (Throwable e) {
LOGGER.errorWithApp(consumerConfig.getAppName(),
LOGGER.warnWithApp(consumerConfig.getAppName(),
"Exception when retry connect to provider", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.ext.ExtensionLoaderListener;
import com.alipay.sofa.rpc.invoke.Invoker;
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

Expand Down Expand Up @@ -128,8 +129,8 @@ protected FilterChain(List<Filter> filters, FilterInvoker lastInvoker, AbstractI
loadedFilters.add(filter);
}
} catch (Exception e) {
LOGGER.error("Error when build filter chain", e);
throw new SofaRpcRuntimeException("Error when build filter chain", e);
LOGGER.error(LogCodes.getLog(LogCodes.ERROR_FILTER_CONSTRUCT), e);
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_FILTER_CONSTRUCT), e);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions core/api/src/main/java/com/alipay/sofa/rpc/log/LogCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public class LogCodes {
//01002 dynamic
//01003 ext
public static final String ERROR_METRIC_REPORT_ERROR = "010030001";
public static final String ERROR_TRACER_INIT = "010030002";
public static final String ERROR_FILTER_CONSTRUCT = "010030003";

//01004 listener
//01005 module

Expand All @@ -109,6 +112,8 @@ public class LogCodes {
public static final String INFO_REGISTRY_IGNORE = "010060003";
public static final String LOCALFILEREGISTRY_FAIL_READFILE = "010060004";
public static final String ERROR_RPC_NETWORK_ADDRESS_LOAD = "010060005";
public static final String ERROR_DESTRORY_REGISTRY = "010060006";

//01007 log
//01008 proxy generate
//01009 transmit
Expand Down Expand Up @@ -144,6 +149,7 @@ public class LogCodes {
public static final String ERROR_OSGI_UNRESGISTER_SERVICE = "019990016";
public static final String ERROR_ADDRESSING_CHAIN_EMPTY = "019990017";
public static final String ERROR_PROVIDER_GRPC_START = "019990018";
public static final String ERROR_SERVER_DESTROY = "019990019";

//02 运行
// 02000 泛化
Expand All @@ -165,6 +171,11 @@ public class LogCodes {
public static final String ERROR_INVOKE_GET_CLIENT = "020020008";
public static final String ERROR_TARGET_URL_INVALID = "020020009";
public static final String LOCALFILEREGISTRY_FAIL_INVOKE = "020020010";
public static final String ERROR_NOTIFY_CONSUMER_STATE_UN = "020020011";
public static final String WARN_NOTIFY_CONSUMER_STATE = "020020012";
public static final String ERROR_UPDATE_PROVIDERS = "020020013";
public static final String ERROR_DELETE_PROVIDERS = "020020014";

// 02003 loadbalancer
// 02004 router
// 02005 codec
Expand All @@ -180,6 +191,8 @@ public class LogCodes {
// 02007 cache
// 02008 context
// 02009 tracer
public static final String ERROR_TRACER_UNKNOWN_EXP = "020090001";

// 02010 server process
public static final String INFO_SERVICE_METADATA_IS_NULL = "020100001";
public static final String WARN_CANNOT_FOUND_SERVICE_4_SERVER = "020100002";
Expand All @@ -196,6 +209,8 @@ public class LogCodes {
public static final String ERROR_DECODE_REQ_SIG_CLASS_NOT_FOUND = "020100013";
public static final String ERROR_DISCARD_TIMEOUT_REQUEST = "020100014";
public static final String ERROR_DISCARD_TIMEOUT_RESPONSE = "020100015";
public static final String ERROR_PROCESS_UNKNOWN = "020100016";

// 02011 protocol
// 02012 filter
// 02013 event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.ext.ExtensionClass;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

Expand Down Expand Up @@ -110,8 +111,7 @@ public static void destroyAll() {
registry.destroy();
ALL_REGISTRIES.remove(config);
} catch (Exception e) {
LOGGER.error("Error when destroy registry :" + config
+ ", but you can ignore if it's called by JVM shutdown hook", e);
LOGGER.error(LogCodes.getLog(LogCodes.ERROR_DESTRORY_REGISTRY, config), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.ext.ExtensionClass;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

Expand Down Expand Up @@ -144,7 +145,7 @@ public static void destroyAll() {
try {
server.destroy();
} catch (Exception e) {
LOGGER.error("Error when destroy server with key:" + key, e);
LOGGER.error(LogCodes.getLog(LogCodes.ERROR_SERVER_DESTROY, key), e);
}
}
SERVER_MAP.clear();
Expand All @@ -159,7 +160,7 @@ public static void destroyServer(ServerConfig serverConfig) {
server.destroy();
}
} catch (Exception e) {
LOGGER.error("Error when destroy server with key:" + serverConfig.getPort(), e);
LOGGER.error(LogCodes.getLog(LogCodes.ERROR_SERVER_DESTROY, serverConfig.getPort()), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

Expand Down Expand Up @@ -74,7 +75,7 @@ public final class Tracers {
}
}
} catch (Exception e) {
LOGGER.error("Failed to init Tracers", e);
LOGGER.error(LogCodes.getLog(LogCodes.ERROR_TRACER_INIT), e);
}
}

Expand Down
12 changes: 12 additions & 0 deletions core/api/src/main/resources/sofa-rpc/logcodes-common_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
##01002 dynamic
##01003 ext
010030001=Metrics report error
010030002=Failed to init Tracers
010030003=Error when build filter chain
##01004 listener
##01005 module
010050001=Cannot load RPC module
Expand All @@ -48,6 +50,8 @@
010060003=rpc_register_registry_ignore is true, ignore register registry
010060004=LocalFileRegistry failed to read services file
010060005=Cannot get the valid IP address, rpc_enabled_ip_range: [{0}], rpc_bind_network_interface: [{1}].
010060006=Error destroy registry when shutdown,Config:[{0}],but you can ignore if it's called by JVM shutdown hook

##01007 log
##01008 proxy generate
##01009 transmit
Expand Down Expand Up @@ -81,6 +85,8 @@
019990016=Unregister service of OSGi failed
019990017=Route service of SOFA RPC is empty, please make sure the application start success
019990018=Start RPC GRPC server failed, listening address: [{0}:{1}]
019990019=Error when destroy server with key: [{0}]

#02 运行

## 02000 泛化
Expand All @@ -102,6 +108,10 @@
020020008=Cannot get socket connect of service [{0}]
020020009=The service addresses [{1}] of service [{0}] is not available,or specify url not exist in providers
020020010=Cannot get socket connection of service [{0}],Make sure target service is published successfully, and address on local registry file is correct!
020020011=Failed to notify consumer state listener when state change to unavailable,consumer=[{0}]
020020012=Failed to notify consumer state listener when state change to available,consumer=[{0}]
020020013=Update [{0}] provider,providerGroup:[{1}] from list error
020020014=Remove [{0}] provider,providerInfo:[{1}] from list error

## 02003 loadbalancer
## 02004 router
Expand All @@ -118,6 +128,7 @@
## 02007 cache
## 02008 context
## 02009 tracer
020090001=The Stage [{0}],process of [{1}] tracer [{2}] request occur error
## 02010 server process
020100001=getServiceMetadata return null, serviceUniqueName: [{0}]
020100002=Cannot found the service: [{0}]
Expand All @@ -134,6 +145,7 @@
020100013=sig[x] [{0}] class not found
020100014=Discard request because consumer has been timeout, service is: [{0}], from: [{1}]
020100015=Discard response because consumer has been timeout, service is: [{0}], from: [{1}]
020100016=Process occurs an unknown error


## 02011 protocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
##01002 dynamic
##01003 ext
010030001=上报metrics信息错误
010030002=初始化Tracer失败
010030003=构造Filter链出现异常


##01004 listener
##01005 module
010050001=无法加载到 RPC 内部模块
Expand All @@ -48,6 +52,8 @@
010060003=rpc_register_registry_ignore等于true, 忽略注册至服务注册中心
010060004=LocalFileRegistry 读取文件失败
010060005=根据当前的配置rpc_enabled_ip_range[{0}], rpc_bind_network_interface[{1}],无法获得有效IP地址。请检查 Cloudengine 插件目录下或工程 Classpath 下的 config/rpc-config.properties 文件中的配置
010060006=当关闭注册中心的时候出现异常,配置是[{0}],一般可以忽略

##01007 log
##01008 proxy generate
##01009 transmit
Expand Down Expand Up @@ -81,6 +87,8 @@
019990016=注销 OSGi 的 service 失败
019990017=Sofa RPC 的路由组件为空,请检查应用是否正常启动或者还有其它异常
019990018=启动 Sofa GRPC 服务器端错误,监听地址[{0}:{1}]
019990019=以Key纬度关闭 Server 异常: [{0}]

#02 运行

## 02000 泛化
Expand All @@ -102,6 +110,10 @@
020020008=调用远程 Sofa RPC 服务时无法取得连接[{0}]
020020009=当前服务[{0}]的地址[{1}]不可用,或指定的地址不在可用的地址列表中
020020010=获取不到服务[{0}]对应网络链接,请确保对方的服务成功发布,并且本地注册文件对应地址正确!
020020011=通知消费方:[{0}],有服务提供方不可用
020020012=通知消费方:[{0}],有服务提供方开始可用
020020013=更新接口 [{0}] 的 providerGroup:[{1}] 地址列表失败
020020014=删除接口 [{0}] 的 providerInfo:[{1}] 地址列表失败

## 02003 loadbalancer
## 02004 router
Expand All @@ -118,6 +130,8 @@
## 02007 cache
## 02008 context
## 02009 tracer
020090001=在阶段[{0}],处理 [{1}] 的 tracer,在 [{2}] 端出现异常

## 02010 server process
020100001=getServiceMetadata 返回 null, serviceUniqueName: [{0}]
020100002=没有找到对应的服务:[{0}]
Expand All @@ -134,6 +148,7 @@
020100013=找不到 sig[x] [{0}] 类
020100014=丢弃客户端已经超时的请求,不再处理请求,接口:[{0}], 来自:[{1}]
020100015=丢弃客户端已经超时的响应,不再返回响应,接口:[{0}], 来自:[{1}]
020100016=处理过程出现未知异常


## 02011 protocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alipay.sofa.rpc.event.ServerEndHandleEvent;
import com.alipay.sofa.rpc.event.rest.RestServerReceiveEvent;
import com.alipay.sofa.rpc.event.rest.RestServerSendEvent;
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.lookout.RestLookoutAdapter;
Expand All @@ -36,6 +37,7 @@
import org.jboss.resteasy.plugins.server.netty.NettyHttpResponse;
import org.jboss.resteasy.plugins.server.netty.RequestDispatcher;
import org.jboss.resteasy.spi.Failure;
import sun.rmi.runtime.Log;

import javax.ws.rs.core.HttpHeaders;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -107,7 +109,7 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except
} catch (Exception ex) {
response.reset();
response.setStatus(500);
logger.error("Unexpected", ex); // todo 异常带给用户?
logger.error(LogCodes.getLog(LogCodes.ERROR_PROCESS_UNKNOWN), ex); // todo 异常带给用户?
exception = ex;
} finally {
if (EventBus.isEnable(RestServerSendEvent.class)) {
Expand Down
Loading

0 comments on commit 5bb37d9

Please sign in to comment.