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

stmtctx: add mutex to protect stmtCache(#36159) (#36351) #36389

Merged
merged 2 commits into from
Sep 14, 2022
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
27 changes: 19 additions & 8 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ type StatementContext struct {
TaskMapBakTS uint64 // counter for

// stmtCache is used to store some statement-related values.
stmtCache map[StmtCacheKey]interface{}
// add mutex to protect stmtCache concurrent access
// https://github.com/pingcap/tidb/issues/36159
stmtCache struct {
mu sync.Mutex
data map[StmtCacheKey]interface{}
}

// Map to store all CTE storages of current SQL.
// Will clean up at the end of the execution.
Expand Down Expand Up @@ -277,23 +282,29 @@ const (

// GetOrStoreStmtCache gets the cached value of the given key if it exists, otherwise stores the value.
func (sc *StatementContext) GetOrStoreStmtCache(key StmtCacheKey, value interface{}) interface{} {
if sc.stmtCache == nil {
sc.stmtCache = make(map[StmtCacheKey]interface{})
sc.stmtCache.mu.Lock()
defer sc.stmtCache.mu.Unlock()
if sc.stmtCache.data == nil {
sc.stmtCache.data = make(map[StmtCacheKey]interface{})
}
if _, ok := sc.stmtCache[key]; !ok {
sc.stmtCache[key] = value
if _, ok := sc.stmtCache.data[key]; !ok {
sc.stmtCache.data[key] = value
}
return sc.stmtCache[key]
return sc.stmtCache.data[key]
}

// ResetInStmtCache resets the cache of given key.
func (sc *StatementContext) ResetInStmtCache(key StmtCacheKey) {
delete(sc.stmtCache, key)
sc.stmtCache.mu.Lock()
defer sc.stmtCache.mu.Unlock()
delete(sc.stmtCache.data, key)
}

// ResetStmtCache resets all cached values.
func (sc *StatementContext) ResetStmtCache() {
sc.stmtCache = make(map[StmtCacheKey]interface{})
sc.stmtCache.mu.Lock()
defer sc.stmtCache.mu.Unlock()
sc.stmtCache.data = make(map[StmtCacheKey]interface{})
}

// SQLDigest gets normalized and digest for provided sql.
Expand Down