Search in sources :

Example 11 with RpcRequestCommand

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

the class ProtobufSerializer method deserializeHeader.

@Override
public <T extends RequestCommand> boolean deserializeHeader(T request) throws DeserializationException {
    final RpcRequestCommand cmd = (RpcRequestCommand) request;
    final String className = cmd.getRequestClass();
    if (className.equals(RpcRequests.AppendEntriesRequest.class.getName())) {
        final byte[] header = cmd.getHeader();
        cmd.setRequestHeader(ProtobufMsgFactory.newMessageByJavaClassName(RpcRequests.AppendEntriesRequestHeader.class.getName(), header));
        return true;
    }
    return false;
}
Also used : RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand)

Example 12 with RpcRequestCommand

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

the class ProtobufSerializerTest method testEncodeDecodeAppendEntiresRequestHeader.

@Test
public void testEncodeDecodeAppendEntiresRequestHeader() throws Exception {
    final AppendEntriesRequest reqObject = // 
    AppendEntriesRequest.newBuilder().setGroupId(// 
    "testGroup").setPeerId(// 
    "testPeer").setServerId(// 
    "testServer").setTerm(// 
    1).setPrevLogIndex(// 
    1).setPrevLogTerm(// 
    0).setCommittedIndex(1).build();
    final RpcCommandFactory cmdFactory = new RpcCommandFactory();
    final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
    request.setRequestClass(AppendEntriesRequest.class.getName());
    assertNull(request.getHeader());
    assertTrue(serializer.serializeContent(request, null));
    assertTrue(serializer.serializeHeader(request, null));
    assertNull(request.getRequestHeader());
    request.setRequestObject(null);
    assertTrue(serializer.deserializeContent(request));
    assertTrue(serializer.deserializeHeader(request));
    assertNotNull(request.getRequestObject());
    assertNotNull(request.getRequestHeader());
    assertEquals(reqObject, request.getRequestObject());
    assertNotSame(reqObject, request.getRequestObject());
    final AppendEntriesRequestHeader header = (AppendEntriesRequestHeader) request.getRequestHeader();
    assertEquals("testGroup", header.getGroupId());
    assertEquals("testPeer", header.getPeerId());
    assertEquals("testServer", header.getServerId());
}
Also used : AppendEntriesRequestHeader(com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequestHeader) AppendEntriesRequest(com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest) RpcCommandFactory(com.alipay.remoting.rpc.RpcCommandFactory) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) Test(org.junit.Test)

Example 13 with RpcRequestCommand

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

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 14 with RpcRequestCommand

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

the class SofaRpcSerializationTest method deserializeRequestContent.

@Test
public void deserializeRequestContent() {
    String traceId = "traceId";
    String rpcId = "rpcId";
    Map<String, String> headerMap = new HashMap<>();
    headerMap.put("rpc_trace_context.sofaTraceId", traceId);
    headerMap.put("rpc_trace_context.sofaRpcId", rpcId);
    RpcRequestCommand command = new RpcRequestCommand();
    command.setRequestHeader(headerMap);
    SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization();
    boolean exp = false;
    try {
        sofaRpcSerialization.deserializeContent(command);
    } catch (DeserializationException e) {
        exp = true;
        Assert.assertEquals("Content of request is null, traceId=" + traceId + ", rpcId=" + rpcId, e.getMessage());
    }
    Assert.assertTrue(exp);
}
Also used : HashMap(java.util.HashMap) DeserializationException(com.alipay.remoting.exception.DeserializationException) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) Test(org.junit.Test)

Example 15 with RpcRequestCommand

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

the class SofaRpcSerialization method serializeHeader.

@Override
public <Request extends RequestCommand> boolean serializeHeader(Request request, InvokeContext invokeContext) throws SerializationException {
    if (request instanceof RpcRequestCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        Object requestObject = requestCommand.getRequestObject();
        String service = getTargetServiceName(requestObject);
        if (StringUtils.isNotEmpty(service)) {
            Map<String, String> header = new HashMap<String, String>(16);
            header.put(RemotingConstants.HEAD_SERVICE, service);
            putRequestMetadataToHeader(requestObject, header);
            requestCommand.setHeader(mapSerializer.encode(header));
        }
        return true;
    }
    return false;
}
Also used : HashMap(java.util.HashMap) 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