Search in sources :

Example 1 with MergedWarpMessage

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);
                }
            }
        }
    }
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) MergeResultMessage(io.seata.core.protocol.MergeResultMessage) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) MessageFuture(io.seata.core.protocol.MessageFuture)

Example 2 with MergedWarpMessage

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());
}
Also used : GlobalBeginRequest(io.seata.core.protocol.transaction.GlobalBeginRequest) AbstractMessage(io.seata.core.protocol.AbstractMessage) ArrayList(java.util.ArrayList) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) Test(org.junit.jupiter.api.Test)

Example 3 with MergedWarpMessage

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;
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) Any(com.google.protobuf.Any)

Example 4 with MergedWarpMessage

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;
}
Also used : MergedWarpMessageProto(io.seata.serializer.protobuf.generated.MergedWarpMessageProto) AbstractMessage(io.seata.core.protocol.AbstractMessage) AbstractMessage(io.seata.core.protocol.AbstractMessage) Message(com.google.protobuf.Message) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) ArrayList(java.util.ArrayList) Any(com.google.protobuf.Any) AbstractMessageProto(io.seata.serializer.protobuf.generated.AbstractMessageProto)

Example 5 with MergedWarpMessage

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);
}
Also used : AbstractMessage(io.seata.core.protocol.AbstractMessage) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) ByteBuf(io.netty.buffer.ByteBuf) MessageSeataCodec(io.seata.serializer.seata.MessageSeataCodec)

Aggregations

MergedWarpMessage (io.seata.core.protocol.MergedWarpMessage)9 AbstractMessage (io.seata.core.protocol.AbstractMessage)6 AbstractResultMessage (io.seata.core.protocol.AbstractResultMessage)3 MergeResultMessage (io.seata.core.protocol.MergeResultMessage)3 ArrayList (java.util.ArrayList)3 Any (com.google.protobuf.Any)2 GlobalBeginRequest (io.seata.core.protocol.transaction.GlobalBeginRequest)2 MergedWarpMessageProto (io.seata.serializer.protobuf.generated.MergedWarpMessageProto)2 Test (org.junit.jupiter.api.Test)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 ByteBuf (io.netty.buffer.ByteBuf)1 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)1 MessageFuture (io.seata.core.protocol.MessageFuture)1 RpcContext (io.seata.core.rpc.RpcContext)1 AbstractMessageProto (io.seata.serializer.protobuf.generated.AbstractMessageProto)1 MessageSeataCodec (io.seata.serializer.seata.MessageSeataCodec)1 ByteBuffer (java.nio.ByteBuffer)1