-
Notifications
You must be signed in to change notification settings - Fork 453
/
index.go
281 lines (234 loc) · 7.27 KB
/
index.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package storage
import (
"fmt"
"github.com/m3db/m3/src/dbnode/storage/index"
"github.com/m3db/m3/src/m3ninx/idx"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/storage/m3/consolidators"
"github.com/m3db/m3/src/x/ident"
)
const (
dot = byte('.')
plus = byte('+')
star = byte('*')
)
// FromM3IdentToMetric converts an M3 ident metric to a coordinator metric.
func FromM3IdentToMetric(
identID ident.ID,
iterTags ident.TagIterator,
tagOptions models.TagOptions,
) (models.Metric, error) {
tags, err := consolidators.FromIdentTagIteratorToTags(iterTags, tagOptions)
if err != nil {
return models.Metric{}, err
}
return models.Metric{
ID: identID.Bytes(),
Tags: tags,
}, nil
}
// TagsToIdentTagIterator converts coordinator tags to ident tags.
func TagsToIdentTagIterator(tags models.Tags) ident.TagIterator {
// TODO: get a tags and tag iterator from an ident.Pool here rather than allocing them here
identTags := make([]ident.Tag, 0, tags.Len())
for _, t := range tags.Tags {
identTags = append(identTags, ident.Tag{
Name: ident.BytesID(t.Name),
Value: ident.BytesID(t.Value),
})
}
return ident.NewTagsIterator(ident.NewTags(identTags...))
}
// FetchOptionsToM3Options converts a set of coordinator options to M3 options.
func FetchOptionsToM3Options(fetchOptions *FetchOptions, fetchQuery *FetchQuery) index.QueryOptions {
return index.QueryOptions{
SeriesLimit: fetchOptions.SeriesLimit,
DocsLimit: fetchOptions.DocsLimit,
RequireExhaustive: fetchOptions.RequireExhaustive,
StartInclusive: fetchQuery.Start,
EndExclusive: fetchQuery.End,
}
}
func convertAggregateQueryType(completeNameOnly bool) index.AggregationType {
if completeNameOnly {
return index.AggregateTagNames
}
return index.AggregateTagNamesAndValues
}
// FetchOptionsToAggregateOptions converts a set of coordinator options as well
// as complete tags query to an M3 aggregate query option.
func FetchOptionsToAggregateOptions(
fetchOptions *FetchOptions,
tagQuery *CompleteTagsQuery,
) index.AggregationOptions {
return index.AggregationOptions{
QueryOptions: index.QueryOptions{
SeriesLimit: fetchOptions.SeriesLimit,
DocsLimit: fetchOptions.DocsLimit,
StartInclusive: tagQuery.Start,
EndExclusive: tagQuery.End,
},
FieldFilter: tagQuery.FilterNameTags,
Type: convertAggregateQueryType(tagQuery.CompleteNameOnly),
}
}
// FetchQueryToM3Query converts an m3coordinator fetch query to an M3 query.
func FetchQueryToM3Query(
fetchQuery *FetchQuery,
options *FetchOptions,
) (index.Query, error) {
fetchQuery = fetchQuery.WithAppliedOptions(options)
matchers := fetchQuery.TagMatchers
// If no matchers provided, explicitly set this to an AllQuery.
if len(matchers) == 0 {
return index.Query{
Query: idx.NewAllQuery(),
}, nil
}
// Optimization for single matcher case.
if len(matchers) == 1 {
specialCase := isSpecialCaseMatcher(matchers[0])
if specialCase.skip {
// NB: only matcher has no effect; this is synonymous to an AllQuery.
return index.Query{
Query: idx.NewAllQuery(),
}, nil
}
if specialCase.isSpecial {
return index.Query{Query: specialCase.query}, nil
}
q, err := matcherToQuery(matchers[0])
if err != nil {
return index.Query{}, err
}
return index.Query{Query: q}, nil
}
idxQueries := make([]idx.Query, 0, len(matchers))
for _, matcher := range matchers {
specialCase := isSpecialCaseMatcher(matcher)
if specialCase.skip {
continue
}
if specialCase.isSpecial {
idxQueries = append(idxQueries, specialCase.query)
continue
}
q, err := matcherToQuery(matcher)
if err != nil {
return index.Query{}, err
}
idxQueries = append(idxQueries, q)
}
q := idx.NewConjunctionQuery(idxQueries...)
return index.Query{Query: q}, nil
}
type specialCase struct {
query idx.Query
isSpecial bool
skip bool
}
func isSpecialCaseMatcher(matcher models.Matcher) specialCase {
if len(matcher.Value) == 0 {
if matcher.Type == models.MatchNotRegexp ||
matcher.Type == models.MatchNotEqual {
query := idx.NewFieldQuery(matcher.Name)
return specialCase{query: query, isSpecial: true}
}
if matcher.Type == models.MatchRegexp ||
matcher.Type == models.MatchEqual {
query := idx.NewNegationQuery(idx.NewFieldQuery(matcher.Name))
return specialCase{query: query, isSpecial: true}
}
return specialCase{}
}
// NB: no special case for regex / not regex here.
isNegatedRegex := matcher.Type == models.MatchNotRegexp
isRegex := matcher.Type == models.MatchRegexp
if !isNegatedRegex && !isRegex {
return specialCase{}
}
if len(matcher.Value) != 2 || matcher.Value[0] != dot {
return specialCase{}
}
if matcher.Value[1] == star {
if isNegatedRegex {
// NB: This should match no results.
query := idx.NewNegationQuery(idx.NewAllQuery())
return specialCase{query: query, isSpecial: true}
}
// NB: this matcher should not affect query results.
return specialCase{skip: true}
}
if matcher.Value[1] == plus {
query := idx.NewFieldQuery(matcher.Name)
if isNegatedRegex {
query = idx.NewNegationQuery(query)
}
return specialCase{query: query, isSpecial: true}
}
return specialCase{}
}
func matcherToQuery(matcher models.Matcher) (idx.Query, error) {
negate := false
switch matcher.Type {
// Support for Regexp types
case models.MatchNotRegexp:
negate = true
fallthrough
case models.MatchRegexp:
var (
query idx.Query
err error
)
query, err = idx.NewRegexpQuery(matcher.Name, matcher.Value)
if err != nil {
return idx.Query{}, err
}
if negate {
query = idx.NewNegationQuery(query)
}
return query, nil
// Support exact matches
case models.MatchNotEqual:
negate = true
fallthrough
case models.MatchEqual:
query := idx.NewTermQuery(matcher.Name, matcher.Value)
if negate {
query = idx.NewNegationQuery(query)
}
return query, nil
case models.MatchNotField:
negate = true
fallthrough
case models.MatchField:
query := idx.NewFieldQuery(matcher.Name)
if negate {
query = idx.NewNegationQuery(query)
}
return query, nil
case models.MatchAll:
return idx.NewAllQuery(), nil
default:
return idx.Query{}, fmt.Errorf("unsupported query type: %v", matcher)
}
}