funky-eyes commented on code in PR #6230: URL: https://github.com/apache/incubator-seata/pull/6230#discussion_r1493947660
########## rocketmq/pom.xml: ########## @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>org.apache.seata</groupId> + <artifactId>seata-parent</artifactId> + <version>${revision}</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>seata-rocketmq</artifactId> + <packaging>jar</packaging> + <name>seata-rocketmq ${project.version}</name> + <description>rocketmq integration for Seata built with Maven</description> + + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>seata-tcc</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.rocketmq</groupId> + <artifactId>rocketmq-client</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> Review Comment: Why rely on spring? 为什么依赖spring? ########## seata-spring-boot-starter/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataRocketMQAutoConfiguration.java: ########## @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.spring.boot.autoconfigure; + +import org.apache.seata.integration.rocketmq.SeataMQProducerFactory; +import org.apache.seata.integration.rocketmq.TccRocketDefinitionRegistry; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * SeataRocketMQAutoConfiguration + */ +@ConditionalOnClass(name = "org.apache.rocketmq.client.producer.DefaultMQProducer") +@ConditionalOnExpression("${seata.enabled:true}") +@Configuration(proxyBeanMethods = false) +@AutoConfigureAfter(SeataAutoConfiguration.class) +public class SeataRocketMQAutoConfiguration { Review Comment: 第一期不要实现autoconfiguration Don't implement autoconfiguration in the first issue ########## seata-spring-boot-starter/pom.xml: ########## @@ -66,6 +66,11 @@ <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> + <dependency> Review Comment: remove ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/TCCRocketMQ.java: ########## @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.seata.core.exception.TransactionException; +import org.apache.seata.rm.tcc.api.BusinessActionContext; +import org.apache.seata.rm.tcc.api.LocalTCC; +import org.apache.seata.rm.tcc.api.TwoPhaseBusinessAction; + +import java.net.UnknownHostException; +import java.util.concurrent.TimeoutException; + +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.remoting.exception.RemotingException; + +/** + * The interface Tcc rocket mq. + */ +@LocalTCC Review Comment: Putting annotations into implementation classes 把注解放到实现类中 ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/TCCRocketMQ.java: ########## @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.seata.core.exception.TransactionException; +import org.apache.seata.rm.tcc.api.BusinessActionContext; +import org.apache.seata.rm.tcc.api.LocalTCC; +import org.apache.seata.rm.tcc.api.TwoPhaseBusinessAction; + +import java.net.UnknownHostException; +import java.util.concurrent.TimeoutException; + +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.remoting.exception.RemotingException; + +/** + * The interface Tcc rocket mq. + */ +@LocalTCC +public interface TCCRocketMQ { + + + /** + * set SeataMQProducer + * + * @param producer the producer + */ + void setProducer(SeataMQProducer producer); + + /** + * RocketMQ half send + * + * @param message the message + * @param timeout the timeout + * @return SendResult + */ + @TwoPhaseBusinessAction(name = SeataMQProducerFactory.ROCKET_TCC_NAME) Review Comment: Putting annotations into implementation classes 把注解放到实现类中 ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducerFactory.java: ########## @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.commons.lang.ObjectUtils; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.seata.common.exception.NotSupportYetException; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +/** + * SeataMQProducer Factory + **/ +public class SeataMQProducerFactory implements ApplicationContextAware, InitializingBean { Review Comment: 为什么要依赖spring?为什么不能有一个更底层的factory的方法? Why do you rely on spring and why can't there be a way to have a more underlying factory? ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducer.java: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.rm.DefaultResourceManager; +import org.apache.rocketmq.client.Validators; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.LocalTransactionState; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.TransactionListener; +import org.apache.rocketmq.client.producer.TransactionMQProducer; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageConst; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; + +/** + * Seata MQ Producer + **/ +public class SeataMQProducer extends TransactionMQProducer { + + private static final Logger LOGGER = LoggerFactory.getLogger(SeataMQProducer.class); + + public static String PROPERTY_SEATA_XID = RootContext.KEY_XID; + public static String PROPERTY_SEATA_BRANCHID = "TX_BRANCHID"; + private TransactionListener transactionListener; + + SeataMQProducer(final String producerGroup) { + this(null, producerGroup, null); + } + + SeataMQProducer(final String namespace, final String producerGroup, RPCHook rpcHook) { + super(namespace, producerGroup, rpcHook); + this.transactionListener = new TransactionListener() { + @Override + public LocalTransactionState executeLocalTransaction(Message msg, Object arg) { + return LocalTransactionState.UNKNOW; + } + + @Override + public LocalTransactionState checkLocalTransaction(MessageExt msg) { + String xid = msg.getProperty(PROPERTY_SEATA_XID); + if (StringUtils.isBlank(xid)) { + LOGGER.error("msg has no xid, msgTransactionId: {}, msg will be rollback", msg.getTransactionId()); + return LocalTransactionState.ROLLBACK_MESSAGE; + } + List<GlobalStatus> commitStatuses = Arrays.asList(GlobalStatus.Committed, GlobalStatus.Committing, GlobalStatus.CommitRetrying); + List<GlobalStatus> rollbackStatuses = Arrays.asList(GlobalStatus.Rollbacked, GlobalStatus.Rollbacking, GlobalStatus.RollbackRetrying); + try { + GlobalStatus globalStatus = DefaultResourceManager.get().getGlobalStatus(xid); + if (commitStatuses.contains(globalStatus)) { + return LocalTransactionState.COMMIT_MESSAGE; + } else if (rollbackStatuses.contains(globalStatus) || GlobalStatus.isOnePhaseTimeout(globalStatus)) { + return LocalTransactionState.ROLLBACK_MESSAGE; + } else if (GlobalStatus.Finished.equals(globalStatus)) { + LOGGER.error("global transaction finished, msg will be rollback, xid: {}", xid); + return LocalTransactionState.ROLLBACK_MESSAGE; + } + } catch (TimeoutException e) { + LOGGER.error("getGlobalStatus error, xid: {}, msgTransactionId: {}", xid, msg.getTransactionId(), e); + } + return LocalTransactionState.UNKNOW; + } + }; + } + + + public SendResult send(Message msg) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { + return send(msg, this.defaultMQProducerImpl.getDefaultMQProducer().getSendMsgTimeout()); + } + + @Override + public SendResult send(Message msg, long timeout) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { + if (RootContext.inGlobalTransaction()) { + if (SeataMQProducerFactory.getTccRocketMQ() == null) { + throw new RuntimeException("TCCRocketMQ is not initialized"); + } + return SeataMQProducerFactory.getTccRocketMQ().prepare(msg, timeout); + } else { + return super.send(msg, timeout); + } + } + + public SendResult doSendMessageInTransaction(final Message msg, long timeout, String xid, long branchId) throws MQClientException { + msg.setTopic(withNamespace(msg.getTopic())); + if (msg.getDelayTimeLevel() != 0) { + MessageAccessor.clearProperty(msg, MessageConst.PROPERTY_DELAY_TIME_LEVEL); + } + + Validators.checkMessage(msg, this.defaultMQProducerImpl.getDefaultMQProducer()); + + SendResult sendResult = null; + MessageAccessor.putProperty(msg, MessageConst.PROPERTY_TRANSACTION_PREPARED, "true"); + MessageAccessor.putProperty(msg, MessageConst.PROPERTY_PRODUCER_GROUP, this.defaultMQProducerImpl.getDefaultMQProducer().getProducerGroup()); + MessageAccessor.putProperty(msg, PROPERTY_SEATA_XID, xid); + MessageAccessor.putProperty(msg, PROPERTY_SEATA_BRANCHID, String.valueOf(branchId)); + try { + sendResult = super.send(msg, timeout); + } catch (Exception e) { + throw new MQClientException("send message Exception", e); + } + + if (SendStatus.SEND_OK != sendResult.getSendStatus()) { Review Comment: A fail should be reported on a branch failure so that there is no need for a two-stage rollback 在分支失败的时候应该上报一个fail,这样不需要二阶段回滚了 ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/TccRocketDefinitionRegistry.java: ########## @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; + +/** + * the type rocket mq tcc definition registry + */ +public class TccRocketDefinitionRegistry implements BeanDefinitionRegistryPostProcessor { Review Comment: remove ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducer.java: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.rm.DefaultResourceManager; +import org.apache.rocketmq.client.Validators; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.LocalTransactionState; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.TransactionListener; +import org.apache.rocketmq.client.producer.TransactionMQProducer; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageConst; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; + +/** + * Seata MQ Producer + **/ +public class SeataMQProducer extends TransactionMQProducer { + + private static final Logger LOGGER = LoggerFactory.getLogger(SeataMQProducer.class); + + public static String PROPERTY_SEATA_XID = RootContext.KEY_XID; + public static String PROPERTY_SEATA_BRANCHID = "TX_BRANCHID"; Review Comment: Why not move it into Rootcontext? 为什么不移入到Rootcontext中? ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducer.java: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.rm.DefaultResourceManager; +import org.apache.rocketmq.client.Validators; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.LocalTransactionState; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.TransactionListener; +import org.apache.rocketmq.client.producer.TransactionMQProducer; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageConst; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; + +/** + * Seata MQ Producer + **/ +public class SeataMQProducer extends TransactionMQProducer { + + private static final Logger LOGGER = LoggerFactory.getLogger(SeataMQProducer.class); + + public static String PROPERTY_SEATA_XID = RootContext.KEY_XID; + public static String PROPERTY_SEATA_BRANCHID = "TX_BRANCHID"; + private TransactionListener transactionListener; + + SeataMQProducer(final String producerGroup) { + this(null, producerGroup, null); + } + + SeataMQProducer(final String namespace, final String producerGroup, RPCHook rpcHook) { + super(namespace, producerGroup, rpcHook); + this.transactionListener = new TransactionListener() { + @Override + public LocalTransactionState executeLocalTransaction(Message msg, Object arg) { + return LocalTransactionState.UNKNOW; + } + + @Override + public LocalTransactionState checkLocalTransaction(MessageExt msg) { + String xid = msg.getProperty(PROPERTY_SEATA_XID); + if (StringUtils.isBlank(xid)) { + LOGGER.error("msg has no xid, msgTransactionId: {}, msg will be rollback", msg.getTransactionId()); + return LocalTransactionState.ROLLBACK_MESSAGE; + } + List<GlobalStatus> commitStatuses = Arrays.asList(GlobalStatus.Committed, GlobalStatus.Committing, GlobalStatus.CommitRetrying); + List<GlobalStatus> rollbackStatuses = Arrays.asList(GlobalStatus.Rollbacked, GlobalStatus.Rollbacking, GlobalStatus.RollbackRetrying); + try { + GlobalStatus globalStatus = DefaultResourceManager.get().getGlobalStatus(xid); + if (commitStatuses.contains(globalStatus)) { + return LocalTransactionState.COMMIT_MESSAGE; + } else if (rollbackStatuses.contains(globalStatus) || GlobalStatus.isOnePhaseTimeout(globalStatus)) { + return LocalTransactionState.ROLLBACK_MESSAGE; + } else if (GlobalStatus.Finished.equals(globalStatus)) { + LOGGER.error("global transaction finished, msg will be rollback, xid: {}", xid); + return LocalTransactionState.ROLLBACK_MESSAGE; + } + } catch (TimeoutException e) { + LOGGER.error("getGlobalStatus error, xid: {}, msgTransactionId: {}", xid, msg.getTransactionId(), e); + } + return LocalTransactionState.UNKNOW; + } + }; + } + + + public SendResult send(Message msg) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { + return send(msg, this.defaultMQProducerImpl.getDefaultMQProducer().getSendMsgTimeout()); + } + + @Override + public SendResult send(Message msg, long timeout) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { + if (RootContext.inGlobalTransaction()) { + if (SeataMQProducerFactory.getTccRocketMQ() == null) { Review Comment: Why getTccRocketMQ multiple times? 为什么要多次的getTccRocketMQ? ########## rm/src/main/java/org/apache/seata/rm/DefaultResourceManager.java: ########## @@ -150,6 +155,13 @@ public BranchType getBranchType() { throw new FrameworkException("DefaultResourceManager isn't a real ResourceManager"); } + public GlobalStatus getGlobalStatus(String xid) throws TimeoutException { + GlobalStatusRequest queryGlobalStatus = new GlobalStatusRequest(); + queryGlobalStatus.setXid(xid); + GlobalStatusResponse response = (GlobalStatusResponse) RmNettyRemotingClient.getInstance().sendSyncRequest(queryGlobalStatus); Review Comment: 实现不应该放在defaultresourcemanager中,参考registry的写法 The implementation should not be placed in the defaultresourcemanager, refer to the registry writeup ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducer.java: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.rm.DefaultResourceManager; +import org.apache.rocketmq.client.Validators; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.LocalTransactionState; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.TransactionListener; +import org.apache.rocketmq.client.producer.TransactionMQProducer; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageConst; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; + +/** + * Seata MQ Producer + **/ +public class SeataMQProducer extends TransactionMQProducer { + + private static final Logger LOGGER = LoggerFactory.getLogger(SeataMQProducer.class); + + public static String PROPERTY_SEATA_XID = RootContext.KEY_XID; + public static String PROPERTY_SEATA_BRANCHID = "TX_BRANCHID"; + private TransactionListener transactionListener; + + SeataMQProducer(final String producerGroup) { + this(null, producerGroup, null); + } + + SeataMQProducer(final String namespace, final String producerGroup, RPCHook rpcHook) { + super(namespace, producerGroup, rpcHook); + this.transactionListener = new TransactionListener() { + @Override + public LocalTransactionState executeLocalTransaction(Message msg, Object arg) { + return LocalTransactionState.UNKNOW; + } + + @Override + public LocalTransactionState checkLocalTransaction(MessageExt msg) { + String xid = msg.getProperty(PROPERTY_SEATA_XID); + if (StringUtils.isBlank(xid)) { + LOGGER.error("msg has no xid, msgTransactionId: {}, msg will be rollback", msg.getTransactionId()); + return LocalTransactionState.ROLLBACK_MESSAGE; + } + List<GlobalStatus> commitStatuses = Arrays.asList(GlobalStatus.Committed, GlobalStatus.Committing, GlobalStatus.CommitRetrying); + List<GlobalStatus> rollbackStatuses = Arrays.asList(GlobalStatus.Rollbacked, GlobalStatus.Rollbacking, GlobalStatus.RollbackRetrying); + try { + GlobalStatus globalStatus = DefaultResourceManager.get().getGlobalStatus(xid); + if (commitStatuses.contains(globalStatus)) { + return LocalTransactionState.COMMIT_MESSAGE; + } else if (rollbackStatuses.contains(globalStatus) || GlobalStatus.isOnePhaseTimeout(globalStatus)) { + return LocalTransactionState.ROLLBACK_MESSAGE; + } else if (GlobalStatus.Finished.equals(globalStatus)) { + LOGGER.error("global transaction finished, msg will be rollback, xid: {}", xid); + return LocalTransactionState.ROLLBACK_MESSAGE; + } + } catch (TimeoutException e) { + LOGGER.error("getGlobalStatus error, xid: {}, msgTransactionId: {}", xid, msg.getTransactionId(), e); + } + return LocalTransactionState.UNKNOW; + } + }; + } + + + public SendResult send(Message msg) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { Review Comment: It doesn't need `@Override`? 它不需要`@Override`? ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducer.java: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.rm.DefaultResourceManager; +import org.apache.rocketmq.client.Validators; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.LocalTransactionState; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.TransactionListener; +import org.apache.rocketmq.client.producer.TransactionMQProducer; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageConst; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; + +/** + * Seata MQ Producer + **/ +public class SeataMQProducer extends TransactionMQProducer { + + private static final Logger LOGGER = LoggerFactory.getLogger(SeataMQProducer.class); + + public static String PROPERTY_SEATA_XID = RootContext.KEY_XID; + public static String PROPERTY_SEATA_BRANCHID = "TX_BRANCHID"; + private TransactionListener transactionListener; + + SeataMQProducer(final String producerGroup) { + this(null, producerGroup, null); + } + + SeataMQProducer(final String namespace, final String producerGroup, RPCHook rpcHook) { + super(namespace, producerGroup, rpcHook); + this.transactionListener = new TransactionListener() { + @Override + public LocalTransactionState executeLocalTransaction(Message msg, Object arg) { + return LocalTransactionState.UNKNOW; + } + + @Override + public LocalTransactionState checkLocalTransaction(MessageExt msg) { + String xid = msg.getProperty(PROPERTY_SEATA_XID); + if (StringUtils.isBlank(xid)) { + LOGGER.error("msg has no xid, msgTransactionId: {}, msg will be rollback", msg.getTransactionId()); + return LocalTransactionState.ROLLBACK_MESSAGE; + } + List<GlobalStatus> commitStatuses = Arrays.asList(GlobalStatus.Committed, GlobalStatus.Committing, GlobalStatus.CommitRetrying); + List<GlobalStatus> rollbackStatuses = Arrays.asList(GlobalStatus.Rollbacked, GlobalStatus.Rollbacking, GlobalStatus.RollbackRetrying); Review Comment: Why not define it as a constant 为什么不将其定义为一个常量 ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/TCCRocketMQImpl.java: ########## @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.seata.core.exception.TransactionException; +import org.apache.seata.rm.tcc.api.BusinessActionContext; +import org.apache.seata.rm.tcc.api.BusinessActionContextUtil; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.LocalTransactionState; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeoutException; + +/** + * the type TCCRocketMQImpl + */ +public class TCCRocketMQImpl implements TCCRocketMQ { + private static final Logger LOGGER = LoggerFactory.getLogger(TCCRocketMQImpl.class); + + private static final String ROCKET_MSG_KEY = "ROCKET_MSG"; + private static final String ROCKET_SEND_RESULT_KEY = "ROCKET_SEND_RESULT"; + + private SeataMQProducer producer; + + @Override + public void setProducer(SeataMQProducer producer) { + this.producer = producer; + } + + @Override + public SendResult prepare(Message message, long timeout) throws MQClientException { + BusinessActionContext context = BusinessActionContextUtil.getContext(); + LOGGER.info("RocketMQ message send prepare, xid = {}", context.getXid()); + Map<String, Object> params = new HashMap<>(8); + SendResult sendResult = producer.doSendMessageInTransaction(message, timeout, context.getXid(), context.getBranchId()); + params.put(ROCKET_MSG_KEY, message); + params.put(ROCKET_SEND_RESULT_KEY, sendResult); + BusinessActionContextUtil.addContext(params); + return sendResult; + } + + @Override + public boolean commit(BusinessActionContext context) + throws UnknownHostException, MQBrokerException, RemotingException, InterruptedException, TimeoutException, TransactionException { + Message message = context.getActionContext(ROCKET_MSG_KEY, Message.class); + SendResult sendResult = context.getActionContext(ROCKET_SEND_RESULT_KEY, SendResult.class); + if (message == null || sendResult == null) { + throw new TransactionException("TCCRocketMQ commit but cannot find message and sendResult"); + } + producer.getDefaultMQProducerImpl().endTransaction(message, sendResult, LocalTransactionState.COMMIT_MESSAGE, null); + LOGGER.info("RocketMQ message send commit, xid = {}, branchId = {}", context.getXid(), context.getBranchId()); + return true; + } + + @Override + public boolean rollback(BusinessActionContext context) + throws UnknownHostException, MQBrokerException, RemotingException, InterruptedException, TransactionException { + Message message = context.getActionContext(ROCKET_MSG_KEY, Message.class); + SendResult sendResult = context.getActionContext(ROCKET_SEND_RESULT_KEY, SendResult.class); + if (message == null || sendResult == null) { + LOGGER.error("TCCRocketMQ rollback but cannot find message and sendResult"); + } + producer.getDefaultMQProducerImpl().endTransaction(message, sendResult, LocalTransactionState.ROLLBACK_MESSAGE, null); Review Comment: 非常多的代码使用了getDefaultMQProducerImpl,应该将其设置为成员变量,避免无意义开销 A very large amount of code uses getDefaultMQProducerImpl, which should be set as a member variable to avoid pointless overhead ########## rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducer.java: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.integration.rocketmq; + +import org.apache.rocketmq.client.producer.SendStatus; +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.core.context.RootContext; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.rm.DefaultResourceManager; +import org.apache.rocketmq.client.Validators; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.producer.LocalTransactionState; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.client.producer.TransactionListener; +import org.apache.rocketmq.client.producer.TransactionMQProducer; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageConst; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.remoting.RPCHook; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; + +/** + * Seata MQ Producer + **/ +public class SeataMQProducer extends TransactionMQProducer { + + private static final Logger LOGGER = LoggerFactory.getLogger(SeataMQProducer.class); + + public static String PROPERTY_SEATA_XID = RootContext.KEY_XID; + public static String PROPERTY_SEATA_BRANCHID = "TX_BRANCHID"; + private TransactionListener transactionListener; + + SeataMQProducer(final String producerGroup) { + this(null, producerGroup, null); + } + + SeataMQProducer(final String namespace, final String producerGroup, RPCHook rpcHook) { + super(namespace, producerGroup, rpcHook); + this.transactionListener = new TransactionListener() { + @Override + public LocalTransactionState executeLocalTransaction(Message msg, Object arg) { + return LocalTransactionState.UNKNOW; + } + + @Override + public LocalTransactionState checkLocalTransaction(MessageExt msg) { + String xid = msg.getProperty(PROPERTY_SEATA_XID); + if (StringUtils.isBlank(xid)) { + LOGGER.error("msg has no xid, msgTransactionId: {}, msg will be rollback", msg.getTransactionId()); + return LocalTransactionState.ROLLBACK_MESSAGE; + } + List<GlobalStatus> commitStatuses = Arrays.asList(GlobalStatus.Committed, GlobalStatus.Committing, GlobalStatus.CommitRetrying); + List<GlobalStatus> rollbackStatuses = Arrays.asList(GlobalStatus.Rollbacked, GlobalStatus.Rollbacking, GlobalStatus.RollbackRetrying); + try { + GlobalStatus globalStatus = DefaultResourceManager.get().getGlobalStatus(xid); + if (commitStatuses.contains(globalStatus)) { + return LocalTransactionState.COMMIT_MESSAGE; + } else if (rollbackStatuses.contains(globalStatus) || GlobalStatus.isOnePhaseTimeout(globalStatus)) { Review Comment: The transaction corresponding to the xid should be counted in rollbackStatuses if the second phase has already been completed. xid对应的事务如果二阶段已经执行完成了,这里应该算在rollbackStatuses中 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
