Search in sources :

Example 1 with AbstractResultMessage

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

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

the class MergeResultMessageConvertorTest method convert2Proto.

@Test
public void convert2Proto() {
    MergeResultMessage mergeResultMessage = new MergeResultMessage();
    AbstractResultMessage[] msgs = new AbstractResultMessage[1];
    final GlobalCommitResponse globalCommitResponse = new GlobalCommitResponse();
    globalCommitResponse.setGlobalStatus(GlobalStatus.AsyncCommitting);
    globalCommitResponse.setMsg("msg");
    globalCommitResponse.setResultCode(ResultCode.Failed);
    globalCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchRegisterFailed);
    msgs[0] = globalCommitResponse;
    mergeResultMessage.setMsgs(msgs);
    MergeResultMessageConvertor convertor = new MergeResultMessageConvertor();
    MergedResultMessageProto proto = convertor.convert2Proto(mergeResultMessage);
    MergeResultMessage real = convertor.convert2Model(proto);
    GlobalCommitResponse realObj = (GlobalCommitResponse) real.getMsgs()[0];
    assertThat((realObj.getTypeCode())).isEqualTo(globalCommitResponse.getTypeCode());
    assertThat((realObj.getMsg())).isEqualTo(globalCommitResponse.getMsg());
    assertThat((realObj.getResultCode())).isEqualTo(globalCommitResponse.getResultCode());
    assertThat((realObj.getTransactionExceptionCode())).isEqualTo(globalCommitResponse.getTransactionExceptionCode());
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) GlobalCommitResponse(io.seata.core.protocol.transaction.GlobalCommitResponse) MergeResultMessage(io.seata.core.protocol.MergeResultMessage) MergedResultMessageProto(io.seata.serializer.protobuf.generated.MergedResultMessageProto) Test(org.junit.jupiter.api.Test)

Example 3 with AbstractResultMessage

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

the class AbstractResultMessageCodec method encode.

@Override
public <T> void encode(T t, ByteBuf out) {
    AbstractResultMessage abstractResultMessage = (AbstractResultMessage) t;
    ResultCode resultCode = abstractResultMessage.getResultCode();
    String resultMsg = abstractResultMessage.getMsg();
    out.writeByte(resultCode.ordinal());
    if (resultCode == ResultCode.Failed) {
        if (StringUtils.isNotEmpty(resultMsg)) {
            String msg;
            if (resultMsg.length() > Short.MAX_VALUE) {
                msg = resultMsg.substring(0, Short.MAX_VALUE);
            } else {
                msg = resultMsg;
            }
            byte[] bs = msg.getBytes(UTF8);
            out.writeShort((short) bs.length);
            out.writeBytes(bs);
        } else {
            out.writeShort((short) 0);
        }
    }
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) ResultCode(io.seata.core.protocol.ResultCode)

Example 4 with AbstractResultMessage

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

the class MergeResultMessageConvertor method convert2Model.

@Override
public MergeResultMessage convert2Model(MergedResultMessageProto mergedResultMessageProto) {
    MergeResultMessage result = new MergeResultMessage();
    List<Any> anys = mergedResultMessageProto.getMsgsList();
    List<AbstractResultMessage> temp = new ArrayList<>();
    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);
                temp.add((AbstractResultMessage) model);
            } catch (InvalidProtocolBufferException e) {
                throw new ShouldNeverHappenException(e);
            }
        }
    }
    result.setMsgs(temp.toArray(new AbstractResultMessage[temp.size()]));
    return result;
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) MergeResultMessage(io.seata.core.protocol.MergeResultMessage) Any(com.google.protobuf.Any)

Example 5 with AbstractResultMessage

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

Aggregations

AbstractResultMessage (io.seata.core.protocol.AbstractResultMessage)11 MergeResultMessage (io.seata.core.protocol.MergeResultMessage)7 AbstractMessage (io.seata.core.protocol.AbstractMessage)4 MergedWarpMessage (io.seata.core.protocol.MergedWarpMessage)3 ResultCode (io.seata.core.protocol.ResultCode)2 RpcContext (io.seata.core.rpc.RpcContext)2 MessageSeataCodec (io.seata.serializer.seata.MessageSeataCodec)2 Test (org.junit.jupiter.api.Test)2 Any (com.google.protobuf.Any)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)1 MessageFuture (io.seata.core.protocol.MessageFuture)1 GlobalBeginResponse (io.seata.core.protocol.transaction.GlobalBeginResponse)1 GlobalCommitResponse (io.seata.core.protocol.transaction.GlobalCommitResponse)1 MergedResultMessageProto (io.seata.serializer.protobuf.generated.MergedResultMessageProto)1 ArrayList (java.util.ArrayList)1