Search in sources :

Example 1 with InvokeContext

use of com.alipay.remoting.InvokeContext in project sofa-rpc by sofastack.

the class BoltServerProcessor method handleRequest.

@Override
public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, SofaRequest request) {
    // RPC内置上下文
    RpcInternalContext context = RpcInternalContext.getContext();
    context.setProviderSide(true);
    String appName = request.getTargetAppName();
    if (appName == null) {
        // 默认全局appName
        appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
    }
    // 是否链路异步化中
    boolean isAsyncChain = false;
    try {
        // 这个 try-finally 为了保证Context一定被清理
        // 统计值加1
        processingCount.incrementAndGet();
        // 远程地址
        context.setRemoteAddress(bizCtx.getRemoteHost(), bizCtx.getRemotePort());
        // 远程返回的通道
        context.setAttachment(RpcConstants.HIDDEN_KEY_ASYNC_CONTEXT, asyncCtx);
        InvokeContext boltInvokeCtx = bizCtx.getInvokeContext();
        if (RpcInternalContext.isAttachmentEnable()) {
            if (boltInvokeCtx != null) {
                // rpc线程池等待时间 Long
                putToContextIfNotNull(boltInvokeCtx, InvokeContext.BOLT_PROCESS_WAIT_TIME, context, RpcConstants.INTERNAL_KEY_PROCESS_WAIT_TIME);
            }
        }
        putToContext(boltInvokeCtx);
        if (EventBus.isEnable(ServerReceiveEvent.class)) {
            EventBus.post(new ServerReceiveEvent(request));
        }
        // 开始处理
        // 响应,用于返回
        SofaResponse response = null;
        // 异常,用于记录
        Throwable throwable = null;
        ProviderConfig providerConfig = null;
        String serviceName = request.getTargetServiceUniqueName();
        try {
            // 这个try-catch 保证一定有Response
            invoke: {
                if (!boltServer.isStarted()) {
                    // 服务端已关闭
                    throwable = new SofaRpcException(RpcErrorType.SERVER_CLOSED, LogCodes.getLog(LogCodes.WARN_PROVIDER_STOPPED, SystemInfo.getLocalHost() + ":" + boltServer.serverConfig.getPort()));
                    response = MessageBuilder.buildSofaErrorResponse(throwable.getMessage());
                    break invoke;
                }
                if (bizCtx.isRequestTimeout()) {
                    // 加上丢弃超时的请求的逻辑
                    throwable = clientTimeoutWhenReceiveRequest(appName, serviceName, bizCtx.getRemoteAddress());
                    break invoke;
                }
                // 查找服务
                Invoker invoker = boltServer.findInvoker(serviceName);
                if (invoker == null) {
                    throwable = cannotFoundService(appName, serviceName);
                    response = MessageBuilder.buildSofaErrorResponse(throwable.getMessage());
                    break invoke;
                }
                if (invoker instanceof ProviderProxyInvoker) {
                    providerConfig = ((ProviderProxyInvoker) invoker).getProviderConfig();
                    // 找到服务后,打印服务的appName
                    appName = providerConfig != null ? providerConfig.getAppName() : null;
                }
                // 查找方法
                String methodName = request.getMethodName();
                Method serviceMethod = ReflectCache.getOverloadMethodCache(serviceName, methodName, request.getMethodArgSigs());
                if (serviceMethod == null) {
                    throwable = cannotFoundServiceMethod(appName, methodName, serviceName);
                    response = MessageBuilder.buildSofaErrorResponse(throwable.getMessage());
                    break invoke;
                } else {
                    request.setMethod(serviceMethod);
                }
                // 真正调用
                response = doInvoke(serviceName, invoker, request);
                if (bizCtx.isRequestTimeout()) {
                    // 加上丢弃超时的响应的逻辑
                    throwable = clientTimeoutWhenSendResponse(appName, serviceName, bizCtx.getRemoteAddress());
                    break invoke;
                }
            }
        } catch (Exception e) {
            // 服务端异常,不管是啥异常
            LOGGER.errorWithApp(appName, "Server Processor Error!", e);
            throwable = e;
            response = MessageBuilder.buildSofaErrorResponse(e.getMessage());
        }
        // Response不为空,代表需要返回给客户端
        if (response != null) {
            RpcInvokeContext invokeContext = RpcInvokeContext.peekContext();
            isAsyncChain = CommonUtils.isTrue(invokeContext != null ? (Boolean) invokeContext.remove(RemotingConstants.INVOKE_CTX_IS_ASYNC_CHAIN) : null);
            // 如果是服务端异步代理模式,特殊处理,因为该模式是在业务代码自主异步返回的
            if (!isAsyncChain) {
                // 其它正常请求
                try {
                    // 这个try-catch 保证一定要记录tracer
                    asyncCtx.sendResponse(response);
                } finally {
                    if (EventBus.isEnable(ServerSendEvent.class)) {
                        EventBus.post(new ServerSendEvent(request, response, throwable));
                    }
                }
            }
        }
    } catch (Throwable e) {
        // 可能有返回时的异常
        if (LOGGER.isErrorEnabled(appName)) {
            LOGGER.errorWithApp(appName, e.getMessage(), e);
        }
    } finally {
        processingCount.decrementAndGet();
        if (!isAsyncChain) {
            if (EventBus.isEnable(ServerEndHandleEvent.class)) {
                EventBus.post(new ServerEndHandleEvent());
            }
        }
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
Also used : InvokeContext(com.alipay.remoting.InvokeContext) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) ServerSendEvent(com.alipay.sofa.rpc.event.ServerSendEvent) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ServerEndHandleEvent(com.alipay.sofa.rpc.event.ServerEndHandleEvent) Method(java.lang.reflect.Method) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ProviderProxyInvoker(com.alipay.sofa.rpc.server.ProviderProxyInvoker) Invoker(com.alipay.sofa.rpc.invoke.Invoker) ProviderProxyInvoker(com.alipay.sofa.rpc.server.ProviderProxyInvoker) ServerReceiveEvent(com.alipay.sofa.rpc.event.ServerReceiveEvent) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 2 with InvokeContext

use of com.alipay.remoting.InvokeContext in project sofa-rpc by sofastack.

the class BoltClientTransport method syncSend.

@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext boltInvokeContext = createInvokeContext(request);
    SofaResponse response = null;
    SofaRpcException throwable = null;
    try {
        beforeSend(context, request);
        response = doInvokeSync(request, boltInvokeContext, timeout);
        return response;
    } catch (Exception e) {
        // 其它异常
        throwable = convertToRpcException(e);
        throw throwable;
    } finally {
        afterSend(context, boltInvokeContext, request);
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, response, throwable));
        }
    }
}
Also used : InvokeContext(com.alipay.remoting.InvokeContext) 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) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) DeserializationException(com.alipay.remoting.exception.DeserializationException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) RemotingException(com.alipay.remoting.exception.RemotingException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 3 with InvokeContext

use of com.alipay.remoting.InvokeContext in project sofa-rpc by sofastack.

the class BoltClientTransport method asyncSend.

@Override
public ResponseFuture asyncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext boltInvokeContext = createInvokeContext(request);
    try {
        beforeSend(context, request);
        boltInvokeContext.put(RemotingConstants.INVOKE_CTX_RPC_CTX, context);
        return doInvokeAsync(request, context, boltInvokeContext, timeout);
    } catch (Exception e) {
        throw convertToRpcException(e);
    } finally {
        afterSend(context, boltInvokeContext, request);
    }
}
Also used : InvokeContext(com.alipay.remoting.InvokeContext) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) DeserializationException(com.alipay.remoting.exception.DeserializationException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) RemotingException(com.alipay.remoting.exception.RemotingException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 4 with InvokeContext

use of com.alipay.remoting.InvokeContext in project sofa-rpc by sofastack.

the class BoltClientTransport method oneWaySend.

@Override
public void oneWaySend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext invokeContext = createInvokeContext(request);
    SofaRpcException throwable = null;
    try {
        beforeSend(context, request);
        doOneWay(request, invokeContext, timeout);
    } catch (Exception e) {
        // 其它异常
        throwable = convertToRpcException(e);
        throw throwable;
    } finally {
        afterSend(context, invokeContext, request);
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, null, throwable));
        }
    }
}
Also used : InvokeContext(com.alipay.remoting.InvokeContext) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) DeserializationException(com.alipay.remoting.exception.DeserializationException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) RemotingException(com.alipay.remoting.exception.RemotingException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 5 with InvokeContext

use of com.alipay.remoting.InvokeContext in project sofa-rpc by sofastack.

the class BoltClientTransport method createInvokeContext.

protected InvokeContext createInvokeContext(SofaRequest request) {
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, request.getSerializeType());
    invokeContext.put(RemotingConstants.HEAD_TARGET_SERVICE, request.getTargetServiceUniqueName());
    invokeContext.put(RemotingConstants.HEAD_METHOD_NAME, request.getMethodName());
    String genericType = (String) request.getRequestProp(RemotingConstants.HEAD_GENERIC_TYPE);
    if (genericType != null) {
        invokeContext.put(RemotingConstants.HEAD_GENERIC_TYPE, genericType);
    }
    return invokeContext;
}
Also used : InvokeContext(com.alipay.remoting.InvokeContext) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext)

Aggregations

InvokeContext (com.alipay.remoting.InvokeContext)5 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)5 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)4 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)4 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 InvokeServerBusyException (com.alipay.remoting.rpc.exception.InvokeServerBusyException)3 InvokeServerException (com.alipay.remoting.rpc.exception.InvokeServerException)3 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)3 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)3 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)3 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)2 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 ServerEndHandleEvent (com.alipay.sofa.rpc.event.ServerEndHandleEvent)1 ServerReceiveEvent (com.alipay.sofa.rpc.event.ServerReceiveEvent)1 ServerSendEvent (com.alipay.sofa.rpc.event.ServerSendEvent)1