Skip to content

Commit

Permalink
session: fix update process info will data race with expensive-query …
Browse files Browse the repository at this point in the history
…check (#50696)

close #50607
  • Loading branch information
AilinKid authored Jan 25, 2024
1 parent e42d0bf commit cf320b2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1558,9 +1558,12 @@ func (s *session) UpdateProcessInfo() {
if pi == nil || pi.CurTxnStartTS != 0 {
return
}
// do not modify this two fields in place, see issue: issues/50607
shallowCP := pi.Clone()
// Update the current transaction start timestamp.
pi.CurTxnStartTS = s.sessionVars.TxnCtx.StartTS
pi.CurTxnCreateTime = s.sessionVars.TxnCtx.CreateTime
shallowCP.CurTxnStartTS = s.sessionVars.TxnCtx.StartTS
shallowCP.CurTxnCreateTime = s.sessionVars.TxnCtx.CreateTime
s.processInfo.Store(shallowCP)
}

func (s *session) getOomAlarmVariablesInfo() util.OOMAlarmVariablesInfo {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ go_test(
"main_test.go",
"misc_test.go",
"prefix_helper_test.go",
"processinfo_test.go",
"security_test.go",
"urls_test.go",
"util_test.go",
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/processinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ type ProcessInfo struct {
RedactSQL bool
}

// Clone return a shallow clone copy of this processInfo.
func (pi *ProcessInfo) Clone() *ProcessInfo {
cp := *pi
return &cp
}

// ToRowForShow returns []interface{} for the row data of "SHOW [FULL] PROCESSLIST".
func (pi *ProcessInfo) ToRowForShow(full bool) []interface{} {
var info interface{}
Expand Down
67 changes: 67 additions & 0 deletions pkg/util/processinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed 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 util

import (
"fmt"
"reflect"
"testing"

"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
"github.com/pingcap/tidb/pkg/util/memory"
"github.com/stretchr/testify/require"
)

func MyFunc(interface{}) map[string]uint64 {
return nil
}

func TestProcessInfoShallowCP(t *testing.T) {
mem := memory.NewTracker(-1, -1)
mem.Consume(1<<30 + 1<<29 + 1<<28 + 1<<27)

var refCount stmtctx.ReferenceCount = 0
info := &ProcessInfo{
ID: 233,
User: "PingCAP",
Host: "127.0.0.1",
DB: "Database",
Info: "select * from table where a > 1",
CurTxnStartTS: 23333,
StatsInfo: MyFunc,
StmtCtx: stmtctx.NewStmtCtx(),
RefCountOfStmtCtx: &refCount,
MemTracker: mem,
RedactSQL: false,
SessionAlias: "alias123",
}

cp := info.Clone()
require.False(t, cp == info)
require.True(t, cp.ID == info.ID)
require.True(t, cp.User == info.User)
require.True(t, cp.Host == info.Host)
require.True(t, cp.DB == info.DB)
require.True(t, cp.Info == info.Info)
require.True(t, cp.CurTxnStartTS == info.CurTxnStartTS)
// reflect.DeepEqual couldn't cmp two function pointer.
require.True(t, reflect.ValueOf(cp.StatsInfo).Pointer() == reflect.ValueOf(info.StatsInfo).Pointer())
fmt.Println(reflect.ValueOf(cp.StatsInfo).Pointer(), reflect.ValueOf(info.StatsInfo).Pointer())
require.True(t, cp.StmtCtx == info.StmtCtx)
require.True(t, cp.RefCountOfStmtCtx == info.RefCountOfStmtCtx)
require.True(t, cp.MemTracker == info.MemTracker)
require.True(t, cp.RedactSQL == info.RedactSQL)
require.True(t, cp.SessionAlias == info.SessionAlias)
}

0 comments on commit cf320b2

Please sign in to comment.