Skip to content

Commit

Permalink
Memdb tracker (#3)
Browse files Browse the repository at this point in the history
* add a memdb memory tracker

Signed-off-by: ekexium <[email protected]>

* update deps

* fix test

Signed-off-by: ekexium <[email protected]>

* remove the fake tracking of memdb in insert

Signed-off-by: ekexium <[email protected]>

* simplify the signature

Signed-off-by: ekexium <[email protected]>

* fix conflict

Signed-off-by: ekexium <[email protected]>

* fix conflict

Signed-off-by: ekexium <[email protected]>

* fix: move the tracker out of TxnInfo

Signed-off-by: ekexium <[email protected]>

* refactor: use switch-default

Signed-off-by: ekexium <[email protected]>

* fix: only reset session tracker when detach SQLText tracker

Signed-off-by: ekexium <[email protected]>

* test: fix TestTiDBTrx

Signed-off-by: ekexium <[email protected]>

* update client-go

Signed-off-by: ekexium <[email protected]>

Signed-off-by: ekexium <[email protected]>
Signed-off-by: ekexium <[email protected]>
Co-authored-by: ekexium <[email protected]>
Co-authored-by: Weizhen Wang <[email protected]>
Co-authored-by: Ti Chi Robot <[email protected]>
  • Loading branch information
4 people authored Nov 24, 2022
1 parent d100e93 commit bfc880b
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 54 deletions.
8 changes: 4 additions & 4 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2915,8 +2915,8 @@ def go_deps():
name = "com_github_pingcap_kvproto",
build_file_proto_mode = "disable_global",
importpath = "github.com/pingcap/kvproto",
sum = "h1:HyWSOT/drBEtfXK2HLkWWR8dCO+rcf7OiRDRhBxAfU4=",
version = "v0.0.0-20221114102356-3debb6820e46",
sum = "h1:Ywk7n+4zm6W6T9XSyAwihBWdxXR2ALQzswQMEOglHkM=",
version = "v0.0.0-20221117075110-51120697d051",
)
go_repository(
name = "com_github_pingcap_log",
Expand Down Expand Up @@ -3519,8 +3519,8 @@ def go_deps():
name = "com_github_tikv_client_go_v2",
build_file_proto_mode = "disable_global",
importpath = "github.com/tikv/client-go/v2",
sum = "h1:nFVdyTXcQYZwQQCdSJcFI1vBFyzG1hVuZ39MAK6wqK4=",
version = "v2.0.3-0.20221108030801-9c0835c80eba",
sum = "h1:G44ccTqXvE3uZgA+8Y71RQmw/1gsst+wXtn2+qw5ykI=",
version = "v2.0.3-0.20221124031013-92f0a82e1a9f",
)
go_repository(
name = "com_github_tikv_pd_client",
Expand Down
4 changes: 4 additions & 0 deletions br/pkg/streamhelper/basic_lib_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (f *fakeStore) GetLastFlushTSOfRegion(ctx context.Context, in *logbackup.Ge
return resp, nil
}

func (f *fakeStore) SubscribeFlushEvent(ctx context.Context, in *logbackup.SubscribeFlushEventRequest, opts ...grpc.CallOption) (logbackup.LogBackup_SubscribeFlushEventClient, error) {
panic("unimplemented")
}

// RegionScan gets a list of regions, starts from the region that contains key.
// Limit limits the maximum number of regions returned.
func (f *fakeCluster) RegionScan(ctx context.Context, key []byte, endKey []byte, limit int) ([]streamhelper.RegionWithLeader, error) {
Expand Down
8 changes: 1 addition & 7 deletions executor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,14 @@ func (e *DeleteExec) removeRowsInTblRowMap(tblRowMap tableRowMapType) error {
}

func (e *DeleteExec) removeRow(ctx sessionctx.Context, t table.Table, h kv.Handle, data []types.Datum) error {
txnState, err := e.ctx.Txn(false)
if err != nil {
return err
}
memUsageOfTxnState := txnState.Size()
err = t.RemoveRecord(ctx, h, data)
err := t.RemoveRecord(ctx, h, data)
if err != nil {
return err
}
err = e.onRemoveRowForFK(ctx, t, data)
if err != nil {
return err
}
e.memTracker.Consume(int64(txnState.Size() - memUsageOfTxnState))
ctx.GetSessionVars().StmtCtx.AddAffectedRows(1)
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6237,8 +6237,7 @@ func TestSessionRootTrackerDetach(t *testing.T) {
tk.MustExec("create table t(a int, b int, index idx(a))")
tk.MustExec("create table t1(a int, c int, index idx(a))")
tk.MustExec("set tidb_mem_quota_query=10")
err := tk.ExecToErr("select /*+hash_join(t1)*/ t.a, t1.a from t use index(idx), t1 use index(idx) where t.a = t1.a")
require.Contains(t, err.Error(), "Out Of Memory Quota!")
tk.MustContainErrMsg("select /*+hash_join(t1)*/ t.a, t1.a from t use index(idx), t1 use index(idx) where t.a = t1.a", "Out Of Memory Quota!")
tk.MustExec("set tidb_mem_quota_query=1000")
rs, err := tk.Exec("select /*+hash_join(t1)*/ t.a, t1.a from t use index(idx), t1 use index(idx) where t.a = t1.a")
require.NoError(t, err)
Expand Down
12 changes: 11 additions & 1 deletion executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2607,7 +2607,17 @@ func (e *tidbTrxTableRetriever) retrieve(ctx context.Context, sctx sessionctx.Co
row = append(row, types.NewDatum(nil))
}
} else {
row = append(row, e.txnInfo[i].ToDatum(c.Name.O))
switch c.Name.O {
case txninfo.MemBufferBytesStr:
memDBFootprint := sctx.GetSessionVars().MemDBFootprint
var bytesConsumed int64
if memDBFootprint != nil {
bytesConsumed = memDBFootprint.BytesConsumed()
}
row = append(row, types.NewDatum(bytesConsumed))
default:
row = append(row, e.txnInfo[i].ToDatum(c.Name.O))
}
}
}
res = append(res, row)
Expand Down
2 changes: 0 additions & 2 deletions executor/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func (e *InsertExec) exec(ctx context.Context, rows [][]types.Datum) error {
return err
}
setOptionForTopSQL(sessVars.StmtCtx, txn)
txnSize := txn.Size()
sessVars.StmtCtx.AddRecordRows(uint64(len(rows)))
// If you use the IGNORE keyword, duplicate-key error that occurs while executing the INSERT statement are ignored.
// For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in
Expand Down Expand Up @@ -113,7 +112,6 @@ func (e *InsertExec) exec(ctx context.Context, rows [][]types.Datum) error {
e.stats.CheckInsertTime += time.Since(start)
}
}
e.memTracker.Consume(int64(txn.Size() - txnSize))
return nil
}

