Search in sources :

Example 21 with SofaRpcException

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);
}
Also used : InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) DeserializationException(com.alipay.remoting.exception.DeserializationException) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 22 with SofaRpcException

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);
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) AbstractChannel(com.alipay.sofa.rpc.transport.AbstractChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) NettyChannel(com.alipay.sofa.rpc.transport.netty.NettyChannel) Channel(io.netty.channel.Channel) NettyChannel(com.alipay.sofa.rpc.transport.netty.NettyChannel) Bootstrap(io.netty.bootstrap.Bootstrap) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) AsciiString(io.netty.util.AsciiString) TimeoutException(java.util.concurrent.TimeoutException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 23 with SofaRpcException

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);
        }
    }
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) HashMap(java.util.HashMap) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) StringSerializer(com.alipay.sofa.rpc.codec.common.StringSerializer) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Example 24 with SofaRpcException

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();
    }
}
Also used : ClientEndInvokeEvent(com.alipay.sofa.rpc.event.ClientEndInvokeEvent) FilterChain(com.alipay.sofa.rpc.filter.FilterChain) ClientAsyncReceiveEvent(com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 25 with SofaRpcException

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;
}
Also used : ArrayList(java.util.ArrayList) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Aggregations

SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)91 Test (org.junit.Test)35 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)28 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)27 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)24 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)23 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)22 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)19 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)16 HelloService (com.alipay.sofa.rpc.test.HelloService)16 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)15 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)14 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)11 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)11 HashMap (java.util.HashMap)9 Filter (com.alipay.sofa.rpc.filter.Filter)8 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)7