Search in sources :

Example 1 with CheckTransactionStateRequestHeader

use of org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQProducerImpl method checkTransactionState.

@Override
public void checkTransactionState(final String addr, final MessageExt msg, final CheckTransactionStateRequestHeader header) {
    Runnable request = new Runnable() {

        private final String brokerAddr = addr;

        private final MessageExt message = msg;

        private final CheckTransactionStateRequestHeader checkRequestHeader = header;

        private final String group = DefaultMQProducerImpl.this.defaultMQProducer.getProducerGroup();

        @Override
        public void run() {
            TransactionCheckListener transactionCheckListener = DefaultMQProducerImpl.this.checkListener();
            if (transactionCheckListener != null) {
                LocalTransactionState localTransactionState = LocalTransactionState.UNKNOW;
                Throwable exception = null;
                try {
                    localTransactionState = transactionCheckListener.checkLocalTransactionState(message);
                } catch (Throwable e) {
                    log.error("Broker call checkTransactionState, but checkLocalTransactionState exception", e);
                    exception = e;
                }
                // 
                this.processTransactionState(// 
                localTransactionState, // 
                group, exception);
            } else {
                log.warn("checkTransactionState, pick transactionCheckListener by group[{}] failed", group);
            }
        }

        private // 
        void processTransactionState(// 
        final LocalTransactionState localTransactionState, // 
        final String producerGroup, final Throwable exception) {
            final EndTransactionRequestHeader thisHeader = new EndTransactionRequestHeader();
            thisHeader.setCommitLogOffset(checkRequestHeader.getCommitLogOffset());
            thisHeader.setProducerGroup(producerGroup);
            thisHeader.setTranStateTableOffset(checkRequestHeader.getTranStateTableOffset());
            thisHeader.setFromTransactionCheck(true);
            String uniqueKey = message.getProperties().get(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
            if (uniqueKey == null) {
                uniqueKey = message.getMsgId();
            }
            thisHeader.setMsgId(uniqueKey);
            thisHeader.setTransactionId(checkRequestHeader.getTransactionId());
            switch(localTransactionState) {
                case COMMIT_MESSAGE:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_COMMIT_TYPE);
                    break;
                case ROLLBACK_MESSAGE:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_ROLLBACK_TYPE);
                    log.warn("when broker check, client rollback this transaction, {}", thisHeader);
                    break;
                case UNKNOW:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_NOT_TYPE);
                    log.warn("when broker check, client does not know this transaction state, {}", thisHeader);
                    break;
                default:
                    break;
            }
            String remark = null;
            if (exception != null) {
                remark = "checkLocalTransactionState Exception: " + RemotingHelper.exceptionSimpleDesc(exception);
            }
            try {
                DefaultMQProducerImpl.this.mQClientFactory.getMQClientAPIImpl().endTransactionOneway(brokerAddr, thisHeader, remark, 3000);
            } catch (Exception e) {
                log.error("endTransactionOneway exception", e);
            }
        }
    };
    this.checkExecutor.submit(request);
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) LocalTransactionState(org.apache.rocketmq.client.producer.LocalTransactionState) CheckTransactionStateRequestHeader(org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader) TransactionCheckListener(org.apache.rocketmq.client.producer.TransactionCheckListener) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) EndTransactionRequestHeader(org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader)

Example 2 with CheckTransactionStateRequestHeader

use of org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ClientRemotingProcessor method checkTransactionState.

