Search in sources :

Example 11 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class AbstractCluster method doSendMsg.

/**
 * 调用客户端
 *
 * @param transport 客户端连接
 * @param request   Request对象
 * @return 调用结果
 * @throws SofaRpcException rpc异常
 */
protected SofaResponse doSendMsg(ProviderInfo providerInfo, ClientTransport transport, SofaRequest request) throws SofaRpcException {
    RpcInternalContext context = RpcInternalContext.getContext();
    // 添加调用的服务端远程地址
    RpcInternalContext.getContext().setRemoteAddress(providerInfo.getHost(), providerInfo.getPort());
    try {
        // 根据服务端版本特殊处理
        checkProviderVersion(providerInfo, request);
        String invokeType = request.getInvokeType();
        int timeout = resolveTimeout(request, consumerConfig, providerInfo);
        SofaResponse response = null;
        // 同步调用
        if (RpcConstants.INVOKER_TYPE_SYNC.equals(invokeType)) {
            long start = RpcRuntimeContext.now();
            try {
                response = transport.syncSend(request, timeout);
            } finally {
                if (RpcInternalContext.isAttachmentEnable()) {
                    long elapsed = RpcRuntimeContext.now() - start;
                    context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE, elapsed);
                }
            }
        } else // 单向调用
        if (RpcConstants.INVOKER_TYPE_ONEWAY.equals(invokeType)) {
            long start = RpcRuntimeContext.now();
            try {
                transport.oneWaySend(request, timeout);
                response = buildEmptyResponse(request);
            } finally {
                if (RpcInternalContext.isAttachmentEnable()) {
                    long elapsed = RpcRuntimeContext.now() - start;
                    context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE, elapsed);
                }
            }
        } else // Callback调用
        if (RpcConstants.INVOKER_TYPE_CALLBACK.equals(invokeType)) {
            // 调用级别回调监听器
            SofaResponseCallback sofaResponseCallback = request.getSofaResponseCallback();
            if (sofaResponseCallback == null) {
                SofaResponseCallback methodResponseCallback = consumerConfig.getMethodOnreturn(request.getMethodName());
                if (methodResponseCallback != null) {
                    // 方法的Callback
                    request.setSofaResponseCallback(methodResponseCallback);
                }
            }
            // 记录发送开始时间
            context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_SEND_TIME, RpcRuntimeContext.now());
            // 开始调用
            transport.asyncSend(request, timeout);
            response = buildEmptyResponse(request);
        } else // Future调用
        if (RpcConstants.INVOKER_TYPE_FUTURE.equals(invokeType)) {
            // 记录发送开始时间
            context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_SEND_TIME, RpcRuntimeContext.now());
            // 开始调用
            ResponseFuture future = transport.asyncSend(request, timeout);
            // 放入线程上下文
            RpcInternalContext.getContext().setFuture(future);
            response = buildEmptyResponse(request);
        } else {
            throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Unknown invoke type:" + invokeType);
        }
        return response;
    } catch (SofaRpcException e) {
        throw e;
    } catch (Throwable e) {
        // 客户端其它异常
        throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e);
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) ResponseFuture(com.alipay.sofa.rpc.message.ResponseFuture) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 12 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class AbstractSerializerTest method buildDeserializeError.

@Test
public void buildDeserializeError() {
    RpcInternalContext old = RpcInternalContext.peekContext();
    try {
        RpcInternalContext.removeContext();
        SofaRpcException exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.SERVER_DESERIALIZE, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.CLIENT_DESERIALIZE, exception.getErrorType());
        RpcInternalContext.removeContext();
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.SERVER_DESERIALIZE, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.CLIENT_DESERIALIZE, 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)

Example 13 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class TripleClientTransport method syncSend.

@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
    SofaResponse sofaResponse = null;
    SofaRpcException throwable = null;
    try {
        RpcInternalContext context = RpcInternalContext.getContext();
        beforeSend(context, request);
        RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
        invokeContext.put(TripleContants.SOFA_REQUEST_KEY, request);
        invokeContext.put(TripleContants.SOFA_CONSUMER_CONFIG_KEY, transportConfig.getConsumerConfig());
        sofaResponse = tripleClientInvoker.invoke(request, timeout);
        return sofaResponse;
    } catch (Exception e) {
        throwable = convertToRpcException(e);
        throw throwable;
    } finally {
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, sofaResponse, throwable));
        }
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) StatusException(io.grpc.StatusException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 14 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class ConsumerTracerFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
    clientSpan.setTag(RpcSpanTags.INVOKE_TYPE, request.getInvokeType());
    RpcInternalContext context = RpcInternalContext.getContext();
    clientSpan.setTag(RpcSpanTags.ROUTE_RECORD, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
    ProviderInfo providerInfo = context.getProviderInfo();
    if (providerInfo != null) {
        clientSpan.setTag(RpcSpanTags.REMOTE_APP, providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
        clientSpan.setTag(RpcSpanTags.REMOTE_IP, providerInfo.getHost() + ":" + providerInfo.getPort());
    }
    return invoker.invoke(request);
// 因为异步的场景,所以received不写在这里
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Example 15 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class ProviderTracerFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    SofaTracerSpan serverSpan = null;
    try {
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        serverSpan = sofaTraceContext.getCurrentSpan();
        if (serverSpan != null) {
            RpcInternalContext context = RpcInternalContext.getContext();
            serverSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
            serverSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
            // 客户端地址
            serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName());
            // 从请求里获取ConsumerTracerFilter额外传递的信息
            serverSpan.setTag(RpcSpanTags.REMOTE_APP, (String) request.getRequestProp(HEAD_APP_NAME));
            serverSpan.setTag(RpcSpanTags.PROTOCOL, (String) request.getRequestProp(HEAD_PROTOCOL));
            serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, (String) request.getRequestProp(HEAD_INVOKE_TYPE));
            ProviderConfig providerConfig = (ProviderConfig) invoker.getConfig();
            serverSpan.setTag(RpcSpanTags.LOCAL_APP, providerConfig.getAppName());
            serverSpan.setTag(RpcSpanTags.SERVER_THREAD_POOL_WAIT_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_PROCESS_WAIT_TIME));
        }
        return invoker.invoke(request);
    } finally {
        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Aggregations

RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)52 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)15 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)13 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)12 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)12 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)10 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 HashMap (java.util.HashMap)5 InvokeContext (com.alipay.remoting.InvokeContext)4 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)4 ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)4 Test (org.junit.Test)4 SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)3 ConnectionClosedException (com.alipay.remoting.exception.ConnectionClosedException)3 DeserializationException (com.alipay.remoting.exception.DeserializationException)3 RemotingException (com.alipay.remoting.exception.RemotingException)3 SerializationException (com.alipay.remoting.exception.SerializationException)3 InvokeSendFailedException (com.alipay.remoting.rpc.exception.InvokeSendFailedException)3