You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "CAICAIIs (via GitHub)" <gi...@apache.org> on 2026/03/02 04:00:01 UTC
[PR] feature: add RocketMQ TCC integration [incubator-seata-go]
CAICAIIs opened a new pull request, #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054
<!-- Thanks for sending a pull request!
-->
<!-- Please make sure you have read and understood the contributing guidelines -->
- [x] I have registered the PR [changes](https://github.com/apache/incubator-seata-go/tree/master/changes).
**What this PR does**:
This PR implements RocketMQ TCC integration for distributed transactions, enabling RocketMQ transaction messages to participate in Seata global transactions.
1. TCCRocketMQAction - Implements TwoPhaseInterface to wrap RocketMQ transaction messages as TCC resources:
- Prepare: Sends half-message and stores transaction metadata
- Commit: Relies on broker check-back mechanism
- Rollback: Relies on broker check-back mechanism
2. SeataMQProducer - User-facing producer wrapper:
- Auto-detects global transaction context via tm.IsGlobalTx(ctx)
- Routes messages to TCC flow when in global transaction
- Falls back to normal send for non-transactional messages
3. SeataTransactionListener - Handles broker transaction state queries:
- ExecuteLocalTransaction: Returns UnknownState to defer decision
- CheckLocalTransaction: Queries TC for global transaction status
4. Producer Factory - Singleton pattern for producer lifecycle management
**Which issue(s) this PR fixes**:
### Usage Example
```go
// Initialize producer
cfg := &rocketmq.SeataMQProducerConfig{
NameServerAddrs: []string{"127.0.0.1:9876"},
GroupName: "seata-producer-group",
}
rocketmq.InitSeataMQProducer(cfg)
defer rocketmq.ShutdownSeataMQProducer()
producer, _ := rocketmq.GetSeataMQProducer()
// Send message in global transaction
tm.WithGlobalTx(ctx, tm.GtxConfig{Name: "order-create"}, func(ctx context.Context) error {
msg := primitive.NewMessage("order-topic", []byte("order created"))
_, err := producer.Send(ctx, msg)
return err
})
```
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Related to #765
**Special notes for your reviewer**:
1. Message Property Injection: XID and BranchId are injected into message properties for transaction correlation during broker check-back.
2. Broker Check-back: Commit/Rollback relies on RocketMQ's built-in check-back mechanism. When the broker queries transaction state, CheckLocalTransaction queries TC's global transaction status.
3. API Compatibility: Uses rocketmq-client-go/v2 public APIs only.
4. Thread Safety: Producer uses read/write mutex for safe concurrent access.
**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note
Add RocketMQ TCC integration support for distributed transaction messaging
```
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#issuecomment-3988778485
新增修改
1. pkg/protocol/message/other_message.go
- 问题: 无缓冲通道导致响应信号丢失
- 修复: Done: make(chan struct{}, 1) - 改为缓冲通道
- 影响: 解决20秒超时问题,注册时间从20秒降至<1秒
2. pkg/remoting/getty/getty_remoting.go
- 问题: 响应到达时如果接收者已超时,发送者会永久阻塞
- 修复: 添加 select + default 防御性发送
- 影响: 防止goroutine泄漏,增加可观测性
3. pkg/integration/rocketmq/seata_producer.go
- 问题: 两个producer用相同的group name,RocketMQ不允许
- 修复: 普通producer的group name加 -normal 后缀
- 影响: 解决producer启动失败问题
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#issuecomment-4150471179
新更改:补强了 RocketMQ producer factory 的单测,覆盖初始化失败后重试、启动失败后重试、成功后不重复初始化,以及关闭后的状态重置这些关键生命周期场景。顺手把测试里共用的 stub 抽成了独立的测试辅助文件,让各个测试文件职责更清晰,但没有改动业务逻辑。
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "ssshr-66 (via GitHub)" <gi...@apache.org>.
ssshr-66 merged PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#issuecomment-4136669150
<img width="7013" height="891" alt="image" src="https://github.com/user-attachments/assets/6c290bf7-25f6-4a04-9a99-efd9e9d235bc" />
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#discussion_r2986830795
##########
pkg/remoting/getty/getty_remoting.go:
##########
@@ -128,7 +128,11 @@ func (g *GettyRemoting) NotifyRpcMessageResponse(rpcMessage message.RpcMessage)
messageFuture.Response = rpcMessage.Body
// todo add messageFuture.Err
// messageFuture.Err = rpcMessage.Err
- messageFuture.Done <- struct{}{}
+ select {
+ case messageFuture.Done <- struct{}{}:
+ default:
+ log.Warnf("response arrived after timeout for msg ID: %d", rpcMessage.ID)
Review Comment:
The `default` branch here triggers when `Done` already has a signal (buffer full), which is not necessarily “response arrived after timeout”. This can make the warning misleading (e.g., duplicate/late responses). Consider adjusting the log message to reflect the actual condition (e.g., “future already completed; dropping signal”) or tracking timeout state explicitly if you want to log timeouts.
```suggestion
log.Warnf("message future already completed; dropping additional response for msg ID: %d", rpcMessage.ID)
```
##########
pkg/integration/rocketmq/transaction_listener.go:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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 rocketmq
+
+import (
+ "fmt"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+
+ "seata.apache.org/seata-go/v2/pkg/constant"
+ "seata.apache.org/seata-go/v2/pkg/protocol/message"
+ "seata.apache.org/seata-go/v2/pkg/remoting/getty"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type SeataTransactionListener struct {
+ producer *SeataMQProducer
+}
+
+func NewSeataTransactionListener(producer *SeataMQProducer) *SeataTransactionListener {
+ return &SeataTransactionListener{producer: producer}
+}
+
+func (l *SeataTransactionListener) ExecuteLocalTransaction(msg *primitive.Message) primitive.LocalTransactionState {
+ xid := msg.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ return primitive.CommitMessageState
+ }
+ log.Debugf("[SeataTransactionListener] ExecuteLocalTransaction, xid=%s, returning UnknownState", xid)
+ return primitive.UnknowState
+}
+
+func (l *SeataTransactionListener) CheckLocalTransaction(msgExt *primitive.MessageExt) primitive.LocalTransactionState {
+ xid := msgExt.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ log.Warnf("[SeataTransactionListener] CheckLocalTransaction: missing XID, rollback")
+ return primitive.RollbackMessageState
+ }
+
+ branchIdStr := msgExt.GetProperty(constant.PropertySeataBranchId)
+ log.Infof("[SeataTransactionListener] CheckLocalTransaction, xid=%s, branchId=%s", xid, branchIdStr)
+
+ globalStatus, err := l.queryGlobalStatus(xid)
+ if err != nil {
+ log.Errorf("[SeataTransactionListener] Query global status failed, xid=%s, err=%v", xid, err)
+ return primitive.UnknowState
+ }
+
+ switch globalStatus {
+ case message.GlobalStatusCommitted:
+ log.Infof("[SeataTransactionListener] Global tx committed, xid=%s", xid)
+ return primitive.CommitMessageState
+ case message.GlobalStatusCommitting, message.GlobalStatusBegin:
+ log.Infof("[SeataTransactionListener] Global tx committing/in progress, xid=%s, status=%v", xid, globalStatus)
+ return primitive.UnknowState
+ case message.GlobalStatusRollbacking:
+ log.Infof("[SeataTransactionListener] Global tx rolling back, xid=%s, status=%v", xid, globalStatus)
+ return primitive.UnknowState
+ case message.GlobalStatusRollbacked, message.GlobalStatusTimeoutRollbacked, message.GlobalStatusRollbackFailed:
+ log.Infof("[SeataTransactionListener] Global tx rollbacked, xid=%s, status=%v", xid, globalStatus)
Review Comment:
`CheckLocalTransaction` only handles a subset of `message.GlobalStatus` values. Final statuses like `GlobalStatusCommitFailed`, `GlobalStatusTimeoutRollbackFailed`, and `GlobalStatusFinished`, and transitional ones like `GlobalStatusAsyncCommitting` / `GlobalStatusCommitRetrying` / `GlobalStatusRollbackRetrying` currently fall into `default` and return `UnknowState`, which can cause RocketMQ to keep checking indefinitely. Please map the remaining statuses explicitly to Commit/Rollback/Unknown according to Seata semantics (and ideally document the mapping).
```suggestion
// Map Seata global transaction status to RocketMQ local transaction state.
// - Committed / Finished -> CommitMessageState
// - Rollbacked / rollback failed -> RollbackMessageState
// - Begin / *Committing / *Rollbacking (including retry/async) -> UnknowState (broker will re-check)
switch globalStatus {
case message.GlobalStatusCommitted, message.GlobalStatusFinished:
log.Infof("[SeataTransactionListener] Global tx committed/finished, xid=%s, status=%v", xid, globalStatus)
return primitive.CommitMessageState
case message.GlobalStatusCommitting,
message.GlobalStatusBegin,
message.GlobalStatusAsyncCommitting,
message.GlobalStatusCommitRetrying:
log.Infof("[SeataTransactionListener] Global tx committing/in progress, xid=%s, status=%v", xid, globalStatus)
return primitive.UnknowState
case message.GlobalStatusRollbacking,
message.GlobalStatusRollbackRetrying,
message.GlobalStatusTimeoutRollbacking,
message.GlobalStatusTimeoutRollbackRetrying:
log.Infof("[SeataTransactionListener] Global tx rolling back/in progress, xid=%s, status=%v", xid, globalStatus)
return primitive.UnknowState
case message.GlobalStatusRollbacked,
message.GlobalStatusTimeoutRollbacked,
message.GlobalStatusRollbackFailed,
message.GlobalStatusCommitFailed,
message.GlobalStatusTimeoutRollbackFailed:
log.Infof("[SeataTransactionListener] Global tx rollbacked/failed, xid=%s, status=%v", xid, globalStatus)
```
##########
go.mod:
##########
@@ -52,6 +52,7 @@ require (
github.com/Workiva/go-datastructures v1.0.52 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
github.com/apache/dubbo-go-hessian2 v1.11.4 // indirect
+ github.com/apache/rocketmq-client-go/v2 v2.1.2 // indirect
Review Comment:
`github.com/apache/rocketmq-client-go/v2` is imported directly by the new RocketMQ integration, so it should not be marked as `// indirect` in go.mod. Please run `go mod tidy` (or update the require line) so direct dependencies are recorded correctly.
```suggestion
github.com/apache/rocketmq-client-go/v2 v2.1.2
```
##########
pkg/integration/rocketmq/seata_producer.go:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 rocketmq
+
+import (
+ "context"
+ "fmt"
+ "sync"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+ "github.com/apache/rocketmq-client-go/v2/producer"
+
+ "seata.apache.org/seata-go/v2/pkg/rm/tcc"
+ "seata.apache.org/seata-go/v2/pkg/tm"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type transactionProducerInterface interface {
+ Start() error
+ Shutdown() error
+ SendMessageInTransaction(context.Context, *primitive.Message) (*primitive.TransactionSendResult, error)
+}
+
+type normalProducerInterface interface {
+ Start() error
+ Shutdown() error
+ SendSync(ctx context.Context, msg ...*primitive.Message) (*primitive.SendResult, error)
+}
+
+type SeataMQProducer struct {
+ config *SeataMQProducerConfig
+ transactionProducer transactionProducerInterface
+ normalProducer normalProducerInterface
+ tccAction *TCCRocketMQAction
+ tccProxy *tcc.TCCServiceProxy
+
+ mu sync.RWMutex
+ closed bool
+}
+
+func NewSeataMQProducer(cfg *SeataMQProducerConfig) (*SeataMQProducer, error) {
+ if cfg == nil {
+ return nil, fmt.Errorf("config cannot be nil")
+ }
+
+ if cfg.NameServerAddrs == nil || len(cfg.NameServerAddrs) == 0 {
+ return nil, fmt.Errorf("NameServerAddrs cannot be empty")
+ }
+
+ if cfg.GroupName == "" {
+ return nil, fmt.Errorf("GroupName cannot be empty")
+ }
+
+ p := &SeataMQProducer{
+ config: cfg,
+ }
+
+ p.tccAction = NewTCCRocketMQAction(p)
+
+ // NewTCCServiceProxy internally calls ParseTCCResource, so we pass the action directly
+ var err error
+ p.tccProxy, err = tcc.NewTCCServiceProxy(p.tccAction)
+ if err != nil {
+ return nil, fmt.Errorf("create TCC proxy failed: %w", err)
+ }
+
+ listener := NewSeataTransactionListener(p)
+ opts := cfg.ToRocketMQProducerOptions()
+
+ p.transactionProducer, err = producer.NewTransactionProducer(listener, opts...)
+ if err != nil {
+ return nil, fmt.Errorf("create transaction producer failed: %w", err)
+ }
+
+ normalCfg := *cfg
+ normalCfg.GroupName = cfg.GroupName + "-normal"
+ normalOpts := normalCfg.ToRocketMQProducerOptions()
+ p.normalProducer, err = producer.NewDefaultProducer(normalOpts...)
+ if err != nil {
+ return nil, fmt.Errorf("create normal producer failed: %w", err)
+ }
+
+ return p, nil
+}
+
+func (p *SeataMQProducer) Start() error {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+
+ if p.closed {
+ return fmt.Errorf("producer already closed")
+ }
+
+ if err := p.transactionProducer.Start(); err != nil {
+ return err
+ }
+
+ if err := p.normalProducer.Start(); err != nil {
+ p.transactionProducer.Shutdown()
+ return err
+ }
+
+ return nil
+}
+
+func (p *SeataMQProducer) Shutdown() error {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+
+ if p.closed {
+ return nil
+ }
+
+ p.closed = true
+
+ var errs []error
+ if err := p.transactionProducer.Shutdown(); err != nil {
+ errs = append(errs, err)
+ }
+ if err := p.normalProducer.Shutdown(); err != nil {
+ errs = append(errs, err)
+ }
+
+ if len(errs) > 0 {
+ return fmt.Errorf("shutdown errors: %v", errs)
+ }
+ return nil
+}
+
+func (p *SeataMQProducer) Send(ctx context.Context, msg *primitive.Message) (*primitive.SendResult, error) {
+ p.mu.RLock()
+ defer p.mu.RUnlock()
+
+ if p.closed {
+ return nil, fmt.Errorf("producer is closed")
+ }
+
+ if msg == nil {
+ return nil, fmt.Errorf("message cannot be nil")
+ }
+
+ if !tm.IsGlobalTx(ctx) {
+ return p.sendSync(ctx, msg)
+ }
+
+ _, err := p.tccProxy.Prepare(ctx, msg)
+ if err != nil {
+ log.Errorf("[SeataMQProducer] Send in global tx failed, xid=%s, err=%v", tm.GetXID(ctx), err)
+ return nil, err
+ }
+
+ bac := tm.GetBusinessActionContext(ctx)
+ return &primitive.SendResult{
+ Status: primitive.SendOK,
+ MsgID: getStringFromMap(bac.ActionContext, ActionContextKeyMsgId),
+ OffsetMsgID: getStringFromMap(bac.ActionContext, ActionContextKeyOffsetMsgId),
+ }, nil
+}
Review Comment:
This new global-tx send path (`tm.IsGlobalTx` → `tccProxy.Prepare` → transactional half-message) and the returned `SendResult` are not covered by tests. Adding unit tests with stubbed `transactionProducer` / `normalProducer` (and a mocked Getty client for status queries) would help validate property injection (XID/BranchId), error paths, and the behavior difference between global vs non-global contexts.
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "AlexStocks (via GitHub)" <gi...@apache.org>.
AlexStocks commented on code in PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#discussion_r2990636009
##########
pkg/integration/rocketmq/transaction_listener.go:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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 rocketmq
+
+import (
+ "fmt"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+
+ "seata.apache.org/seata-go/v2/pkg/constant"
+ "seata.apache.org/seata-go/v2/pkg/protocol/message"
+ "seata.apache.org/seata-go/v2/pkg/remoting/getty"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type SeataTransactionListener struct {
+ producer *SeataMQProducer
+}
+
+func NewSeataTransactionListener(producer *SeataMQProducer) *SeataTransactionListener {
+ return &SeataTransactionListener{producer: producer}
+}
+
+func (l *SeataTransactionListener) ExecuteLocalTransaction(msg *primitive.Message) primitive.LocalTransactionState {
+ xid := msg.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ return primitive.CommitMessageState
+ }
+ log.Debugf("[SeataTransactionListener] ExecuteLocalTransaction, xid=%s, returning UnknownState", xid)
+ return primitive.UnknowState
+}
+
+func (l *SeataTransactionListener) CheckLocalTransaction(msgExt *primitive.MessageExt) primitive.LocalTransactionState {
+ xid := msgExt.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ log.Warnf("[SeataTransactionListener] CheckLocalTransaction: missing XID, rollback")
+ return primitive.RollbackMessageState
+ }
+
+ branchIdStr := msgExt.GetProperty(constant.PropertySeataBranchId)
+ log.Infof("[SeataTransactionListener] CheckLocalTransaction, xid=%s, branchId=%s", xid, branchIdStr)
+
+ globalStatus, err := l.queryGlobalStatus(xid)
+ if err != nil {
+ log.Errorf("[SeataTransactionListener] Query global status failed, xid=%s, err=%v", xid, err)
+ return primitive.UnknowState
+ }
+
+ switch globalStatus {
Review Comment:
[P1] 这块是新 feature 的核心完成路径,但当前新增测试只覆盖了 `ParseTCCResource/GetActionName` 这种静态元数据,完全没有覆盖 `CheckLocalTransaction()` 对 `Committed/Rollbacked/Begin/...` 的状态映射,也没有覆盖 `queryGlobalStatus()` 的异常分支。回查状态一旦映射错,消息就会被错误提交或回滚,风险不低。建议至少补一组 listener 单测,把主要全局状态和请求失败分支都跑一遍。
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#issuecomment-3981882506
Next steps: After this PR is merged, the next PR will implement direct commit/rollback via END_TRANSACTION to reduce commit latency from ~60s to milliseconds.
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#issuecomment-4148458083
> [P2] go.mod 依赖更新,建议确认 rocketmq-client-go v2.1.2 的安全漏洞和兼容性。
已确认过这次新增的 github.com/apache/rocketmq-client-go/v2 v2.1.2 依赖:目前它仍是该模块最新的稳定 2.x 版本,我没有查到针对这个模块版本本身的公开漏洞记录。
兼容性方面,这个 PR 只使用了它公开稳定的 2.x API(如 NewTransactionProducer、NewDefaultProducer、primitive.Message),并且我已按项目 CI 使用的 Go 1.20.14 实际跑过 pkg/integration/rocketmq 相关测试,结果通过。
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#issuecomment-4131376551
新更改:补强了 RocketMQ TCC 集成里的健壮性:完善了 CheckLocalTransaction 对 Seata 全局事务状态的映射,避免一些最终失败态一直返回 Unknown 导致 broker 反复回查。
同时给 SeataMQProducer.Send 和 TCC Prepare 增加了必要的检查,补了覆盖发送路径、状态查询和上下文回填的单元测试。
最后按 Go 1.20 做了 go mod tidy
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "thunguo (via GitHub)" <gi...@apache.org>.
thunguo commented on code in PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#discussion_r2872473019
##########
pkg/integration/rocketmq/seata_producer.go:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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 rocketmq
+
+import (
+ "context"
+ "fmt"
+ "sync"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+ "github.com/apache/rocketmq-client-go/v2/producer"
+
+ "seata.apache.org/seata-go/v2/pkg/rm/tcc"
+ "seata.apache.org/seata-go/v2/pkg/tm"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type transactionProducerInterface interface {
+ Start() error
+ Shutdown() error
+ SendMessageInTransaction(context.Context, *primitive.Message) (*primitive.TransactionSendResult, error)
+}
+
+type SeataMQProducer struct {
+ config *SeataMQProducerConfig
+ transactionProducer transactionProducerInterface
+ tccAction *TCCRocketMQAction
+ tccProxy *tcc.TCCServiceProxy
+
+ mu sync.RWMutex
+ closed bool
+}
+
+func NewSeataMQProducer(cfg *SeataMQProducerConfig) (*SeataMQProducer, error) {
+ if cfg == nil {
+ return nil, fmt.Errorf("config cannot be nil")
+ }
+
+ if cfg.NameServerAddrs == nil || len(cfg.NameServerAddrs) == 0 {
+ return nil, fmt.Errorf("NameServerAddrs cannot be empty")
+ }
+
+ if cfg.GroupName == "" {
+ return nil, fmt.Errorf("GroupName cannot be empty")
+ }
+
+ p := &SeataMQProducer{
+ config: cfg,
+ }
+
+ p.tccAction = NewTCCRocketMQAction(p)
+
+ tccResource, err := tcc.ParseTCCResource(p.tccAction)
Review Comment:
Is `ParseTCCResource` being called repeatedly here? `tcc.NewTCCServiceProxy` calls `ParseTCCResource` again.
##########
pkg/integration/rocketmq/transaction_listener.go:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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 rocketmq
+
+import (
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+
+ "seata.apache.org/seata-go/v2/pkg/constant"
+ "seata.apache.org/seata-go/v2/pkg/protocol/message"
+ "seata.apache.org/seata-go/v2/pkg/remoting/getty"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type SeataTransactionListener struct {
+ producer *SeataMQProducer
+}
+
+func NewSeataTransactionListener(producer *SeataMQProducer) *SeataTransactionListener {
+ return &SeataTransactionListener{producer: producer}
+}
+
+func (l *SeataTransactionListener) ExecuteLocalTransaction(msg *primitive.Message) primitive.LocalTransactionState {
+ xid := msg.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ return primitive.CommitMessageState
+ }
+ log.Debugf("[SeataTransactionListener] ExecuteLocalTransaction, xid=%s, returning UnknownState", xid)
+ return primitive.UnknowState
+}
+
+func (l *SeataTransactionListener) CheckLocalTransaction(msgExt *primitive.MessageExt) primitive.LocalTransactionState {
+ xid := msgExt.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ log.Warnf("[SeataTransactionListener] CheckLocalTransaction: missing XID, rollback")
+ return primitive.RollbackMessageState
+ }
+
+ branchIdStr := msgExt.GetProperty(constant.PropertySeataBranchId)
+ log.Infof("[SeataTransactionListener] CheckLocalTransaction, xid=%s, branchId=%s", xid, branchIdStr)
+
+ globalStatus, err := l.queryGlobalStatus(xid)
+ if err != nil {
+ log.Errorf("[SeataTransactionListener] Query global status failed, xid=%s, err=%v", xid, err)
+ return primitive.UnknowState
+ }
+
+ switch globalStatus {
+ case message.GlobalStatusCommitted:
+ log.Infof("[SeataTransactionListener] Global tx committed, xid=%s", xid)
+ return primitive.CommitMessageState
+ case message.GlobalStatusRollbacked, message.GlobalStatusTimeoutRollbacked, message.GlobalStatusRollbackFailed:
+ log.Infof("[SeataTransactionListener] Global tx rollbacked, xid=%s, status=%v", xid, globalStatus)
+ return primitive.RollbackMessageState
+ default:
Review Comment:
Do you need to handle intermediate states like GlobalStatusBegin, Committing, and Rollbacking?
##########
pkg/integration/rocketmq/producer_factory.go:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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 rocketmq
+
+import (
+ "fmt"
+ "sync"
+)
+
+var (
+ globalProducer *SeataMQProducer
+ producerMutex sync.RWMutex
+ producerInitOnce sync.Once
+ producerInitialized bool
+)
+
+func InitSeataMQProducer(cfg *SeataMQProducerConfig) error {
+ var initErr error
+ producerInitOnce.Do(func() {
Review Comment:
Wouldn't it be better to manually control `Init` here? If `Once` fails, it won't be executed again, instead, it will simply be set to `nil`, and the error will be delayed until the actual action occurs.
##########
pkg/integration/rocketmq/transaction_listener.go:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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 rocketmq
+
+import (
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+
+ "seata.apache.org/seata-go/v2/pkg/constant"
+ "seata.apache.org/seata-go/v2/pkg/protocol/message"
+ "seata.apache.org/seata-go/v2/pkg/remoting/getty"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type SeataTransactionListener struct {
+ producer *SeataMQProducer
+}
+
+func NewSeataTransactionListener(producer *SeataMQProducer) *SeataTransactionListener {
+ return &SeataTransactionListener{producer: producer}
+}
+
+func (l *SeataTransactionListener) ExecuteLocalTransaction(msg *primitive.Message) primitive.LocalTransactionState {
+ xid := msg.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ return primitive.CommitMessageState
+ }
+ log.Debugf("[SeataTransactionListener] ExecuteLocalTransaction, xid=%s, returning UnknownState", xid)
+ return primitive.UnknowState
+}
+
+func (l *SeataTransactionListener) CheckLocalTransaction(msgExt *primitive.MessageExt) primitive.LocalTransactionState {
+ xid := msgExt.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ log.Warnf("[SeataTransactionListener] CheckLocalTransaction: missing XID, rollback")
+ return primitive.RollbackMessageState
+ }
+
+ branchIdStr := msgExt.GetProperty(constant.PropertySeataBranchId)
+ log.Infof("[SeataTransactionListener] CheckLocalTransaction, xid=%s, branchId=%s", xid, branchIdStr)
+
+ globalStatus, err := l.queryGlobalStatus(xid)
+ if err != nil {
+ log.Errorf("[SeataTransactionListener] Query global status failed, xid=%s, err=%v", xid, err)
+ return primitive.UnknowState
+ }
+
+ switch globalStatus {
+ case message.GlobalStatusCommitted:
+ log.Infof("[SeataTransactionListener] Global tx committed, xid=%s", xid)
+ return primitive.CommitMessageState
+ case message.GlobalStatusRollbacked, message.GlobalStatusTimeoutRollbacked, message.GlobalStatusRollbackFailed:
+ log.Infof("[SeataTransactionListener] Global tx rollbacked, xid=%s, status=%v", xid, globalStatus)
+ return primitive.RollbackMessageState
+ default:
+ log.Infof("[SeataTransactionListener] Global tx in progress, xid=%s, status=%v", xid, globalStatus)
+ return primitive.UnknowState
+ }
+}
+
+func (l *SeataTransactionListener) queryGlobalStatus(xid string) (message.GlobalStatus, error) {
+ req := message.GlobalStatusRequest{
+ AbstractGlobalEndRequest: message.AbstractGlobalEndRequest{
+ Xid: xid,
+ },
+ }
+ res, err := getty.GetGettyRemotingClient().SendSyncRequest(req)
+ if err != nil {
+ return message.GlobalStatusUnKnown, err
+ }
+ return res.(message.GlobalStatusResponse).GlobalStatus, nil
Review Comment:
Avoid using the approach `res.(message.GlobalStatusResponse).GlobalStatus`. First check the assertion, then retrieve the value to prevent panics that cannot be handled on the main chain, as follows:
```go
gsResp, ok := res.(message.GlobalStatusResponse)
if !ok {
// ...
}
return gsResp.GlobalStatus, nil
```
##########
pkg/integration/rocketmq/tcc_rocketmq_action.go:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 rocketmq
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+
+ "seata.apache.org/seata-go/v2/pkg/constant"
+ "seata.apache.org/seata-go/v2/pkg/tm"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type TCCRocketMQAction struct {
+ producer *SeataMQProducer
+}
+
+func NewTCCRocketMQAction(producer *SeataMQProducer) *TCCRocketMQAction {
+ return &TCCRocketMQAction{
+ producer: producer,
+ }
+}
+
+func (a *TCCRocketMQAction) GetActionName() string {
+ return ResourceIDTCCRocketMQ
+}
+
+func (a *TCCRocketMQAction) Prepare(ctx context.Context, params interface{}) (bool, error) {
+ msg, ok := params.(*primitive.Message)
+ if !ok {
+ return false, fmt.Errorf("params must be *primitive.Message, got %T", params)
+ }
+
+ bac := tm.GetBusinessActionContext(ctx)
+ if bac == nil {
+ return false, fmt.Errorf("BusinessActionContext not found in context")
+ }
+
+ xid := tm.GetXID(ctx)
+ if xid == "" {
+ return false, fmt.Errorf("XID not found in context")
+ }
+
+ msg.WithProperty(constant.PropertySeataXID, xid)
+ msg.WithProperty(constant.PropertySeataBranchId, fmt.Sprintf("%d", bac.BranchId))
+
+ result, err := a.producer.transactionProducer.SendMessageInTransaction(ctx, msg)
+ if err != nil {
+ log.Errorf("[TCCRocketMQ] Prepare failed, xid=%s, err=%v", xid, err)
+ return false, err
+ }
+
+ bac.ActionContext[ActionContextKeyMsgId] = result.MsgID
+ bac.ActionContext[ActionContextKeyOffsetMsgId] = result.OffsetMsgID
+ bac.ActionContext[ActionContextKeyQueueOffset] = result.QueueOffset
+ bac.ActionContext[ActionContextKeyTransactionId] = result.TransactionID
+ if result.MessageQueue != nil {
+ bac.ActionContext[ActionContextKeyQueueId] = result.MessageQueue.QueueId
+ bac.ActionContext[ActionContextKeyBrokerName] = result.MessageQueue.BrokerName
+ }
+
+ log.Infof("[TCCRocketMQ] Prepare success, xid=%s, branchId=%d, msgId=%s", xid, bac.BranchId, result.MsgID)
+
+ return true, nil
+}
+
+func (a *TCCRocketMQAction) Commit(ctx context.Context, bac *tm.BusinessActionContext) (bool, error) {
Review Comment:
Why do both `Commit` and `Rollback` return `true` here? Doesn't this seem to contradict the semantics of TCC?
##########
pkg/integration/rocketmq/seata_producer.go:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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 rocketmq
+
+import (
+ "context"
+ "fmt"
+ "sync"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+ "github.com/apache/rocketmq-client-go/v2/producer"
+
+ "seata.apache.org/seata-go/v2/pkg/rm/tcc"
+ "seata.apache.org/seata-go/v2/pkg/tm"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type transactionProducerInterface interface {
+ Start() error
+ Shutdown() error
+ SendMessageInTransaction(context.Context, *primitive.Message) (*primitive.TransactionSendResult, error)
+}
+
+type SeataMQProducer struct {
+ config *SeataMQProducerConfig
+ transactionProducer transactionProducerInterface
+ tccAction *TCCRocketMQAction
+ tccProxy *tcc.TCCServiceProxy
+
+ mu sync.RWMutex
+ closed bool
+}
+
+func NewSeataMQProducer(cfg *SeataMQProducerConfig) (*SeataMQProducer, error) {
+ if cfg == nil {
+ return nil, fmt.Errorf("config cannot be nil")
+ }
+
+ if cfg.NameServerAddrs == nil || len(cfg.NameServerAddrs) == 0 {
+ return nil, fmt.Errorf("NameServerAddrs cannot be empty")
+ }
+
+ if cfg.GroupName == "" {
+ return nil, fmt.Errorf("GroupName cannot be empty")
+ }
+
+ p := &SeataMQProducer{
+ config: cfg,
+ }
+
+ p.tccAction = NewTCCRocketMQAction(p)
+
+ tccResource, err := tcc.ParseTCCResource(p.tccAction)
+ if err != nil {
+ return nil, fmt.Errorf("parse TCC resource failed: %w", err)
+ }
+
+ p.tccProxy, err = tcc.NewTCCServiceProxy(tccResource)
+ if err != nil {
+ return nil, fmt.Errorf("create TCC proxy failed: %w", err)
+ }
+
+ listener := NewSeataTransactionListener(p)
+ opts := cfg.ToRocketMQProducerOptions()
+
+ p.transactionProducer, err = producer.NewTransactionProducer(listener, opts...)
+ if err != nil {
+ return nil, fmt.Errorf("create transaction producer failed: %w", err)
+ }
+
+ return p, nil
+}
+
+func (p *SeataMQProducer) Start() error {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+
+ if p.closed {
+ return fmt.Errorf("producer already closed")
+ }
+
+ return p.transactionProducer.Start()
+}
+
+func (p *SeataMQProducer) Shutdown() error {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+
+ if p.closed {
+ return nil
+ }
+
+ p.closed = true
+ return p.transactionProducer.Shutdown()
+}
+
+func (p *SeataMQProducer) Send(ctx context.Context, msg *primitive.Message) (*primitive.SendResult, error) {
+ p.mu.RLock()
+ defer p.mu.RUnlock()
+
+ if p.closed {
+ return nil, fmt.Errorf("producer is closed")
+ }
+
+ if msg == nil {
+ return nil, fmt.Errorf("message cannot be nil")
+ }
+
+ if !tm.IsGlobalTx(ctx) {
+ return p.sendSync(ctx, msg)
+ }
+
+ _, err := p.tccProxy.Prepare(ctx, msg)
+ if err != nil {
+ log.Errorf("[SeataMQProducer] Send in global tx failed, xid=%s, err=%v", tm.GetXID(ctx), err)
+ return nil, err
+ }
+
+ bac := tm.GetBusinessActionContext(ctx)
+ return &primitive.SendResult{
+ Status: primitive.SendOK,
+ MsgID: getStringFromMap(bac.ActionContext, ActionContextKeyMsgId),
+ OffsetMsgID: getStringFromMap(bac.ActionContext, ActionContextKeyOffsetMsgId),
+ }, nil
+}
+
+func (p *SeataMQProducer) sendSync(ctx context.Context, msg *primitive.Message) (*primitive.SendResult, error) {
Review Comment:
There seems to be a semantic issue with `sendSync` here, as it internally uses `SendMessageInTransaction`. This causes the message to pass through `ExecuteLocalTransaction`, at which point `xid` is empty, triggering a commit. Currently, this process seems fine, but if the logic of `ExecuteLocalTransaction` is modified later, this behavior might become abnormal. You could embed a `rocketmq.NewProducer` within `SeataMQProducer` to handle common messages.
##########
pkg/integration/rocketmq/constants.go:
##########
@@ -0,0 +1,32 @@
+/*
+ * 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 rocketmq
+
+const (
+ ResourceIDTCCRocketMQ = "tccRocketMQ"
+
+ ActionContextKeyMessage = "message"
+ ActionContextKeySendResult = "sendResult"
Review Comment:
It appears that the constant `ActionContextKeySendResult` has not been used.
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "AlexStocks (via GitHub)" <gi...@apache.org>.
AlexStocks commented on code in PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#discussion_r2992663755
##########
pkg/integration/rocketmq/transaction_listener.go:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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 rocketmq
+
+import (
+ "fmt"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+
+ "seata.apache.org/seata-go/v2/pkg/constant"
+ "seata.apache.org/seata-go/v2/pkg/protocol/message"
+ "seata.apache.org/seata-go/v2/pkg/remoting/getty"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type SeataTransactionListener struct {
+ producer *SeataMQProducer
+ remotingClient globalStatusRequestSender
+}
+
+type globalStatusRequestSender interface {
+ SendSyncRequest(msg interface{}) (interface{}, error)
+}
+
+func NewSeataTransactionListener(producer *SeataMQProducer) *SeataTransactionListener {
+ return &SeataTransactionListener{
+ producer: producer,
+ remotingClient: getty.GetGettyRemotingClient(),
+ }
+}
+
+func (l *SeataTransactionListener) ExecuteLocalTransaction(msg *primitive.Message) primitive.LocalTransactionState {
+ xid := msg.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ return primitive.CommitMessageState
+ }
+ log.Debugf("[SeataTransactionListener] ExecuteLocalTransaction, xid=%s, returning UnknownState", xid)
+ return primitive.UnknowState
+}
+
+func (l *SeataTransactionListener) CheckLocalTransaction(msgExt *primitive.MessageExt) primitive.LocalTransactionState {
+ xid := msgExt.GetProperty(constant.PropertySeataXID)
+ if xid == "" {
+ log.Warnf("[SeataTransactionListener] CheckLocalTransaction: missing XID, rollback")
+ return primitive.RollbackMessageState
+ }
+
+ branchIdStr := msgExt.GetProperty(constant.PropertySeataBranchId)
+ log.Infof("[SeataTransactionListener] CheckLocalTransaction, xid=%s, branchId=%s", xid, branchIdStr)
+
+ globalStatus, err := l.queryGlobalStatus(xid)
+ if err != nil {
+ log.Errorf("[SeataTransactionListener] Query global status failed, xid=%s, err=%v", xid, err)
+ return primitive.UnknowState
+ }
+
+ localTransactionState := mapGlobalStatusToLocalTransactionState(globalStatus)
+ switch localTransactionState {
+ case primitive.CommitMessageState:
+ log.Infof("[SeataTransactionListener] Global tx committed, xid=%s", xid)
+ case primitive.RollbackMessageState:
+ log.Infof("[SeataTransactionListener] Global tx rollbacked, xid=%s, status=%v", xid, globalStatus)
+ default:
+ log.Infof("[SeataTransactionListener] Global tx waiting for final state, xid=%s, status=%v", xid, globalStatus)
+ }
+ return localTransactionState
+}
+
+func (l *SeataTransactionListener) queryGlobalStatus(xid string) (message.GlobalStatus, error) {
+ req := message.GlobalStatusRequest{
+ AbstractGlobalEndRequest: message.AbstractGlobalEndRequest{
+ Xid: xid,
+ },
+ }
+ res, err := l.remotingClient.SendSyncRequest(req)
+ if err != nil {
+ return message.GlobalStatusUnKnown, err
+ }
+ gsResp, ok := res.(message.GlobalStatusResponse)
+ if !ok {
+ log.Errorf("[SeataTransactionListener] Invalid response type for GetGlobalStatus, xid=%s", xid)
+ return message.GlobalStatusUnKnown, fmt.Errorf("invalid response type: %T", res)
+ }
+ return gsResp.GlobalStatus, nil
+}
+
+func mapGlobalStatusToLocalTransactionState(globalStatus message.GlobalStatus) primitive.LocalTransactionState {
+ switch globalStatus {
+ case message.GlobalStatusCommitted, message.GlobalStatusAsyncCommitting:
+ return primitive.CommitMessageState
+ case message.GlobalStatusRollbacked, message.GlobalStatusTimeoutRollbacked, message.GlobalStatusRollbackFailed,
+ message.GlobalStatusTimeoutRollbackFailed, message.GlobalStatusCommitFailed, message.GlobalStatusFinished:
Review Comment:
[P1] GlobalStatusFinished 不能直接映射成 RollbackMessageState。pkg/protocol/message/constant.go 对这个状态的定义只是 "Not managed in session MAP any more",它并不等价于“已回滚”;全局事务在二阶段完成并被 TC 清理后也可能返回 Finished。这里如果 broker 的回查晚于 TC 清理,会把实际上已经提交的消息误判成回滚并直接丢弃。建议把 Finished 视为 UnknowState,或者补一条能区分最终提交/回滚结果的查询路径,并同步修正这里锁定错误语义的单测。
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#issuecomment-3984400231
## [Codecov](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `1.08108%` with `183 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 55.91%. Comparing base ([`66e7777`](https://app.codecov.io/gh/apache/incubator-seata-go/commit/66e777708c1b3538a717cc380f03e7a856a8ff0a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`72ae5a0`](https://app.codecov.io/gh/apache/incubator-seata-go/commit/72ae5a0eb401e679c818d868d2944a93d20d778e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
| [Files with missing lines](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [pkg/integration/rocketmq/seata\_producer.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&filepath=pkg%2Fintegration%2Frocketmq%2Fseata_producer.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2ludGVncmF0aW9uL3JvY2tldG1xL3NlYXRhX3Byb2R1Y2VyLmdv) | 0.00% | [64 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [pkg/integration/rocketmq/transaction\_listener.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&filepath=pkg%2Fintegration%2Frocketmq%2Ftransaction_listener.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2ludGVncmF0aW9uL3JvY2tldG1xL3RyYW5zYWN0aW9uX2xpc3RlbmVyLmdv) | 0.00% | [38 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [pkg/integration/rocketmq/tcc\_rocketmq\_action.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&filepath=pkg%2Fintegration%2Frocketmq%2Ftcc_rocketmq_action.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2ludGVncmF0aW9uL3JvY2tldG1xL3RjY19yb2NrZXRtcV9hY3Rpb24uZ28=) | 5.55% | [34 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [pkg/integration/rocketmq/producer\_factory.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&filepath=pkg%2Fintegration%2Frocketmq%2Fproducer_factory.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2ludGVncmF0aW9uL3JvY2tldG1xL3Byb2R1Y2VyX2ZhY3RvcnkuZ28=) | 0.00% | [32 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [pkg/integration/rocketmq/config.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&filepath=pkg%2Fintegration%2Frocketmq%2Fconfig.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2ludGVncmF0aW9uL3JvY2tldG1xL2NvbmZpZy5nbw==) | 0.00% | [15 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #1054 +/- ##
==========================================
- Coverage 56.50% 55.91% -0.60%
==========================================
Files 266 271 +5
Lines 17484 17669 +185
==========================================
Hits 9879 9879
- Misses 6771 6955 +184
- Partials 834 835 +1
```
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1054?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feature: add RocketMQ TCC integration [incubator-seata-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on code in PR #1054:
URL: https://github.com/apache/incubator-seata-go/pull/1054#discussion_r2873565035
##########
pkg/integration/rocketmq/tcc_rocketmq_action.go:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 rocketmq
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+
+ "seata.apache.org/seata-go/v2/pkg/constant"
+ "seata.apache.org/seata-go/v2/pkg/tm"
+ "seata.apache.org/seata-go/v2/pkg/util/log"
+)
+
+type TCCRocketMQAction struct {
+ producer *SeataMQProducer
+}
+
+func NewTCCRocketMQAction(producer *SeataMQProducer) *TCCRocketMQAction {
+ return &TCCRocketMQAction{
+ producer: producer,
+ }
+}
+
+func (a *TCCRocketMQAction) GetActionName() string {
+ return ResourceIDTCCRocketMQ
+}
+
+func (a *TCCRocketMQAction) Prepare(ctx context.Context, params interface{}) (bool, error) {
+ msg, ok := params.(*primitive.Message)
+ if !ok {
+ return false, fmt.Errorf("params must be *primitive.Message, got %T", params)
+ }
+
+ bac := tm.GetBusinessActionContext(ctx)
+ if bac == nil {
+ return false, fmt.Errorf("BusinessActionContext not found in context")
+ }
+
+ xid := tm.GetXID(ctx)
+ if xid == "" {
+ return false, fmt.Errorf("XID not found in context")
+ }
+
+ msg.WithProperty(constant.PropertySeataXID, xid)
+ msg.WithProperty(constant.PropertySeataBranchId, fmt.Sprintf("%d", bac.BranchId))
+
+ result, err := a.producer.transactionProducer.SendMessageInTransaction(ctx, msg)
+ if err != nil {
+ log.Errorf("[TCCRocketMQ] Prepare failed, xid=%s, err=%v", xid, err)
+ return false, err
+ }
+
+ bac.ActionContext[ActionContextKeyMsgId] = result.MsgID
+ bac.ActionContext[ActionContextKeyOffsetMsgId] = result.OffsetMsgID
+ bac.ActionContext[ActionContextKeyQueueOffset] = result.QueueOffset
+ bac.ActionContext[ActionContextKeyTransactionId] = result.TransactionID
+ if result.MessageQueue != nil {
+ bac.ActionContext[ActionContextKeyQueueId] = result.MessageQueue.QueueId
+ bac.ActionContext[ActionContextKeyBrokerName] = result.MessageQueue.BrokerName
+ }
+
+ log.Infof("[TCCRocketMQ] Prepare success, xid=%s, branchId=%d, msgId=%s", xid, bac.BranchId, result.MsgID)
+
+ return true, nil
+}
+
+func (a *TCCRocketMQAction) Commit(ctx context.Context, bac *tm.BusinessActionContext) (bool, error) {
Review Comment:
当前采用的纯回查模式,TCC的Commit/Rollback不做操作,是因为实际的消息状态决策已经委托给RocketMQ的CheckLocalTransaction回调,该回调通过查询Seata TC获取权威的全局事务状态。(依赖Broker回查间隔,适合mvp验证)
而预计下一个pr会用主动通知模式改进这个问题,这时候将会和java那边达成一致
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org