From a2986cde70ffb8202fcf1c8f6efd12b0cac34e51 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 2 Jun 2019 21:58:14 +0300 Subject: [PATCH] lib/storage: tune updating a map with today`s metric ids - Increase update iterval from 1s to 10s. This should reduce CPU usage for large amounts of metric ids with constant churn. - Reduce pendingTodayMetricIDsLock lock duration during the update. --- lib/storage/storage.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 6b851bebed..347beaafe8 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -360,15 +360,17 @@ func (s *Storage) startTodayMetricIDsUpdater() { }() } +var todayMetricIDsUpdateInterval = time.Second * 10 + func (s *Storage) todayMetricIDsUpdater() { - t := time.NewTimer(time.Second) + t := time.NewTimer(todayMetricIDsUpdateInterval) for { select { case <-s.stop: return case <-t.C: s.updateTodayMetricIDs() - t.Reset(time.Second) + t.Reset(todayMetricIDsUpdateInterval) } } } @@ -766,20 +768,20 @@ func (s *Storage) updateTodayMetricIDs() { return } - // Slow path tm.m must be updated with non-empty s.pendingTodayMetricIDs. + // Slow path: tm.m must be updated with non-empty s.pendingTodayMetricIDs. m := make(map[uint64]struct{}, len(tm.m)+newMetricIDsLen) if tm.date == today { for metricID := range tm.m { m[metricID] = struct{}{} } } - s.pendingTodayMetricIDsLock.Lock() - for metricID := range s.pendingTodayMetricIDs { + newMetricIDs := s.pendingTodayMetricIDs + s.pendingTodayMetricIDs = make(map[uint64]struct{}, len(newMetricIDs)) + s.pendingTodayMetricIDsLock.Unlock() + for metricID := range newMetricIDs { m[metricID] = struct{}{} } - s.pendingTodayMetricIDs = make(map[uint64]struct{}, len(s.pendingTodayMetricIDs)) - s.pendingTodayMetricIDsLock.Unlock() tmNew := &todayMetricIDs{ m: m,