Search in sources :

Example 16 with SofaResponse

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

the class CallbackInvokeClientHandler method doOnResponse.

@Override
public void doOnResponse(Object result) {
    if (callback == 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));
        }
        decode(response);
        Object appResp = response.getAppResponse();
        if (response.isError()) {
            // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            callback.onSofaException(sofaRpcException, request.getMethodName(), request);
        } else if (appResp instanceof Throwable) {
            // 业务层异常
            throwable = (Throwable) appResp;
            callback.onAppException(throwable, request.getMethodName(), request);
        } else {
            callback.onAppResponse(appResp, request.getMethodName(), request);
        }
    } 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)

Example 17 with SofaResponse

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

the class FutureInvokeClientHandler method doOnResponse.

@Override
public void doOnResponse(Object result) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    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));
        }
        decode(response);
        rpcFuture.setSuccess(response);
    } 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)

Example 18 with SofaResponse

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

the class FailoverCluster method doInvoke.

@Override
public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException {
    String methodName = request.getMethodName();
    int retries = consumerConfig.getMethodRetries(methodName);
    int time = 0;
    // 异常日志
    SofaRpcException throwable = null;
    List<ProviderInfo> invokedProviderInfos = new ArrayList<ProviderInfo>(retries + 1);
    do {
        ProviderInfo providerInfo = null;
        try {
            providerInfo = select(request, invokedProviderInfos);
            SofaResponse response = filterChain(providerInfo, request);
            if (response != null) {
                if (throwable != null) {
                    if (LOGGER.isWarnEnabled(consumerConfig.getAppName())) {
                        LOGGER.warnWithApp(consumerConfig.getAppName(), LogCodes.getLog(LogCodes.WARN_SUCCESS_BY_RETRY, throwable.getClass() + ":" + throwable.getMessage(), invokedProviderInfos));
                    }
                }
                return response;
            } else {
                throwable = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Failed to call " + request.getInterfaceName() + "." + methodName + " on remote server " + providerInfo + ", return null");
                time++;
            }
        } catch (SofaRpcException e) {
            // 服务端异常+ 超时异常 才发起rpc异常重试
            if (e.getErrorType() == RpcErrorType.SERVER_BUSY || e.getErrorType() == RpcErrorType.CLIENT_TIMEOUT) {
                throwable = e;
                time++;
            } else {
                // throw the exception that caused the retry
                if (throwable != null) {
                    throw throwable;
                } else {
                    throw e;
                }
            }
        } catch (Exception e) {
            // 其它异常不重试
            throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Failed to call " + request.getInterfaceName() + "." + request.getMethodName() + " on remote server: " + providerInfo + ", cause by unknown exception: " + e.getClass().getName() + ", message is: " + e.getMessage(), e);
        } finally {
            if (RpcInternalContext.isAttachmentEnable()) {
                RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_INVOKE_TIMES, // 重试次数
                time + 1);
            }
        }
        if (providerInfo != null) {
            invokedProviderInfos.add(providerInfo);
        }
    } while (time <= retries);
    throw throwable;
}
Also used : ArrayList(java.util.ArrayList) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 19 with SofaResponse

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

the class ProtostuffSerializerTest method testSofaResponse.

@Test
public void testSofaResponse() throws Exception {
    SofaResponse response = new SofaResponse();
    response.setAppResponse("1233");
    AbstractByteBuf data = serializer.encode(response, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaResponse.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, null, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, new SofaResponse(), null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // success response
    Map<String, String> head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    response = new SofaResponse();
    final ExampleObj exampleObj = new ExampleObj();
    exampleObj.setName("result");
    response.setAppResponse(exampleObj);
    data = serializer.encode(response, null);
    SofaResponse newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertEquals(response.getAppResponse(), newResponse.getAppResponse());
    Assert.assertEquals("result", ((ExampleObj) newResponse.getAppResponse()).getName());
    // null response
    head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    newResponse = new SofaResponse();
    serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertNotNull(newResponse.getAppResponse());
    Assert.assertNull(((ExampleObj) newResponse.getAppResponse()).getName());
    // error response
    head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    head.put(RemotingConstants.HEAD_RESPONSE_ERROR, "true");
    response = new SofaResponse();
    response.setErrorMsg("1233");
    data = serializer.encode(response, null);
    newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertTrue(newResponse.isError());
    Assert.assertEquals(response.getErrorMsg(), newResponse.getErrorMsg());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) HashMap(java.util.HashMap) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 20 with SofaResponse

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

the class JDKInvocationHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] paramValues) throws Throwable {
    String methodName = method.getName();
    Class[] paramTypes = method.getParameterTypes();
    if ("toString".equals(methodName) && paramTypes.length == 0) {
        return proxyInvoker.toString();
    } else if ("hashCode".equals(methodName) && paramTypes.length == 0) {
        return proxyInvoker.hashCode();
    } else if ("equals".equals(methodName) && paramTypes.length == 1) {
        Object another = paramValues[0];
        return proxy == another || (proxy.getClass().isInstance(another) && proxyInvoker.equals(JDKProxy.parseInvoker(another)));
    }
    SofaRequest sofaRequest = MessageBuilder.buildSofaRequest(method.getDeclaringClass(), method, paramTypes, paramValues);
    SofaResponse response = proxyInvoker.invoke(sofaRequest);
    if (response.isError()) {
        throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
    }
    Object ret = response.getAppResponse();
    if (ret instanceof Throwable) {
        throw (Throwable) ret;
    } else {
        if (ret == null) {
            return ClassUtils.getDefaultPrimitiveValue(method.getReturnType());
        }
        return ret;
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

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