From 83d3e582ab22f740726e3057091cbc0791d33472 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 4 Feb 2021 18:46:20 +0200 Subject: [PATCH] lib/storage: check for prevHourMetricIDs cache before falling back to checking for (date, metricID) entries during data ingestion This should reduce possible CPU usage spikes at the beginning of every hour. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1046 --- lib/storage/storage.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/storage/storage.go b/lib/storage/storage.go index e8c808293b..86584dc801 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -1573,6 +1573,8 @@ func (s *Storage) updatePerDateData(rows []rawRow) error { prevMetricID uint64 ) hm := s.currHourMetricIDs.Load().(*hourMetricIDs) + hmPrev := s.prevHourMetricIDs.Load().(*hourMetricIDs) + hmPrevDate := hmPrev.hour / 24 nextDayMetricIDs := &s.nextDayMetricIDs.Load().(*byDateMetricIDEntry).v todayShare16bit := uint64((float64(fasttime.UnixTimestamp()%(3600*24)) / (3600 * 24)) * (1 << 16)) type pendingDateMetricID struct { @@ -1613,6 +1615,10 @@ func (s *Storage) updatePerDateData(rows []rawRow) error { s.pendingHourEntriesLock.Lock() s.pendingHourEntries.Add(metricID) s.pendingHourEntriesLock.Unlock() + if date == hmPrevDate && hmPrev.m.Has(metricID) { + // The metricID is already registered for the current day on the previous hour. + continue + } } // Slower path: check global cache for (date, metricID) entry.