diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 74acf08e9d..437dd42c55 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -2027,9 +2027,14 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCountOptimized(tfs *TagFilter } func (is *indexSearch) getTagFilterWithMinMetricIDsCountAdaptive(tfs *TagFilters, maxMetrics int) (*tagFilter, *uint64set.Set, error) { + appendCacheKeyPrefix := func(dst []byte, prefix byte) []byte { + dst = append(dst, prefix) + dst = append(dst, is.db.name...) + dst = encoding.MarshalUint64(dst, uint64(maxMetrics)) + return dst + } kb := &is.kb - kb.B = append(kb.B[:0], uselessMultiTagFiltersKeyPrefix) - kb.B = encoding.MarshalUint64(kb.B, uint64(maxMetrics)) + kb.B = appendCacheKeyPrefix(kb.B[:0], uselessMultiTagFiltersKeyPrefix) kb.B = tfs.marshal(kb.B) if len(is.db.uselessTagFiltersCache.Get(nil, kb.B)) > 0 { // Skip useless work below, since the tfs doesn't contain tag filters matching less than maxMetrics metrics. @@ -2057,8 +2062,7 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCountAdaptive(tfs *TagFilters // Too many metrics matched. if maxAllowedMetrics >= maxMetrics { // The tag filter with minimum matching metrics matches at least maxMetrics metrics. - kb.B = append(kb.B[:0], uselessMultiTagFiltersKeyPrefix) - kb.B = encoding.MarshalUint64(kb.B, uint64(maxMetrics)) + kb.B = appendCacheKeyPrefix(kb.B[:0], uselessMultiTagFiltersKeyPrefix) kb.B = tfs.marshal(kb.B) is.db.uselessTagFiltersCache.Set(kb.B, uselessTagFilterCacheValue) return nil, nil, errTooManyMetrics @@ -2075,6 +2079,12 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCountAdaptive(tfs *TagFilters var errTooManyMetrics = errors.New("all the tag filters match too many metrics") func (is *indexSearch) getTagFilterWithMinMetricIDsCount(tfs *TagFilters, maxMetrics int) (*tagFilter, *uint64set.Set, error) { + appendCacheKeyPrefix := func(dst []byte, prefix byte) []byte { + dst = append(dst, prefix) + dst = append(dst, is.db.name...) + dst = encoding.MarshalUint64(dst, uint64(maxMetrics)) + return dst + } var minMetricIDs *uint64set.Set var minTf *tagFilter kb := &is.kb @@ -2086,8 +2096,7 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCount(tfs *TagFilters, maxMet continue } - kb.B = append(kb.B[:0], uselessSingleTagFilterKeyPrefix) - kb.B = encoding.MarshalUint64(kb.B, uint64(maxMetrics)) + kb.B = appendCacheKeyPrefix(kb.B[:0], uselessSingleTagFilterKeyPrefix) kb.B = tf.Marshal(kb.B, is.accountID, is.projectID) if len(is.db.uselessTagFiltersCache.Get(nil, kb.B)) > 0 { // Skip useless work below, since the tf matches at least maxMetrics metrics. @@ -2101,8 +2110,7 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCount(tfs *TagFilters, maxMet } if metricIDs.Len() >= maxMetrics { // The tf matches at least maxMetrics. Skip it - kb.B = append(kb.B[:0], uselessSingleTagFilterKeyPrefix) - kb.B = encoding.MarshalUint64(kb.B, uint64(maxMetrics)) + kb.B = appendCacheKeyPrefix(kb.B[:0], uselessSingleTagFilterKeyPrefix) kb.B = tf.Marshal(kb.B, is.accountID, is.projectID) is.db.uselessTagFiltersCache.Set(kb.B, uselessTagFilterCacheValue) uselessTagFilters++ @@ -2128,8 +2136,7 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCount(tfs *TagFilters, maxMet // There is no positive filter with small number of matching metrics. // Create it, so it matches all the MetricIDs. - kb.B = append(kb.B[:0], uselessNegativeTagFilterKeyPrefix) - kb.B = encoding.MarshalUint64(kb.B, uint64(maxMetrics)) + kb.B = appendCacheKeyPrefix(kb.B[:0], uselessNegativeTagFilterKeyPrefix) kb.B = tfs.marshal(kb.B) if len(is.db.uselessTagFiltersCache.Get(nil, kb.B)) > 0 { return nil, nil, errTooManyMetrics @@ -2139,8 +2146,7 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCount(tfs *TagFilters, maxMet return nil, nil, err } if metricIDs.Len() >= maxMetrics { - kb.B = append(kb.B[:0], uselessNegativeTagFilterKeyPrefix) - kb.B = encoding.MarshalUint64(kb.B, uint64(maxMetrics)) + kb.B = appendCacheKeyPrefix(kb.B[:0], uselessNegativeTagFilterKeyPrefix) kb.B = tfs.marshal(kb.B) is.db.uselessTagFiltersCache.Set(kb.B, uselessTagFilterCacheValue) } @@ -3137,7 +3143,7 @@ func (is *indexSearch) getMetricIDsForDateTagFilter(tf *tagFilter, date uint64, } func (is *indexSearch) getLoopsCountAndTimestampForDateFilter(date uint64, tf *tagFilter) (int64, int64, uint64) { - is.kb.B = appendDateTagFilterCacheKey(is.kb.B[:0], date, tf, is.accountID, is.projectID) + is.kb.B = appendDateTagFilterCacheKey(is.kb.B[:0], is.db.name, date, tf, is.accountID, is.projectID) kb := kbPool.Get() defer kbPool.Put(kb) kb.B = is.db.loopsPerDateTagFilterCache.Get(kb.B[:0], is.kb.B) @@ -3152,7 +3158,7 @@ func (is *indexSearch) getLoopsCountAndTimestampForDateFilter(date uint64, tf *t func (is *indexSearch) storeLoopsCountForDateFilter(date uint64, tf *tagFilter, loopsCount, filterLoopsCount int64) { currentTimestamp := fasttime.UnixTimestamp() - is.kb.B = appendDateTagFilterCacheKey(is.kb.B[:0], date, tf, is.accountID, is.projectID) + is.kb.B = appendDateTagFilterCacheKey(is.kb.B[:0], is.db.name, date, tf, is.accountID, is.projectID) kb := kbPool.Get() kb.B = encoding.MarshalInt64(kb.B[:0], loopsCount) kb.B = encoding.MarshalInt64(kb.B, filterLoopsCount) @@ -3161,7 +3167,8 @@ func (is *indexSearch) storeLoopsCountForDateFilter(date uint64, tf *tagFilter, kbPool.Put(kb) } -func appendDateTagFilterCacheKey(dst []byte, date uint64, tf *tagFilter, accountID, projectID uint32) []byte { +func appendDateTagFilterCacheKey(dst []byte, indexDBName string, date uint64, tf *tagFilter, accountID, projectID uint32) []byte { + dst = append(dst, indexDBName...) dst = encoding.MarshalUint64(dst, date) dst = tf.Marshal(dst, accountID, projectID) return dst