Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: pass sql, plan digest down to KV request #24854

Merged
merged 22 commits into from
May 25, 2021

Conversation

crazycs520
Copy link
Contributor

@crazycs520 crazycs520 commented May 24, 2021

Track issue: #24875

Related PR: #24380

What problem does this PR solve?

This PR use to implement the part of TopSQL feature.

For TiKV to collection resource by SQL statements, TiDB need pass the sql digest and plan digest down to TiKV.

What is changed and how it works?

  1. Encode the sql digest and plan digest to []byte and the set on kv.Context.ResourceGroupTag.

Related PR

Check List

Tests

  • Unit test

Side effects

  • Performance regression
    • Consumes more CPU
    • Consumes more MEM
  • Breaking backward compatibility

Release note

  • No release note

Signed-off-by: crazycs <crazycs520@gmail.com>
Signed-off-by: crazycs <crazycs520@gmail.com>
Signed-off-by: crazycs <crazycs520@gmail.com>
Signed-off-by: crazycs <crazycs520@gmail.com>
Signed-off-by: crazycs <crazycs520@gmail.com>
Signed-off-by: crazycs <crazycs520@gmail.com>
Signed-off-by: crazycs <crazycs520@gmail.com>
Signed-off-by: crazycs <crazycs520@gmail.com>
@crazycs520 crazycs520 requested review from a team as code owners May 24, 2021 03:23
@crazycs520 crazycs520 requested review from lzmhhh123 and removed request for a team May 24, 2021 03:23
@ti-chi-bot ti-chi-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label May 24, 2021
@github-actions github-actions bot added component/config sig/execution SIG execution sig/planner SIG: Planner sig/sql-infra SIG: SQL Infra labels May 24, 2021
Copy link
Contributor

@MyonKeminta MyonKeminta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it doesn't look so good that the pessimistic lock requests use a different way to set the resource group tag. I think there should be a better way, but it's ok for now since we don't have too much time.

config/config.go Outdated Show resolved Hide resolved
Signed-off-by: crazycs <crazycs520@gmail.com>
@ti-chi-bot ti-chi-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels May 24, 2021
@lysu lysu self-requested a review May 24, 2021 07:46
@@ -919,11 +921,11 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
statsInfos := plannercore.GetStatsInfo(a.Plan)
memMax := sessVars.StmtCtx.MemTracker.MaxConsumed()
diskMax := sessVars.StmtCtx.DiskTracker.MaxConsumed()
_, planDigest := getPlanDigest(a.Ctx, a.Plan)
planDigest := getPlanDigest(a.Ctx, a.Plan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated with line 336?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 maybe make getPlanDigest be memoized, just calculate plan once in 336 and directly reuse result in here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 336 uses to initialize the plan digest, and it is already cache in StatementContext

executor/update.go Outdated Show resolved Hide resolved
util/resourcegrouptag/resource_group_tag_test.go Outdated Show resolved Hide resolved
@@ -919,11 +921,11 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
statsInfos := plannercore.GetStatsInfo(a.Plan)
memMax := sessVars.StmtCtx.MemTracker.MaxConsumed()
diskMax := sessVars.StmtCtx.DiskTracker.MaxConsumed()
_, planDigest := getPlanDigest(a.Ctx, a.Plan)
planDigest := getPlanDigest(a.Ctx, a.Plan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 maybe make getPlanDigest be memoized, just calculate plan once in 336 and directly reuse result in here

sessionctx/stmtctx/stmtctx.go Outdated Show resolved Hide resolved
@lysu
Copy link
Contributor

lysu commented May 24, 2021

func (txn *tikvTxn) BatchGet(ctx context.Context, keys []kv.Key) (map[string][]byte, error) {

will be used in insert/replace code path (prefetchUniqueIndices..) , maybe also need setResourceGroupTagForTxn(stmtCtx, snapshot), because it's new snapshot

Signed-off-by: crazycs <crazycs520@gmail.com>
@crazycs520
Copy link
Contributor Author

func (txn *tikvTxn) BatchGet(ctx context.Context, keys []kv.Key) (map[string][]byte, error) {

will be used in insert/replace code path (prefetchUniqueIndices..) , maybe also need setResourceGroupTagForTxn(stmtCtx, snapshot), because it's new snapshot

@lysu , Already call setResourceGroupTagForTxn for replace SQL in replace.go#227

Signed-off-by: crazycs <crazycs520@gmail.com>
@lysu
Copy link
Contributor

lysu commented May 25, 2021

/lgtm

@tiancaiamao
Copy link
Contributor

/LGTM

@ti-chi-bot ti-chi-bot added the status/LGT2 Indicates that a PR has LGTM 2. label May 25, 2021
@crazycs520
Copy link
Contributor Author

/run-all-tests

Signed-off-by: crazycs <crazycs520@gmail.com>
Signed-off-by: crazycs <crazycs520@gmail.com>
@crazycs520
Copy link
Contributor Author

/run-all-tests

@crazycs520
Copy link
Contributor Author

/run-all-tests

@crazycs520
Copy link
Contributor Author

/run-e2e-test

@crazycs520
Copy link
Contributor Author

/run-all-tests

@crazycs520
Copy link
Contributor Author

/run-unit-test

@lysu
Copy link
Contributor

lysu commented May 25, 2021

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 0b35aba

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label May 25, 2021
@ti-chi-bot
Copy link
Member

@crazycs520: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@crazycs520
Copy link
Contributor Author

/run-sqllogic-test-1
/run-integration-copr-test

@ti-chi-bot ti-chi-bot merged commit 2580240 into pingcap:master May 25, 2021
Comment on lines +260 to +265
normalized, sqlDigest := sc.SQLDigest()
if len(normalized) == 0 {
return nil
}
tag = resourcegrouptag.EncodeResourceGroupTag(sqlDigest, sc.planDigest)
sc.resourceGroupTag.Store(tag)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about concurrently calling this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concurrently calling doesn't affect correctness.

@@ -183,7 +183,7 @@ func ErrDeadlockToDeadlockRecord(dl *tikverr.ErrDeadlock) *DeadlockRecord {
}
waitChain = append(waitChain, WaitChainItem{
TryLockTxn: rawItem.Txn,
SQLDigest: sqlDigest,
SQLDigest: hex.EncodeToString(sqlDigest),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to be NewDigest(sqlDigest).String()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, use NewDigest(sqlDigest).String() is better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/config sig/execution SIG execution sig/planner SIG: Planner sig/sql-infra SIG: SQL Infra size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants