lib/storage: free up memory occupied by Storage.pendingHourEntries after a temporary spike in its memory usage

This reduces vmstorage memory usage by up to 20% in production workload
This commit is contained in:
Aliaksandr Valialkin 2022-10-21 14:59:04 +03:00
parent 32b6ce691b
commit fe5611d6e1
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -796,7 +796,7 @@ func (s *Storage) mustRotateIndexDB() {
// So queries for the last 24 hours stop returning samples added at step 3.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2698
s.pendingHourEntriesLock.Lock()
s.pendingHourEntries = []pendingHourMetricIDEntry{}
s.pendingHourEntries = nil
s.pendingHourEntriesLock.Unlock()
s.currHourMetricIDs.Store(&hourMetricIDs{})
s.prevHourMetricIDs.Store(&hourMetricIDs{})
@ -2516,9 +2516,21 @@ func (s *Storage) updateNextDayMetricIDs() {
func (s *Storage) updateCurrHourMetricIDs() {
hm := s.currHourMetricIDs.Load().(*hourMetricIDs)
var newEntries []pendingHourMetricIDEntry
s.pendingHourEntriesLock.Lock()
newEntries := append([]pendingHourMetricIDEntry{}, s.pendingHourEntries...)
s.pendingHourEntries = s.pendingHourEntries[:0]
if len(s.pendingHourEntries) < cap(s.pendingHourEntries)/2 {
// Free up memory occupied by s.pendingHourEntries,
// since it looks like now it needs much lower amounts of memory.
newEntries = s.pendingHourEntries
s.pendingHourEntries = nil
} else {
// Copy s.pendingHourEntries to newEntries and re-use s.pendingHourEntries capacity,
// since its memory usage is at stable state.
// This should reduce the number of additional memory re-allocations
// when adding new items to s.pendingHourEntries.
newEntries = append([]pendingHourMetricIDEntry{}, s.pendingHourEntries...)
s.pendingHourEntries = s.pendingHourEntries[:0]
}
s.pendingHourEntriesLock.Unlock()
hour := fasttime.UnixHour()