Search in sources :

Example 6 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class RestTracerAdapter method serverSend.

/**
 * 适配服务端serverSend
 */
public static void serverSend(NettyHttpResponse response, Throwable throwable) {
    try {
        SofaRequest sofaRequest = new SofaRequest();
        SofaResponse sofaResponse = new SofaResponse();
        if (response == null) {
            sofaResponse.setErrorMsg("rest path ends with /favicon.ico");
        } else if (throwable != null) {
            if (response.getStatus() == 500) {
                sofaResponse.setAppResponse(throwable);
            } else {
                sofaResponse.setErrorMsg(throwable.getMessage());
            }
            Object method = RpcInternalContext.getContext().getAttachment(METHOD_TYPE_STRING);
            if (method != null) {
                Class[] parameterTypes = ((Method) method).getParameterTypes();
                String[] methodTypeString = new String[parameterTypes.length];
                for (int i = 0; i < methodTypeString.length; i++) {
                    methodTypeString[i] = (parameterTypes[i].getName());
                }
                sofaRequest.setMethodArgSigs(methodTypeString);
            }
        }
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
        RpcInternalContext context = RpcInternalContext.getContext();
        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }
        RestBaggageItemsHandler.encodeBaggageItemsToResponse(response, sofaResponse);
        Tracers.serverSend(sofaRequest, sofaResponse, null);
    } catch (Throwable t) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("the process of rest tracer server send occur error ", t);
        }
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 7 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class RpcReferenceContextFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    ConsumerConfig config = (ConsumerConfig) invoker.getConfig();
    RpcReferenceContext referenceCtx = new RpcReferenceContext();
    referenceCtx.setGeneric(config.isGeneric());
    referenceCtx.setInterfaceName(config.getInterfaceId());
    referenceCtx.setUniqueId(config.getUniqueId());
    referenceCtx.setServiceName(request.getTargetServiceUniqueName());
    referenceCtx.setMethodName(request.getMethodName());
    RpcInternalContext context = RpcInternalContext.getContext();
    ProviderInfo providerInfo = context.getProviderInfo();
    if (providerInfo != null) {
        referenceCtx.setTargetAppName(providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
        referenceCtx.setTargetUrl(providerInfo.getHost() + ":" + providerInfo.getPort());
    }
    referenceCtx.setProtocol(config.getProtocol());
    referenceCtx.setInvokeType(request.getInvokeType());
    referenceCtx.setRouteRecord((String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
    RpcInvokeContext.getContext().put(RemotingConstants.INVOKE_CTX_RPC_REF_CTX, referenceCtx);
    SofaResponse response = invoker.invoke(request);
    // 调用后
    InetSocketAddress local = context.getLocalAddress();
    if (local != null) {
        referenceCtx.setClientIP(NetUtils.toIpString(local));
        referenceCtx.setClientPort(local.getPort());
    }
    Long ct = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME);
    if (ct != null) {
        referenceCtx.setConnEstablishedSpan(ct);
    }
    Integer qs = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE);
    if (qs != null) {
        referenceCtx.setRequestSize(qs.longValue());
    }
    Integer ps = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE);
    if (ps != null) {
        referenceCtx.setResponseSize(ps.longValue());
    }
    referenceCtx.setTraceId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID));
    referenceCtx.setRpcId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID));
    Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
    if (ce != null) {
        referenceCtx.setCostTime(ce);
    }
    return response;
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) InetSocketAddress(java.net.InetSocketAddress) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RpcReferenceContext(com.alipay.sofa.rpc.api.context.RpcReferenceContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 8 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class BoltSendableResponseCallback method sendSofaException.

/**
 * A->B(当前)->C的场景下,将远程服务端C的RPc异常异步返回给调用者A
 *
 * @see SofaResponseCallback#onSofaException(SofaRpcException, String, RequestBase)
 */
@Override
public void sendSofaException(SofaRpcException sofaException) {
    checkState();
    SofaResponse response = new SofaResponse();
    response.setErrorMsg(sofaException.getMessage());
    sendSofaResponse(response, sofaException);
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 9 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class BoltSendableResponseCallback method sendAppException.

/**
 * A->B(当前)->C的场景下,将远程服务端C的业务异常异步返回给调用者A
 *
 * @see SofaResponseCallback#onAppException(Throwable, String, RequestBase)
 */
@Override
public void sendAppException(Throwable throwable) {
    checkState();
    SofaResponse response = new SofaResponse();
    response.setAppResponse(throwable);
    sendSofaResponse(response, null);
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 10 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class BoltSendableResponseCallback method sendAppResponse.

/**
 * A->B(当前)->C的场景下,将远程服务端C的结果异步返回给调用者A
 *
 * @see SofaResponseCallback#onAppResponse(Object, String, RequestBase)
 */
@Override
public void sendAppResponse(Object appResponse) {
    checkState();
    SofaResponse response = new SofaResponse();
    response.setAppResponse(appResponse);
    sendSofaResponse(response, null);
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Aggregations

SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)85 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)25 Test (org.junit.Test)21 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)19 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)12 Method (java.lang.reflect.Method)10 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)9 HashMap (java.util.HashMap)9 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)6 ClientEndInvokeEvent (com.alipay.sofa.rpc.event.ClientEndInvokeEvent)6 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 ClientAsyncReceiveEvent (com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent)5 ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)5 Serializer (com.alipay.sofa.rpc.codec.Serializer)4 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 FilterChain (com.alipay.sofa.rpc.filter.FilterChain)4 Hessian2Input (com.caucho.hessian.io.Hessian2Input)4 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)3 DeserializationException (com.alipay.remoting.exception.DeserializationException)3