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

topsql: fix nil pointer panic in stmtctx #30181

Merged
merged 7 commits into from
Nov 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 7 additions & 32 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,7 @@ type StatementContext struct {

// stmtCache is used to store some statement-related values.
stmtCache map[StmtCacheKey]interface{}
// resourceGroupTagWithRow cache for the current statement resource group tag (with `Row` label).
resourceGroupTagWithRow atomic.Value
// resourceGroupTagWithIndex cache for the current statement resource group tag (with `Index` label).
resourceGroupTagWithIndex atomic.Value
// resourceGroupTagWithUnknown cache for the current statement resource group tag (with `Unknown` label).
resourceGroupTagWithUnknown atomic.Value

// Map to store all CTE storages of current SQL.
// Will clean up at the end of the execution.
CTEStorageMap interface{}
Expand Down Expand Up @@ -287,44 +282,24 @@ func (sc *StatementContext) GetPlanDigest() (normalized string, planDigest *pars
// GetResourceGroupTagger returns the implementation of tikvrpc.ResourceGroupTagger related to self.
func (sc *StatementContext) GetResourceGroupTagger() tikvrpc.ResourceGroupTagger {
return func(req *tikvrpc.Request) {
if req == nil {
return
}
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 {
switch label {
case tipb.ResourceGroupTagLabel_ResourceGroupTagLabelRow:
v := sc.resourceGroupTagWithRow.Load()
if v != nil {
return v.([]byte)
}
case tipb.ResourceGroupTagLabel_ResourceGroupTagLabelIndex:
v := sc.resourceGroupTagWithIndex.Load()
if v != nil {
return v.([]byte)
}
case tipb.ResourceGroupTagLabel_ResourceGroupTagLabelUnknown:
v := sc.resourceGroupTagWithUnknown.Load()
if v != nil {
return v.([]byte)
}
if sc == nil {
return nil
}
normalized, sqlDigest := sc.SQLDigest()
if len(normalized) == 0 {
return nil
}
newTag := resourcegrouptag.EncodeResourceGroupTag(sqlDigest, sc.planDigest, label)
switch label {
case tipb.ResourceGroupTagLabel_ResourceGroupTagLabelRow:
sc.resourceGroupTagWithRow.Store(newTag)
case tipb.ResourceGroupTagLabel_ResourceGroupTagLabelIndex:
sc.resourceGroupTagWithIndex.Store(newTag)
case tipb.ResourceGroupTagLabel_ResourceGroupTagLabelUnknown:
sc.resourceGroupTagWithUnknown.Store(newTag)
}
return newTag
return resourcegrouptag.EncodeResourceGroupTag(sqlDigest, sc.planDigest, label)
}

// SetPlanDigest sets the normalized plan and plan digest.
Expand Down