Search in sources :

Example 1 with RpcResponseCommand

use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project sofa-jraft by sofastack.

the class ProtobufSerializerTest method testEncodeDecodeResponseContent.

@Test
public void testEncodeDecodeResponseContent() throws Exception {
    final PingRequest reqObject = TestUtils.createPingRequest();
    final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
    final ErrorResponse respObject = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, new Status(-1, "test"));
    final RpcResponseCommand response = cmdFactory.createResponse(respObject, request);
    response.setResponseClass(ErrorResponse.class.getName());
    assertTrue(serializer.serializeContent(response));
    response.setResponseObject(null);
    assertTrue(serializer.deserializeContent(response, null));
    assertNotNull(response.getResponseObject());
    assertEquals(respObject, response.getResponseObject());
    assertNotSame(respObject, response.getResponseObject());
}
Also used : Status(com.alipay.sofa.jraft.Status) PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 2 with RpcResponseCommand

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

the class SofaRpcSerialization method deserializeHeader.

@Override
public <Response extends ResponseCommand> boolean deserializeHeader(Response response, InvokeContext invokeContext) throws DeserializationException {
    if (response instanceof RpcResponseCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte[] header = responseCommand.getHeader();
        responseCommand.setResponseHeader(mapSerializer.decode(header));
        return true;
    }
    return false;
}
Also used : RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand)

Example 3 with RpcResponseCommand

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

the class SofaRpcSerialization method serializeHeader.

@Override
public <Response extends ResponseCommand> boolean serializeHeader(Response response) throws SerializationException {
    if (response instanceof RpcResponseCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();
        Object responseObject = ((RpcResponseCommand) response).getResponseObject();
        if (responseObject instanceof SofaResponse) {
            SofaResponse sofaResponse = (SofaResponse) responseObject;
            if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) {
                sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE);
            }
            byte[] header = null;
            try {
                header = mapSerializer.encode(sofaResponse.getResponseProps());
            } catch (Exception e) {
                String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
                String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
                LOGGER.error("traceId={}, rpcId={}, Response serializeHeader exception, msg={}", traceId, rpcId, e.getMessage(), e);
                throw new SerializationException(e.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, e);
            }
            response.setHeader(header);
        }
        return true;
    }
    return false;
}
Also used : SerializationException(com.alipay.remoting.exception.SerializationException) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException)

Example 4 with RpcResponseCommand

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

the class SofaRpcSerialization method serializeContent.

@Override
public <Response extends ResponseCommand> boolean serializeContent(Response response) throws SerializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte serializerCode = response.getSerializer();
        long serializeStartTime = System.nanoTime();
        try {
            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
            AbstractByteBuf byteBuf = rpcSerializer.encode(responseCommand.getResponseObject(), null);
            responseCommand.setContent(byteBuf.array());
            return true;
        } catch (Exception ex) {
            String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
            String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
            LOGGER.error("traceId={}, rpcId={}, Response serializeContent exception, msg = {}", traceId, rpcId, ex.getMessage(), ex);
            throw new SerializationException(ex.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, ex);
        } finally {
            // R6:Record response serialization time
            recordSerializeResponse(responseCommand, serializeStartTime);
        }
    }
    return false;
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SerializationException(com.alipay.remoting.exception.SerializationException) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException) Serializer(com.alipay.sofa.rpc.codec.Serializer) DefaultCustomSerializer(com.alipay.remoting.DefaultCustomSerializer)

Example 5 with RpcResponseCommand

use of com.alipay.remoting.rpc.protocol.RpcResponseCommand in project dingo by dingodb.

the class ProtobufSerializer method deserializeContent.

@Override
public <T extends ResponseCommand> boolean deserializeContent(T response, InvokeContext invokeContext) throws DeserializationException {
    final RpcResponseCommand cmd = (RpcResponseCommand) response;
    final String className = cmd.getResponseClass();
    cmd.setResponseObject(ProtobufMsgFactory.newMessageByJavaClassName(className, response.getContent()));
    return true;
}
Also used : RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand)

Aggregations

RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)10 SerializationException (com.alipay.remoting.exception.SerializationException)4 DeserializationException (com.alipay.remoting.exception.DeserializationException)3 DefaultCustomSerializer (com.alipay.remoting.DefaultCustomSerializer)2 Serializer (com.alipay.sofa.rpc.codec.Serializer)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 Message (com.google.protobuf.Message)2 Test (org.junit.Test)2 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)1 Status (com.alipay.sofa.jraft.Status)1 ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)1 PingRequest (com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest)1 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)1 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1