-
Notifications
You must be signed in to change notification settings - Fork 453
/
ruleset.go
671 lines (593 loc) · 19.2 KB
/
ruleset.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// 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"
"math"
"sort"
"time"
"github.com/m3db/m3/src/cluster/kv"
merrors "github.com/m3db/m3/src/metrics/errors"
"github.com/m3db/m3/src/metrics/filters"
"github.com/m3db/m3/src/metrics/generated/proto/rulepb"
metricid "github.com/m3db/m3/src/metrics/metric/id"
"github.com/m3db/m3/src/metrics/rules/view"
"github.com/m3db/m3/src/metrics/rules/view/changes"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/pborman/uuid"
)
const (
timeNanosMax = int64(math.MaxInt64)
)
var (
errNilRuleSetProto = errors.New("nil rule set proto")
errRuleSetNotTombstoned = errors.New("ruleset is not tombstoned")
errRuleNotFound = errors.New("rule not found")
errNoRuleSnapshots = errors.New("rule has no snapshots")
ruleIDNotFoundErrorFmt = "no rule with id %v"
ruleActionErrorFmt = "cannot %s rule %s"
ruleSetActionErrorFmt = "cannot %s ruleset %s"
unknownOpTypeFmt = "unknown op type %v"
)
// RuleSet is a read-only set of rules associated with a namespace.
type RuleSet interface {
// Namespace is the metrics namespace the ruleset applies to.
Namespace() []byte
// Version returns the ruleset version.
Version() int
// CutoverNanos returns when the ruleset takes effect.
CutoverNanos() int64
// TombStoned returns whether the ruleset is tombstoned.
Tombstoned() bool
// CreatedAtNanos returns the creation time for this ruleset.
CreatedAtNanos() int64
// LastUpdatedAtNanos returns the time when this ruleset was last updated.
LastUpdatedAtNanos() int64
// Proto returns the rulepb.Ruleset representation of this ruleset.
Proto() (*rulepb.RuleSet, error)
// MappingRuleHistory returns a map of mapping rule id to states that rule has been in.
MappingRules() (view.MappingRules, error)
// RollupRuleHistory returns a map of rollup rule id to states that rule has been in.
RollupRules() (view.RollupRules, error)
// Latest returns the latest snapshot of a ruleset containing the latest snapshots
// of each rule in the ruleset.
Latest() (view.RuleSet, error)
// ActiveSet returns the active ruleset at a given time.
ActiveSet(timeNanos int64) Matcher
// ToMutableRuleSet returns a mutable version of this ruleset.
ToMutableRuleSet() MutableRuleSet
}
// MutableRuleSet is mutable ruleset.
type MutableRuleSet interface {
RuleSet
// Clone returns a copy of this MutableRuleSet.
Clone() MutableRuleSet
// AppendMappingRule creates a new mapping rule and adds it to this ruleset.
// Should return the id of the newly created rule.
AddMappingRule(view.MappingRule, UpdateMetadata) (string, error)
// UpdateMappingRule creates a new mapping rule and adds it to this ruleset.
UpdateMappingRule(view.MappingRule, UpdateMetadata) error
// DeleteMappingRule deletes a mapping rule
DeleteMappingRule(string, UpdateMetadata) error
// AppendRollupRule creates a new rollup rule and adds it to this ruleset.
// Should return the id of the newly created rule.
AddRollupRule(view.RollupRule, UpdateMetadata) (string, error)
// UpdateRollupRule creates a new rollup rule and adds it to this ruleset.
UpdateRollupRule(view.RollupRule, UpdateMetadata) error
// DeleteRollupRule deletes a rollup rule
DeleteRollupRule(string, UpdateMetadata) error
// Tombstone tombstones this ruleset and all of its rules.
Delete(UpdateMetadata) error
// Revive removes the tombstone from this ruleset. It does not revive any rules.
Revive(UpdateMetadata) error
// ApplyRuleSetChanges takes set of rule set changes and applies them to a ruleset.
ApplyRuleSetChanges(changes.RuleSetChanges, UpdateMetadata) error
}
type ruleSet struct {
uuid string
version int
namespace []byte
createdAtNanos int64
lastUpdatedAtNanos int64
lastUpdatedBy string
tombstoned bool
cutoverNanos int64
mappingRules []*mappingRule
rollupRules []*rollupRule
tagsFilterOpts filters.TagsFilterOptions
newRollupIDFn metricid.NewIDFn
isRollupIDFn metricid.MatchIDFn
}
// NewRuleSetFromProto creates a new RuleSet from a proto object.
func NewRuleSetFromProto(version int, rs *rulepb.RuleSet, opts Options) (RuleSet, error) {
if rs == nil {
return nil, errNilRuleSetProto
}
tagsFilterOpts := opts.TagsFilterOptions()
mappingRules := make([]*mappingRule, 0, len(rs.MappingRules))
for _, mappingRule := range rs.MappingRules {
mc, err := newMappingRuleFromProto(mappingRule, tagsFilterOpts)
if err != nil {
return nil, err
}
mappingRules = append(mappingRules, mc)
}
rollupRules := make([]*rollupRule, 0, len(rs.RollupRules))
for _, rollupRule := range rs.RollupRules {
rc, err := newRollupRuleFromProto(rollupRule, tagsFilterOpts)
if err != nil {
return nil, err
}
rollupRules = append(rollupRules, rc)
}
return &ruleSet{
uuid: rs.Uuid,
version: version,
namespace: []byte(rs.Namespace),
createdAtNanos: rs.CreatedAtNanos,
lastUpdatedAtNanos: rs.LastUpdatedAtNanos,
lastUpdatedBy: rs.LastUpdatedBy,
tombstoned: rs.Tombstoned,
cutoverNanos: rs.CutoverNanos,
mappingRules: mappingRules,
rollupRules: rollupRules,
tagsFilterOpts: tagsFilterOpts,
newRollupIDFn: opts.NewRollupIDFn(),
isRollupIDFn: opts.IsRollupIDFn(),
}, nil
}
// NewEmptyRuleSet returns an empty ruleset to be used with a new namespace.
func NewEmptyRuleSet(namespaceName string, meta UpdateMetadata) MutableRuleSet {
rs := &ruleSet{
uuid: uuid.NewUUID().String(),
version: kv.UninitializedVersion,
namespace: []byte(namespaceName),
tombstoned: false,
mappingRules: make([]*mappingRule, 0),
rollupRules: make([]*rollupRule, 0),
}
rs.updateMetadata(meta)
return rs
}
func (rs *ruleSet) Namespace() []byte { return rs.namespace }
func (rs *ruleSet) Version() int { return rs.version }
func (rs *ruleSet) CutoverNanos() int64 { return rs.cutoverNanos }
func (rs *ruleSet) Tombstoned() bool { return rs.tombstoned }
func (rs *ruleSet) LastUpdatedAtNanos() int64 { return rs.lastUpdatedAtNanos }
func (rs *ruleSet) CreatedAtNanos() int64 { return rs.createdAtNanos }
func (rs *ruleSet) ToMutableRuleSet() MutableRuleSet { return rs }
func (rs *ruleSet) ActiveSet(timeNanos int64) Matcher {
mappingRules := make([]*mappingRule, 0, len(rs.mappingRules))
for _, mappingRule := range rs.mappingRules {
activeRule := mappingRule.activeRule(timeNanos)
mappingRules = append(mappingRules, activeRule)
}
rollupRules := make([]*rollupRule, 0, len(rs.rollupRules))
for _, rollupRule := range rs.rollupRules {
activeRule := rollupRule.activeRule(timeNanos)
rollupRules = append(rollupRules, activeRule)
}
return newActiveRuleSet(
rs.version,
mappingRules,
rollupRules,
rs.tagsFilterOpts,
rs.newRollupIDFn,
rs.isRollupIDFn,
)
}
// Proto returns the protobuf representation of a ruleset.
func (rs *ruleSet) Proto() (*rulepb.RuleSet, error) {
res := &rulepb.RuleSet{
Uuid: rs.uuid,
Namespace: string(rs.namespace),
CreatedAtNanos: rs.createdAtNanos,
LastUpdatedAtNanos: rs.lastUpdatedAtNanos,
LastUpdatedBy: rs.lastUpdatedBy,
Tombstoned: rs.tombstoned,
CutoverNanos: rs.cutoverNanos,
}
mappingRules := make([]*rulepb.MappingRule, len(rs.mappingRules))
for i, m := range rs.mappingRules {
mr, err := m.proto()
if err != nil {
return nil, err
}
mappingRules[i] = mr
}
res.MappingRules = mappingRules
rollupRules := make([]*rulepb.RollupRule, len(rs.rollupRules))
for i, r := range rs.rollupRules {
rr, err := r.proto()
if err != nil {
return nil, err
}
rollupRules[i] = rr
}
res.RollupRules = rollupRules
return res, nil
}
func (rs *ruleSet) MappingRules() (view.MappingRules, error) {
mappingRules := make(view.MappingRules, len(rs.mappingRules))
for _, m := range rs.mappingRules {
hist, err := m.history()
if err != nil {
return nil, err
}
mappingRules[m.uuid] = hist
}
return mappingRules, nil
}
func (rs *ruleSet) RollupRules() (view.RollupRules, error) {
rollupRules := make(view.RollupRules, len(rs.rollupRules))
for _, r := range rs.rollupRules {
hist, err := r.history()
if err != nil {
return nil, err
}
rollupRules[r.uuid] = hist
}
return rollupRules, nil
}
func (rs *ruleSet) Latest() (view.RuleSet, error) {
mrs, err := rs.latestMappingRules()
if err != nil {
return view.RuleSet{}, err
}
rrs, err := rs.latestRollupRules()
if err != nil {
return view.RuleSet{}, err
}
return view.RuleSet{
Namespace: string(rs.Namespace()),
Version: rs.Version(),
CutoverMillis: rs.CutoverNanos() / nanosPerMilli,
MappingRules: mrs,
RollupRules: rrs,
}, nil
}
func (rs *ruleSet) Clone() MutableRuleSet {
namespace := make([]byte, len(rs.namespace))
copy(namespace, rs.namespace)
mappingRules := make([]*mappingRule, len(rs.mappingRules))
for i, m := range rs.mappingRules {
c := m.clone()
mappingRules[i] = &c
}
rollupRules := make([]*rollupRule, len(rs.rollupRules))
for i, r := range rs.rollupRules {
c := r.clone()
rollupRules[i] = &c
}
// This clone deliberately ignores tagFliterOpts and rollupIDFn
// as they are not useful for the MutableRuleSet.
return &ruleSet{
uuid: rs.uuid,
version: rs.version,
createdAtNanos: rs.createdAtNanos,
lastUpdatedAtNanos: rs.lastUpdatedAtNanos,
lastUpdatedBy: rs.lastUpdatedBy,
tombstoned: rs.tombstoned,
cutoverNanos: rs.cutoverNanos,
namespace: namespace,
mappingRules: mappingRules,
rollupRules: rollupRules,
tagsFilterOpts: rs.tagsFilterOpts,
newRollupIDFn: rs.newRollupIDFn,
isRollupIDFn: rs.isRollupIDFn,
}
}
func (rs *ruleSet) AddMappingRule(mrv view.MappingRule, meta UpdateMetadata) (string, error) {
m, err := rs.getMappingRuleByName(mrv.Name)
if err != nil && err != errRuleNotFound {
return "", xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "add", mrv.Name))
}
if err == errRuleNotFound {
m = newEmptyMappingRule()
if err = m.addSnapshot(
mrv.Name,
mrv.Filter,
mrv.AggregationID,
mrv.StoragePolicies,
mrv.DropPolicy,
mrv.Tags,
meta,
); err != nil {
return "", xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "add", mrv.Name))
}
rs.mappingRules = append(rs.mappingRules, m)
} else {
if err := m.revive(
mrv.Name,
mrv.Filter,
mrv.AggregationID,
mrv.StoragePolicies,
mrv.DropPolicy,
mrv.Tags,
meta,
); err != nil {
return "", xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "revive", mrv.Name))
}
}
rs.updateMetadata(meta)
return m.uuid, nil
}
func (rs *ruleSet) UpdateMappingRule(mrv view.MappingRule, meta UpdateMetadata) error {
m, err := rs.getMappingRuleByID(mrv.ID)
if err != nil {
return merrors.NewInvalidInputError(fmt.Sprintf(ruleIDNotFoundErrorFmt, mrv.ID))
}
if err := m.addSnapshot(
mrv.Name,
mrv.Filter,
mrv.AggregationID,
mrv.StoragePolicies,
mrv.DropPolicy,
mrv.Tags,
meta,
); err != nil {
return xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "update", mrv.Name))
}
rs.updateMetadata(meta)
return nil
}
func (rs *ruleSet) DeleteMappingRule(id string, meta UpdateMetadata) error {
m, err := rs.getMappingRuleByID(id)
if err != nil {
return merrors.NewInvalidInputError(fmt.Sprintf(ruleIDNotFoundErrorFmt, id))
}
if err := m.markTombstoned(meta); err != nil {
return xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "delete", id))
}
rs.updateMetadata(meta)
return nil
}
func (rs *ruleSet) AddRollupRule(rrv view.RollupRule, meta UpdateMetadata) (string, error) {
r, err := rs.getRollupRuleByName(rrv.Name)
if err != nil && err != errRuleNotFound {
return "", xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "add", rrv.Name))
}
targets := newRollupTargetsFromView(rrv.Targets)
if err == errRuleNotFound {
r = newEmptyRollupRule()
if err = r.addSnapshot(
rrv.Name,
rrv.Filter,
targets,
meta,
rrv.KeepOriginal,
); err != nil {
return "", xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "add", rrv.Name))
}
rs.rollupRules = append(rs.rollupRules, r)
} else {
if err := r.revive(
rrv.Name,
rrv.Filter,
targets,
meta,
rrv.KeepOriginal,
); err != nil {
return "", xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "revive", rrv.Name))
}
}
rs.updateMetadata(meta)
return r.uuid, nil
}
func (rs *ruleSet) UpdateRollupRule(rrv view.RollupRule, meta UpdateMetadata) error {
r, err := rs.getRollupRuleByID(rrv.ID)
if err != nil {
return merrors.NewInvalidInputError(fmt.Sprintf(ruleIDNotFoundErrorFmt, rrv.ID))
}
targets := newRollupTargetsFromView(rrv.Targets)
if err = r.addSnapshot(
rrv.Name,
rrv.Filter,
targets,
meta,
rrv.KeepOriginal,
); err != nil {
return xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "update", rrv.Name))
}
rs.updateMetadata(meta)
return nil
}
func (rs *ruleSet) DeleteRollupRule(id string, meta UpdateMetadata) error {
r, err := rs.getRollupRuleByID(id)
if err != nil {
return merrors.NewInvalidInputError(fmt.Sprintf(ruleIDNotFoundErrorFmt, id))
}
if err := r.markTombstoned(meta); err != nil {
return xerrors.Wrap(err, fmt.Sprintf(ruleActionErrorFmt, "delete", id))
}
rs.updateMetadata(meta)
return nil
}
func (rs *ruleSet) Delete(meta UpdateMetadata) error {
if rs.tombstoned {
return fmt.Errorf("%s is already tombstoned", string(rs.namespace))
}
rs.tombstoned = true
rs.updateMetadata(meta)
// Make sure that all of the rules in the ruleset are tombstoned as well.
for _, m := range rs.mappingRules {
if t := m.tombstoned(); !t {
_ = m.markTombstoned(meta)
}
}
for _, r := range rs.rollupRules {
if t := r.tombstoned(); !t {
_ = r.markTombstoned(meta)
}
}
return nil
}
func (rs *ruleSet) ApplyRuleSetChanges(rsc changes.RuleSetChanges, meta UpdateMetadata) error {
if err := rs.applyMappingRuleChanges(rsc.MappingRuleChanges, meta); err != nil {
return err
}
return rs.applyRollupRuleChanges(rsc.RollupRuleChanges, meta)
}
func (rs *ruleSet) Revive(meta UpdateMetadata) error {
if !rs.Tombstoned() {
return xerrors.Wrap(errRuleSetNotTombstoned, fmt.Sprintf(ruleSetActionErrorFmt, "revive", string(rs.namespace)))
}
rs.tombstoned = false
rs.updateMetadata(meta)
return nil
}
func (rs *ruleSet) updateMetadata(meta UpdateMetadata) {
rs.cutoverNanos = meta.cutoverNanos
rs.lastUpdatedAtNanos = meta.updatedAtNanos
rs.lastUpdatedBy = meta.updatedBy
}
func (rs *ruleSet) getMappingRuleByName(name string) (*mappingRule, error) {
for _, m := range rs.mappingRules {
n, err := m.name()
if err != nil {
continue
}
if n == name {
return m, nil
}
}
return nil, errRuleNotFound
}
func (rs *ruleSet) getMappingRuleByID(id string) (*mappingRule, error) {
for _, m := range rs.mappingRules {
if m.uuid == id {
return m, nil
}
}
return nil, errRuleNotFound
}
func (rs *ruleSet) getRollupRuleByName(name string) (*rollupRule, error) {
for _, r := range rs.rollupRules {
n, err := r.name()
if err != nil {
return nil, err
}
if n == name {
return r, nil
}
}
return nil, errRuleNotFound
}
func (rs *ruleSet) getRollupRuleByID(id string) (*rollupRule, error) {
for _, r := range rs.rollupRules {
if r.uuid == id {
return r, nil
}
}
return nil, errRuleNotFound
}
func (rs *ruleSet) latestMappingRules() ([]view.MappingRule, error) {
mrs, err := rs.MappingRules()
if err != nil {
return nil, err
}
filtered := make([]view.MappingRule, 0, len(mrs))
for _, m := range mrs {
if len(m) > 0 && !m[0].Tombstoned {
// Rule snapshots are sorted by cutover time in descending order.
filtered = append(filtered, m[0])
}
}
sort.Sort(view.MappingRulesByNameAsc(filtered))
return filtered, nil
}
func (rs *ruleSet) latestRollupRules() ([]view.RollupRule, error) {
rrs, err := rs.RollupRules()
if err != nil {
return nil, err
}
filtered := make([]view.RollupRule, 0, len(rrs))
for _, r := range rrs {
if len(r) > 0 && !r[0].Tombstoned {
// Rule snapshots are sorted by cutover time in descending order.
filtered = append(filtered, r[0])
}
}
sort.Sort(view.RollupRulesByNameAsc(filtered))
return filtered, nil
}
func (rs *ruleSet) applyMappingRuleChanges(mrChanges []changes.MappingRuleChange, meta UpdateMetadata) error {
for _, mrChange := range mrChanges {
switch mrChange.Op {
case changes.AddOp:
if _, err := rs.AddMappingRule(*mrChange.RuleData, meta); err != nil {
return err
}
case changes.ChangeOp:
if err := rs.UpdateMappingRule(*mrChange.RuleData, meta); err != nil {
return err
}
case changes.DeleteOp:
if err := rs.DeleteMappingRule(*mrChange.RuleID, meta); err != nil {
return err
}
default:
return merrors.NewInvalidInputError(fmt.Sprintf(unknownOpTypeFmt, mrChange.Op))
}
}
return nil
}
func (rs *ruleSet) applyRollupRuleChanges(rrChanges []changes.RollupRuleChange, meta UpdateMetadata) error {
for _, rrChange := range rrChanges {
switch rrChange.Op {
case changes.AddOp:
if _, err := rs.AddRollupRule(*rrChange.RuleData, meta); err != nil {
return err
}
case changes.ChangeOp:
if err := rs.UpdateRollupRule(*rrChange.RuleData, meta); err != nil {
return err
}
case changes.DeleteOp:
if err := rs.DeleteRollupRule(*rrChange.RuleID, meta); err != nil {
return err
}
default:
return merrors.NewInvalidInputError(fmt.Sprintf(unknownOpTypeFmt, rrChange.Op))
}
}
return nil
}
// RuleSetUpdateHelper stores the necessary details to create an UpdateMetadata.
type RuleSetUpdateHelper struct {
propagationDelay time.Duration
}
// NewRuleSetUpdateHelper creates a new RuleSetUpdateHelper struct.
func NewRuleSetUpdateHelper(propagationDelay time.Duration) RuleSetUpdateHelper {
return RuleSetUpdateHelper{propagationDelay: propagationDelay}
}
// UpdateMetadata contains descriptive information that needs to be updated
// with any modification of the ruleset.
type UpdateMetadata struct {
cutoverNanos int64
updatedAtNanos int64
updatedBy string
}
// NewUpdateMetadata creates a properly initialized UpdateMetadata object.
func (r RuleSetUpdateHelper) NewUpdateMetadata(updateTime int64, updatedBy string) UpdateMetadata {
cutoverNanos := updateTime + int64(r.propagationDelay)
return UpdateMetadata{updatedAtNanos: updateTime, cutoverNanos: cutoverNanos, updatedBy: updatedBy}
}