Skip to content

handle Unexpected transaction rollback #828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/datasource/sql/undo/base/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"

"strconv"
"strings"

Expand All @@ -36,6 +35,7 @@ import (
"seata.apache.org/seata-go/pkg/datasource/sql/undo/factor"
"seata.apache.org/seata-go/pkg/datasource/sql/undo/parser"
"seata.apache.org/seata-go/pkg/util/collection"
serr "seata.apache.org/seata-go/pkg/util/errors"
"seata.apache.org/seata-go/pkg/util/log"
)

Expand Down Expand Up @@ -354,6 +354,10 @@ func (m *BaseUndoLogManager) Undo(ctx context.Context, dbType types.DBType, xid

if err = undoExecutor.ExecuteOn(ctx, dbType, conn); err != nil {
log.Errorf("execute on fail, err: %v", err)
if undoErr, ok := err.(*serr.SeataError); ok && undoErr.Code == serr.SQLUndoDirtyError {
return serr.New(serr.TransactionErrorCodeBranchRollbackFailedUnretriable, fmt.Sprintf(
"Branch session rollback failed because of dirty undo log, please delete the relevant undolog after manually calibrating the data. xid = %s branchId = %d: %v", xid, branchID, undoErr), nil)
}
return err
}
}
Expand All @@ -375,6 +379,7 @@ func (m *BaseUndoLogManager) Undo(ctx context.Context, dbType types.DBType, xid

if err = tx.Commit(); err != nil {
log.Errorf("[Undo] execute on fail, err: %v", err)

return nil
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/datasource/sql/undo/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"seata.apache.org/seata-go/pkg/datasource/sql/datasource"
"seata.apache.org/seata-go/pkg/datasource/sql/types"
"seata.apache.org/seata-go/pkg/datasource/sql/undo"
serr "seata.apache.org/seata-go/pkg/util/errors"
"seata.apache.org/seata-go/pkg/util/log"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func (b *BaseExecutor) dataValidationAndGoOn(ctx context.Context, conn *sql.Conn
newRowJson, _ := json.Marshal(currentImage.Rows)
log.Infof("check dirty data failed, old and new data are not equal, "+
"tableName:[%s], oldRows:[%s],newRows:[%s].", afterImage.TableName, oldRowJson, newRowJson)
return false, fmt.Errorf("Has dirty records when undo.")
return false, serr.New(serr.SQLUndoDirtyError, "has dirty records when undo", nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a unit test here, thank you.

}
}
return true, nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/errors/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ const (

// FencePhaseError have fence phase but is not illegal value
FencePhaseError

SQLUndoDirtyError
)
1 change: 1 addition & 0 deletions pkg/util/errors/error_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ func TestTransactionErrorCode(t *testing.T) {
assert.Equal(t, int(PrepareFenceError), 24)
assert.Equal(t, int(FenceBusinessError), 25)
assert.Equal(t, int(FencePhaseError), 26)
assert.Equal(t, int(SQLUndoDirtyError), 27)
}
Loading