Expand Down
6 changes: 2 additions & 4 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2307,16 +2307,14 @@ func TestIssue18070(t *testing.T) {
tk.MustExec("insert into t1 values(1),(2)")
tk.MustExec("insert into t2 values(1),(1),(2),(2)")
tk.MustExec("set @@tidb_mem_quota_query=1000")
err := tk.QueryToErr("select /*+ inl_hash_join(t1)*/ * from t1 join t2 on t1.a = t2.a;")
require.True(t, strings.Contains(err.Error(), "Out Of Memory Quota!"))
tk.MustContainErrMsg("select /*+ inl_hash_join(t1)*/ * from t1 join t2 on t1.a = t2.a;", "Out Of Memory Quota!")

fpName := "github.com/pingcap/tidb/executor/mockIndexMergeJoinOOMPanic"
require.NoError(t, failpoint.Enable(fpName, `panic("ERROR 1105 (HY000): Out Of Memory Quota![conn_id=1]")`))
defer func() {
require.NoError(t, failpoint.Disable(fpName))
}()
err = tk.QueryToErr("select /*+ inl_merge_join(t1)*/ * from t1 join t2 on t1.a = t2.a;")
require.True(t, strings.Contains(err.Error(), "Out Of Memory Quota!"))
tk.MustContainErrMsg("select /*+ inl_merge_join(t1)*/ * from t1 join t2 on t1.a = t2.a;", "Out Of Memory Quota!")
}

