Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize to posting group label value matching #7343

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions pkg/store/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2725,8 +2725,11 @@ func matchersToPostingGroups(ctx context.Context, lvalsFn func(name string) ([]s
matchers := make([]*labels.Matcher, 0, len(vals))
// Merge PostingGroups with the same matcher into 1 to
// avoid fetching duplicate postings.
cnt := 0
for _, val := range values {
pg, vals, err = toPostingGroup(ctx, lvalsFunc, val)
cnt++
// We can only reuse the values if the values won't be reused anymore.
pg, vals, err = toPostingGroup(ctx, lvalsFunc, val, cnt == len(values))
if err != nil {
return nil, errors.Wrap(err, "toPostingGroup")
}
Expand Down Expand Up @@ -2770,13 +2773,11 @@ func matchersToPostingGroups(ctx context.Context, lvalsFn func(name string) ([]s
}

// NOTE: Derived from tsdb.postingsForMatcher. index.Merge is equivalent to map duplication.
func toPostingGroup(ctx context.Context, lvalsFn func(name string) ([]string, error), m *labels.Matcher) (*postingGroup, []string, error) {
func toPostingGroup(ctx context.Context, lvalsFn func(name string) ([]string, error), m *labels.Matcher, reuseValues bool) (*postingGroup, []string, error) {
// If the matcher selects an empty value, it selects all the series which don't
// have the label name set too. See: https://github.com/prometheus/prometheus/issues/3575
// and https://github.com/prometheus/prometheus/pull/3578#issuecomment-351653555.
if m.Matches("") {
var toRemove []string

// Fast-path for MatchNotRegexp matching.
// Inverse of a MatchNotRegexp is MatchRegexp (double negation).
// Fast-path for set matching.
Expand All @@ -2798,12 +2799,22 @@ func toPostingGroup(ctx context.Context, lvalsFn func(name string) ([]string, er
return nil, nil, err
}

for _, val := range vals {
if ctx.Err() != nil {
return nil, nil, ctx.Err()
}
if !m.Matches(val) {
toRemove = append(toRemove, val)
var toRemove []string
if reuseValues {
toRemove = vals[:0]
}
// If equal matcher matches an empty string, shortcut all values since label
// values should always be non-empty string.
if m.Value == "" && (m.Type == labels.MatchRegexp || m.Type == labels.MatchEqual) {
toRemove = vals
} else {
for _, val := range vals {
if ctx.Err() != nil {
return nil, nil, ctx.Err()
}
if !m.Matches(val) {
toRemove = append(toRemove, val)
}
}
}

Expand All @@ -2827,12 +2838,21 @@ func toPostingGroup(ctx context.Context, lvalsFn func(name string) ([]string, er
}

var toAdd []string
for _, val := range vals {
if ctx.Err() != nil {
return nil, nil, ctx.Err()
}
if m.Matches(val) {
toAdd = append(toAdd, val)
if reuseValues {
toAdd = vals[:0]
}
// If non-equal matcher matches a non-empty string, shortcut all values since label
// values should always be non-empty string.
if m.Value == "" && (m.Type == labels.MatchNotRegexp || m.Type == labels.MatchNotEqual) {
toAdd = vals
} else {
for _, val := range vals {
if ctx.Err() != nil {
return nil, nil, ctx.Err()
}
if m.Matches(val) {
toAdd = append(toAdd, val)
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions pkg/store/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -2953,9 +2952,20 @@ func samePostingGroup(a, b *postingGroup) bool {
return false
}

if !reflect.DeepEqual(a.addKeys, b.addKeys) || !reflect.DeepEqual(a.removeKeys, b.removeKeys) {
if len(a.addKeys) != len(b.addKeys) || len(a.removeKeys) != len(b.removeKeys) {
return false
}
for i, key := range a.addKeys {
if key != b.addKeys[i] {
return false
}
}

for i, key := range a.removeKeys {
if key != b.removeKeys[i] {
return false
}
}

for i := 0; i < len(a.matchers); i++ {
if a.matchers[i].String() != b.matchers[i].String() {
Expand Down
Loading