Search in sources :

Example 46 with SofaRpcException

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

the class RpcSofaTracer method serverSend.

@Override
public void serverSend(SofaRequest request, SofaResponse response, Throwable exception) {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan serverSpan = sofaTraceContext.pop();
    if (serverSpan == null) {
        return;
    }
    // Record server send event
    serverSpan.log(LogData.SERVER_SEND_EVENT_VALUE);
    RpcInternalContext context = RpcInternalContext.getContext();
    RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
    serverSpan.setTag(RpcSpanTags.RESP_SERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SERIALIZE_TIME));
    serverSpan.setTag(RpcSpanTags.REQ_DESERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_DESERIALIZE_TIME));
    serverSpan.setTag(RpcSpanTags.RESP_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE));
    serverSpan.setTag(RpcSpanTags.REQ_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE));
    // 当前线程名
    serverSpan.setTag(RpcSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
    serverSpan.setTag(RpcSpanTags.SERVER_PHASE_TIME_COST, generateServerPhaseTimeCostSpan(invokeContext));
    serverSpan.setTag(RpcSpanTags.SERVER_SPECIAL_TIME_MARK, generateServerSpecialTimeMark(invokeContext));
    Throwable throwableShow = exception;
    String tracerErrorCode = StringUtils.EMPTY;
    String errorSourceApp = StringUtils.EMPTY;
    String resultCode = StringUtils.EMPTY;
    if (throwableShow != null) {
        // 当前即服务端应用
        errorSourceApp = serverSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
        // 结果码(00=成功/01=业务异常/02=RPC逻辑错误)
        // 不会业务异常
        resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
        tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
    } else if (response != null) {
        // 判断是否是业务异常
        if (response.isError()) {
            errorSourceApp = serverSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
            resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
            tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
            // 改变打印的 throwable
            throwableShow = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
        } else {
            Object ret = response.getAppResponse();
            if (ret instanceof Throwable) {
                throwableShow = (Throwable) ret;
                errorSourceApp = serverSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                // 业务异常
                resultCode = TracerResultCode.RPC_RESULT_BIZ_FAILED;
                tracerErrorCode = TracerResultCode.RPC_RESULT_BIZ_FAILED;
            } else {
                resultCode = TracerResultCode.RPC_RESULT_SUCCESS;
            }
        }
    }
    if (throwableShow != null) {
        // 打印错误
        // result code
        Map<String, String> errorContext = new HashMap<String, String>();
        // 记录的上下文信息
        this.generateServerErrorContext(errorContext, request, serverSpan);
        // report
        serverSpan.reportError(tracerErrorCode, errorContext, throwableShow, errorSourceApp, ERROR_SOURCE);
    }
    // 结果码(00=成功/01=业务异常/02=RPC逻辑错误)
    serverSpan.setTag(RpcSpanTags.RESULT_CODE, resultCode);
    serverSpan.finish();
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) HashMap(java.util.HashMap) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 47 with SofaRpcException

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

the class RpcSofaTracer method clientReceived.

