Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Reuse RuleSetSnapshot struct to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Oct 10, 2017
1 parent cdf29d4 commit bfb59e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 54 deletions.
16 changes: 8 additions & 8 deletions rules/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,31 +967,31 @@ func (rs *ruleSet) getRollupRuleByID(id string) (*rollupRule, error) {
return nil, errNoSuchRule
}

func (rs *ruleSet) latestMappingRules() ([]*MappingRuleView, error) {
func (rs *ruleSet) latestMappingRules() (map[string]*MappingRuleView, error) {
mrs, err := rs.MappingRules()
if err != nil {
return nil, err
}
result := make([]*MappingRuleView, 0, len(mrs))
result := make(map[string]*MappingRuleView, len(mrs))
for _, m := range mrs {
if len(m) > 0 && !m[0].Tombstoned {
// views included in m are sorted latest first.
result = append(result, m[0])
result[m[0].ID] = m[0]
}
}
return result, nil
}

func (rs *ruleSet) latestRollupRules() ([]*RollupRuleView, error) {
func (rs *ruleSet) latestRollupRules() (map[string]*RollupRuleView, error) {
rrs, err := rs.RollupRules()
if err != nil {
return nil, err
}
result := make([]*RollupRuleView, 0, len(rrs))
result := make(map[string]*RollupRuleView, len(rrs))
for _, r := range rrs {
if len(r) > 0 && !r[0].Tombstoned {
// views included in m are sorted latest first.
result = append(result, r[0])
result[r[0].ID] = r[0]
}
}
return result, nil
Expand Down Expand Up @@ -1151,8 +1151,8 @@ type RuleSetSnapshot struct {
Namespace string
Version int
CutoverNanos int64
MappingRules []*MappingRuleView
RollupRules []*RollupRuleView
MappingRules map[string]*MappingRuleView
RollupRules map[string]*RollupRuleView
}

func (rs ruleSet) validateMappingRuleUpdate(mrv MappingRuleView) error {
Expand Down
60 changes: 14 additions & 46 deletions rules/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@
package rules

import (
"errors"
"fmt"

"github.com/m3db/m3metrics/filters"
"github.com/m3db/m3metrics/metric"
"github.com/m3db/m3metrics/policy"
)

var (
errEmptyMappingRuleViewList = errors.New("empty mapping rule view list")
errEmptyRollupRuleViewList = errors.New("empty rollup rule view list")
)

// Validator validates a ruleset.
type Validator interface {
// Validate validates a ruleset.
Expand All @@ -50,42 +44,27 @@ func NewValidator(opts ValidatorOptions) Validator {
}

func (v *validator) Validate(rs 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()

// Validate mapping rules.
mappingRules, err := rs.MappingRules()
if err != nil {
return err
return NewValidationError(fmt.Sprintf("could not get the latest ruleset snapshot: %v", err))
}
if err := v.validateMappingRules(mappingRules); err != nil {
return err
if err := v.validateMappingRules(latest.MappingRules); err != nil {
return NewValidationError(fmt.Sprintf("could not validate mapping rules: %v", err))
}

// Validate rollup rules.
rollupRules, err := rs.RollupRules()
if err != nil {
return err
if err := v.validateRollupRules(latest.RollupRules); err != nil {
return NewValidationError(fmt.Sprintf("could not validate rollup rules: %v", err))
}
return v.validateRollupRules(rollupRules)
return nil
}

func (v *validator) validateMappingRules(rules MappingRules) error {
func (v *validator) validateMappingRules(rules map[string]*MappingRuleView) error {
namesSeen := make(map[string]struct{}, len(rules))
for _, views := range rules {
if len(views) == 0 {
return errEmptyMappingRuleViewList
}
// 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.
view := views[0]
if view.Tombstoned {
continue
}

for _, view := range rules {
// Validate that no rules with the same name exist.
if _, exists := namesSeen[view.Name]; exists {
msg := fmt.Sprintf("mapping rule %s already exists", view.Name)
return RuleConflictError{msg: msg, ConflictRuleUUID: view.ID}
return NewRuleConflictError(fmt.Sprintf("mapping rule %s already exists", view.Name))
}
namesSeen[view.Name] = struct{}{}

Expand All @@ -105,23 +84,12 @@ func (v *validator) validateMappingRules(rules MappingRules) error {
return nil
}

func (v *validator) validateRollupRules(rules RollupRules) error {
func (v *validator) validateRollupRules(rules map[string]*RollupRuleView) error {
namesSeen := make(map[string]struct{}, len(rules))
for _, views := range rules {
if len(views) == 0 {
return errEmptyRollupRuleViewList
}
// 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.
view := views[0]
if view.Tombstoned {
continue
}

for _, view := range rules {
// Validate that no rules with the same name exist.
if _, exists := namesSeen[view.Name]; exists {
msg := fmt.Sprintf("rollup rule %s already exists", view.Name)
return RuleConflictError{msg: msg, ConflictRuleUUID: view.ID}
return NewRuleConflictError(fmt.Sprintf("rollup rule %s already exists", view.Name))
}
namesSeen[view.Name] = struct{}{}

Expand Down

0 comments on commit bfb59e5

Please sign in to comment.