This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
tag_query.go
226 lines (193 loc) · 7.13 KB
/
tag_query.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
package memory
import (
"sort"
"sync"
"sync/atomic"
"github.com/grafana/metrictank/schema"
"github.com/grafana/metrictank/expr/tagquery"
"github.com/grafana/metrictank/idx"
log "github.com/sirupsen/logrus"
)
// TagQueryContext runs a set of pattern or string matches on tag keys and values against
// the index. It is executed via:
// Run() which returns a set of matching MetricIDs
// RunGetTags() which returns a list of tags of the matching metrics
type TagQueryContext struct {
wg sync.WaitGroup
query tagquery.Query
selector *idSelector
filter *idFilter
index TagIndex // the tag index, hierarchy of tags & values, set by Run()/RunGetTags()
byId map[schema.MKey]*idx.Archive // the metric index by ID, set by Run()/RunGetTags()
metaTagIndex *metaTagHierarchy // the meta tag index
metaTagRecords *metaTagRecords // meta tag records keyed by their recordID
startWith int // the expression index to start with
subQuery bool // true if this is a subquery created from the expressions of a meta tag record
}
// NewTagQueryContext takes a tag query and wraps it into all the
// context structs necessary to execute the query on the indexes
func NewTagQueryContext(query tagquery.Query) TagQueryContext {
ctx := TagQueryContext{
query: query,
startWith: -1,
}
return ctx
}
type expressionCost struct {
operatorCost uint32
cardinality uint32
metaTag bool
expressionIdx int
}
// newerThanFrom takes a metric key, it returns true if the lastUpdate
// property of the metric associated with that key is at least equal
// to this queries' from timestamp.
// if the key doesn't exist it returns false
func (q *TagQueryContext) newerThanFrom(id schema.MKey) bool {
md, ok := q.byId[id]
if !ok {
return false
}
return atomic.LoadInt64(&md.LastUpdate) >= q.query.From
}
func (q *TagQueryContext) useMetaTagIndex() bool {
// if this is a sub query we want to ignore the meta tag index,
// otherwise we'd risk to create a loop of sub queries creating
// each other
return MetaTagSupport && !q.subQuery && q.metaTagIndex != nil && q.metaTagRecords != nil
}
func (q *TagQueryContext) evaluateExpressionCosts() []expressionCost {
costs := make([]expressionCost, len(q.query.Expressions))
var metaTagWg sync.WaitGroup
if q.useMetaTagIndex() {
metaTagWg.Add(1)
go func() {
defer metaTagWg.Done()
q.metaTagIndex.updateExpressionCosts(costs, q.query.Expressions)
}()
}
for i, expr := range q.query.Expressions {
costs[i].expressionIdx = i
costs[i].operatorCost = expr.GetOperatorCost()
if expr.OperatesOnTag() {
if expr.MatchesExactly() {
costs[i].cardinality = uint32(len(q.index[expr.GetKey()]))
} else {
costs[i].cardinality = uint32(len(q.index))
}
} else {
if expr.MatchesExactly() {
costs[i].cardinality = uint32(len(q.index[expr.GetKey()][expr.GetValue()]))
} else {
costs[i].cardinality = uint32(len(q.index[expr.GetKey()]))
}
}
}
// wait for meta tag index to update expression costs
metaTagWg.Wait()
sort.Slice(costs, func(i, j int) bool {
// if one of the two is a meta tag, but the other isn't, then we always
// want to move the meta tag to the back
if costs[i].metaTag != costs[j].metaTag {
if costs[i].metaTag {
return false
}
return true
}
if costs[i].operatorCost == costs[j].operatorCost {
return costs[i].cardinality < costs[j].cardinality
}
return costs[i].operatorCost < costs[j].operatorCost
})
return costs
}
func (q *TagQueryContext) prepareExpressions() {
costs := q.evaluateExpressionCosts()
// the number of filters is equal to the number of expressions - 1 because one of the
// expressions will be chosen to be the one that we start with.
// we don't need the filter function, nor the default decision, of the expression which
// we start with.
// all the remaining expressions will be used as filter expressions, for which we need
// to obtain their filter functions and their default decisions.
filterExpressions := make([]tagquery.Expression, 0, len(q.query.Expressions)-1)
// Every tag query has at least one expression which requires a non-empty value according to:
// https://graphite.readthedocs.io/en/latest/tags.html#querying
// This rule is enforced by tagquery.NewQuery, here we trust that the queries which get passed
// into the index have already been validated
for _, cost := range costs {
if q.startWith < 0 && q.query.Expressions[cost.expressionIdx].RequiresNonEmptyValue() {
q.startWith = cost.expressionIdx
} else {
filterExpressions = append(filterExpressions, q.query.Expressions[cost.expressionIdx])
}
}
q.selector = newIdSelector(q.query.Expressions[q.startWith], q)
if len(filterExpressions) > 0 {
q.filter = newIdFilter(filterExpressions, q)
}
}
// testByFrom filters a given metric by its LastUpdate time
func (q *TagQueryContext) testByFrom(def *idx.Archive) bool {
return q.query.From <= atomic.LoadInt64(&def.LastUpdate)
}
// filterIdsFromChan takes a channel of metric ids and runs them through the
// required tests to decide whether a metric should be part of the final
// result set or not
// it returns the final result set via the given resCh parameter
func (q *TagQueryContext) filterIdsFromChan(idCh, resCh chan schema.MKey) {
for id := range idCh {
var def *idx.Archive
var ok bool
if def, ok = q.byId[id]; !ok {
// should never happen because every ID in the tag index
// must be present in the byId lookup table
corruptIndex.Inc()
log.Errorf("memory-idx: ID %q is in tag index but not in the byId lookup table", id)
continue
}
if q.testByFrom(def) && q.filter.matches(id, schema.SanitizeNameAsTagValue(def.Name), def.Tags) {
resCh <- id
}
}
q.wg.Done()
}
// Run executes this query on the given indexes and passes the results into the given result channel.
// It blocks until query execution is finished, but it does not close the result channel.
func (q *TagQueryContext) Run(index TagIndex, byId map[schema.MKey]*idx.Archive, mti *metaTagHierarchy, mtr *metaTagRecords, resCh chan schema.MKey) {
q.index = index
q.byId = byId
q.metaTagIndex = mti
q.metaTagRecords = mtr
q.prepareExpressions()
// no initial expression has been chosen, returning empty result
if q.startWith < 0 || q.startWith >= len(q.query.Expressions) {
return
}
// if this query needs to filter down the initial result set, which is
// only the case if the number of expressions is >1, then we start filter
// workers to apply the filter functions
if q.filter != nil {
idCh := make(chan schema.MKey)
q.wg.Add(1)
go func() {
defer q.wg.Done()
q.selector.getIds(idCh, nil)
close(idCh)
}()
// start the tag query workers. they'll consume the ids on the idCh and
// evaluate for each of them whether it satisfies all the conditions
// defined in the query expressions. those that satisfy all conditions
// will be pushed into the resCh
q.wg.Add(TagQueryWorkers)
for i := 0; i < TagQueryWorkers; i++ {
go q.filterIdsFromChan(idCh, resCh)
}
} else {
q.wg.Add(1)
go func() {
defer q.wg.Done()
q.selector.getIds(resCh, nil)
}()
}
q.wg.Wait()
}