apache / incubator-seata-go

Go Implementation For Seata
https://seata.apache.org/
Apache License 2.0
1.53k stars 269 forks source link

测试一个长时间运行的sql,timeout时,xa是否正确处理回滚遇到报错 #676

Open smiletrl opened 7 months ago

smiletrl commented 7 months ago

用这个patch模拟耗时sql查询,然后提前timeout的场景。 使用的是samples里的XA basic的代码 test-timeout.patch

diff --git a/pkg/datasource/sql/conn_xa.go b/pkg/datasource/sql/conn_xa.go
index f557c1e..8625f9e 100644
--- a/pkg/datasource/sql/conn_xa.go
+++ b/pkg/datasource/sql/conn_xa.go
@@ -199,6 +199,7 @@ func (c *XAConn) createNewTxOnExecIfNeed(ctx context.Context, f func() (types.Ex

    // execute SQL
    ret, err := f()
+   time.Sleep(10 * time.Second)
    if err != nil {
        // XA End & Rollback
        if rollbackErr := c.Rollback(ctx); rollbackErr != nil {
diff --git a/pkg/tm/transaction_executor.go b/pkg/tm/transaction_executor.go
index 22b662e..4665cf4 100644
--- a/pkg/tm/transaction_executor.go
+++ b/pkg/tm/transaction_executor.go
@@ -180,6 +180,8 @@ func beginNewGtx(ctx context.Context, gc *GtxConfig) error {
        timeout = config.DefaultGlobalTransactionTimeout
    }

+   timeout = 2 * time.Second
+
    SetTxRole(ctx, Launcher)
    SetTxName(ctx, gc.Name)
    SetTxStatus(ctx, message.GlobalStatusBegin)

What happened:

报错, XAER_NOTA: Unknown XID

2024-03-04 19:36:07.434     INFO    getty/getty_remoting.go:92                      send async message: {message.RpcMessage{ID:4, Type:0x0, Codec:0x1, Compressor:0x0, HeadMap:map[string]string(nil), Body:message.BranchRegisterRequest{Xid:"172.24.0.3:8091:27517667274727427", BranchType:3, ResourceId:"root:12345678@tcp(127.0.0.1:3308)/seata_client", LockKey:"", ApplicationData:[]uint8{}}}}
2024-03-04 19:36:07.436     INFO    client/client_on_response_processor.go:48       the rm client received  clientOnResponse msg message.RpcMessage{ID:4, Type:0x1, Codec:0x1, Compressor:0x0, HeadMap:map[string]string{}, Body:message.BranchRegisterResponse{AbstractTransactionResponse:message.AbstractTransactionResponse{AbstractResultMessage:message.AbstractResultMessage{ResultCode:0x1, Msg:""}, TransactionErrorCode:0}, BranchId:27517667274727428}} from tc server.
2024-03-04 19:36:07.436     INFO    xa/mysql_xa_connection.go:184                   xa branch start, xid 172.24.0.3:8091:27517667274727427-27517667274727428
2024-03-04 19:36:11.266     INFO    client/rm_branch_rollback_processor.go:38       the rm client received  rmBranchRollback msg message.RpcMessage{ID:2, Type:0x0, Codec:0x1, Compressor:0x0, HeadMap:map[string]string{}, Body:message.BranchRollbackRequest{AbstractBranchEndRequest:message.AbstractBranchEndRequest{MessageTypeAware:message.MessageTypeAware(nil), Xid:"172.24.0.3:8091:27517667274727427", BranchId:27517667274727428, BranchType:3, ResourceId:"root:12345678@tcp(127.0.0.1:3308)/seata_client", ApplicationData:[]uint8{}}}} from tc server.
2024-03-04 19:36:11.266     INFO    client/rm_branch_rollback_processor.go:44       Branch rollback request: xid 172.24.0.3:8091:27517667274727427, branchID 27517667274727428, resourceID root:12345678@tcp(127.0.0.1:3308)/seata_client, applicationData []
2024-03-04 19:36:11.268     INFO    xa/mysql_xa_connection.go:163                   xa branch rollback, xid 172.24.0.3:8091:27517667274727427-27517667274727428
2024-03-04 19:36:11.268     ERROR   xa/mysql_xa_connection.go:174                   xa branch rollback failed, xid 172.24.0.3:8091:27517667274727427-27517667274727428, err Error 1397 (XAE04): XAER_NOTA: Unknown XID
github.com/seata/seata-go/pkg/datasource/sql/xa.(*MysqlXAConn).Rollback
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/datasource/sql/xa/mysql_xa_connection.go:174
github.com/seata/seata-go/pkg/datasource/sql.(*XAConn).XaRollback
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/datasource/sql/conn_xa.go:399
github.com/seata/seata-go/pkg/datasource/sql.(*XAConn).XaRollbackByBranchId
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/datasource/sql/conn_xa.go:395
github.com/seata/seata-go/pkg/datasource/sql.(*XAResourceManager).BranchRollback
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/datasource/sql/xa_resource_manager.go:187
github.com/seata/seata-go/pkg/remoting/processor/client.(*rmBranchRollbackProcessor).Process
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/remoting/processor/client/rm_branch_rollback_processor.go:52
github.com/seata/seata-go/pkg/remoting/getty.(*gettyClientHandler).OnMessage
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/remoting/getty/listener.go:107
github.com/apache/dubbo-getty.(*session).addTask.func1
    /Users/smiletrl/go/src/pkg/mod/github.com/apache/dubbo-getty@v1.4.10-0.20230731065302-7c0f0039e59c/session.go:565
github.com/dubbogo/gost/sync.(*taskPoolSimple).worker
    /Users/smiletrl/go/src/pkg/mod/github.com/dubbogo/gost@v1.13.2/sync/task_pool.go:305
2024-03-04 19:36:11.268     ERROR   sql/xa_resource_manager.go:188                  rollback xa, resourceId: root:12345678@tcp(127.0.0.1:3308)/seata_client, err Error 1397 (XAE04): XAER_NOTA: Unknown XID
github.com/seata/seata-go/pkg/datasource/sql.(*XAResourceManager).BranchRollback
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/datasource/sql/xa_resource_manager.go:188
github.com/seata/seata-go/pkg/remoting/processor/client.(*rmBranchRollbackProcessor).Process
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/remoting/processor/client/rm_branch_rollback_processor.go:52
github.com/seata/seata-go/pkg/remoting/getty.(*gettyClientHandler).OnMessage
    /Users/smiletrl/go/src/jihulab.com/yanlin/incubator-seata-go/pkg/remoting/getty/listener.go:107
github.com/apache/dubbo-getty.(*session).addTask.func1
    /Users/smiletrl/go/src/pkg/mod/github.com/apache/dubbo-getty@v1.4.10-0.20230731065302-7c0f0039e59c/session.go:565
github.com/dubbogo/gost/sync.(*taskPoolSimple).worker
    /Users/smiletrl/go/src/pkg/mod/github.com/dubbogo/gost@v1.13.2/sync/task_pool.go:305

What you expected to happen: 应该可以正确处理 xa的回滚

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?: