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

domain: fix data race in runaway DeriveChecker #49354

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pkg/domain/resourcegroup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
deps = [
"//pkg/metrics",
"//pkg/util/dbterror/exeerrors",
"//pkg/util/generic",
"//pkg/util/logutil",
"@com_github_jellydator_ttlcache_v3//:ttlcache",
"@com_github_pingcap_kvproto//pkg/resource_manager",
Expand Down
9 changes: 5 additions & 4 deletions pkg/domain/resourcegroup/runaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/util/dbterror/exeerrors"
"github.com/pingcap/tidb/pkg/util/generic"
"github.com/pingcap/tidb/pkg/util/logutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/tikv/client-go/v2/tikv"
Expand Down Expand Up @@ -191,7 +192,7 @@ type RunawayManager struct {
// activeGroup is used to manage the active runaway watches of resource group
activeGroup map[string]int64
activeLock sync.RWMutex
metricsMap map[string]prometheus.Counter
Copy link
Member

Choose a reason for hiding this comment

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

Please remember to run make bazel_prepare.

metricsMap generic.SyncMap[string, prometheus.Counter]

resourceGroupCtl *rmclient.ResourceGroupsController
serverID string
Expand Down Expand Up @@ -225,7 +226,7 @@ func NewRunawayManager(resourceGroupCtl *rmclient.ResourceGroupsController, serv
quarantineChan: make(chan *QuarantineRecord, maxWatchRecordChannelSize),
staleQuarantineRecord: staleQuarantineChan,
activeGroup: make(map[string]int64),
metricsMap: make(map[string]prometheus.Counter),
metricsMap: generic.NewSyncMap[string, prometheus.Counter](8),
}
m.insertionCancel = watchList.OnInsertion(func(ctx context.Context, i *ttlcache.Item[string, *QuarantineRecord]) {
m.activeLock.Lock()
Expand Down Expand Up @@ -256,10 +257,10 @@ func (rm *RunawayManager) DeriveChecker(resourceGroupName, originalSQL, sqlDiges
if group.RunawaySettings == nil && rm.activeGroup[resourceGroupName] == 0 {
return nil
}
counter, ok := rm.metricsMap[resourceGroupName]
counter, ok := rm.metricsMap.Load(resourceGroupName)
if !ok {
counter = metrics.RunawayCheckerCounter.WithLabelValues(resourceGroupName, "hit", "")
rm.metricsMap[resourceGroupName] = counter
rm.metricsMap.Store(resourceGroupName, counter)
}
counter.Inc()
return newRunawayChecker(rm, resourceGroupName, group.RunawaySettings, originalSQL, sqlDigest, planDigest)
Expand Down