@Override
public void clientReceived(SofaRequest request, SofaResponse response, Throwable exceptionThrow) {
    // 客户端的启动
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan clientSpan = sofaTraceContext.pop();
    if (clientSpan == null) {
        return;
    }
    // Record client receive event
    clientSpan.log(LogData.CLIENT_RECV_EVENT_VALUE);
    // rpc 上下文
    RpcInternalContext context = null;
    if (RpcInternalContext.isAttachmentEnable()) {
        context = RpcInternalContext.getContext();
        if (!clientSpan.getTagsWithStr().containsKey(RpcSpanTags.ROUTE_RECORD)) {
            clientSpan.setTag(RpcSpanTags.ROUTE_RECORD, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
        }
        clientSpan.setTag(RpcSpanTags.REQ_SERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SERIALIZE_TIME));
        clientSpan.setTag(RpcSpanTags.RESP_DESERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_DESERIALIZE_TIME));
        clientSpan.setTag(RpcSpanTags.RESP_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE));
        clientSpan.setTag(RpcSpanTags.REQ_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE));
        clientSpan.setTag(RpcSpanTags.CLIENT_CONN_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME));
        Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
        if (ce != null) {
            clientSpan.setTag(RpcSpanTags.CLIENT_ELAPSE_TIME, ce);
        }
        InetSocketAddress address = context.getLocalAddress();
        if (address != null) {
            clientSpan.setTag(RpcSpanTags.LOCAL_IP, NetUtils.toIpString(address));
            clientSpan.setTag(RpcSpanTags.LOCAL_PORT, address.getPort());
        }
        // adjust for generic invoke
        clientSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
    }
    RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
    clientSpan.setTag(RpcSpanTags.PHASE_TIME_COST, generateClientPhaseTimeCostSpan(invokeContext));
    clientSpan.setTag(RpcSpanTags.SPECIAL_TIME_MARK, generateClientSpecialTimeMarkSpan(invokeContext));
    Throwable throwableShow = exceptionThrow;
    // 区分出各个异常信息
    String resultCode = StringUtils.EMPTY;
    // 当前应用或者目标应用
    String errorSourceApp = StringUtils.EMPTY;
    String tracerErrorCode = StringUtils.EMPTY;
    if (throwableShow != null) {
        // 客户端异常
        if (throwableShow instanceof SofaRpcException) {
            SofaRpcException exception = (SofaRpcException) throwableShow;
            // 摘要打印
            int errorType = exception.getErrorType();
            switch(errorType) {
                case RpcErrorType.CLIENT_TIMEOUT:
                    resultCode = TracerResultCode.RPC_RESULT_TIMEOUT_FAILED;
                    // filter 已经存放
                    errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                    tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_TIMEOUT_ERROR;
                    break;
                case RpcErrorType.CLIENT_ROUTER:
                    resultCode = TracerResultCode.RPC_RESULT_ROUTE_FAILED;
                    errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                    tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_ADDRESS_ROUTE_ERROR;
                    break;
                case RpcErrorType.CLIENT_SERIALIZE:
                case RpcErrorType.CLIENT_DESERIALIZE:
                    resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
                    errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                    tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_SERIALIZE_ERROR;
                    break;
                default:
                    resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
                    errorSourceApp = ExceptionUtils.isServerException(exception) ? clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP) : clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                    tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
                    break;
            }
        } else {
            // 这里是客户端的未知异常,目前不会走到这里
            resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
            errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
            tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
        }
    } else if (response != null) {
        // 服务端rpc异常
        if (response.isError()) {
            errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP);
            tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
            resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
            // 客户端服务端均打印
            throwableShow = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
        } else {
            Object ret = response.getAppResponse();
            // for server throw exception ,but this class can not be found in current
            if (ret instanceof Throwable || "true".equals(response.getResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR))) {
                errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP);
                // 业务异常
                resultCode = TracerResultCode.RPC_RESULT_BIZ_FAILED;
                tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_BIZ_ERROR;
            } else {
                resultCode = TracerResultCode.RPC_RESULT_SUCCESS;
            }
        }
    }
    if (throwableShow != null) {
        Map<String, String> contextMap = new HashMap<String, String>();
        this.generateClientErrorContext(contextMap, request, clientSpan);
        clientSpan.reportError(tracerErrorCode, contextMap, throwableShow, errorSourceApp, ERROR_SOURCE);
    }
    clientSpan.setTag(RpcSpanTags.RESULT_CODE, resultCode);
    // finish client
    clientSpan.finish();
    if (context != null) {
        context.setAttachment(RpcConstants.INTERNAL_KEY_RESULT_CODE, resultCode);
    }
    // client span
    if (clientSpan.getParentSofaTracerSpan() != null) {
        // restore parent
        sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan)

Example 48 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project Sentinel by alibaba.

the class AbstractSofaRpcFilter method traceResponseException.

protected void traceResponseException(SofaResponse response, Entry interfaceEntry, Entry methodEntry) {
    if (response.isError()) {
        SofaRpcException rpcException = new SofaRpcException(RpcErrorType.SERVER_FILTER, response.getErrorMsg());
        Tracer.traceEntry(rpcException, interfaceEntry);
        Tracer.traceEntry(rpcException, methodEntry);
    } else {
        Object appResponse = response.getAppResponse();
        if (appResponse instanceof Throwable) {
            Tracer.traceEntry((Throwable) appResponse, interfaceEntry);
            Tracer.traceEntry((Throwable) appResponse, methodEntry);
        }
    }
}
Also used : SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 49 with SofaRpcException

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

