From 4d22da3cb89c55797a721ed13892e159a87228cd Mon Sep 17 00:00:00 2001
From: Nikolay <nik@victoriametrics.com>
Date: Sat, 6 May 2023 06:45:47 +0200
Subject: [PATCH] lib/storage: properly update link for entry at dateMetricID
 cache (#4258)

previously during sync for mutable and immutable cache parts, link for hotEntry with current date may be not properly updated
it corrupts cache for backfilling metrics and increased cpu load
---
 lib/storage/storage.go | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/storage/storage.go b/lib/storage/storage.go
index 49c103be76..9ef67381d8 100644
--- a/lib/storage/storage.go
+++ b/lib/storage/storage.go
@@ -2304,10 +2304,14 @@ func (dmc *dateMetricIDCache) syncLocked() {
 		}
 		v = v.Clone()
 		v.Union(&e.v)
-		byDateMutable.m[date] = &byDateMetricIDEntry{
+		dme := &byDateMetricIDEntry{
 			date: date,
 			v:    *v,
 		}
+		if date == byDateMutable.hotEntry.Load().(*byDateMetricIDEntry).date {
+			byDateMutable.hotEntry.Store(dme)
+		}
+		byDateMutable.m[date] = dme
 	}
 	for date, e := range byDate.m {
 		v := byDateMutable.get(date)