Search in sources :

Example 26 with SofaResponse

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

the class BaggageResolverTest method testPickupFromResponse.

@Test
public void testPickupFromResponse() {
    SofaResponse sofaResponse = new SofaResponse();
    RpcInvokeContext context = RpcInvokeContext.getContext();
    context.putResponseBaggage(KEY, VALUE);
    sofaResponse.addResponseProp(RemotingConstants.RPC_RESPONSE_BAGGAGE + ".key", VALUE);
    sofaResponse.addResponseProp(RemotingConstants.RPC_RESPONSE_BAGGAGE + ".key2", null);
    BaggageResolver.pickupFromResponse(context, sofaResponse);
    assertEquals(VALUE, RpcInvokeContext.getContext().getAllResponseBaggage().get(KEY));
    assertEquals(null, RpcInvokeContext.getContext().getAllResponseBaggage().get("key2"));
    assertEquals(1, RpcInvokeContext.getContext().getAllResponseBaggage().size());
    assertEquals(SafeConcurrentHashMap.class, RpcInvokeContext.getContext().getAllResponseBaggage().getClass());
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 27 with SofaResponse

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

the class BaggageResolverTest method testCarryWithResponse.

@Test
public void testCarryWithResponse() {
    SofaResponse sofaResponse = new SofaResponse();
    RpcInvokeContext context = RpcInvokeContext.getContext();
    context.putResponseBaggage(KEY, VALUE);
    BaggageResolver.carryWithResponse(context, sofaResponse);
    String baggage = (String) sofaResponse.getResponseProp(RemotingConstants.RPC_RESPONSE_BAGGAGE + "." + KEY);
    assertNotNull(baggage);
    assertEquals(VALUE, baggage);
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 28 with SofaResponse

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

the class ProviderProxyInvoker method invoke.

/**
 * proxy拦截的调用
 *
 * @param request 请求消息
 * @return 调用结果
 * @throws SofaRpcException rpc异常
 */
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
    RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_FILTER_START_TIME_NANO, System.nanoTime());
    SofaResponse sofaResponse = filterChain.invoke(request);
    RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_FILTER_END_TIME_NANO, System.nanoTime());
    calculateProviderFilterTime();
    return sofaResponse;
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 29 with SofaResponse

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

the class MessageBuilder method buildSofaErrorResponse.

/**
 * 构建rpc错误结果
 *
 * @param errorMsg 错误消息
 * @return rpc结果
 */
public static SofaResponse buildSofaErrorResponse(String errorMsg) {
    SofaResponse sofaResponse = new SofaResponse();
    sofaResponse.setErrorMsg(errorMsg);
    return sofaResponse;
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 30 with SofaResponse

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

the class TripleClientInvoker method invoke.

@Override
public SofaResponse invoke(SofaRequest sofaRequest, int timeout) throws Exception {
    if (!useGeneric) {
        SofaResponse sofaResponse = new SofaResponse();
        Object stub = sofaStub.invoke(null, channel, buildCustomCallOptions(sofaRequest, timeout), timeout);
        final Method method = sofaRequest.getMethod();
        Object appResponse = method.invoke(stub, sofaRequest.getMethodArgs()[0]);
        sofaResponse.setAppResponse(appResponse);
        return sofaResponse;
    } else {
        String serviceName = sofaRequest.getInterfaceName();
        String methodName = sofaRequest.getMethodName();
        MethodDescriptor.Marshaller<?> requestMarshaller = null;
        MethodDescriptor.Marshaller<?> responseMarshaller = null;
        requestMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Request.getDefaultInstance());
        responseMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Response.getDefaultInstance());
        String fullMethodName = generateFullMethodName(serviceName, methodName);
        MethodDescriptor methodDescriptor = io.grpc.MethodDescriptor.newBuilder().setType(io.grpc.MethodDescriptor.MethodType.UNARY).setFullMethodName(fullMethodName).setSampledToLocalTracing(true).setRequestMarshaller((MethodDescriptor.Marshaller<Object>) requestMarshaller).setResponseMarshaller((MethodDescriptor.Marshaller<Object>) responseMarshaller).build();
        Request request = getRequest(sofaRequest, serialization, serializer);
        Response response = (Response) ClientCalls.blockingUnaryCall(channel, methodDescriptor, buildCustomCallOptions(sofaRequest, timeout), request);
        SofaResponse sofaResponse = new SofaResponse();
        byte[] responseDate = response.getData().toByteArray();
        Class returnType = sofaRequest.getMethod().getReturnType();
        if (returnType != void.class) {
            if (responseDate != null && responseDate.length > 0) {
                Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
                Object appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
                sofaResponse.setAppResponse(appResponse);
            }
        }
        return sofaResponse;
    }
}
Also used : Request(triple.Request) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Method(java.lang.reflect.Method) ByteString(com.google.protobuf.ByteString) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) MethodDescriptor(io.grpc.MethodDescriptor) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Response(triple.Response) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Serializer(com.alipay.sofa.rpc.codec.Serializer)

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