From baaec4c3e1bef3cf36fdf40682961dba6113c148 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 22 Sep 2023 13:20:28 +0200 Subject: [PATCH] wip --- lib/storage/storage.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/storage/storage.go b/lib/storage/storage.go index 31d57d31d..77ff26249 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -1989,6 +1989,7 @@ func (s *Storage) add(rows []rawRow, dstMrs []*MetricRow, mrs []MetricRow, preci if err != nil { err = fmt.Errorf("cannot update per-date data: %w", err) } else { + s.addMissingMetricIDToTSIDEntries(rows) s.tb.MustAddRows(rows) } if err != nil { @@ -1997,6 +1998,40 @@ func (s *Storage) add(rows []rawRow, dstMrs []*MetricRow, mrs []MetricRow, preci return nil } +func (s *Storage) addMissingMetricIDToTSIDEntries(rows []rawRow) { + // this function is needed for recovering missing metricID -> TSID entries + // in the issue https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4972 + + idb := s.idb() + is := idb.getIndexSearch(0, 0, noDeadline) + defer idb.putIndexSearch(is) + ii := getIndexItems() + defer putIndexItems(ii) + + var tsidBuf TSID + for i := range rows { + tsid := &rows[i].TSID + err := idb.getFromMetricIDCache(&tsidBuf, tsid.MetricID) + if err == nil && *tsid == tsidBuf { + continue + } + if err != io.EOF { + logger.Panicf("FATAL: unexpected error when obtaining TSID for the given MetricID from cache: %s", err) + } + if !is.getTSIDByMetricID(&tsidBuf, tsid.MetricID) || *tsid != tsidBuf { + logger.Infof("Creating missing MetricID->TSID entry for MetricID=%d", tsid.MetricID) + ii.B = marshalCommonPrefix(ii.B, nsPrefixMetricIDToMetricName, tsid.AccountID, tsid.ProjectID) + ii.B = encoding.MarshalUint64(ii.B, tsid.MetricID) + ii.B = tsid.Marshal(ii.B) + ii.Next() + } + idb.putToMetricIDCache(tsid.MetricID, tsid) + } + if len(ii.Items) > 0 { + is.db.tb.AddItems(ii.Items) + } +} + var storageAddRowsLogger = logger.WithThrottler("storageAddRows", 5*time.Second) // SetLogNewSeries updates new series logging.