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;
}
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());
}
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;
}
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);
}
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;
}
Aggregations