Search in sources :

Example 6 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class MergeResultMessageConvertor method convert2Proto.

@Override
public MergedResultMessageProto convert2Proto(MergeResultMessage mergeResultMessage) {
    final short typeCode = mergeResultMessage.getTypeCode();
    final AbstractMessageProto abstractMessage = AbstractMessageProto.newBuilder().setMessageType(MessageTypeProto.forNumber(typeCode)).build();
    List<Any> lists = new ArrayList<>();
    for (AbstractMessage msg : mergeResultMessage.msgs) {
        final PbConvertor pbConvertor = ProtobufConvertManager.getInstance().fetchConvertor(msg.getClass().getName());
        lists.add(Any.pack((Message) pbConvertor.convert2Proto(msg)));
    }
    MergedResultMessageProto mergedWarpMessageProto = MergedResultMessageProto.newBuilder().setAbstractMessage(abstractMessage).addAllMsgs(lists).build();
    return mergedWarpMessageProto;
}
Also used : AbstractMessage(io.seata.core.protocol.AbstractMessage) MergeResultMessage(io.seata.core.protocol.MergeResultMessage) AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) AbstractMessage(io.seata.core.protocol.AbstractMessage) Message(com.google.protobuf.Message) ArrayList(java.util.ArrayList) Any(com.google.protobuf.Any) AbstractMessageProto(io.seata.serializer.protobuf.generated.AbstractMessageProto) MergedResultMessageProto(io.seata.serializer.protobuf.generated.MergedResultMessageProto)

Example 7 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage 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)

Example 8 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class MergeResultMessageCodec method encode.

@Override
public <T> void encode(T t, ByteBuf out) {
    MergeResultMessage mergeResultMessage = (MergeResultMessage) t;
    AbstractResultMessage[] msgs = mergeResultMessage.getMsgs();
    int writeIndex = out.writerIndex();
    out.writeInt(0);
    out.writeShort((short) msgs.length);
    for (AbstractMessage msg : msgs) {
        // get messageCodec
        short typeCode = msg.getTypeCode();
        // put typeCode
        out.writeShort(typeCode);
        MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode);
        messageCodec.encode(msg, out);
    }
    int length = out.readableBytes() - 4;
    out.setInt(writeIndex, length);
    if (msgs.length > 20) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("msg in one services merge packet:" + msgs.length + ",buffer size:" + length);
        }
    }
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) AbstractMessage(io.seata.core.protocol.AbstractMessage) MergeResultMessage(io.seata.core.protocol.MergeResultMessage) MessageSeataCodec(io.seata.serializer.seata.MessageSeataCodec)

Example 9 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class MergeResultMessageCodec method decode.

/**
 * Decode.
 *
 * @param mergeResultMessage the merge result message
 * @param byteBuffer         the byte buffer
 */
protected void decode(MergeResultMessage mergeResultMessage, ByteBuffer byteBuffer) {
    // msgs size
    short msgNum = byteBuffer.getShort();
    AbstractResultMessage[] msgs = new AbstractResultMessage[msgNum];
    for (int idx = 0; idx < msgNum; idx++) {
        short typeCode = byteBuffer.getShort();
        AbstractMessage abstractResultMessage = MessageCodecFactory.getMessage(typeCode);
        MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode);
        messageCodec.decode(abstractResultMessage, byteBuffer);
        msgs[idx] = (AbstractResultMessage) abstractResultMessage;
    }
    mergeResultMessage.setMsgs(msgs);
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) AbstractMessage(io.seata.core.protocol.AbstractMessage) MessageSeataCodec(io.seata.serializer.seata.MessageSeataCodec)

Example 10 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class MergeMessageConvertorTest method test.

@Test
public void test() {
    MergedWarpMessage mergedWarpMessage = new MergedWarpMessage();
    final ArrayList<AbstractMessage> msgs = new ArrayList<>();
    final GlobalBeginRequest globalBeginRequest = buildGlobalBeginRequest();
    msgs.add(globalBeginRequest);
    mergedWarpMessage.msgs = msgs;
    MergedWarpMessageConvertor pbConvertor = new MergedWarpMessageConvertor();
    MergedWarpMessageProto globalBeginRequestProto = pbConvertor.convert2Proto(mergedWarpMessage);
    MergedWarpMessage model = pbConvertor.convert2Model(globalBeginRequestProto);
    GlobalBeginRequest decodeModel = (GlobalBeginRequest) model.msgs.get(0);
    assertThat(decodeModel.getTransactionName()).isEqualTo(globalBeginRequest.getTransactionName());
    assertThat(decodeModel.getTimeout()).isEqualTo(globalBeginRequest.getTimeout());
    assertThat(decodeModel.getTypeCode()).isEqualTo(globalBeginRequest.getTypeCode());
}
Also used : MergedWarpMessageProto(io.seata.serializer.protobuf.generated.MergedWarpMessageProto) 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)

Aggregations

AbstractMessage (io.seata.core.protocol.AbstractMessage)12 MergedWarpMessage (io.seata.core.protocol.MergedWarpMessage)6 AbstractResultMessage (io.seata.core.protocol.AbstractResultMessage)5 ArrayList (java.util.ArrayList)5 MergeResultMessage (io.seata.core.protocol.MergeResultMessage)4 MessageSeataCodec (io.seata.serializer.seata.MessageSeataCodec)4 Any (com.google.protobuf.Any)2 Message (com.google.protobuf.Message)2 ByteBuf (io.netty.buffer.ByteBuf)2 GlobalBeginRequest (io.seata.core.protocol.transaction.GlobalBeginRequest)2 AbstractMessageProto (io.seata.serializer.protobuf.generated.AbstractMessageProto)2 MergedWarpMessageProto (io.seata.serializer.protobuf.generated.MergedWarpMessageProto)2 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.jupiter.api.Test)2 RpcContext (io.seata.core.rpc.RpcContext)1 MergedResultMessageProto (io.seata.serializer.protobuf.generated.MergedResultMessageProto)1