Skip to content

Commit

Permalink
txn: Fix the issue that analyze panics when aggressive locking is ena…
Browse files Browse the repository at this point in the history
…bled globally (pingcap#41196)

close pingcap#41194
  • Loading branch information
MyonKeminta authored and blacktear23 committed Feb 15, 2023
1 parent 6551a16 commit b811752
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/bindinfo"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
Expand Down Expand Up @@ -2490,6 +2491,13 @@ func doDMLWorks(s Session) {
case variable.TiDBEnableMutationChecker:
vVal = variable.On
}

failpoint.Inject("enableAggressiveLockingOnBootstrap", func() {
if v.Name == variable.TiDBPessimisticTransactionAggressiveLocking {
vVal = variable.On
}
})

// sanitize k and vVal
value := fmt.Sprintf(`("%s", "%s")`, sqlexec.EscapeString(k), sqlexec.EscapeString(vVal))
values = append(values, value)
Expand Down
5 changes: 4 additions & 1 deletion sessiontxn/isolation/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,10 @@ func (p *basePessimisticTxnContextProvider) OnHandlePessimisticStmtStart(ctx con
if err := p.baseTxnContextProvider.OnHandlePessimisticStmtStart(ctx); err != nil {
return err
}
if p.sctx.GetSessionVars().PessimisticTransactionAggressiveLocking && p.txn != nil {
if p.sctx.GetSessionVars().PessimisticTransactionAggressiveLocking &&
p.txn != nil &&
p.sctx.GetSessionVars().ConnectionID != 0 &&
!p.sctx.GetSessionVars().InRestrictedSQL {
if err := p.txn.StartAggressiveLocking(); err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions sessiontxn/isolation/repeatable_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,3 +700,18 @@ func TestRRWaitTSTimeInSlowLog(t *testing.T) {
require.NotEqual(t, waitTS1, waitTS3)
require.NotEqual(t, waitTS2, waitTS3)
}

func TestIssue41194(t *testing.T) {
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/session/enableAggressiveLockingOnBootstrap", "return"))
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/session/enableAggressiveLockingOnBootstrap"))
}()

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("create table t (id int primary key, v int)")
tk.MustExec("insert into t values (1, 1), (2, 2), (3, 3)")
tk.MustExec("analyze table t")
}
4 changes: 4 additions & 0 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,10 @@ func SaveTableStatsToStorage(sctx sessionctx.Context, results *statistics.Analyz
if err != nil {
return err
}
err = rs.Close()
if err != nil {
return err
}
var curCnt, curModifyCnt int64
if len(rows) > 0 {
snapshot := rows[0].GetUint64(0)
Expand Down

0 comments on commit b811752

Please sign in to comment.