use of io.seata.core.protocol.MergedWarpMessage in project seata by seata.
the class ClientOnResponseProcessor method process.
@Override
public void process(ChannelHandlerContext ctx, RpcMessage rpcMessage) throws Exception {
if (rpcMessage.getBody() instanceof MergeResultMessage) {
MergeResultMessage results = (MergeResultMessage) rpcMessage.getBody();
MergedWarpMessage mergeMessage = (MergedWarpMessage) mergeMsgMap.remove(rpcMessage.getId());
for (int i = 0; i < mergeMessage.msgs.size(); i++) {
int msgId = mergeMessage.msgIds.get(i);
MessageFuture future = futures.remove(msgId);
if (future == null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("msg: {} is not found in futures.", msgId);
}
} else {
future.setResultMessage(results.getMsgs()[i]);
}
}
} else {
MessageFuture messageFuture = futures.remove(rpcMessage.getId());
if (messageFuture != null) {
messageFuture.setResultMessage(rpcMessage.getBody());
} else {
if (rpcMessage.getBody() instanceof AbstractResultMessage) {
if (transactionMessageHandler != null) {
transactionMessageHandler.onResponse((AbstractResultMessage) rpcMessage.getBody(), null);
}
}
}
}
}
use of io.seata.core.protocol.MergedWarpMessage in project seata by seata.
the class MergedWarpMessageSerializerTest method test_codec.
/**
* Test codec.
*/
@Test
public void test_codec() {
MergedWarpMessage mergedWarpMessage = new MergedWarpMessage();
final ArrayList<AbstractMessage> msgs = new ArrayList<>();
final GlobalBeginRequest globalBeginRequest1 = buildGlobalBeginRequest("x1");
final GlobalBeginRequest globalBeginRequest2 = buildGlobalBeginRequest("x2");
msgs.add(globalBeginRequest1);
msgs.add(globalBeginRequest2);
mergedWarpMessage.msgs = msgs;
byte[] body = seataSerializer.serialize(mergedWarpMessage);
MergedWarpMessage mergedWarpMessage2 = seataSerializer.deserialize(body);
assertThat(mergedWarpMessage2.msgs.size()).isEqualTo(mergedWarpMessage.msgs.size());
GlobalBeginRequest globalBeginRequest21 = (GlobalBeginRequest) mergedWarpMessage2.msgs.get(0);
assertThat(globalBeginRequest21.getTimeout()).isEqualTo(globalBeginRequest1.getTimeout());
assertThat(globalBeginRequest21.getTransactionName()).isEqualTo(globalBeginRequest1.getTransactionName());
GlobalBeginRequest globalBeginRequest22 = (GlobalBeginRequest) mergedWarpMessage2.msgs.get(1);
assertThat(globalBeginRequest22.getTimeout()).isEqualTo(globalBeginRequest2.getTimeout());
assertThat(globalBeginRequest22.getTransactionName()).isEqualTo(globalBeginRequest2.getTransactionName());
}
use of io.seata.core.protocol.MergedWarpMessage in project seata by seata.
the class MergedWarpMessageConvertor method convert2Model.
@Override
public MergedWarpMessage convert2Model(MergedWarpMessageProto mergedWarpMessageProto) {
MergedWarpMessage result = new MergedWarpMessage();
List<Any> anys = mergedWarpMessageProto.getMsgsList();
for (Any any : anys) {
final Class clazz = ProtobufConvertManager.getInstance().fetchProtoClass(getTypeNameFromTypeUrl(any.getTypeUrl()));
if (any.is(clazz)) {
try {
Object ob = any.unpack(clazz);
final PbConvertor pbConvertor = ProtobufConvertManager.getInstance().fetchReversedConvertor(clazz.getName());
Object model = pbConvertor.convert2Model(ob);
result.msgs.add((AbstractMessage) model);
} catch (InvalidProtocolBufferException e) {
throw new ShouldNeverHappenException(e);
}
}
}
result.msgIds = mergedWarpMessageProto.getMsgIdsList();
return result;
}
use of io.seata.core.protocol.MergedWarpMessage in project seata by seata.
the class MergedWarpMessageConvertor method convert2Proto.
@Override
public MergedWarpMessageProto convert2Proto(MergedWarpMessage mergedWarpMessage) {
final short typeCode = mergedWarpMessage.getTypeCode();
final AbstractMessageProto abstractMessage = AbstractMessageProto.newBuilder().setMessageType(MessageTypeProto.forNumber(typeCode)).build();
List<Any> lists = new ArrayList<>();
for (AbstractMessage msg : mergedWarpMessage.msgs) {
final PbConvertor pbConvertor = ProtobufConvertManager.getInstance().fetchConvertor(msg.getClass().getName());
lists.add(Any.pack((Message) pbConvertor.convert2Proto(msg)));
}
MergedWarpMessageProto mergedWarpMessageProto = MergedWarpMessageProto.newBuilder().setAbstractMessage(abstractMessage).addAllMsgs(lists).addAllMsgIds(mergedWarpMessage.msgIds).build();
return mergedWarpMessageProto;
}
use of io.seata.core.protocol.MergedWarpMessage in project seata by seata.
the class MergedWarpMessageCodec method encode.
@Override
public <T> void encode(T t, ByteBuf out) {
MergedWarpMessage mergedWarpMessage = (MergedWarpMessage) t;
List<AbstractMessage> msgs = mergedWarpMessage.msgs;
final ByteBuf buffer = Unpooled.buffer(1024);
// write placeholder for content length
buffer.writeInt(0);
buffer.writeShort((short) msgs.size());
for (final AbstractMessage msg : msgs) {
final ByteBuf subBuffer = Unpooled.buffer(1024);
short typeCode = msg.getTypeCode();
MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode);
messageCodec.encode(msg, subBuffer);
buffer.writeShort(msg.getTypeCode());
buffer.writeBytes(subBuffer);
}
final int length = buffer.readableBytes();
final byte[] content = new byte[length];
// minus the placeholder length itself
buffer.setInt(0, length - 4);
buffer.readBytes(content);
if (msgs.size() > 20) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("msg in one packet:" + msgs.size() + ",buffer size:" + content.length);
}
}
out.writeBytes(content);
}
Aggregations