Search in sources :

Example 81 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class CallbackInvokeClientHandler method doOnException.

@Override
public void doOnException(Throwable e) {
    if (callback == null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, request, null, e));
        }
        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, null, e);
        }
        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, null, e));
        }
        SofaRpcException sofaRpcException = e instanceof SofaRpcException ? (SofaRpcException) e : new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, e.getMessage(), e);
        callback.onSofaException(sofaRpcException, request.getMethodName(), request);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
        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) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 82 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class Http1ServerChannelHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
    if (HttpUtil.is100ContinueExpected(req)) {
        ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
    }
    boolean keepAlive = HttpUtil.isKeepAlive(req);
    String uri = req.uri();
    // ignore uris
    if (RemotingConstants.IGNORE_WEB_BROWSER.equals(uri)) {
        sendHttp1Response(ctx, HttpResponseStatus.OK, StringUtils.EMPTY, keepAlive);
        return;
    }
    HttpMethod reqMethod = req.method();
    // HEAD for check method exists
    if (reqMethod == HttpMethod.HEAD) {
        String[] iam = HttpTransportUtils.getInterfaceIdAndMethod(uri);
        boolean exists = serverHandler.checkService(iam[0], iam[1]);
        sendHttp1Response(ctx, exists ? HttpResponseStatus.OK : HttpResponseStatus.NOT_FOUND, "", keepAlive);
        return;
    }
    // POST(primary) / GET for invoke
    if (reqMethod != HttpMethod.POST && reqMethod != HttpMethod.GET) {
        sendHttp1Response(ctx, HttpResponseStatus.BAD_REQUEST, "Only support GET/POST/HEAD", keepAlive);
        return;
    }
    // call service
    SofaRequest sofaRequest = new SofaRequest();
    try {
        String[] iam = HttpTransportUtils.getInterfaceIdAndMethod(uri);
        String serviceName = iam[0];
        String methodName = iam[1];
        sofaRequest.setTargetServiceUniqueName(serviceName);
        sofaRequest.setMethodName(methodName);
        parseHeader(req, sofaRequest);
        if (reqMethod == HttpMethod.GET) {
            Method method = ReflectCache.getMethodCache(serviceName, methodName);
            if (method == null) {
                sendHttp1Response(ctx, HttpResponseStatus.NOT_FOUND, "Not found method:" + serviceName + "." + methodName, keepAlive);
                return;
            }
            String params = null;
            Class[] classArray = method.getParameterTypes();
            int length = classArray.length;
            Object[] paramList = new Object[length];
            int i = uri.indexOf('?');
            if (i >= 0) {
                params = uri.substring(i + 1);
                paramList = this.parseParamArg(classArray, params);
            } else {
                if (length != 0) {
                    throw new SofaRpcException(RpcErrorType.SERVER_DESERIALIZE, "The number of parameter is wrong.");
                }
            }
            sofaRequest.setMethodArgSigs(ReflectCache.getMethodSigsCache(serviceName, methodName));
            sofaRequest.setMethodArgs(paramList);
        } else {
            sofaRequest.setData(new NettyByteBuffer(req.content()));
        }
    } catch (Exception e) {
        String message = "Failed to parse http2 request for uri " + uri + " form " + NetUtils.channelToString(ctx.channel().remoteAddress(), ctx.channel().localAddress()) + ", cause by: " + e.getMessage();
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn(message, e);
        }
        sendHttp1Response(ctx, HttpResponseStatus.BAD_REQUEST, message, keepAlive);
        return;
    }
    try {
        serverHandler.handleHttp1Request(sofaRequest, ctx, keepAlive);
    } catch (SofaRpcException e) {
        int type = e.getErrorType();
        if (type == RpcErrorType.SERVER_BUSY) {
            sendHttp1Response(ctx, HttpResponseStatus.SERVICE_UNAVAILABLE, e.getMessage(), keepAlive);
        } else if (type == RpcErrorType.SERVER_NOT_FOUND_INVOKER) {
            sendHttp1Response(ctx, HttpResponseStatus.NOT_FOUND, e.getMessage(), keepAlive);
        } else {
            sendHttp1Response(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage(), keepAlive);
        }
    } catch (Exception e) {
        sendHttp1Response(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage(), keepAlive);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Method(java.lang.reflect.Method) HttpMethod(io.netty.handler.codec.http.HttpMethod) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NettyByteBuffer(com.alipay.sofa.rpc.transport.netty.NettyByteBuffer) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 83 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class Http2ClientChannelHandler method channelInactive.

@Override
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.channel();
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Channel inactive: {}", channel);
    }
    final Exception e = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, "Channel " + NetUtils.channelToString(channel.localAddress(), channel.remoteAddress()) + " has been closed, remove future when channel inactive.");
    Iterator<Entry<Integer, Entry<ChannelFuture, AbstractHttpClientHandler>>> it = streamIdPromiseMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Integer, Entry<ChannelFuture, AbstractHttpClientHandler>> mapEntry = it.next();
        it.remove();
        Entry<ChannelFuture, AbstractHttpClientHandler> entry = mapEntry.getValue();
        entry.getValue().onException(e);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) Channel(io.netty.channel.Channel) Map(java.util.Map) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 84 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class ConsumerGenericFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    try {
        final String revised = (String) request.getRequestProp(REVISE_KEY);
        // if has revised, invoke directly
        if (REVISE_VALUE.equals(revised)) {
            return invoker.invoke(request);
        }
        String type = getSerializeFactoryType(request.getMethodName(), request.getMethodArgs());
        request.addRequestProp(RemotingConstants.HEAD_GENERIC_TYPE, type);
        // 修正超时时间
        Long clientTimeout = getClientTimeoutFromGenericContext(request.getMethodName(), request.getMethodArgs());
        if (clientTimeout != null && clientTimeout != 0) {
            request.setTimeout(clientTimeout.intValue());
        }
        // 修正请求对象
        Object[] genericArgs = request.getMethodArgs();
        String methodName = (String) genericArgs[0];
        String[] argTypes = (String[]) genericArgs[1];
        Object[] args = (Object[]) genericArgs[2];
        request.setMethodName(methodName);
        request.setMethodArgSigs(argTypes);
        request.setMethodArgs(args);
        // 修正类型
        ConsumerConfig consumerConfig = (ConsumerConfig) invoker.getConfig();
        String invokeType = consumerConfig.getMethodInvokeType(methodName);
        request.setInvokeType(invokeType);
        request.addRequestProp(RemotingConstants.HEAD_INVOKE_TYPE, invokeType);
        request.addRequestProp(REVISE_KEY, REVISE_VALUE);
        return invoker.invoke(request);
    } catch (SofaRpcException e) {
        throw e;
    } catch (Exception e) {
        throw new SofaRpcException(RpcErrorType.CLIENT_FILTER, e.getMessage(), e);
    }
}
Also used : ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 85 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class BoltFutureInvokeCallback method onResponse.

@Override
public void onResponse(Object result) {
    if (rpcFuture == 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));
        }
        Object appResp = response.getAppResponse();
        if (response.isError()) {
            // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            rpcFuture.setFailure(sofaRpcException);
        } else if (appResp instanceof Throwable) {
            // 业务层异常
            throwable = (Throwable) appResp;
            rpcFuture.setFailure(throwable);
        } else {
            rpcFuture.setSuccess(appResp);
        }
    } 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)

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