public RemotingCommand checkTransactionState(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final CheckTransactionStateRequestHeader requestHeader = (CheckTransactionStateRequestHeader) request.decodeCommandCustomHeader(CheckTransactionStateRequestHeader.class);
    final ByteBuffer byteBuffer = ByteBuffer.wrap(request.getBody());
    final MessageExt messageExt = MessageDecoder.decode(byteBuffer);
    if (messageExt != null) {
        final String group = messageExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
        if (group != null) {
            MQProducerInner producer = this.mqClientFactory.selectProducer(group);
            if (producer != null) {
                final String addr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
                producer.checkTransactionState(addr, messageExt, requestHeader);
            } else {
                log.debug("checkTransactionState, pick producer by group[{}] failed", group);
            }
        } else {
            log.warn("checkTransactionState, pick producer group failed");
        }
    } else {
        log.warn("checkTransactionState, decode message failed");
    }
    return null;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MQProducerInner(org.apache.rocketmq.client.impl.producer.MQProducerInner) ByteBuffer(java.nio.ByteBuffer) CheckTransactionStateRequestHeader(org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader)

Example 3 with CheckTransactionStateRequestHeader

use of org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader in project rocketmq by apache.

the class DefaultMQProducerImpl method checkTransactionState.

@Override
public void checkTransactionState(final String addr, final MessageExt msg, final CheckTransactionStateRequestHeader header) {
    Runnable request = new Runnable() {

        private final String brokerAddr = addr;

        private final MessageExt message = msg;

        private final CheckTransactionStateRequestHeader checkRequestHeader = header;

        private final String group = DefaultMQProducerImpl.this.defaultMQProducer.getProducerGroup();

        @Override
        public void run() {
            TransactionCheckListener transactionCheckListener = DefaultMQProducerImpl.this.checkListener();
            if (transactionCheckListener != null) {
                LocalTransactionState localTransactionState = LocalTransactionState.UNKNOW;
                Throwable exception = null;
                try {
                    localTransactionState = transactionCheckListener.checkLocalTransactionState(message);
                } catch (Throwable e) {
                    log.error("Broker call checkTransactionState, but checkLocalTransactionState exception", e);
                    exception = e;
                }
                this.processTransactionState(localTransactionState, group, exception);
            } else {
                log.warn("checkTransactionState, pick transactionCheckListener by group[{}] failed", group);
            }
        }

        private void processTransactionState(final LocalTransactionState localTransactionState, final String producerGroup, final Throwable exception) {
            final EndTransactionRequestHeader thisHeader = new EndTransactionRequestHeader();
            thisHeader.setCommitLogOffset(checkRequestHeader.getCommitLogOffset());
            thisHeader.setProducerGroup(producerGroup);
            thisHeader.setTranStateTableOffset(checkRequestHeader.getTranStateTableOffset());
            thisHeader.setFromTransactionCheck(true);
            String uniqueKey = message.getProperties().get(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
            if (uniqueKey == null) {
                uniqueKey = message.getMsgId();
            }
            thisHeader.setMsgId(uniqueKey);
            thisHeader.setTransactionId(checkRequestHeader.getTransactionId());
            switch(localTransactionState) {
                case COMMIT_MESSAGE:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_COMMIT_TYPE);
                    break;
                case ROLLBACK_MESSAGE:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_ROLLBACK_TYPE);
                    log.warn("when broker check, client rollback this transaction, {}", thisHeader);
                    break;
                case UNKNOW:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_NOT_TYPE);
                    log.warn("when broker check, client does not know this transaction state, {}", thisHeader);
                    break;
                default:
                    break;
            }
            String remark = null;
            if (exception != null) {
                remark = "checkLocalTransactionState Exception: " + RemotingHelper.exceptionSimpleDesc(exception);
            }
            try {
                DefaultMQProducerImpl.this.mQClientFactory.getMQClientAPIImpl().endTransactionOneway(brokerAddr, thisHeader, remark, 3000);
            } catch (Exception e) {
                log.error("endTransactionOneway exception", e);
            }
        }
    };
    this.checkExecutor.submit(request);
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) LocalTransactionState(org.apache.rocketmq.client.producer.LocalTransactionState) CheckTransactionStateRequestHeader(org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader) TransactionCheckListener(org.apache.rocketmq.client.producer.TransactionCheckListener) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) EndTransactionRequestHeader(org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader)

Example 4 with CheckTransactionStateRequestHeader

use of org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader in project rocketmq by apache.

the class ClientRemotingProcessor method checkTransactionState.

public RemotingCommand checkTransactionState(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final CheckTransactionStateRequestHeader requestHeader = (CheckTransactionStateRequestHeader) request.decodeCommandCustomHeader(CheckTransactionStateRequestHeader.class);
    final ByteBuffer byteBuffer = ByteBuffer.wrap(request.getBody());
    final MessageExt messageExt = MessageDecoder.decode(byteBuffer);
    if (messageExt != null) {
        final String group = messageExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
        if (group != null) {
            MQProducerInner producer = this.mqClientFactory.selectProducer(group);
            if (producer != null) {
                final String addr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
                producer.checkTransactionState(addr, messageExt, requestHeader);
            } else {
                log.debug("checkTransactionState, pick producer by group[{}] failed", group);
            }
        } else {
            log.warn("checkTransactionState, pick producer group failed");
        }
    } else {
        log.warn("checkTransactionState, decode message failed");
    }
    return null;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MQProducerInner(org.apache.rocketmq.client.impl.producer.MQProducerInner) ByteBuffer(java.nio.ByteBuffer) CheckTransactionStateRequestHeader(org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader)

Aggregations

MessageExt (org.apache.rocketmq.common.message.MessageExt)4 CheckTransactionStateRequestHeader (org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader)4 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 ByteBuffer (java.nio.ByteBuffer)2 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 MQProducerInner (org.apache.rocketmq.client.impl.producer.MQProducerInner)2 LocalTransactionState (org.apache.rocketmq.client.producer.LocalTransactionState)2 TransactionCheckListener (org.apache.rocketmq.client.producer.TransactionCheckListener)2 EndTransactionRequestHeader (org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader)2 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)2 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)2 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)2