-
Notifications
You must be signed in to change notification settings - Fork 451
/
validator.go
503 lines (456 loc) · 17.4 KB
/
validator.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright (c) 2017 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 validator
import (
"errors"
"fmt"
"github.com/m3db/m3/src/metrics/aggregation"
merrors "github.com/m3db/m3/src/metrics/errors"
"github.com/m3db/m3/src/metrics/filters"
"github.com/m3db/m3/src/metrics/metric"
mpipeline "github.com/m3db/m3/src/metrics/pipeline"
"github.com/m3db/m3/src/metrics/policy"
"github.com/m3db/m3/src/metrics/rules"
"github.com/m3db/m3/src/metrics/rules/validator/namespace"
"github.com/m3db/m3/src/metrics/rules/view"
)
var (
errNoStoragePolicies = errors.New("no storage policies")
errEmptyRollupMetricName = errors.New("empty rollup metric name")
errEmptyPipeline = errors.New("empty pipeline")
errMoreThanOneAggregationOpInPipeline = errors.New("more than one aggregation operation in pipeline")
errAggregationOpNotFirstInPipeline = errors.New("aggregation operation is not the first operation in pipeline")
errNoRollupOpInPipeline = errors.New("no rollup operation in pipeline")
)
type validator struct {
opts Options
nsValidator namespace.Validator
}
// NewValidator creates a new validator.
func NewValidator(opts Options) rules.Validator {
return &validator{
opts: opts,
nsValidator: opts.NamespaceValidator(),
}
}
func (v *validator) Validate(rs rules.RuleSet) error {
// Only the latest (a.k.a. the first) view needs to be validated
// because that is the view that may be invalid due to latest update.
latest, err := rs.Latest()
if err != nil {
return v.wrapError(fmt.Errorf("could not get the latest ruleset snapshot: %v", err))
}
return v.ValidateSnapshot(latest)
}
func (v *validator) ValidateSnapshot(snapshot view.RuleSet) error {
if err := v.validateSnapshot(snapshot); err != nil {
return v.wrapError(err)
}
return nil
}
func (v *validator) Close() {
v.nsValidator.Close()
}
func (v *validator) validateSnapshot(snapshot view.RuleSet) error {
if err := v.validateNamespace(snapshot.Namespace); err != nil {
return err
}
if err := v.validateMappingRules(snapshot.MappingRules); err != nil {
return err
}
return v.validateRollupRules(snapshot.RollupRules)
}
func (v *validator) validateNamespace(ns string) error {
return v.nsValidator.Validate(ns)
}
func (v *validator) validateMappingRules(mrv []view.MappingRule) error {
namesSeen := make(map[string]struct{}, len(mrv))
for _, rule := range mrv {
if rule.Tombstoned {
continue
}
// Validate that no rules with the same name exist.
if _, exists := namesSeen[rule.Name]; exists {
return merrors.NewInvalidInputError(fmt.Sprintf("mapping rule '%s' already exists", rule.Name))
}
namesSeen[rule.Name] = struct{}{}
// Validate that the filter is valid.
filterValues, err := v.validateFilter(rule.Filter)
if err != nil {
return fmt.Errorf("mapping rule '%s' has invalid filter %s: %v", rule.Name, rule.Filter, err)
}
// Validate the metric types.
types, err := v.opts.MetricTypesFn()(filterValues)
if err != nil {
return fmt.Errorf("mapping rule '%s' cannot infer metric types from filter %v: %v", rule.Name, rule.Filter, err)
}
if len(types) == 0 {
return fmt.Errorf("mapping rule '%s' does not match any allowed metric types, filter=%s", rule.Name, rule.Filter)
}
// Validate the aggregation ID.
if err := v.validateAggregationID(rule.AggregationID, firstLevelAggregationType, types); err != nil {
return fmt.Errorf("mapping rule '%s' has invalid aggregation ID %v: %v", rule.Name, rule.AggregationID, err)
}
// Validate the drop policy is valid.
if !rule.DropPolicy.IsValid() {
return fmt.Errorf("mapping rule '%s' has an invalid drop policy: value=%d, string=%s, valid_values=%v",
rule.Name, int(rule.DropPolicy), rule.DropPolicy.String(), policy.ValidDropPolicies())
}
// Validate the storage policies if drop policy not active, otherwise ensure none.
if rule.DropPolicy.IsDefault() {
// Drop policy not set, validate that the storage policies are valid.
if err := v.validateStoragePolicies(rule.StoragePolicies, types); err != nil {
return fmt.Errorf("mapping rule '%s' has invalid storage policies in %v: %v", rule.Name, rule.StoragePolicies, err)
}
} else {
// Drop policy is set, ensure default aggregation ID and no storage policies set.
if !rule.AggregationID.IsDefault() {
return fmt.Errorf("mapping rule '%s' has a drop policy error: must use default aggregation ID", rule.Name)
}
if len(rule.StoragePolicies) != 0 {
return fmt.Errorf("mapping rule '%s' has a drop policy error: cannot specify storage policies", rule.Name)
}
}
}
return nil
}
func (v *validator) validateRollupRules(rrv []view.RollupRule) error {
var (
namesSeen = make(map[string]struct{}, len(rrv))
pipelines = make([]mpipeline.Pipeline, 0, len(rrv))
)
for _, rule := range rrv {
if rule.Tombstoned {
continue
}
// Validate that no rules with the same name exist.
if _, exists := namesSeen[rule.Name]; exists {
return merrors.NewInvalidInputError(fmt.Sprintf("rollup rule '%s' already exists", rule.Name))
}
namesSeen[rule.Name] = struct{}{}
// Validate that the filter is valid.
filterValues, err := v.validateFilter(rule.Filter)
if err != nil {
return fmt.Errorf("rollup rule '%s' has invalid filter %s: %v", rule.Name, rule.Filter, err)
}
// Validate the metric types.
types, err := v.opts.MetricTypesFn()(filterValues)
if err != nil {
return fmt.Errorf("rollup rule '%s' cannot infer metric types from filter %v: %v", rule.Name, rule.Filter, err)
}
if len(types) == 0 {
return fmt.Errorf("rollup rule '%s' does not match any allowed metric types, filter=%s", rule.Name, rule.Filter)
}
for _, target := range rule.Targets {
// Validate the pipeline is valid.
if err := v.validatePipeline(target.Pipeline, types); err != nil {
return fmt.Errorf("rollup rule '%s' has invalid pipeline '%v': %v", rule.Name, target.Pipeline, err)
}
// Validate that the storage policies are valid.
if err := v.validateStoragePolicies(target.StoragePolicies, types); err != nil {
return fmt.Errorf("rollup rule '%s' has invalid storage policies in %v: %v", rule.Name, target.StoragePolicies, err)
}
pipelines = append(pipelines, target.Pipeline)
}
}
return validateNoDuplicateRollupIDIn(pipelines)
}
func (v *validator) validateFilter(f string) (filters.TagFilterValueMap, error) {
filterValues, err := filters.ValidateTagsFilter(f)
if err != nil {
return nil, err
}
for tag := range filterValues {
// Validating the filter tag name does not contain invalid chars.
if err := v.opts.CheckInvalidCharactersForTagName(tag); err != nil {
return nil, fmt.Errorf("tag name '%s' contains invalid character, err: %v", tag, err)
}
if err := v.opts.CheckFilterTagNameValid(tag); err != nil {
return nil, err
}
}
return filterValues, nil
}
func (v *validator) validateAggregationID(
aggregationID aggregation.ID,
aggregationType aggregationType,
types []metric.Type,
) error {
// Default aggregation types are always allowed.
if aggregationID.IsDefault() {
return nil
}
aggTypes, err := aggregationID.Types()
if err != nil {
return err
}
if len(aggTypes) > 1 {
for _, t := range types {
if !v.opts.IsMultiAggregationTypesEnabledFor(t) {
return fmt.Errorf("metric type %v does not support multiple aggregation types %v", t, aggTypes)
}
}
}
isAllowedAggregationTypeForFn := v.opts.IsAllowedFirstLevelAggregationTypeFor
if aggregationType == nonFirstLevelAggregationType {
isAllowedAggregationTypeForFn = v.opts.IsAllowedNonFirstLevelAggregationTypeFor
}
for _, t := range types {
for _, aggType := range aggTypes {
if !isAllowedAggregationTypeForFn(t, aggType) {
return fmt.Errorf("aggregation type %v is not allowed for metric type %v", aggType, t)
}
}
}
return nil
}
func (v *validator) validateStoragePolicies(
storagePolicies policy.StoragePolicies,
types []metric.Type,
) error {
// Validating that at least one storage policy is provided.
if len(storagePolicies) == 0 {
return errNoStoragePolicies
}
// Validating that no duplicate storage policies exist.
seen := make(map[policy.StoragePolicy]struct{}, len(storagePolicies))
for _, sp := range storagePolicies {
if _, exists := seen[sp]; exists {
return fmt.Errorf("duplicate storage policy '%s'", sp.String())
}
seen[sp] = struct{}{}
}
// Validating that provided storage policies are allowed for the specified metric type.
for _, t := range types {
for _, sp := range storagePolicies {
if !v.opts.IsAllowedStoragePolicyFor(t, sp) {
return fmt.Errorf("storage policy '%s' is not allowed for metric type %v", sp.String(), t)
}
}
}
return nil
}
// validatePipeline validates the rollup pipeline as follows:
// * The pipeline must contain at least one operation.
// * The pipeline can contain at most one aggregation operation, and if there is one,
// it must be the first operation.
// * The pipeline can contain arbitrary number of transformation operations. However,
// the transformation derivative order computed from the list of transformations must
// be no more than the maximum transformation derivative order that is supported.
// * The pipeline must contain at least one rollup operation and at most `n` rollup operations,
// where `n` is the maximum supported number of rollup levels.
func (v *validator) validatePipeline(pipeline mpipeline.Pipeline, types []metric.Type) error {
if pipeline.IsEmpty() {
return errEmptyPipeline
}
var (
numAggregationOps int
transformationDerivativeOrder int
numRollupOps int
previousRollupTags map[string]struct{}
numPipelineOps = pipeline.Len()
)
for i := 0; i < numPipelineOps; i++ {
pipelineOp := pipeline.At(i)
switch pipelineOp.Type {
case mpipeline.AggregationOpType:
numAggregationOps++
if numAggregationOps > 1 {
return errMoreThanOneAggregationOpInPipeline
}
if i != 0 {
return errAggregationOpNotFirstInPipeline
}
if err := v.validateAggregationOp(pipelineOp.Aggregation, types); err != nil {
return fmt.Errorf("invalid aggregation operation at index %d: %v", i, err)
}
case mpipeline.TransformationOpType:
transformOp := pipelineOp.Transformation
if transformOp.Type.IsBinaryTransform() {
transformationDerivativeOrder++
if transformationDerivativeOrder > v.opts.MaxTransformationDerivativeOrder() {
return fmt.Errorf("transformation derivative order is %d higher than supported %d", transformationDerivativeOrder, v.opts.MaxTransformationDerivativeOrder())
}
}
if err := validateTransformationOp(transformOp); err != nil {
return fmt.Errorf("invalid transformation operation at index %d: %v", i, err)
}
case mpipeline.RollupOpType:
// We only care about the derivative order of transformation operations in between
// two consecutive rollup operations and as such we reset the derivative order when
// encountering a rollup operation.
transformationDerivativeOrder = 0
numRollupOps++
if numRollupOps > v.opts.MaxRollupLevels() {
return fmt.Errorf("number of rollup levels is %d higher than supported %d", numRollupOps, v.opts.MaxRollupLevels())
}
if err := v.validateRollupOp(pipelineOp.Rollup, i, types, previousRollupTags); err != nil {
return fmt.Errorf("invalid rollup operation at index %d: %v", i, err)
}
previousRollupTags = make(map[string]struct{}, len(pipelineOp.Rollup.Tags))
for _, tag := range pipelineOp.Rollup.Tags {
previousRollupTags[string(tag)] = struct{}{}
}
default:
return fmt.Errorf("operation at index %d has invalid type: %v", i, pipelineOp.Type)
}
}
if numRollupOps == 0 {
return errNoRollupOpInPipeline
}
return nil
}
func (v *validator) validateAggregationOp(
aggregationOp mpipeline.AggregationOp,
types []metric.Type,
) error {
aggregationID, err := aggregation.CompressTypes(aggregationOp.Type)
if err != nil {
return err
}
return v.validateAggregationID(aggregationID, firstLevelAggregationType, types)
}
func validateTransformationOp(transformationOp mpipeline.TransformationOp) error {
if !transformationOp.Type.IsValid() {
return fmt.Errorf("invalid transformation type: %v", transformationOp.Type)
}
return nil
}
func (v *validator) validateRollupOp(
rollupOp mpipeline.RollupOp,
opIdxInPipeline int,
types []metric.Type,
previousRollupTags map[string]struct{},
) error {
// Validate that the rollup metric name is valid.
if err := v.validateRollupMetricName(rollupOp.NewName); err != nil {
return fmt.Errorf("invalid rollup metric name '%s': %v", rollupOp.NewName, err)
}
// Validate that the rollup tags are valid.
if err := v.validateRollupTags(rollupOp.Tags, previousRollupTags); err != nil {
return fmt.Errorf("invalid rollup tags %v: %v", rollupOp.Tags, err)
}
// Validate that the aggregation ID is valid.
aggType := firstLevelAggregationType
if opIdxInPipeline > 0 {
aggType = nonFirstLevelAggregationType
}
if err := v.validateAggregationID(rollupOp.AggregationID, aggType, types); err != nil {
return fmt.Errorf("invalid aggregation ID %v: %v", rollupOp.AggregationID, err)
}
return nil
}
func (v *validator) validateRollupMetricName(metricName []byte) error {
// Validate that rollup metric name is not empty.
if len(metricName) == 0 {
return errEmptyRollupMetricName
}
// Validate that rollup metric name has valid characters.
return v.opts.CheckInvalidCharactersForMetricName(string(metricName))
}
func (v *validator) validateRollupTags(
tags [][]byte,
previousRollupTags map[string]struct{},
) error {
// Validating that all tag names have valid characters.
for _, tag := range tags {
if err := v.opts.CheckInvalidCharactersForTagName(string(tag)); err != nil {
return fmt.Errorf("invalid rollup tag '%s': %v", tag, err)
}
}
// Validating that there are no duplicate rollup tags.
rollupTags := make(map[string]struct{}, len(tags))
for _, tag := range tags {
tagStr := string(tag)
if _, exists := rollupTags[tagStr]; exists {
return fmt.Errorf("duplicate rollup tag: '%s'", tagStr)
}
rollupTags[tagStr] = struct{}{}
}
// Validate that the set of rollup tags are a strict subset of those in
// previous rollup operations.
// NB: `previousRollupTags` is nil for the first rollup operation.
if previousRollupTags != nil {
var numSeenTags int
for _, tag := range tags {
if _, exists := previousRollupTags[string(tag)]; !exists {
return fmt.Errorf("tag %s not found in previous rollup operations", tag)
}
numSeenTags++
}
if numSeenTags == len(previousRollupTags) {
return fmt.Errorf("same set of %d rollup tags in consecutive rollup operations", numSeenTags)
}
}
// Validating the list of rollup tags in the rule contain all required tags.
requiredTags := v.opts.RequiredRollupTags()
if len(requiredTags) == 0 {
return nil
}
for _, requiredTag := range requiredTags {
if _, exists := rollupTags[requiredTag]; !exists {
return fmt.Errorf("missing required rollup tag: '%s'", requiredTag)
}
}
return nil
}
func validateNoDuplicateRollupIDIn(pipelines []mpipeline.Pipeline) error {
rollupOps := make([]mpipeline.RollupOp, 0, len(pipelines))
for _, pipeline := range pipelines {
numOps := pipeline.Len()
for i := 0; i < numOps; i++ {
pipelineOp := pipeline.At(i)
if pipelineOp.Type != mpipeline.RollupOpType {
continue
}
rollupOp := pipelineOp.Rollup
for _, existing := range rollupOps {
if rollupOp.SameTransform(existing) {
return merrors.NewInvalidInputError(fmt.Sprintf("more than one rollup operations with name '%s' and tags '%s' exist", rollupOp.NewName, rollupOp.Tags))
}
}
rollupOps = append(rollupOps, rollupOp)
}
}
return nil
}
func (v *validator) wrapError(err error) error {
if err == nil {
return nil
}
switch err.(type) {
// Do not wrap error for these error types so caller can take actions
// based on the correct error type.
case merrors.InvalidInputError, merrors.ValidationError:
return err
default:
return merrors.NewValidationError(err.Error())
}
}
type aggregationType int
const (
// First-level aggregation refers to the aggregation operation performed as the first
// step of metrics processing, such as the aggregations specified by a mapping rule,
// or those specified by the first operation in a rollup pipeline.
firstLevelAggregationType aggregationType = iota
// Non-first-level aggregation refers to the aggregation operation performed as the
// second step or later step of a rollup pipeline.
nonFirstLevelAggregationType
)