mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-15 16:30:55 +01:00
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:
parent
082eabf51e
commit
5c9715a89a
@ -1769,11 +1769,12 @@ func (s *Storage) updatePerDateData(rows []rawRow) error {
|
|||||||
is := idb.getIndexSearch(0, 0, noDeadline)
|
is := idb.getIndexSearch(0, 0, noDeadline)
|
||||||
defer idb.putIndexSearch(is)
|
defer idb.putIndexSearch(is)
|
||||||
var firstError error
|
var firstError error
|
||||||
for _, dateMetricID := range pendingDateMetricIDs {
|
dateMetricIDsForCache := make([]dateMetricID, 0, len(pendingDateMetricIDs))
|
||||||
date := dateMetricID.date
|
for _, dmid := range pendingDateMetricIDs {
|
||||||
metricID := dateMetricID.metricID
|
date := dmid.date
|
||||||
is.accountID = dateMetricID.accountID
|
metricID := dmid.metricID
|
||||||
is.projectID = dateMetricID.projectID
|
is.accountID = dmid.accountID
|
||||||
|
is.projectID = dmid.projectID
|
||||||
ok, err := is.hasDateMetricID(date, metricID)
|
ok, err := is.hasDateMetricID(date, metricID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if firstError == nil {
|
if firstError == nil {
|
||||||
@ -1793,9 +1794,13 @@ func (s *Storage) updatePerDateData(rows []rawRow) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// The metric must be added to cache only after it has been successfully added to indexDB.
|
dateMetricIDsForCache = append(dateMetricIDsForCache, dateMetricID{
|
||||||
s.dateMetricIDCache.Set(date, metricID)
|
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
|
return firstError
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1878,6 +1883,34 @@ func (dmc *dateMetricIDCache) Has(date, metricID uint64) bool {
|
|||||||
return ok
|
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) {
|
func (dmc *dateMetricIDCache) Set(date, metricID uint64) {
|
||||||
dmc.mu.Lock()
|
dmc.mu.Lock()
|
||||||
v := dmc.byDateMutable.getOrCreate(date)
|
v := dmc.byDateMutable.getOrCreate(date)
|
||||||
|
Loading…
Reference in New Issue
Block a user