Search in sources :

Example 6 with RpcRequestCommand

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

the class SofaRpcSerialization method deserializeHeader.

@Override
public <Request extends RequestCommand> boolean deserializeHeader(Request request) throws DeserializationException {
    if (request instanceof RpcRequestCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        if (requestCommand.getRequestHeader() != null) {
            // 代表已经提前解析过了,例如使用自定义业务线程池的时候,bolt会提前解析变长Header的数据
            return true;
        }
        byte[] header = requestCommand.getHeader();
        // 解析头部
        Map<String, String> headerMap = mapSerializer.decode(header);
        requestCommand.setRequestHeader(headerMap);
        RpcInvokeContext.getContext().put(RpcConstants.SOFA_REQUEST_HEADER_KEY, Collections.unmodifiableMap(headerMap));
        return true;
    }
    return false;
}
Also used : RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand)

Example 7 with RpcRequestCommand

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

the class SofaRpcSerialization method serializeContent.

@Override
public <Request extends RequestCommand> boolean serializeContent(Request request, InvokeContext invokeContext) throws SerializationException {
    if (request instanceof RpcRequestCommand) {
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        Object requestObject = requestCommand.getRequestObject();
        byte serializerCode = requestCommand.getSerializer();
        long serializeStartTime = System.nanoTime();
        try {
            Map<String, String> header = (Map<String, String>) requestCommand.getRequestHeader();
            if (header == null) {
                header = new HashMap<String, String>();
            }
            putKV(header, RemotingConstants.HEAD_GENERIC_TYPE, (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));
            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
            AbstractByteBuf byteBuf = rpcSerializer.encode(requestObject, header);
            request.setContent(byteBuf.array());
            return true;
        } catch (Exception ex) {
            throw new SerializationException(ex.getMessage(), ex);
        } finally {
            // R5:record request serialization time
            recordSerializeRequest(requestCommand, invokeContext, serializeStartTime);
        }
    }
    return false;
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SerializationException(com.alipay.remoting.exception.SerializationException) HashMap(java.util.HashMap) Map(java.util.Map) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) Serializer(com.alipay.sofa.rpc.codec.Serializer) DefaultCustomSerializer(com.alipay.remoting.DefaultCustomSerializer)

Example 8 with RpcRequestCommand

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

the class ProtobufSerializer method serializeHeader.

@Override
public <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext) throws SerializationException {
    final RpcRequestCommand cmd = (RpcRequestCommand) request;
    final Message msg = (Message) cmd.getRequestObject();
    if (msg instanceof RpcRequests.AppendEntriesRequest) {
        final RpcRequests.AppendEntriesRequest req = (RpcRequests.AppendEntriesRequest) msg;
        final RpcRequests.AppendEntriesRequestHeader.Builder hb = RpcRequests.AppendEntriesRequestHeader.newBuilder().setGroupId(// 
        req.getGroupId()).setPeerId(// 
        req.getPeerId()).setServerId(req.getServerId());
        cmd.setHeader(hb.build().toByteArray());
        return true;
    }
    return false;
}
Also used : Message(com.google.protobuf.Message) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand)

Example 9 with RpcRequestCommand

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

the class ProtobufSerializer method deserializeContent.

@Override
public <T extends RequestCommand> boolean deserializeContent(T request) throws DeserializationException {
    final RpcRequestCommand cmd = (RpcRequestCommand) request;
    final String className = cmd.getRequestClass();
    cmd.setRequestObject(ProtobufMsgFactory.newMessageByJavaClassName(className, request.getContent()));
    return true;
}
Also used : RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand)

Example 10 with RpcRequestCommand

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

the class ProtobufSerializer method serializeContent.

@Override
public <T extends RequestCommand> boolean serializeContent(T request, InvokeContext invokeContext) throws SerializationException {
    final RpcRequestCommand cmd = (RpcRequestCommand) request;
    final Message msg = (Message) cmd.getRequestObject();
    cmd.setContent(msg.toByteArray());
    return true;
}
Also used : Message(com.google.protobuf.Message) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand)

Aggregations

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