func TestIssue18564(t *testing.T) {
Expand Down
8 changes: 1 addition & 7 deletions executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old
defer span1.Finish()
ctx = opentracing.ContextWithSpan(ctx, span1)
}
txn, err := sctx.Txn(false)
if err != nil {
return false, err
}
memUsageOfTxnState := txn.Size()
defer memTracker.Consume(int64(txn.Size() - memUsageOfTxnState))
sc := sctx.GetSessionVars().StmtCtx
changed, handleChanged := false, false
// onUpdateSpecified is for "UPDATE SET ts_field = old_value", the
Expand Down Expand Up @@ -207,7 +201,7 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old
}
} else {
// Update record to new value and update index.
if err = t.UpdateRecord(ctx, sctx, h, oldData, newData, modified); err != nil {
if err := t.UpdateRecord(ctx, sctx, h, oldData, newData, modified); err != nil {
if terr, ok := errors.Cause(err).(*terror.Error); sctx.GetSessionVars().StmtCtx.IgnoreNoPartition && ok && terr.Code() == errno.ErrNoPartitionForGivenValue {
return false, nil
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ require (
github.com/pingcap/errors v0.11.5-0.20220729040631-518f63d66278
github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3
github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059
github.com/pingcap/kvproto v0.0.0-20221114102356-3debb6820e46
github.com/pingcap/kvproto v0.0.0-20221117075110-51120697d051
github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c
github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4
github.com/pingcap/tidb/parser v0.0.0-20211011031125-9b13dc409c5e
Expand All @@ -86,7 +86,7 @@ require (
github.com/stretchr/testify v1.8.0
github.com/tdakkota/asciicheck v0.1.1
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/tikv/client-go/v2 v2.0.3-0.20221108030801-9c0835c80eba
github.com/tikv/client-go/v2 v2.0.3-0.20221124031013-92f0a82e1a9f
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
github.com/twmb/murmur3 v1.1.3
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,8 @@ github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw=
github.com/pingcap/kvproto v0.0.0-20191211054548-3c6b38ea5107/go.mod h1:WWLmULLO7l8IOcQG+t+ItJ3fEcrL5FxF0Wu+HrMy26w=
github.com/pingcap/kvproto v0.0.0-20221026112947-f8d61344b172/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/kvproto v0.0.0-20221114102356-3debb6820e46 h1:HyWSOT/drBEtfXK2HLkWWR8dCO+rcf7OiRDRhBxAfU4=
github.com/pingcap/kvproto v0.0.0-20221114102356-3debb6820e46/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/kvproto v0.0.0-20221117075110-51120697d051 h1:Ywk7n+4zm6W6T9XSyAwihBWdxXR2ALQzswQMEOglHkM=
github.com/pingcap/kvproto v0.0.0-20221117075110-51120697d051/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20200511115504-543df19646ad/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM=
Expand Down Expand Up @@ -928,8 +928,8 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpR
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJfDRtkanvQPiooDH8HvJ2FBh+iKT/OmiQQ=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU=
github.com/tikv/client-go/v2 v2.0.3-0.20221108030801-9c0835c80eba h1:nFVdyTXcQYZwQQCdSJcFI1vBFyzG1hVuZ39MAK6wqK4=
github.com/tikv/client-go/v2 v2.0.3-0.20221108030801-9c0835c80eba/go.mod h1:X9s4ct/MLk1sFqe5mU79KClKegLFDTa/FCx3hzexGtk=
github.com/tikv/client-go/v2 v2.0.3-0.20221124031013-92f0a82e1a9f h1:G44ccTqXvE3uZgA+8Y71RQmw/1gsst+wXtn2+qw5ykI=
github.com/tikv/client-go/v2 v2.0.3-0.20221124031013-92f0a82e1a9f/go.mod h1:mQQhAIZ2uJwWXOG2UEz9s9oLGRcNKGGGtDOk4b13Bos=
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07 h1:ckPpxKcl75mO2N6a4cJXiZH43hvcHPpqc9dh1TmH1nc=
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07/go.mod h1:CipBxPfxPUME+BImx9MUYXCnAVLS3VJUr3mnSJwh40A=
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro=
Expand Down
11 changes: 7 additions & 4 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,12 +588,12 @@ INSERT INTO ...;
defer func() { require.NoError(t, os.Remove(slowLogFileName)) }()
tk := testkit.NewTestKit(t, store)

//check schema
// check schema
tk.MustQuery(`select COUNT(*) from information_schema.columns
WHERE table_name = 'slow_query' and column_name = '` + columnName + `'`).
Check(testkit.Rows("1"))

//check select
// check select
tk.MustQuery(`select ` + columnName +
` from information_schema.slow_query`).Check(testkit.Rows("1"))
}
Expand Down Expand Up @@ -1393,16 +1393,19 @@ func TestTiDBTrx(t *testing.T) {
tk.MustExec("update test_tidb_trx set i = i + 1")
_, digest := parser.NormalizeDigest("update test_tidb_trx set i = i + 1")
sm := &testkit.MockSessionManager{TxnInfo: make([]*txninfo.TxnInfo, 2)}
memDBTracker := memory.NewTracker(memory.LabelForMemDB, -1)
memDBTracker.Consume(19)
tk.Session().GetSessionVars().MemDBFootprint = memDBTracker
sm.TxnInfo[0] = &txninfo.TxnInfo{
StartTS: 424768545227014155,
CurrentSQLDigest: digest.String(),
State: txninfo.TxnIdle,
EntriesCount: 1,
EntriesSize: 19,
ConnectionID: 2,
Username: "root",
CurrentDB: "test",
}

blockTime2 := time.Date(2021, 05, 20, 13, 18, 30, 123456000, time.Local)
sm.TxnInfo[1] = &txninfo.TxnInfo{
StartTS: 425070846483628033,
Expand All @@ -1419,7 +1422,7 @@ func TestTiDBTrx(t *testing.T) {

tk.MustQuery("select * from information_schema.TIDB_TRX;").Check(testkit.Rows(
"424768545227014155 2021-05-07 12:56:48.001000 "+digest.String()+" update `test_tidb_trx` set `i` = `i` + ? Idle <nil> 1 19 2 root test [] ",
"425070846483628033 2021-05-20 21:16:35.778000 <nil> <nil> LockWaiting 2021-05-20 13:18:30.123456 0 0 10 user1 db1 [\"sql1\",\"sql2\",\""+digest.String()+"\"] "))
"425070846483628033 2021-05-20 21:16:35.778000 <nil> <nil> LockWaiting 2021-05-20 13:18:30.123456 0 19 10 user1 db1 [\"sql1\",\"sql2\",\""+digest.String()+"\"] "))

// Test the all_sql_digests column can be directly passed to the tidb_decode_sql_digests function.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/expression/sqlDigestRetrieverSkipRetrieveGlobal", "return"))
Expand Down
8 changes: 8 additions & 0 deletions kv/interface_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ func (t *mockTxn) UpdateMemBufferFlags(_ []byte, _ ...FlagsOp) {

}

func (t *mockTxn) SetMemoryFootprintChangeHook(func(uint64)) {

}

func (t *mockTxn) Mem() uint64 {
return 0
}

// newMockTxn new a mockTxn.
func newMockTxn() Transaction {
return &mockTxn{
Expand Down
4 changes: 4 additions & 0 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ type Transaction interface {
AssertionProto
// Size returns sum of keys and values length.
Size() int
// Mem returns the memory consumption of the transaction.
Mem() uint64
// SetMemoryFootprintChangeHook sets the hook that will be called when the memory footprint changes.
SetMemoryFootprintChangeHook(func(uint64))
// Len returns the number of entries in the DB.
Len() int
// Reset reset the Transaction to initial states.
Expand Down
17 changes: 17 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import (
"github.com/pingcap/tidb/util/kvcache"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/logutil/consistency"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/sem"
"github.com/pingcap/tidb/util/sli"
"github.com/pingcap/tidb/util/sqlexec"
Expand Down Expand Up @@ -569,6 +570,7 @@ func (s *session) FieldList(tableName string) ([]*ast.ResultField, error) {
return fields, nil
}

// TxnInfo returns a pointer to a *copy* of the internal TxnInfo, thus is *read only*
func (s *session) TxnInfo() *txninfo.TxnInfo {
s.txn.mu.RLock()
// Copy on read to get a snapshot, this API shouldn't be frequently called.
Expand Down Expand Up @@ -2506,6 +2508,7 @@ func (s *session) Txn(active bool) (kv.Transaction, error) {
return &s.txn, nil
}
_, err := sessiontxn.GetTxnManager(s).ActivateTxn()
s.SetMemoryFootprintChangeHook()
return &s.txn, err
}

Expand Down Expand Up @@ -3659,6 +3662,20 @@ func (s *session) GetStmtStats() *stmtstats.StatementStats {
return s.stmtStats
}

// SetMemoryFootprintChangeHook sets the hook that is called when the memdb changes its size.
// Call this after s.txn becomes valid, since TxnInfo is initialized when the txn becomes valid.
func (s *session) SetMemoryFootprintChangeHook() {
hook := func(mem uint64) {
if s.sessionVars.MemDBFootprint == nil {
tracker := memory.NewTracker(memory.LabelForMemDB, -1)
tracker.AttachTo(s.sessionVars.MemTracker)
s.sessionVars.MemDBFootprint = tracker
}
s.sessionVars.MemDBFootprint.ReplaceBytesUsed(int64(mem))
}
s.txn.SetMemoryFootprintChangeHook(hook)
}

// EncodeSessionStates implements SessionStatesHandler.EncodeSessionStates interface.
func (s *session) EncodeSessionStates(ctx context.Context, sctx sessionctx.Context, sessionStates *sessionstates.SessionStates) error {
// Transaction status is hard to encode, so we do not support it.
Expand Down
24 changes: 18 additions & 6 deletions session/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,14 @@ func (txn *LazyTxn) cleanupStmtBuf() {
txn.mu.Lock()
defer txn.mu.Unlock()
txn.mu.TxnInfo.EntriesCount = uint64(txn.Transaction.Len())
txn.mu.TxnInfo.EntriesSize = uint64(txn.Transaction.Size())
}

// resetTxnInfo resets the transaction info.
// Note: call it under lock!
func (txn *LazyTxn) resetTxnInfo(
startTS uint64,
state txninfo.TxnRunningState,
entriesCount,
entriesSize uint64,
entriesCount uint64,
currentSQLDigest string,
allSQLDigests []string,
) {
Expand All @@ -178,7 +176,7 @@ func (txn *LazyTxn) resetTxnInfo(
txninfo.TxnStatusEnteringCounter(state).Inc()
txn.mu.TxnInfo.LastStateChangeTime = time.Now()
txn.mu.TxnInfo.EntriesCount = entriesCount
txn.mu.TxnInfo.EntriesSize = entriesSize

txn.mu.TxnInfo.CurrentSQLDigest = currentSQLDigest
txn.mu.TxnInfo.AllSQLDigests = allSQLDigests
}
Expand All @@ -191,6 +189,22 @@ func (txn *LazyTxn) Size() int {
return txn.Transaction.Size()
}

// Mem implements the MemBuffer interface.
func (txn *LazyTxn) Mem() uint64 {
if txn.Transaction == nil {
return 0
}
return txn.Transaction.Mem()
}

// SetMemoryFootprintChangeHook sets the hook to be called when the memory footprint of this transaction changes.
func (txn *LazyTxn) SetMemoryFootprintChangeHook(hook func(uint64)) {
if txn.Transaction == nil {
return
}
txn.Transaction.SetMemoryFootprintChangeHook(hook)
}

// Valid implements the kv.Transaction interface.
func (txn *LazyTxn) Valid() bool {
return txn.Transaction != nil && txn.Transaction.Valid()
Expand Down Expand Up @@ -275,7 +289,6 @@ func (txn *LazyTxn) changePendingToValid(ctx context.Context) error {
t.StartTS(),
txninfo.TxnIdle,
uint64(txn.Transaction.Len()),
uint64(txn.Transaction.Size()),
txn.mu.TxnInfo.CurrentSQLDigest,
txn.mu.TxnInfo.AllSQLDigests)

Expand Down Expand Up @@ -433,7 +446,6 @@ func (txn *LazyTxn) LockKeys(ctx context.Context, lockCtx *kv.LockCtx, keys ...k
txn.updateState(originState)
txn.mu.TxnInfo.BlockStartTime.Valid = false
txn.mu.TxnInfo.EntriesCount = uint64(txn.Transaction.Len())
txn.mu.TxnInfo.EntriesSize = uint64(txn.Transaction.Size())
return err
}

Expand Down
5 changes: 0 additions & 5 deletions session/txninfo/txn_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ type TxnInfo struct {
}
// How many entries are in MemDB
EntriesCount uint64
// MemDB used memory
EntriesSize uint64

// The following fields will be filled in `session` instead of `LazyTxn`

Expand Down Expand Up @@ -208,9 +206,6 @@ var columnValueGetterMap = map[string]func(*TxnInfo) types.Datum{
MemBufferKeysStr: func(info *TxnInfo) types.Datum {
return types.NewDatum(info.EntriesCount)
},
MemBufferBytesStr: func(info *TxnInfo) types.Datum {
return types.NewDatum(info.EntriesSize)
},
SessionIDStr: func(info *TxnInfo) types.Datum {
return types.NewDatum(info.ConnectionID)
},
Expand Down
6 changes: 4 additions & 2 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1290,8 +1290,10 @@ type SessionVars struct {
HookContext

// MemTracker indicates the memory tracker of current session.
MemTracker *memory.Tracker
DiskTracker *memory.Tracker
MemTracker *memory.Tracker
// MemDBDBFootprint tracks the memory footprint of memdb, and is attached to `MemTracker`
MemDBFootprint *memory.Tracker
DiskTracker *memory.Tracker

// OptPrefixIndexSingleScan indicates whether to do some optimizations to avoid double scan for prefix index.
// When set to true, `col is (not) null`(`col` is index prefix column) is regarded as index filter rather than table filter.
Expand Down
Loading

0 comments on commit bfc880b

Please sign in to comment.