use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class BoltClientTransportTest method testConvertToRpcException.
@Test
public void testConvertToRpcException() {
ClientTransportConfig config1 = new ClientTransportConfig();
config1.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
BoltClientTransport transport = new BoltClientTransport(config1);
Assert.assertTrue(transport.convertToRpcException(new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "")) instanceof SofaRpcException);
Assert.assertTrue(transport.convertToRpcException(new InvokeTimeoutException()) instanceof SofaTimeOutException);
Assert.assertTrue(transport.convertToRpcException(new InvokeServerBusyException()).getErrorType() == RpcErrorType.SERVER_BUSY);
Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_SERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_SERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_DESERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_DESERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new ConnectionClosedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
Assert.assertTrue(transport.convertToRpcException(new InvokeSendFailedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
Assert.assertTrue(transport.convertToRpcException(new InvokeServerException()).getErrorType() == RpcErrorType.SERVER_UNDECLARED_ERROR);
Assert.assertTrue(transport.convertToRpcException(new UnsupportedOperationException()).getErrorType() == RpcErrorType.CLIENT_UNDECLARED_ERROR);
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class AbstractHttp2ClientTransport method connect.
@Override
public void connect() {
if (isAvailable()) {
return;
}
EventLoopGroup workerGroup = NettyHelper.getClientIOEventLoopGroup();
Http2ClientInitializer initializer = new Http2ClientInitializer(transportConfig);
try {
String host = providerInfo.getHost();
int port = providerInfo.getPort();
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(transportConfig.isUseEpoll() ? EpollSocketChannel.class : NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.remoteAddress(host, port);
b.handler(initializer);
// Start the client.
Channel channel = b.connect().syncUninterruptibly().channel();
this.channel = new NettyChannel(channel);
// Wait for the HTTP/2 upgrade to occur.
Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
http2SettingsHandler.awaitSettings(transportConfig.getConnectTimeout(), TimeUnit.MILLISECONDS);
responseChannelHandler = initializer.responseHandler();
// RESET streamId
streamId.set(START_STREAM_ID);
} catch (Exception e) {
throw new SofaRpcException(RpcErrorType.CLIENT_NETWORK, e);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class AbstractHttpClientHandler method decode.
protected void decode(SofaResponse response) {
AbstractByteBuf byteBuffer = response.getData();
if (byteBuffer != null) {
try {
Map<String, String> context = new HashMap<String, String>(4);
if (response.isError()) {
context.put(RemotingConstants.HEAD_RESPONSE_ERROR, response.isError() + "");
String errorMsg = StringSerializer.decode(byteBuffer.array());
response.setAppResponse(new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, errorMsg));
} else {
context.put(RemotingConstants.HEAD_TARGET_SERVICE, request.getTargetServiceUniqueName());
context.put(RemotingConstants.HEAD_METHOD_NAME, request.getMethodName());
Serializer serializer = SerializerFactory.getSerializer(response.getSerializeType());
serializer.decode(byteBuffer, response, context);
}
} finally {
byteBuffer.release();
response.setData(null);
}
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class CallbackInvokeClientHandler method doOnResponse.
@Override
public void doOnResponse(Object result) {
if (callback == null) {
return;
}
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
SofaResponse response = (SofaResponse) result;
Throwable throwable = null;
try {
Thread.currentThread().setContextClassLoader(this.classLoader);
RpcInternalContext.setContext(context);
if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, request, response, null));
}
pickupBaggage(response);
// do async filter after respond server
FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
if (chain != null) {
chain.onAsyncResponse(consumerConfig, request, response, null);
}
recordClientElapseTime();
if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
EventBus.post(new ClientEndInvokeEvent(request, response, null));
}
decode(response);
Object appResp = response.getAppResponse();
if (response.isError()) {
// rpc层异常
SofaRpcException sofaRpcException = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
callback.onSofaException(sofaRpcException, request.getMethodName(), request);
} else if (appResp instanceof Throwable) {
// 业务层异常
throwable = (Throwable) appResp;
callback.onAppException(throwable, request.getMethodName(), request);
} else {
callback.onAppResponse(appResp, request.getMethodName(), request);
}
} finally {
Thread.currentThread().setContextClassLoader(oldCl);
RpcInvokeContext.removeContext();
RpcInternalContext.removeAllContext();
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class FailoverCluster method doInvoke.
@Override
public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException {
String methodName = request.getMethodName();
int retries = consumerConfig.getMethodRetries(methodName);
int time = 0;
// 异常日志
SofaRpcException throwable = null;
List<ProviderInfo> invokedProviderInfos = new ArrayList<ProviderInfo>(retries + 1);
do {
ProviderInfo providerInfo = null;
try {
providerInfo = select(request, invokedProviderInfos);
SofaResponse response = filterChain(providerInfo, request);
if (response != null) {
if (throwable != null) {
if (LOGGER.isWarnEnabled(consumerConfig.getAppName())) {
LOGGER.warnWithApp(consumerConfig.getAppName(), LogCodes.getLog(LogCodes.WARN_SUCCESS_BY_RETRY, throwable.getClass() + ":" + throwable.getMessage(), invokedProviderInfos));
}
}
return response;
} else {
throwable = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Failed to call " + request.getInterfaceName() + "." + methodName + " on remote server " + providerInfo + ", return null");
time++;
}
} catch (SofaRpcException e) {
// 服务端异常+ 超时异常 才发起rpc异常重试
if (e.getErrorType() == RpcErrorType.SERVER_BUSY || e.getErrorType() == RpcErrorType.CLIENT_TIMEOUT) {
throwable = e;
time++;
} else {
// throw the exception that caused the retry
if (throwable != null) {
throw throwable;
} else {
throw e;
}
}
} catch (Exception e) {
// 其它异常不重试
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Failed to call " + request.getInterfaceName() + "." + request.getMethodName() + " on remote server: " + providerInfo + ", cause by unknown exception: " + e.getClass().getName() + ", message is: " + e.getMessage(), e);
} finally {
if (RpcInternalContext.isAttachmentEnable()) {
RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_INVOKE_TIMES, // 重试次数
time + 1);
}
}
if (providerInfo != null) {
invokedProviderInfos.add(providerInfo);
}
} while (time <= retries);
throw throwable;
}
Aggregations