Skip to content

Commit

Permalink
Rename system.SystemRule to system.Rule (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
louyuting authored Sep 3, 2020
1 parent a9fa300 commit 8b17e24
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 60 deletions.
8 changes: 4 additions & 4 deletions core/system/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ func (t AdaptiveStrategy) String() string {
}
}

type SystemRule struct {
type Rule struct {
ID uint64 `json:"id,omitempty"`

MetricType MetricType `json:"metricType"`
TriggerCount float64 `json:"triggerCount"`
Strategy AdaptiveStrategy `json:"strategy"`
}

func (r *SystemRule) String() string {
func (r *Rule) String() string {
b, err := json.Marshal(r)
if err != nil {
// Return the fallback string
return fmt.Sprintf("SystemRule{metricType=%s, triggerCount=%.2f, adaptiveStrategy=%s}",
return fmt.Sprintf("Rule{metricType=%s, triggerCount=%.2f, adaptiveStrategy=%s}",
r.MetricType, r.TriggerCount, r.Strategy)
}
return string(b)
}

func (r *SystemRule) ResourceName() string {
func (r *Rule) ResourceName() string {
return r.MetricType.String()
}
16 changes: 8 additions & 8 deletions core/system/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/pkg/errors"
)

type RuleMap map[MetricType][]*SystemRule
type RuleMap map[MetricType][]*Rule

// const
var (
Expand All @@ -17,19 +17,19 @@ var (
)

// GetRules return all the rules
func GetRules() []*SystemRule {
func GetRules() []*Rule {
ruleMapMux.RLock()
defer ruleMapMux.RUnlock()

rules := make([]*SystemRule, 0)
rules := make([]*Rule, 0)
for _, rs := range ruleMap {
rules = append(rules, rs...)
}
return rules
}

// LoadRules loads given system rules to the rule manager, while all previous rules will be replaced.
func LoadRules(rules []*SystemRule) (bool, error) {
func LoadRules(rules []*Rule) (bool, error) {
m := buildRuleMap(rules)

if err := onRuleUpdate(m); err != nil {
Expand Down Expand Up @@ -62,7 +62,7 @@ func onRuleUpdate(r RuleMap) error {
return nil
}

func buildRuleMap(rules []*SystemRule) RuleMap {
func buildRuleMap(rules []*Rule) RuleMap {
m := make(RuleMap)

if len(rules) == 0 {
Expand All @@ -76,7 +76,7 @@ func buildRuleMap(rules []*SystemRule) RuleMap {
}
rulesOfRes, exists := m[rule.MetricType]
if !exists {
m[rule.MetricType] = []*SystemRule{rule}
m[rule.MetricType] = []*Rule{rule}
} else {
m[rule.MetricType] = append(rulesOfRes, rule)
}
Expand All @@ -85,9 +85,9 @@ func buildRuleMap(rules []*SystemRule) RuleMap {
}

// IsValidSystemRule determine the system rule is valid or not
func IsValidSystemRule(rule *SystemRule) error {
func IsValidSystemRule(rule *Rule) error {
if rule == nil {
return errors.New("nil SystemRule")
return errors.New("nil Rule")
}
if rule.TriggerCount < 0 {
return errors.New("negative threshold")
Expand Down
36 changes: 18 additions & 18 deletions core/system/rule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ func TestGetRules(t *testing.T) {
t.Run("GetUpdatedRules", func(t *testing.T) {
defer func() { ruleMap = make(RuleMap) }()

r := map[MetricType][]*SystemRule{
InboundQPS: {&SystemRule{MetricType: InboundQPS, TriggerCount: 1}},
Concurrency: {&SystemRule{MetricType: Concurrency, TriggerCount: 2}},
r := map[MetricType][]*Rule{
InboundQPS: {&Rule{MetricType: InboundQPS, TriggerCount: 1}},
Concurrency: {&Rule{MetricType: Concurrency, TriggerCount: 2}},
}
ruleMap = r
rules := GetRules()
assert.Equal(t, 2, len(rules))

r[InboundQPS] = append(r[InboundQPS], &SystemRule{MetricType: InboundQPS, TriggerCount: 2})
r[InboundQPS] = append(r[InboundQPS], &Rule{MetricType: InboundQPS, TriggerCount: 2})
ruleMap = r
rules = GetRules()
assert.Equal(t, 3, len(rules))
Expand All @@ -40,7 +40,7 @@ func TestLoadRules(t *testing.T) {

t.Run("ValidSystemRule", func(t *testing.T) {
defer func() { ruleMap = make(RuleMap) }()
sRule := []*SystemRule{
sRule := []*Rule{
{MetricType: InboundQPS, TriggerCount: 1},
{MetricType: Concurrency, TriggerCount: 2},
}
Expand All @@ -59,9 +59,9 @@ func TestClearRules(t *testing.T) {
})

t.Run("NoEmptyOriginRuleMap", func(t *testing.T) {
r := map[MetricType][]*SystemRule{
InboundQPS: {&SystemRule{MetricType: InboundQPS, TriggerCount: 1}},
Concurrency: {&SystemRule{MetricType: Concurrency, TriggerCount: 2}},
r := map[MetricType][]*Rule{
InboundQPS: {&Rule{MetricType: InboundQPS, TriggerCount: 1}},
Concurrency: {&Rule{MetricType: Concurrency, TriggerCount: 2}},
}
ruleMap = r
err := ClearRules()
Expand All @@ -80,10 +80,10 @@ func TestOnRuleUpdate(t *testing.T) {
t.Run("ValidSystemRule", func(t *testing.T) {
defer func() { ruleMap = make(RuleMap) }()
rMap := RuleMap{
InboundQPS: []*SystemRule{
InboundQPS: []*Rule{
{MetricType: InboundQPS, TriggerCount: 1},
},
Concurrency: []*SystemRule{
Concurrency: []*Rule{
{MetricType: Concurrency, TriggerCount: 2},
},
}
Expand All @@ -100,15 +100,15 @@ func TestBuildRuleMap(t *testing.T) {
})

t.Run("InvalidSystemRule", func(t *testing.T) {
sRule := []*SystemRule{
sRule := []*Rule{
{MetricType: InboundQPS, TriggerCount: -1},
}
r := buildRuleMap(sRule)
assert.Equal(t, 0, len(r))
})

t.Run("ValidSystemRule", func(t *testing.T) {
sRule := []*SystemRule{
sRule := []*Rule{
{MetricType: InboundQPS, TriggerCount: 1},
{MetricType: Concurrency, TriggerCount: 2},
}
Expand All @@ -117,7 +117,7 @@ func TestBuildRuleMap(t *testing.T) {
})

t.Run("MultiRuleOneTypeValidSystemRule", func(t *testing.T) {
sRule := []*SystemRule{
sRule := []*Rule{
{MetricType: InboundQPS, TriggerCount: 1},
{MetricType: InboundQPS, TriggerCount: 2},
}
Expand All @@ -129,29 +129,29 @@ func TestBuildRuleMap(t *testing.T) {
func TestIsValidSystemRule(t *testing.T) {
t.Run("NilSystemRule", func(t *testing.T) {
err := IsValidSystemRule(nil)
assert.EqualError(t, err, "nil SystemRule")
assert.EqualError(t, err, "nil Rule")
})

t.Run("NegativeThreshold", func(t *testing.T) {
sRule := &SystemRule{MetricType: InboundQPS, TriggerCount: -1}
sRule := &Rule{MetricType: InboundQPS, TriggerCount: -1}
err := IsValidSystemRule(sRule)
assert.EqualError(t, err, "negative threshold")
})

t.Run("InvalidMetricType", func(t *testing.T) {
sRule := &SystemRule{MetricType: MetricTypeSize}
sRule := &Rule{MetricType: MetricTypeSize}
err := IsValidSystemRule(sRule)
assert.EqualError(t, err, "invalid metric type")
})

t.Run("InvalidCPUUsage", func(t *testing.T) {
sRule := &SystemRule{MetricType: CpuUsage, TriggerCount: 75}
sRule := &Rule{MetricType: CpuUsage, TriggerCount: 75}
err := IsValidSystemRule(sRule)
assert.EqualError(t, err, "invalid CPU usage, valid range is [0.0, 1.0]")
})

t.Run("ValidSystemRule", func(t *testing.T) {
sRule := &SystemRule{MetricType: Load, TriggerCount: 12, Strategy: BBR}
sRule := &Rule{MetricType: Load, TriggerCount: 12, Strategy: BBR}
err := IsValidSystemRule(sRule)
assert.NoError(t, err)
})
Expand Down
6 changes: 3 additions & 3 deletions core/system/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func TestAdaptiveStrategyString(t *testing.T) {

func TestSystemRuleResourceName(t *testing.T) {
t.Run("ValidResourceName", func(t *testing.T) {
sr := &SystemRule{MetricType: Concurrency}
sr := &Rule{MetricType: Concurrency}
assert.Equal(t, "concurrency", sr.ResourceName())
})
}

func TestSystemRuleString(t *testing.T) {
t.Run("ValidSystemRuleString", func(t *testing.T) {
sr := &SystemRule{MetricType: Concurrency}
assert.NotContains(t, sr.String(), "SystemRule")
sr := &Rule{MetricType: Concurrency}
assert.NotContains(t, sr.String(), "Rule")
})
}
2 changes: 1 addition & 1 deletion core/system/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *SystemAdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
return result
}

func (s *SystemAdaptiveSlot) doCheckRule(rule *SystemRule) (bool, float64) {
func (s *SystemAdaptiveSlot) doCheckRule(rule *Rule) (bool, float64) {
threshold := rule.TriggerCount
switch rule.MetricType {
case InboundQPS:
Expand Down
8 changes: 4 additions & 4 deletions core/system/slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCheckEmptyRule(t *testing.T) {

func TestDoCheckRuleConcurrency(t *testing.T) {
var sas *SystemAdaptiveSlot
rule := &SystemRule{MetricType: Concurrency,
rule := &Rule{MetricType: Concurrency,
TriggerCount: 0.5}

t.Run("TrueConcurrency", func(t *testing.T) {
Expand All @@ -60,7 +60,7 @@ func TestDoCheckRuleConcurrency(t *testing.T) {

func TestDoCheckRuleLoad(t *testing.T) {
var sas *SystemAdaptiveSlot
rule := &SystemRule{MetricType: Load,
rule := &Rule{MetricType: Load,
TriggerCount: 0.5}

t.Run("TrueLoad", func(t *testing.T) {
Expand All @@ -81,7 +81,7 @@ func TestDoCheckRuleLoad(t *testing.T) {

func TestDoCheckRuleCpuUsage(t *testing.T) {
var sas *SystemAdaptiveSlot
rule := &SystemRule{MetricType: CpuUsage,
rule := &Rule{MetricType: CpuUsage,
TriggerCount: 0.5}

t.Run("TrueCpuUsage", func(t *testing.T) {
Expand All @@ -102,7 +102,7 @@ func TestDoCheckRuleCpuUsage(t *testing.T) {

func TestDoCheckRuleDefault(t *testing.T) {
var sas *SystemAdaptiveSlot
rule := &SystemRule{MetricType: MetricTypeSize,
rule := &Rule{MetricType: MetricTypeSize,
TriggerCount: 0.5}

isOK, v := sas.doCheckRule(rule)
Expand Down
2 changes: 1 addition & 1 deletion ext/datasource/consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
)

var (
SystemRules = []*system.SystemRule{
SystemRules = []*system.Rule{
{MetricType: 0, Strategy: 0},
{MetricType: 0, Strategy: 0},
{MetricType: 0, Strategy: 0},
Expand Down
14 changes: 7 additions & 7 deletions ext/datasource/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,34 @@ func NewFlowRulesHandler(converter PropertyConverter) PropertyHandler {
return NewDefaultPropertyHandler(converter, FlowRulesUpdater)
}

// SystemRuleJsonArrayParser provide JSON as the default serialization for list of system.SystemRule
// SystemRuleJsonArrayParser provide JSON as the default serialization for list of system.Rule
func SystemRuleJsonArrayParser(src []byte) (interface{}, error) {
if valid, err := checkSrcComplianceJson(src); !valid {
return nil, err
}

rules := make([]*system.SystemRule, 0)
rules := make([]*system.Rule, 0)
err := json.Unmarshal(src, &rules)
return rules, err
}

// SystemRulesUpdater load the newest []system.SystemRule to downstream system component.
// SystemRulesUpdater load the newest []system.Rule to downstream system component.
func SystemRulesUpdater(data interface{}) error {
if data == nil {
return system.ClearRules()
}

rules := make([]*system.SystemRule, 0)
if val, ok := data.([]system.SystemRule); ok {
rules := make([]*system.Rule, 0)
if val, ok := data.([]system.Rule); ok {
for _, v := range val {
rules = append(rules, &v)
}
} else if val, ok := data.([]*system.SystemRule); ok {
} else if val, ok := data.([]*system.Rule); ok {
rules = val
} else {
return Error{
code: UpdatePropertyError,
desc: fmt.Sprintf("Fail to type assert data to []system.SystemRule or []*system.SystemRule, in fact, data: %+v", data),
desc: fmt.Sprintf("Fail to type assert data to []system.Rule or []*system.Rule, in fact, data: %+v", data),
}
}
succ, err := system.LoadRules(rules)
Expand Down
20 changes: 10 additions & 10 deletions ext/datasource/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,25 @@ func TestSystemRulesJsonConvert(t *testing.T) {
}

got, err := SystemRuleJsonArrayParser(normalSrc)
systemRules := got.([]*system.SystemRule)
systemRules := got.([]*system.Rule)
assert.True(t, err == nil && len(systemRules) == 4)

r0 := &system.SystemRule{
r0 := &system.Rule{
MetricType: system.Load,
TriggerCount: 0.5,
Strategy: system.BBR,
}
r1 := &system.SystemRule{
r1 := &system.Rule{
MetricType: system.AvgRT,
TriggerCount: 0.6,
Strategy: system.BBR,
}
r2 := &system.SystemRule{
r2 := &system.Rule{
MetricType: system.Concurrency,
TriggerCount: 0.7,
Strategy: system.BBR,
}
r3 := &system.SystemRule{
r3 := &system.Rule{
MetricType: system.InboundQPS,
TriggerCount: 0.8,
Strategy: system.BBR,
Expand All @@ -188,7 +188,7 @@ func TestSystemRulesJsonConvert(t *testing.T) {
func TestSystemRulesUpdater(t *testing.T) {
t.Run("TestSystemRulesUpdater_Nil", func(t *testing.T) {
system.ClearRules()
system.LoadRules([]*system.SystemRule{
system.LoadRules([]*system.Rule{
{
ID: 0,
MetricType: 0,
Expand All @@ -204,20 +204,20 @@ func TestSystemRulesUpdater(t *testing.T) {
t.Run("TestSystemRulesUpdater_Assert_Failed", func(t *testing.T) {
system.ClearRules()
err := SystemRulesUpdater("xxxxxxxx")
assert.True(t, err != nil && strings.Contains(err.Error(), "Fail to type assert data to []system.SystemRule"))
assert.True(t, err != nil && strings.Contains(err.Error(), "Fail to type assert data to []system.Rule"))
})

t.Run("TestSystemRulesUpdater_Empty_Rules", func(t *testing.T) {
system.ClearRules()
p := make([]system.SystemRule, 0)
p := make([]system.Rule, 0)
err := SystemRulesUpdater(p)
assert.True(t, err == nil && len(system.GetRules()) == 0)
})

t.Run("TestSystemRulesUpdater_Normal", func(t *testing.T) {
system.ClearRules()
p := make([]system.SystemRule, 0)
sr := system.SystemRule{
p := make([]system.Rule, 0)
sr := system.Rule{
ID: 0,
MetricType: 0,
TriggerCount: 0,
Expand Down
Loading

0 comments on commit 8b17e24

Please sign in to comment.