From a93d16ba84e966eb6004ffa6e5cf299defd69417 Mon Sep 17 00:00:00 2001 From: mornyx Date: Wed, 1 Dec 2021 01:13:26 +0800 Subject: [PATCH 1/2] Try fix data race Signed-off-by: mornyx --- executor/executor.go | 10 +++++++++- sessionctx/stmtctx/stmtctx.go | 13 ++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 209262ac0d4db..02912d61ca065 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -983,7 +983,15 @@ func newLockCtx(seVars *variable.SessionVars, lockWaitTime int64) *tikvstore.Loc } if mutation := req.Mutations[0]; mutation != nil { label := resourcegrouptag.GetResourceGroupLabelByKey(mutation.Key) - return seVars.StmtCtx.GetResourceGroupTagByLabel(label) + // return seVars.StmtCtx.GetResourceGroupTagByLabel(label) + // === + normalized, digest := seVars.StmtCtx.SQLDigest() + if len(normalized) == 0 { + return nil + } + _, planDigest := seVars.StmtCtx.GetPlanDigest() + return resourcegrouptag.EncodeResourceGroupTag(digest, planDigest, label) + // === } return nil } diff --git a/sessionctx/stmtctx/stmtctx.go b/sessionctx/stmtctx/stmtctx.go index 6b5d38ed1292d..4cc7d7f7d2d0e 100644 --- a/sessionctx/stmtctx/stmtctx.go +++ b/sessionctx/stmtctx/stmtctx.go @@ -287,12 +287,23 @@ func (sc *StatementContext) GetPlanDigest() (normalized string, planDigest *pars // GetResourceGroupTagger returns the implementation of tikvrpc.ResourceGroupTagger related to self. func (sc *StatementContext) GetResourceGroupTagger() tikvrpc.ResourceGroupTagger { + // === + normalized, digest := sc.SQLDigest() + planDigest := sc.planDigest + // === return func(req *tikvrpc.Request) { if req == nil { return } - req.ResourceGroupTag = sc.GetResourceGroupTagByLabel( + // === + if len(normalized) == 0 { + return + } + req.ResourceGroupTag = resourcegrouptag.EncodeResourceGroupTag(digest, planDigest, resourcegrouptag.GetResourceGroupLabelByKey(resourcegrouptag.GetFirstKeyFromRequest(req))) + // === + // req.ResourceGroupTag = sc.GetResourceGroupTagByLabel( + // resourcegrouptag.GetResourceGroupLabelByKey(resourcegrouptag.GetFirstKeyFromRequest(req))) } } From 0eabcf0d754f4530ce7a1c89a5db3d8e70020419 Mon Sep 17 00:00:00 2001 From: mornyx Date: Wed, 1 Dec 2021 10:41:12 +0800 Subject: [PATCH 2/2] Remove GetResourceGroupTagByLabel Signed-off-by: mornyx --- executor/executor.go | 3 --- sessionctx/stmtctx/stmtctx.go | 19 ------------------- sessionctx/stmtctx/stmtctx_test.go | 23 ----------------------- 3 files changed, 45 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 02912d61ca065..eee07f8774ed0 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -983,15 +983,12 @@ func newLockCtx(seVars *variable.SessionVars, lockWaitTime int64) *tikvstore.Loc } if mutation := req.Mutations[0]; mutation != nil { label := resourcegrouptag.GetResourceGroupLabelByKey(mutation.Key) - // return seVars.StmtCtx.GetResourceGroupTagByLabel(label) - // === normalized, digest := seVars.StmtCtx.SQLDigest() if len(normalized) == 0 { return nil } _, planDigest := seVars.StmtCtx.GetPlanDigest() return resourcegrouptag.EncodeResourceGroupTag(digest, planDigest, label) - // === } return nil } diff --git a/sessionctx/stmtctx/stmtctx.go b/sessionctx/stmtctx/stmtctx.go index 4cc7d7f7d2d0e..30b63add87053 100644 --- a/sessionctx/stmtctx/stmtctx.go +++ b/sessionctx/stmtctx/stmtctx.go @@ -30,7 +30,6 @@ import ( "github.com/pingcap/tidb/util/memory" "github.com/pingcap/tidb/util/resourcegrouptag" "github.com/pingcap/tidb/util/tracing" - "github.com/pingcap/tipb/go-tipb" "github.com/tikv/client-go/v2/tikvrpc" "github.com/tikv/client-go/v2/util" atomic2 "go.uber.org/atomic" @@ -287,38 +286,20 @@ func (sc *StatementContext) GetPlanDigest() (normalized string, planDigest *pars // GetResourceGroupTagger returns the implementation of tikvrpc.ResourceGroupTagger related to self. func (sc *StatementContext) GetResourceGroupTagger() tikvrpc.ResourceGroupTagger { - // === normalized, digest := sc.SQLDigest() planDigest := sc.planDigest - // === return func(req *tikvrpc.Request) { if req == nil { return } - // === if len(normalized) == 0 { return } req.ResourceGroupTag = resourcegrouptag.EncodeResourceGroupTag(digest, planDigest, resourcegrouptag.GetResourceGroupLabelByKey(resourcegrouptag.GetFirstKeyFromRequest(req))) - // === - // req.ResourceGroupTag = sc.GetResourceGroupTagByLabel( - // resourcegrouptag.GetResourceGroupLabelByKey(resourcegrouptag.GetFirstKeyFromRequest(req))) } } -// GetResourceGroupTagByLabel gets the resource group of the statement based on the label. -func (sc *StatementContext) GetResourceGroupTagByLabel(label tipb.ResourceGroupTagLabel) []byte { - if sc == nil { - return nil - } - normalized, sqlDigest := sc.SQLDigest() - if len(normalized) == 0 { - return nil - } - return resourcegrouptag.EncodeResourceGroupTag(sqlDigest, sc.planDigest, label) -} - // SetPlanDigest sets the normalized plan and plan digest. func (sc *StatementContext) SetPlanDigest(normalized string, planDigest *parser.Digest) { if planDigest != nil { diff --git a/sessionctx/stmtctx/stmtctx_test.go b/sessionctx/stmtctx/stmtctx_test.go index acfc9c00866d1..f5bf2cca866be 100644 --- a/sessionctx/stmtctx/stmtctx_test.go +++ b/sessionctx/stmtctx/stmtctx_test.go @@ -21,7 +21,6 @@ import ( "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/util/execdetails" - "github.com/pingcap/tipb/go-tipb" "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/util" ) @@ -91,25 +90,3 @@ func TestStatementContextPushDownFLags(t *testing.T) { require.Equal(t, tt.out, got) } } - -func TestGetResourceGroupTagByLabel(t *testing.T) { - ctx := stmtctx.StatementContext{OriginalSQL: "SELECT * FROM t"} - tagRow := ctx.GetResourceGroupTagByLabel(tipb.ResourceGroupTagLabel_ResourceGroupTagLabelRow) - tagIndex := ctx.GetResourceGroupTagByLabel(tipb.ResourceGroupTagLabel_ResourceGroupTagLabelIndex) - tagUnknown := ctx.GetResourceGroupTagByLabel(tipb.ResourceGroupTagLabel_ResourceGroupTagLabelUnknown) - tagRow2 := ctx.GetResourceGroupTagByLabel(tipb.ResourceGroupTagLabel_ResourceGroupTagLabelRow) - tagIndex2 := ctx.GetResourceGroupTagByLabel(tipb.ResourceGroupTagLabel_ResourceGroupTagLabelIndex) - tagUnknown2 := ctx.GetResourceGroupTagByLabel(tipb.ResourceGroupTagLabel_ResourceGroupTagLabelUnknown) - require.NotEmpty(t, tagRow) - require.NotEmpty(t, tagIndex) - require.NotEmpty(t, tagUnknown) - require.NotEmpty(t, tagRow2) - require.NotEmpty(t, tagIndex2) - require.NotEmpty(t, tagUnknown2) - require.Equal(t, &tagRow, &tagRow2) // mem addr - require.Equal(t, &tagIndex, &tagIndex2) - require.Equal(t, &tagUnknown, &tagUnknown2) - require.NotEqual(t, &tagRow, &tagIndex) - require.NotEqual(t, &tagRow, &tagUnknown) - require.NotEqual(t, &tagIndex, &tagUnknown) -}