Skip to content

Commit

Permalink
domain: fix data race in runaway DeriveChecker (pingcap#49354)
Browse files Browse the repository at this point in the history
  • Loading branch information
glorv committed Dec 12, 2023
1 parent b478056 commit 0b39f99
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
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
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

0 comments on commit 0b39f99

Please sign in to comment.