This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
tag_query_id_filter.go
326 lines (282 loc) · 11.1 KB
/
tag_query_id_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package memory
import (
"sort"
"strings"
"github.com/grafana/metrictank/expr/tagquery"
"github.com/grafana/metrictank/schema"
log "github.com/sirupsen/logrus"
)
type expressionFilter struct {
// expr is the expression based on which this filter has been generated
expr tagquery.Expression
// test is a filter function which takes certain descriptive properties of a
// MetricDefinition and returns a tagquery.FilterDecision type indicating
// whether the MD satisfies this expression or not
// this decision is made based on the metric tag index, the meta tag index
// does not get take into account
testByMetricTags tagquery.MetricDefinitionFilter
// testByMetaTags is a filter function which has been generated from the meta
// records that match this filter's expression.
// it looks at certain descriptive properties of a MetricDefinition and decides
// whether this metric definition satisfies the given expression based on the
// meta tag index, it does not take the metric tag index into account
testByMetaTags tagquery.MetricDefinitionFilter
// the default decision which should be applied if none of test & testByMetaTags
// have come to a conclusive decision
defaultDecision tagquery.FilterDecision
}
// idFilter contains one or many filter functions which are used to filter metrics
// by the expressions from which its filters have been generated.
// once initialized it can be used concurrently by many worker routines
type idFilter struct {
ctx *TagQueryContext
filters []expressionFilter
}
// newIdFilter takes a set of expressions and a tag query context, then it generates
// various filter functions from the expressions which are going to be used to decide
// whether a given metric matches the provided expressions
func newIdFilter(expressions tagquery.Expressions, ctx *TagQueryContext) *idFilter {
res := idFilter{
ctx: ctx,
filters: make([]expressionFilter, len(expressions)),
}
for i, expr := range expressions {
res.filters[i] = expressionFilter{
expr: expr,
testByMetricTags: expr.GetMetricDefinitionFilter(ctx.index.idHasTag),
defaultDecision: expr.GetDefaultDecision(),
}
if !ctx.useMetaTagIndex() {
continue
}
// this is a performacnce optimization:
// some expressions indicate that they'll likely result in a smaller result set
// if they get inverted.
// f.e. a!=b becomes a=b)
// in this case we want to get the inverted set of meta records that matches
// "expr", because when generating a off of this smaller result set less meta
// record expressions will need to be checked, which will improve the filter
// speed. if the meta record filter set has been generated from an inverted
// expression, then its result will need to be inverted again to get the
// correct result.
// f.e. if a!=b previously has been inverted to a=b, and for a given MD a=b
// results in true, then a!=b would result in false
invertSetOfMetaRecords := expr.ResultIsSmallerWhenInverted()
// if no meta records match this expression, then we don't need to generate
// a meta record filter for it
metaRecordIds := ctx.metaTagIndex.getMetaRecordIdsByExpression(expr, invertSetOfMetaRecords)
if len(metaRecordIds) == 0 {
continue
}
// if we don't use an inverted set of meta records, then we want to check if
// all meta records involved in a meta tag filter use the "=" operator.
// if this is the case then it is cheaper to build a set of acceptable tags
// based on the meta record expressions and just check whether they are present
// in a metric that gets filtered, compared to doing a full tag index lookup
// to check whether a metric has one of the necessary meta tags associated
// with it.
optimizeForOnlyEqualOperators := !invertSetOfMetaRecords
var metaRecordFilters []tagquery.MetricDefinitionFilter
singleExprPerRecord := true
records := make([]tagquery.MetaTagRecord, 0, len(metaRecordIds))
for _, id := range metaRecordIds {
record, ok := ctx.metaTagRecords.getMetaRecordById(id)
if !ok {
corruptIndex.Inc()
log.Errorf("TagQueryContext: Tried to lookup a meta tag record id that does not exist, index is corrupted")
continue
}
if optimizeForOnlyEqualOperators {
for exprIdx := range record.Expressions {
if record.Expressions[exprIdx].GetOperator() != tagquery.EQUAL {
optimizeForOnlyEqualOperators = false
break
}
}
if optimizeForOnlyEqualOperators {
records = append(records, record)
if len(record.Expressions) > 1 {
singleExprPerRecord = false
}
}
}
metaRecordFilters = append(metaRecordFilters, record.GetMetricDefinitionFilter(ctx.index.idHasTag))
}
if optimizeForOnlyEqualOperators {
// there are two different ways how we optimize for the case where all expressions
// of all involved meta records are using the "=" operator. the first and fastest
// way can only be used if each involved meta record only has one single expression,
// otherwise we use the second way which is a bit more expensive but it also works
// if some of the involved meta records have multiple expressions.
if singleExprPerRecord {
res.filters[i].testByMetaTags = metaRecordFilterBySetOfValidValues(records)
} else {
res.filters[i].testByMetaTags = metaRecordFilterBySetOfValidValueSets(records)
}
} else {
if invertSetOfMetaRecords {
res.filters[i].testByMetaTags = metaRecordFilterInverted(metaRecordFilters, res.filters[i].defaultDecision)
} else {
res.filters[i].testByMetaTags = metaRecordFilterNormal(metaRecordFilters, res.filters[i].defaultDecision)
}
}
}
return &res
}
// metaRecordFilterBySetOfValidValues creates a filter function to filter by a meta tag
// which only involves meta records of which each only has exactly one expression and that
// expression is using the "=" operator. this is quite a narrow scenario, but since it is
// a very common use case it makes sense to optimize for it.
func metaRecordFilterBySetOfValidValues(records []tagquery.MetaTagRecord) tagquery.MetricDefinitionFilter {
// we first build a set of valid tags and names.
// since we know that each of the involved meta records uses exactly one expression
// which is using the "=" operator we know that if a given metric's name matches a
// value in validNames or if one of its tags matches a value in validValues then this
// is sufficient to let it pass the filter.
validValues := make(map[string]struct{})
validNames := make(map[string]struct{})
var builder strings.Builder
for _, record := range records {
if len(record.Expressions) < 1 {
corruptIndex.Inc()
log.Errorf("meta-record-filter-by-set-of-valid-values: received a meta record without any associated expressions")
continue
}
if record.Expressions[0].GetKey() == "name" {
validNames[record.Expressions[0].GetValue()] = struct{}{}
} else {
record.Expressions[0].StringIntoWriter(&builder)
validValues[builder.String()] = struct{}{}
builder.Reset()
}
}
return func(_ schema.MKey, name string, tags []string) tagquery.FilterDecision {
for i := range tags {
if _, ok := validValues[tags[i]]; ok {
return tagquery.Pass
}
}
if _, ok := validNames[name]; ok {
return tagquery.Pass
}
return tagquery.None
}
}
// metaRecordFilterBySetOfValidValueSets creates a filter function to filter by a meta tag
// which only involves meta records of which all expressions are only using the "=" operator,
// it is ok if one meta record uses multiple such expressions.
func metaRecordFilterBySetOfValidValueSets(records []tagquery.MetaTagRecord) tagquery.MetricDefinitionFilter {
// we first build a set of tag and name value combinations of which each is sufficient
// to pass the generated filter when a metric contains all values of one of these
// combinations
validValueSets := make([]struct {
name string
tags []string
}, len(records))
var builder strings.Builder
for i := range records {
validValueSets[i].tags = make([]string, 0, len(records[i].Expressions))
for j := range records[i].Expressions {
if records[i].Expressions[j].GetKey() == "name" {
validValueSets[i].name = records[i].Expressions[j].GetValue()
} else {
records[i].Expressions[j].StringIntoWriter(&builder)
validValueSets[i].tags = append(validValueSets[i].tags, builder.String())
builder.Reset()
}
}
// the tags must be sorted because that's a requirement for sliceContainsElements()
sort.Strings(validValueSets[i].tags)
}
return func(_ schema.MKey, name string, tags []string) tagquery.FilterDecision {
// iterate over the acceptable value combinations and check if one matches this metric
for _, validValueSet := range validValueSets {
if len(validValueSet.name) > 0 {
if name != validValueSet.name {
continue
}
}
if sliceContainsElements(validValueSet.tags, tags) {
return tagquery.Pass
}
}
return tagquery.None
}
}
// sliceContainsElements returns true if the elements in the slice "find"
// are all present int the slice "in". It requires both slices to be sorted
func sliceContainsElements(find, in []string) bool {
var findIdx, inIdx int
for {
if findIdx == len(find) {
return true
}
if inIdx == len(in) {
return false
}
if find[findIdx] == in[inIdx] {
findIdx++
inIdx++
continue
}
if find[findIdx] < in[inIdx] {
return false
}
inIdx++
}
}
func metaRecordFilterInverted(metaRecordFilters []tagquery.MetricDefinitionFilter, defaultDecision tagquery.FilterDecision) tagquery.MetricDefinitionFilter {
return func(id schema.MKey, name string, tags []string) tagquery.FilterDecision {
for _, metaRecordFilter := range metaRecordFilters {
decision := metaRecordFilter(id, name, tags)
if decision == tagquery.None {
decision = defaultDecision
}
if decision == tagquery.Pass {
return tagquery.Fail
}
}
return tagquery.Pass
}
}
func metaRecordFilterNormal(metaRecordFilters []tagquery.MetricDefinitionFilter, defaultDecision tagquery.FilterDecision) tagquery.MetricDefinitionFilter {
return func(id schema.MKey, name string, tags []string) tagquery.FilterDecision {
for _, metaRecordFilter := range metaRecordFilters {
decision := metaRecordFilter(id, name, tags)
if decision == tagquery.None {
decision = defaultDecision
}
if decision == tagquery.Pass {
return tagquery.Pass
}
}
return tagquery.Fail
}
}
// matches takes descriptive properties of a metric definition and runs them through all
// filters required to come to a decision whether this metric definition should be part
// of the result or not.
// it uses the filter functions that have previously been generated when this instance
// of idFilter was instantiated
func (f *idFilter) matches(id schema.MKey, name string, tags []string) bool {
for i := range f.filters {
decision := f.filters[i].testByMetricTags(id, name, tags)
if decision == tagquery.Pass {
continue
}
if decision == tagquery.Fail {
return false
}
if f.filters[i].testByMetaTags != nil {
decision = f.filters[i].testByMetaTags(id, name, tags)
}
if decision == tagquery.None {
decision = f.filters[i].defaultDecision
}
if decision == tagquery.Pass {
continue
}
return false
}
return true
}