From 727ded9d4e8329bf3d574fa612a528c6220ef0ca Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 16 Mar 2021 20:01:41 +0200 Subject: [PATCH] lib/storage: time series search optimization according to production workload profiling Do not pass filter metric ids to getMetricIDsForTagFilter, since it has been appeared that this slows down the function by multiple times when it finds big number of metricIDs (tens of millions). --- lib/storage/index_db.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 0276dc36d..db2e6dda4 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -2083,7 +2083,7 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCount(tfs *TagFilters, maxMet continue } - metricIDs, _, err := is.getMetricIDsForTagFilter(tf, nil, maxMetrics, int64Max) + metricIDs, _, err := is.getMetricIDsForTagFilter(tf, maxMetrics, int64Max) if err != nil { return nil, nil, fmt.Errorf("cannot find MetricIDs for tagFilter %s: %w", tf, err) } @@ -2386,20 +2386,14 @@ const ( var uselessTagFilterCacheValue = []byte("1") -func (is *indexSearch) getMetricIDsForTagFilter(tf *tagFilter, filter *uint64set.Set, maxMetrics int, maxLoopsCount int64) (*uint64set.Set, int64, error) { +func (is *indexSearch) getMetricIDsForTagFilter(tf *tagFilter, maxMetrics int, maxLoopsCount int64) (*uint64set.Set, int64, error) { if tf.isNegative { logger.Panicf("BUG: isNegative must be false") } metricIDs := &uint64set.Set{} if len(tf.orSuffixes) > 0 { // Fast path for orSuffixes - seek for rows for each value from orSuffixes. - var loopsCount int64 - var err error - if filter != nil { - loopsCount, err = is.updateMetricIDsForOrSuffixesWithFilter(tf, metricIDs, filter, maxLoopsCount) - } else { - loopsCount, err = is.updateMetricIDsForOrSuffixesNoFilter(tf, metricIDs, maxMetrics, maxLoopsCount) - } + loopsCount, err := is.updateMetricIDsForOrSuffixesNoFilter(tf, metricIDs, maxMetrics, maxLoopsCount) if err != nil { return nil, loopsCount, fmt.Errorf("error when searching for metricIDs for tagFilter in fast path: %w; tagFilter=%s", err, tf) } @@ -2407,7 +2401,9 @@ func (is *indexSearch) getMetricIDsForTagFilter(tf *tagFilter, filter *uint64set } // Slow path - scan for all the rows with the given prefix. - loopsCount, err := is.getMetricIDsForTagFilterSlow(tf, filter, metricIDs.Add, maxLoopsCount) + // Pass nil filter to getMetricIDsForTagFilterSlow, since it works faster on production workloads + // than non-nil filter with many entries. + loopsCount, err := is.getMetricIDsForTagFilterSlow(tf, nil, metricIDs.Add, maxLoopsCount) if err != nil { return nil, loopsCount, fmt.Errorf("error when searching for metricIDs for tagFilter in slow path: %w; tagFilter=%s", err, tf) } @@ -2897,7 +2893,7 @@ func (is *indexSearch) getMetricIDsForDateAndFilters(date uint64, tfs *TagFilter continue } maxLoopsCount := getFirstPositiveLoopsCount(tfws[i+1:]) - m, loopsCount, err := is.getMetricIDsForDateTagFilter(tf, date, nil, tfs.commonPrefix, maxDateMetrics, maxLoopsCount) + m, loopsCount, err := is.getMetricIDsForDateTagFilter(tf, date, tfs.commonPrefix, maxDateMetrics, maxLoopsCount) if err != nil { if errors.Is(err, errTooManyLoops) { // The tf took too many loops compared to the next filter. Postpone applying this filter. @@ -2991,7 +2987,7 @@ func (is *indexSearch) getMetricIDsForDateAndFilters(date uint64, tfs *TagFilter break } maxLoopsCount := getFirstPositiveFilterLoopsCount(tfws[i+1:]) - m, filterLoopsCount, err := is.getMetricIDsForDateTagFilter(tf, date, metricIDs, tfs.commonPrefix, intMax, maxLoopsCount) + m, filterLoopsCount, err := is.getMetricIDsForDateTagFilter(tf, date, tfs.commonPrefix, intMax, maxLoopsCount) if err != nil { if errors.Is(err, errTooManyLoops) { // Postpone tf, since it took more loops than the next filter may need. @@ -3177,8 +3173,7 @@ func (is *indexSearch) hasDateMetricID(date, metricID uint64) (bool, error) { return true, nil } -func (is *indexSearch) getMetricIDsForDateTagFilter(tf *tagFilter, date uint64, filter *uint64set.Set, commonPrefix []byte, - maxMetrics int, maxLoopsCount int64) (*uint64set.Set, int64, error) { +func (is *indexSearch) getMetricIDsForDateTagFilter(tf *tagFilter, date uint64, commonPrefix []byte, maxMetrics int, maxLoopsCount int64) (*uint64set.Set, int64, error) { // Augument tag filter prefix for per-date search instead of global search. if !bytes.HasPrefix(tf.prefix, commonPrefix) { logger.Panicf("BUG: unexpected tf.prefix %q; must start with commonPrefix %q", tf.prefix, commonPrefix) @@ -3190,7 +3185,7 @@ func (is *indexSearch) getMetricIDsForDateTagFilter(tf *tagFilter, date uint64, tfNew := *tf tfNew.isNegative = false // isNegative for the original tf is handled by the caller. tfNew.prefix = kb.B - metricIDs, loopsCount, err := is.getMetricIDsForTagFilter(&tfNew, filter, maxMetrics, maxLoopsCount) + metricIDs, loopsCount, err := is.getMetricIDsForTagFilter(&tfNew, maxMetrics, maxLoopsCount) kbPool.Put(kb) return metricIDs, loopsCount, err }