diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 4d079dd16..7cee37cbd 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -2362,7 +2362,7 @@ func (s *Storage) updateNextDayMetricIDs(date uint64) { if e.date == date { pendingMetricIDs.Union(&e.v) } else { - // Do not add pendingMetricIDs from the previous day to the cyrrent day, + // Do not add pendingMetricIDs from the previous day to the current day, // since this may result in missing registration of the metricIDs in the per-day inverted index. // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3309 pendingMetricIDs = &uint64set.Set{} @@ -2390,14 +2390,15 @@ func (s *Storage) updateCurrHourMetricIDs(hour uint64) { var m *uint64set.Set if hm.hour == hour { m = hm.m.Clone() - } else { - m = &uint64set.Set{} - } - if hour%24 != 0 { - // Do not add pending metricIDs from the previous hour to the current hour on the next day, - // since this may result in missing registration of the metricIDs in the per-day inverted index. - // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3309 m.Union(newMetricIDs) + } else { + m = newMetricIDs + if hour%24 == 0 { + // Do not add pending metricIDs from the previous hour to the current hour on the next day, + // since this may result in missing registration of the metricIDs in the per-day inverted index. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3309 + m = &uint64set.Set{} + } } hmNew := &hourMetricIDs{ m: m, diff --git a/lib/storage/storage_test.go b/lib/storage/storage_test.go index fab81cc9c..547425a14 100644 --- a/lib/storage/storage_test.go +++ b/lib/storage/storage_test.go @@ -157,9 +157,12 @@ func TestUpdateCurrHourMetricIDs(t *testing.T) { t.Run("empty_pending_metric_ids_stale_curr_hour", func(t *testing.T) { s := newStorage() hour := fasttime.UnixHour() + if hour%24 == 0 { + hour++ + } hmOrig := &hourMetricIDs{ m: &uint64set.Set{}, - hour: 123, + hour: hour - 1, } hmOrig.m.Add(12) hmOrig.m.Add(34) @@ -230,9 +233,12 @@ func TestUpdateCurrHourMetricIDs(t *testing.T) { s.pendingHourEntries = pendingHourEntries hour := fasttime.UnixHour() + if hour%24 == 0 { + hour++ + } hmOrig := &hourMetricIDs{ m: &uint64set.Set{}, - hour: 123, + hour: hour - 1, } hmOrig.m.Add(12) hmOrig.m.Add(34) @@ -307,6 +313,49 @@ func TestUpdateCurrHourMetricIDs(t *testing.T) { t.Fatalf("unexpected s.pendingHourEntries.Len(); got %d; want %d", s.pendingHourEntries.Len(), 0) } }) + t.Run("nonempty_pending_metric_ids_valid_curr_hour_start_of_day", func(t *testing.T) { + s := newStorage() + pendingHourEntries := &uint64set.Set{} + pendingHourEntries.Add(343) + pendingHourEntries.Add(32424) + pendingHourEntries.Add(8293432) + s.pendingHourEntries = pendingHourEntries + + hour := fasttime.UnixHour() + hour -= hour % 24 + hmOrig := &hourMetricIDs{ + m: &uint64set.Set{}, + hour: hour, + } + hmOrig.m.Add(12) + hmOrig.m.Add(34) + s.currHourMetricIDs.Store(hmOrig) + s.updateCurrHourMetricIDs(hour) + hmCurr := s.currHourMetricIDs.Load().(*hourMetricIDs) + if hmCurr.hour != hour { + t.Fatalf("unexpected hmCurr.hour; got %d; want %d", hmCurr.hour, hour) + } + m := pendingHourEntries.Clone() + hmOrig.m.ForEach(func(part []uint64) bool { + for _, metricID := range part { + m.Add(metricID) + } + return true + }) + if !hmCurr.m.Equal(m) { + t.Fatalf("unexpected hm.m; got %v; want %v", hmCurr.m, m) + } + + hmPrev := s.prevHourMetricIDs.Load().(*hourMetricIDs) + hmEmpty := &hourMetricIDs{} + if !reflect.DeepEqual(hmPrev, hmEmpty) { + t.Fatalf("unexpected hmPrev; got %v; want %v", hmPrev, hmEmpty) + } + + if s.pendingHourEntries.Len() != 0 { + t.Fatalf("unexpected s.pendingHourEntries.Len(); got %d; want %d", s.pendingHourEntries.Len(), 0) + } + }) t.Run("nonempty_pending_metric_ids_from_previous_hour_new_day", func(t *testing.T) { s := newStorage()