From 540f65cc493b5ce5d95f07bd0b802ac1e2d7f729 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 11 Mar 2024 20:37:05 +0200 Subject: [PATCH] lib/storage: move the conversion of tag filters to composite tag filters into indexSearch.searchMetricIDsInternal This makes the code less fragile - it is harder to skip the convertToCompositeTagFilterss() call now. While at it, call indexSearch.containsTimeRange() inside indexSearch.searchMetricIDsInternal() in order to quickly terminate search of time series in the old indexdb for new time ranges. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5055 This is a follow-up for 2d31fd7855bf304175348b502059eaf7b606af95 --- lib/storage/index_db.go | 44 ++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index dd082fd57..bbfd0eff6 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -593,10 +593,6 @@ func (db *indexDB) SearchLabelNamesWithFiltersOnTimeRange(qt *querytracer.Tracer qt = qt.NewChild("search for label names: filters=%s, timeRange=%s, maxLabelNames=%d, maxMetrics=%d", tfss, &tr, maxLabelNames, maxMetrics) defer qt.Done() - if tr.MinTimestamp >= db.s.minTimestampForCompositeIndex { - tfss = convertToCompositeTagFilterss(tfss) - } - lns := make(map[string]struct{}) qtChild := qt.NewChild("search for label names in the current indexdb") is := db.getIndexSearch(accountID, projectID, deadline) @@ -753,7 +749,9 @@ func (is *indexSearch) searchLabelNamesWithFiltersOnDate(qt *querytracer.Tracer, } func (is *indexSearch) getLabelNamesForMetricIDs(qt *querytracer.Tracer, metricIDs []uint64, lns map[string]struct{}, maxLabelNames int) { - lns["__name__"] = struct{}{} + if len(metricIDs) > 0 { + lns["__name__"] = struct{}{} + } var mn MetricName foundLabelNames := 0 var buf []byte @@ -916,10 +914,6 @@ func (db *indexDB) SearchLabelValuesWithFiltersOnTimeRange(qt *querytracer.Trace qt = qt.NewChild("search for label values: labelName=%q, filters=%s, timeRange=%s, maxLabelNames=%d, maxMetrics=%d", labelName, tfss, &tr, maxLabelValues, maxMetrics) defer qt.Done() - if tr.MinTimestamp >= db.s.minTimestampForCompositeIndex { - tfss = convertToCompositeTagFilterss(tfss) - } - lvs := make(map[string]struct{}) qtChild := qt.NewChild("search for label values in the current indexdb") is := db.getIndexSearch(accountID, projectID, deadline) @@ -1337,10 +1331,6 @@ func (is *indexSearch) getSeriesCount() (uint64, error) { func (db *indexDB) GetTSDBStatus(qt *querytracer.Tracer, accountID, projectID uint32, tfss []*TagFilters, date uint64, focusLabel string, topN, maxMetrics int, deadline uint64) (*TSDBStatus, error) { qtChild := qt.NewChild("collect tsdb stats in the current indexdb") - if int64(date*msecPerDay) >= db.s.minTimestampForCompositeIndex { - tfss = convertToCompositeTagFilterss(tfss) - } - is := db.getIndexSearch(accountID, projectID, deadline) status, err := is.getTSDBStatus(qtChild, tfss, date, focusLabel, topN, maxMetrics) qtChild.Done() @@ -1645,7 +1635,6 @@ func (db *indexDB) DeleteTSIDs(qt *querytracer.Tracer, tfss []*TagFilters) (int, if len(tfss) == 0 { return 0, nil } - tfss = convertToCompositeTagFilterss(tfss) // Obtain metricIDs to delete. tr := TimeRange{ @@ -1751,9 +1740,6 @@ func (db *indexDB) searchMetricIDs(qt *querytracer.Tracer, tfss []*TagFilters, t if len(tfss) == 0 { return nil, nil } - if tr.MinTimestamp >= db.s.minTimestampForCompositeIndex { - tfss = convertToCompositeTagFilterss(tfss) - } qtChild := qt.NewChild("search for metricIDs in the current indexdb") tfKeyBuf := tagFiltersKeyBufPool.Get() @@ -2286,14 +2272,6 @@ func (is *indexSearch) searchMetricIDsWithFiltersOnDate(qt *querytracer.Tracer, // // The returned metricIDs are sorted. func (is *indexSearch) searchMetricIDs(qt *querytracer.Tracer, tfss []*TagFilters, tr TimeRange, maxMetrics int) ([]uint64, error) { - ok, err := is.containsTimeRange(tr) - if err != nil { - return nil, err - } - if !ok { - // Fast path - the index doesn't contain data for the given tr. - return nil, nil - } metricIDs, err := is.searchMetricIDsInternal(qt, tfss, tr, maxMetrics) if err != nil { return nil, err @@ -2325,7 +2303,23 @@ func (is *indexSearch) searchMetricIDs(qt *querytracer.Tracer, tfss []*TagFilter func (is *indexSearch) searchMetricIDsInternal(qt *querytracer.Tracer, tfss []*TagFilters, tr TimeRange, maxMetrics int) (*uint64set.Set, error) { qt = qt.NewChild("search for metric ids: filters=%s, timeRange=%s, maxMetrics=%d", tfss, &tr, maxMetrics) defer qt.Done() + metricIDs := &uint64set.Set{} + + ok, err := is.containsTimeRange(tr) + if err != nil { + return nil, err + } + if !ok { + qt.Printf("indexdb doesn't contain data for the given timeRange=%s", &tr) + return metricIDs, nil + } + + if tr.MinTimestamp >= is.db.s.minTimestampForCompositeIndex { + tfss = convertToCompositeTagFilterss(tfss) + qt.Printf("composite filters=%s", tfss) + } + for _, tfs := range tfss { if len(tfs.tfs) == 0 { // An empty filters must be equivalent to `{__name__!=""}`