lib/storage: reduce lock contention in dateMetricIDCache when registering new time series for the current day

This should help systems with multiple CPU cores
This commit is contained in:
Aliaksandr Valialkin 2021-02-09 23:59:14 +02:00
parent 082eabf51e
commit 5c9715a89a

View file

@ -1769,11 +1769,12 @@ func (s *Storage) updatePerDateData(rows []rawRow) error {
is := idb.getIndexSearch(0, 0, noDeadline)
defer idb.putIndexSearch(is)
var firstError error
for _, dateMetricID := range pendingDateMetricIDs {
date := dateMetricID.date
metricID := dateMetricID.metricID
is.accountID = dateMetricID.accountID
is.projectID = dateMetricID.projectID
dateMetricIDsForCache := make([]dateMetricID, 0, len(pendingDateMetricIDs))
for _, dmid := range pendingDateMetricIDs {
date := dmid.date
metricID := dmid.metricID
is.accountID = dmid.accountID
is.projectID = dmid.projectID
ok, err := is.hasDateMetricID(date, metricID)
if err != nil {
if firstError == nil {
@ -1793,9 +1794,13 @@ func (s *Storage) updatePerDateData(rows []rawRow) error {
continue
}
}
// The metric must be added to cache only after it has been successfully added to indexDB.
s.dateMetricIDCache.Set(date, metricID)
dateMetricIDsForCache = append(dateMetricIDsForCache, dateMetricID{
date: date,
metricID: metricID,
})
}
// The (date, metricID) entries must be added to cache only after they have been successfully added to indexDB.
s.dateMetricIDCache.Store(dateMetricIDsForCache)
return firstError
}
@ -1878,6 +1883,34 @@ func (dmc *dateMetricIDCache) Has(date, metricID uint64) bool {
return ok
}
type dateMetricID struct {
date uint64
metricID uint64
}
func (dmc *dateMetricIDCache) Store(dmids []dateMetricID) {
var prevDate uint64
metricIDs := make([]uint64, 0, len(dmids))
dmc.mu.Lock()
for _, dmid := range dmids {
if prevDate == dmid.date {
metricIDs = append(metricIDs, dmid.metricID)
continue
}
if len(metricIDs) > 0 {
v := dmc.byDateMutable.getOrCreate(prevDate)
v.AddMulti(metricIDs)
}
metricIDs = append(metricIDs[:0], dmid.metricID)
prevDate = dmid.date
}
if len(metricIDs) > 0 {
v := dmc.byDateMutable.getOrCreate(prevDate)
v.AddMulti(metricIDs)
}
dmc.mu.Unlock()
}
func (dmc *dateMetricIDCache) Set(date, metricID uint64) {
dmc.mu.Lock()
v := dmc.byDateMutable.getOrCreate(date)