-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathmapping.go
449 lines (413 loc) · 12.7 KB
/
mapping.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
// 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 rules
import (
"errors"
"fmt"
"time"
"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/generated/proto/policypb"
"github.com/m3db/m3/src/metrics/generated/proto/rulepb"
"github.com/m3db/m3/src/metrics/policy"
"github.com/m3db/m3/src/metrics/rules/view"
"github.com/pborman/uuid"
)
const (
nanosPerMilli = int64(time.Millisecond / time.Nanosecond)
)
var (
errNoStoragePoliciesAndDropPolicyInMappingRuleSnapshot = errors.New("no storage policies and no drop policy in mapping rule snapshot")
errInvalidDropPolicyInMappRuleSnapshot = errors.New("invalid drop policy in mapping rule snapshot")
errStoragePoliciesAndDropPolicyInMappingRuleSnapshot = errors.New("storage policies and a drop policy specified in mapping rule snapshot")
errMappingRuleSnapshotIndexOutOfRange = errors.New("mapping rule snapshot index out of range")
errNilMappingRuleSnapshotProto = errors.New("nil mapping rule snapshot proto")
errNilMappingRuleProto = errors.New("nil mapping rule proto")
)
// mappingRuleSnapshot defines a rule snapshot such that if a metric matches the
// provided filters, it is aggregated and retained under the provided set of policies.
type mappingRuleSnapshot struct {
name string
tombstoned bool
cutoverNanos int64
filter filters.Filter
rawFilter string
aggregationID aggregation.ID
storagePolicies policy.StoragePolicies
dropPolicy policy.DropPolicy
lastUpdatedAtNanos int64
lastUpdatedBy string
}
func newMappingRuleSnapshotFromProto(
r *rulepb.MappingRuleSnapshot,
opts filters.TagsFilterOptions,
) (*mappingRuleSnapshot, error) {
if r == nil {
return nil, errNilMappingRuleSnapshotProto
}
var (
aggregationID aggregation.ID
storagePolicies policy.StoragePolicies
dropPolicy policy.DropPolicy
err error
)
if len(r.Policies) > 0 {
// Extract the aggregation ID and storage policies from v1 proto (i.e., policies list).
aggregationID, storagePolicies, err = toAggregationIDAndStoragePolicies(r.Policies)
if err != nil {
return nil, err
}
} else if len(r.StoragePolicies) > 0 {
// Unmarshal aggregation ID and storage policies directly from v2 proto.
aggregationID, err = aggregation.NewIDFromProto(r.AggregationTypes)
if err != nil {
return nil, err
}
storagePolicies, err = policy.NewStoragePoliciesFromProto(r.StoragePolicies)
if err != nil {
return nil, err
}
}
if r.DropPolicy != policypb.DropPolicy_NONE {
dropPolicy = policy.DropPolicy(r.DropPolicy)
if !dropPolicy.IsValid() {
return nil, errInvalidDropPolicyInMappRuleSnapshot
}
}
if !r.Tombstoned && len(storagePolicies) == 0 && dropPolicy == policy.DropNone {
return nil, errNoStoragePoliciesAndDropPolicyInMappingRuleSnapshot
}
if len(storagePolicies) > 0 && dropPolicy != policy.DropNone {
return nil, errStoragePoliciesAndDropPolicyInMappingRuleSnapshot
}
filterValues, err := filters.ParseTagFilterValueMap(r.Filter)
if err != nil {
return nil, err
}
filter, err := filters.NewTagsFilter(filterValues, filters.Conjunction, opts)
if err != nil {
return nil, err
}
return newMappingRuleSnapshotFromFieldsInternal(
r.Name,
r.Tombstoned,
r.CutoverNanos,
filter,
r.Filter,
aggregationID,
storagePolicies,
policy.DropPolicy(r.DropPolicy),
r.LastUpdatedAtNanos,
r.LastUpdatedBy,
), nil
}
func newMappingRuleSnapshotFromFields(
name string,
cutoverNanos int64,
filter filters.Filter,
rawFilter string,
aggregationID aggregation.ID,
storagePolicies policy.StoragePolicies,
dropPolicy policy.DropPolicy,
lastUpdatedAtNanos int64,
lastUpdatedBy string,
) (*mappingRuleSnapshot, error) {
if _, err := filters.ValidateTagsFilter(rawFilter); err != nil {
return nil, err
}
return newMappingRuleSnapshotFromFieldsInternal(
name,
false,
cutoverNanos,
filter,
rawFilter,
aggregationID,
storagePolicies,
dropPolicy,
lastUpdatedAtNanos,
lastUpdatedBy,
), nil
}
// newMappingRuleSnapshotFromFieldsInternal creates a new mapping rule snapshot
// from various given fields assuming the filter has already been validated.
func newMappingRuleSnapshotFromFieldsInternal(
name string,
tombstoned bool,
cutoverNanos int64,
filter filters.Filter,
rawFilter string,
aggregationID aggregation.ID,
storagePolicies policy.StoragePolicies,
dropPolicy policy.DropPolicy,
lastUpdatedAtNanos int64,
lastUpdatedBy string,
) *mappingRuleSnapshot {
return &mappingRuleSnapshot{
name: name,
tombstoned: tombstoned,
cutoverNanos: cutoverNanos,
filter: filter,
rawFilter: rawFilter,
aggregationID: aggregationID,
storagePolicies: storagePolicies,
dropPolicy: dropPolicy,
lastUpdatedAtNanos: lastUpdatedAtNanos,
lastUpdatedBy: lastUpdatedBy,
}
}
func (mrs *mappingRuleSnapshot) clone() mappingRuleSnapshot {
var filter filters.Filter
if mrs.filter != nil {
filter = mrs.filter.Clone()
}
return mappingRuleSnapshot{
name: mrs.name,
tombstoned: mrs.tombstoned,
cutoverNanos: mrs.cutoverNanos,
filter: filter,
rawFilter: mrs.rawFilter,
aggregationID: mrs.aggregationID,
storagePolicies: mrs.storagePolicies.Clone(),
dropPolicy: mrs.dropPolicy,
lastUpdatedAtNanos: mrs.lastUpdatedAtNanos,
lastUpdatedBy: mrs.lastUpdatedBy,
}
}
// proto returns the given MappingRuleSnapshot in protobuf form.
func (mrs *mappingRuleSnapshot) proto() (*rulepb.MappingRuleSnapshot, error) {
aggTypes, err := mrs.aggregationID.Types()
if err != nil {
return nil, err
}
pbAggTypes, err := aggTypes.Proto()
if err != nil {
return nil, err
}
storagePolicies, err := mrs.storagePolicies.Proto()
if err != nil {
return nil, err
}
return &rulepb.MappingRuleSnapshot{
Name: mrs.name,
Tombstoned: mrs.tombstoned,
CutoverNanos: mrs.cutoverNanos,
Filter: mrs.rawFilter,
LastUpdatedAtNanos: mrs.lastUpdatedAtNanos,
LastUpdatedBy: mrs.lastUpdatedBy,
AggregationTypes: pbAggTypes,
StoragePolicies: storagePolicies,
DropPolicy: policypb.DropPolicy(mrs.dropPolicy),
}, nil
}
// mappingRule stores mapping rule snapshots.
type mappingRule struct {
uuid string
snapshots []*mappingRuleSnapshot
}
func newEmptyMappingRule() *mappingRule {
return &mappingRule{uuid: uuid.New()}
}
func newMappingRuleFromProto(
mc *rulepb.MappingRule,
opts filters.TagsFilterOptions,
) (*mappingRule, error) {
if mc == nil {
return nil, errNilMappingRuleProto
}
snapshots := make([]*mappingRuleSnapshot, 0, len(mc.Snapshots))
for i := 0; i < len(mc.Snapshots); i++ {
mr, err := newMappingRuleSnapshotFromProto(mc.Snapshots[i], opts)
if err != nil {
return nil, err
}
snapshots = append(snapshots, mr)
}
return &mappingRule{
uuid: mc.Uuid,
snapshots: snapshots,
}, nil
}
func (mc *mappingRule) clone() mappingRule {
snapshots := make([]*mappingRuleSnapshot, len(mc.snapshots))
for i, s := range mc.snapshots {
c := s.clone()
snapshots[i] = &c
}
return mappingRule{
uuid: mc.uuid,
snapshots: snapshots,
}
}
// proto returns the given MappingRule in protobuf form.
func (mc *mappingRule) proto() (*rulepb.MappingRule, error) {
snapshots := make([]*rulepb.MappingRuleSnapshot, len(mc.snapshots))
for i, s := range mc.snapshots {
snapshot, err := s.proto()
if err != nil {
return nil, err
}
snapshots[i] = snapshot
}
return &rulepb.MappingRule{
Uuid: mc.uuid,
Snapshots: snapshots,
}, nil
}
// activeSnapshot returns the active rule snapshot whose cutover time is no later than
// the time passed in, or nil if no such rule snapshot exists.
func (mc *mappingRule) activeSnapshot(timeNanos int64) *mappingRuleSnapshot {
idx := mc.activeIndex(timeNanos)
if idx < 0 {
return nil
}
return mc.snapshots[idx]
}
// activeRule returns the rule containing snapshots that's in effect at time timeNanos
// and all future snapshots after time timeNanos.
func (mc *mappingRule) activeRule(timeNanos int64) *mappingRule {
idx := mc.activeIndex(timeNanos)
// If there are no snapshots that are currently in effect, it means either all
// snapshots are in the future, or there are no snapshots.
if idx < 0 {
return mc
}
return &mappingRule{
uuid: mc.uuid,
snapshots: mc.snapshots[idx:],
}
}
func (mc *mappingRule) name() (string, error) {
if len(mc.snapshots) == 0 {
return "", errNoRuleSnapshots
}
latest := mc.snapshots[len(mc.snapshots)-1]
return latest.name, nil
}
func (mc *mappingRule) tombstoned() bool {
if len(mc.snapshots) == 0 {
return true
}
latest := mc.snapshots[len(mc.snapshots)-1]
return latest.tombstoned
}
func (mc *mappingRule) addSnapshot(
name string,
rawFilter string,
aggregationID aggregation.ID,
storagePolicies policy.StoragePolicies,
dropPolicy policy.DropPolicy,
meta UpdateMetadata,
) error {
snapshot, err := newMappingRuleSnapshotFromFields(
name,
meta.cutoverNanos,
nil,
rawFilter,
aggregationID,
storagePolicies,
dropPolicy,
meta.updatedAtNanos,
meta.updatedBy,
)
if err != nil {
return err
}
mc.snapshots = append(mc.snapshots, snapshot)
return nil
}
func (mc *mappingRule) markTombstoned(meta UpdateMetadata) error {
n, err := mc.name()
if err != nil {
return err
}
if mc.tombstoned() {
return merrors.NewInvalidInputError(fmt.Sprintf("%s is already tombstoned", n))
}
if len(mc.snapshots) == 0 {
return errNoRuleSnapshots
}
snapshot := mc.snapshots[len(mc.snapshots)-1].clone()
snapshot.tombstoned = true
snapshot.cutoverNanos = meta.cutoverNanos
snapshot.lastUpdatedAtNanos = meta.updatedAtNanos
snapshot.lastUpdatedBy = meta.updatedBy
snapshot.aggregationID = aggregation.DefaultID
snapshot.storagePolicies = nil
snapshot.dropPolicy = 0
mc.snapshots = append(mc.snapshots, &snapshot)
return nil
}
func (mc *mappingRule) revive(
name string,
rawFilter string,
aggregationID aggregation.ID,
storagePolicies policy.StoragePolicies,
dropPolicy policy.DropPolicy,
meta UpdateMetadata,
) error {
n, err := mc.name()
if err != nil {
return err
}
if !mc.tombstoned() {
return merrors.NewInvalidInputError(fmt.Sprintf("%s is not tombstoned", n))
}
return mc.addSnapshot(name, rawFilter, aggregationID, storagePolicies,
dropPolicy, meta)
}
func (mc *mappingRule) activeIndex(timeNanos int64) int {
idx := len(mc.snapshots) - 1
for idx >= 0 && mc.snapshots[idx].cutoverNanos > timeNanos {
idx--
}
return idx
}
func (mc *mappingRule) history() ([]view.MappingRule, error) {
lastIdx := len(mc.snapshots) - 1
views := make([]view.MappingRule, len(mc.snapshots))
// Snapshots are stored oldest -> newest. History should start with newest.
for i := 0; i < len(mc.snapshots); i++ {
mrs, err := mc.mappingRuleView(lastIdx - i)
if err != nil {
return nil, err
}
views[i] = mrs
}
return views, nil
}
func (mc *mappingRule) mappingRuleView(snapshotIdx int) (view.MappingRule, error) {
if snapshotIdx < 0 || snapshotIdx >= len(mc.snapshots) {
return view.MappingRule{}, errMappingRuleSnapshotIndexOutOfRange
}
mrs := mc.snapshots[snapshotIdx].clone()
return view.MappingRule{
ID: mc.uuid,
Name: mrs.name,
Tombstoned: mrs.tombstoned,
CutoverMillis: mrs.cutoverNanos / nanosPerMilli,
DropPolicy: mrs.dropPolicy,
Filter: mrs.rawFilter,
AggregationID: mrs.aggregationID,
StoragePolicies: mrs.storagePolicies,
LastUpdatedBy: mrs.lastUpdatedBy,
LastUpdatedAtMillis: mrs.lastUpdatedAtNanos / nanosPerMilli,
}, nil
}