Skip to content

Commit

Permalink
fix txn status invalid to 2.0 (#19817)
Browse files Browse the repository at this point in the history
fix txn status invalid if commit failed or commit a readonly txn

Approved by: @iamlinjunhong, @sukki37
  • Loading branch information
zhangxu19830126 authored Nov 6, 2024
1 parent 151aae1 commit 9577256
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/txn/client/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,15 +611,15 @@ func (tc *txnOperator) Commit(ctx context.Context) (err error) {

tc.reset.commitCounter.addEnter()
defer tc.reset.commitCounter.addExit()
txn := tc.getTxnMeta(false)
util.LogTxnCommit(tc.logger, txn)
txnMeta := tc.getTxnMeta(false)
util.LogTxnCommit(tc.logger, txnMeta)

readonly := tc.reset.workspace != nil && tc.reset.workspace.Readonly()
if !readonly {
tc.reset.commitSeq = tc.NextSequence()
tc.reset.commitAt = time.Now()

tc.triggerEvent(newEvent(CommitEvent, txn, tc.reset.commitSeq, nil))
tc.triggerEvent(newEvent(CommitEvent, txnMeta, tc.reset.commitSeq, nil))
defer func() {
cost := time.Since(tc.reset.commitAt)
v2.TxnCNCommitDurationHistogram.Observe(cost.Seconds())
Expand All @@ -630,6 +630,7 @@ func (tc *txnOperator) Commit(ctx context.Context) (err error) {
if tc.opts.options.ReadOnly() {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.mu.txn.Status = txn.TxnStatus_Committed
tc.closeLocked()
return
}
Expand Down Expand Up @@ -1257,6 +1258,9 @@ func (tc *txnOperator) needUnlockLocked() bool {
func (tc *txnOperator) closeLocked() {
if !tc.mu.closed {
tc.mu.closed = true
if tc.reset.commitErr != nil {
tc.mu.txn.Status = txn.TxnStatus_Aborted
}
tc.triggerEventLocked(
TxnEvent{
Event: ClosedEvent,
Expand Down
3 changes: 3 additions & 0 deletions pkg/txn/client/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestCommitWithNoWrite(t *testing.T) {
err := tc.Commit(ctx)
assert.NoError(t, err)
assert.Empty(t, ts.getLastRequests())
assert.Equal(t, txn.TxnStatus_Committed, tc.mu.txn.Status)
})
}

Expand All @@ -137,6 +138,7 @@ func TestCommitReadOnly(t *testing.T) {
err := tc.Commit(ctx)
assert.NoError(t, err)
assert.Empty(t, ts.getLastRequests())
assert.Equal(t, txn.TxnStatus_Committed, tc.mu.txn.Status)
}, WithTxnReadyOnly())
}

Expand Down Expand Up @@ -209,6 +211,7 @@ func TestCommitWithLockTablesChanged(t *testing.T) {
tc.mu.txn.TNShards = append(tc.mu.txn.TNShards, metadata.TNShard{TNShardRecord: metadata.TNShardRecord{ShardID: 1}})
err = tc.Commit(ctx)
assert.Error(t, err)
assert.Equal(t, txn.TxnStatus_Aborted, tc.mu.txn.Status)

// table 1 will be removed
bind, err := s.GetLockTableBind(0, tableID1)
Expand Down

0 comments on commit 9577256

Please sign in to comment.