the class SofaRequestTest method getRequestProp.

@Test
public void getRequestProp() throws Exception {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(Invoker.class.getName());
    request.setMethodName("invoke");
    request.setMethod(Invoker.class.getMethod("invoke", SofaRequest.class));
    request.setMethodArgs(new Object[] { new SofaRequest() });
    request.setMethodArgSigs(new String[] { SofaRequest.class.getCanonicalName() });
    request.setTargetServiceUniqueName(Invoker.class.getName() + ":1.0");
    request.setTargetAppName("targetApp");
    request.setSerializeType((byte) 11);
    request.setTimeout(1024);
    request.setSerializeType((byte) 11);
    request.setData(new ByteArrayWrapperByteBuf(new byte[] { 1, 2, 3 }));
    request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
    request.setSofaResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
        }
    });
    Assert.assertEquals(Invoker.class.getName(), request.getInterfaceName());
    Assert.assertEquals("invoke", request.getMethodName());
    Assert.assertEquals("invoke", request.getMethod().getName());
    Assert.assertArrayEquals(new String[] { SofaRequest.class.getCanonicalName() }, request.getMethodArgSigs());
    Assert.assertEquals(1, request.getMethodArgs().length);
    Assert.assertEquals(Invoker.class.getName() + ":1.0", request.getTargetServiceUniqueName());
    Assert.assertEquals("targetApp", request.getTargetAppName());
    Assert.assertTrue(request.getSerializeType() == 11);
    Assert.assertTrue(request.getTimeout() == 1024);
    Assert.assertEquals(RpcConstants.INVOKER_TYPE_SYNC, request.getInvokeType());
    Assert.assertTrue(request.getSerializeType() == 11);
    Assert.assertTrue(request.getData().array().length == 3);
    Assert.assertNotNull(request.getSofaResponseCallback());
    Map<String, Object> map = request.getRequestProps();
    Assert.assertTrue(map == null);
    request.addRequestProp("1", "1");
    map = request.getRequestProps();
    Assert.assertTrue(map.size() == 1);
    request.addRequestProp(null, "1");
    Assert.assertTrue(map.size() == 1);
    request.addRequestProp("1", null);
    Assert.assertTrue(map.size() == 1);
    request.addRequestProps(null);
    Assert.assertTrue(map.size() == 1);
    request.addRequestProps(new HashMap<String, Object>());
    Assert.assertTrue(map.size() == 1);
    request.removeRequestProp("1");
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("1", "1");
    request.addRequestProps(hashMap);
    hashMap.put("2", "2");
    request.addRequestProps(hashMap);
    Assert.assertTrue(map.size() == 2);
    Assert.assertEquals("2", request.getRequestProp("2"));
    request.removeRequestProp(null);
    Assert.assertTrue(map.size() == 2);
    request.removeRequestProp("2");
    Assert.assertTrue(map.size() == 1);
    Assert.assertNull(request.getRequestProp("2"));
    Assert.assertFalse(request.isAsync());
    request.setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
    Assert.assertTrue(request.isAsync());
    request.setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    Assert.assertTrue(request.isAsync());
    request.setInvokeType(null);
    Assert.assertFalse(request.isAsync());
}
Also used : HashMap(java.util.HashMap) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Invoker(com.alipay.sofa.rpc.invoke.Invoker) Test(org.junit.Test)

Example 50 with SofaRpcException

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

the class AbstractSerializerTest method buildSerializeError.

@Test
public void buildSerializeError() {
    RpcInternalContext old = RpcInternalContext.peekContext();
    try {
        RpcInternalContext.removeContext();
        SofaRpcException exception = serializer.buildSerializeError("xx");
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildSerializeError("xx");
        Assert.assertEquals(RpcErrorType.SERVER_SERIALIZE, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildSerializeError("xx");
        Assert.assertEquals(RpcErrorType.CLIENT_SERIALIZE, exception.getErrorType());
        RpcInternalContext.removeContext();
        exception = serializer.buildSerializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildSerializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.SERVER_SERIALIZE, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildSerializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.CLIENT_SERIALIZE, exception.getErrorType());
    } finally {
        RpcInternalContext.setContext(old);
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

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