From 8b17e248b0236e7ff09366b258c57e0e82917d30 Mon Sep 17 00:00:00 2001 From: louyuting Date: Thu, 3 Sep 2020 20:31:09 +0800 Subject: [PATCH] Rename system.SystemRule to system.Rule (#222) --- core/system/rule.go | 8 +++---- core/system/rule_manager.go | 16 ++++++------- core/system/rule_manager_test.go | 36 ++++++++++++++-------------- core/system/rule_test.go | 6 ++--- core/system/slot.go | 2 +- core/system/slot_test.go | 8 +++---- ext/datasource/consul/consul_test.go | 2 +- ext/datasource/helper.go | 14 +++++------ ext/datasource/helper_test.go | 20 ++++++++-------- ext/datasource/property_test.go | 8 +++---- 10 files changed, 60 insertions(+), 60 deletions(-) diff --git a/core/system/rule.go b/core/system/rule.go index b5da612a7..92570caab 100644 --- a/core/system/rule.go +++ b/core/system/rule.go @@ -56,7 +56,7 @@ func (t AdaptiveStrategy) String() string { } } -type SystemRule struct { +type Rule struct { ID uint64 `json:"id,omitempty"` MetricType MetricType `json:"metricType"` @@ -64,16 +64,16 @@ type SystemRule struct { 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() } diff --git a/core/system/rule_manager.go b/core/system/rule_manager.go index 2d6d95d59..24daf6a71 100644 --- a/core/system/rule_manager.go +++ b/core/system/rule_manager.go @@ -8,7 +8,7 @@ import ( "github.com/pkg/errors" ) -type RuleMap map[MetricType][]*SystemRule +type RuleMap map[MetricType][]*Rule // const var ( @@ -17,11 +17,11 @@ 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...) } @@ -29,7 +29,7 @@ func GetRules() []*SystemRule { } // 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 { @@ -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 { @@ -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) } @@ -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") diff --git a/core/system/rule_manager_test.go b/core/system/rule_manager_test.go index 3b8e5303a..bfeec9a2d 100644 --- a/core/system/rule_manager_test.go +++ b/core/system/rule_manager_test.go @@ -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)) @@ -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}, } @@ -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() @@ -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}, }, } @@ -100,7 +100,7 @@ func TestBuildRuleMap(t *testing.T) { }) t.Run("InvalidSystemRule", func(t *testing.T) { - sRule := []*SystemRule{ + sRule := []*Rule{ {MetricType: InboundQPS, TriggerCount: -1}, } r := buildRuleMap(sRule) @@ -108,7 +108,7 @@ func TestBuildRuleMap(t *testing.T) { }) t.Run("ValidSystemRule", func(t *testing.T) { - sRule := []*SystemRule{ + sRule := []*Rule{ {MetricType: InboundQPS, TriggerCount: 1}, {MetricType: Concurrency, TriggerCount: 2}, } @@ -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}, } @@ -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) }) diff --git a/core/system/rule_test.go b/core/system/rule_test.go index 67a6f72c2..2923c5689 100644 --- a/core/system/rule_test.go +++ b/core/system/rule_test.go @@ -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") }) } diff --git a/core/system/slot.go b/core/system/slot.go index 3198219df..9a8bd325a 100644 --- a/core/system/slot.go +++ b/core/system/slot.go @@ -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: diff --git a/core/system/slot_test.go b/core/system/slot_test.go index 0da5d0509..68bce34a4 100644 --- a/core/system/slot_test.go +++ b/core/system/slot_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) diff --git a/ext/datasource/consul/consul_test.go b/ext/datasource/consul/consul_test.go index 8d085c24f..44412cbf9 100644 --- a/ext/datasource/consul/consul_test.go +++ b/ext/datasource/consul/consul_test.go @@ -33,7 +33,7 @@ const ( ) var ( - SystemRules = []*system.SystemRule{ + SystemRules = []*system.Rule{ {MetricType: 0, Strategy: 0}, {MetricType: 0, Strategy: 0}, {MetricType: 0, Strategy: 0}, diff --git a/ext/datasource/helper.go b/ext/datasource/helper.go index daf96e9f8..468cb1149 100644 --- a/ext/datasource/helper.go +++ b/ext/datasource/helper.go @@ -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) diff --git a/ext/datasource/helper_test.go b/ext/datasource/helper_test.go index 15edf7c64..0d6f8a967 100644 --- a/ext/datasource/helper_test.go +++ b/ext/datasource/helper_test.go @@ -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, @@ -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, @@ -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, diff --git a/ext/datasource/property_test.go b/ext/datasource/property_test.go index 2151f3971..b4d6e6e3e 100644 --- a/ext/datasource/property_test.go +++ b/ext/datasource/property_test.go @@ -11,7 +11,7 @@ import ( ) func MockSystemRulesConverter(src []byte) (interface{}, error) { - ret := make([]system.SystemRule, 0) + ret := make([]system.Rule, 0) _ = json.Unmarshal(src, &ret) return ret, nil } @@ -50,7 +50,7 @@ func TestSinglePropertyHandler_isPropertyConsistent(t *testing.T) { if err != nil { t.Errorf("Fail to get source file, err:%+v", err) } - ret1 := make([]system.SystemRule, 0) + ret1 := make([]system.Rule, 0) _ = json.Unmarshal(src, &ret1) isConsistent := h.isPropertyConsistent(ret1) assert.True(t, isConsistent == false, "Fail to execute isPropertyConsistent.") @@ -59,7 +59,7 @@ func TestSinglePropertyHandler_isPropertyConsistent(t *testing.T) { if err != nil { t.Errorf("Fail to get source file, err:%+v", err) } - ret2 := make([]system.SystemRule, 0) + ret2 := make([]system.Rule, 0) _ = json.Unmarshal(src2, &ret2) isConsistent = h.isPropertyConsistent(ret2) assert.True(t, isConsistent == true, "Fail to execute isPropertyConsistent.") @@ -68,7 +68,7 @@ func TestSinglePropertyHandler_isPropertyConsistent(t *testing.T) { if err != nil { t.Errorf("Fail to get source file, err:%+v", err) } - ret3 := make([]system.SystemRule, 0) + ret3 := make([]system.Rule, 0) _ = json.Unmarshal(src3, &ret3) isConsistent = h.isPropertyConsistent(ret3) assert.True(t, isConsistent == false, "Fail to execute isPropertyConsistent.")