Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache mechanism for LoadRules in isolation module #322

Merged
merged 2 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/hotspot/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func LoadRules(rules []*Rule) (bool, error) {
isEqual := reflect.DeepEqual(currentRules, rules)
tcMux.RUnlock()
if isEqual {
logging.Info("[HotSpot] Load rules repetition, does not load")
logging.Info("[HotSpot] Load rules is the same with current rules, so ignore load operation.")
return false, nil
}

Expand Down
24 changes: 19 additions & 5 deletions core/isolation/rule_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package isolation

import (
"reflect"
"sync"

"github.com/alibaba/sentinel-golang/core/misc"
Expand All @@ -10,15 +11,27 @@ import (
)

var (
ruleMap = make(map[string][]*Rule)
rwMux = &sync.RWMutex{}
ruleMap = make(map[string][]*Rule)
rwMux = &sync.RWMutex{}
currentRules = make([]*Rule, 0)
)

// LoadRules loads the given isolation rules to the rule manager, while all previous rules will be replaced.
func LoadRules(rules []*Rule) (updated bool, err error) {
updated = true
err = nil
// the first returned value indicates whether do real load operation, if the rules is the same with previous rules, return false
func LoadRules(rules []*Rule) (bool, error) {
rwMux.RLock()
isEqual := reflect.DeepEqual(currentRules, rules)
rwMux.RUnlock()
if isEqual {
logging.Info("[Isolation] Load rules is the same with current rules, so ignore load operation.")
return false, nil
}

err := onRuleUpdate(rules)
return true, err
}

func onRuleUpdate(rules []*Rule) (err error) {
m := make(map[string][]*Rule, len(rules))
for _, r := range rules {
if e := IsValid(r); e != nil {
Expand Down Expand Up @@ -47,6 +60,7 @@ func LoadRules(rules []*Rule) (updated bool, err error) {
}
}
ruleMap = m
currentRules = rules
return
}

Expand Down
20 changes: 20 additions & 0 deletions core/isolation/rule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,24 @@ func TestLoadRules(t *testing.T) {
err = ClearRules()
assert.True(t, err == nil)
})

t.Run("loadSameRules", func(t *testing.T) {
_, err := LoadRules([]*Rule{
{
Resource: "abc1",
MetricType: Concurrency,
Threshold: 100,
},
})
assert.Nil(t, err)
ok, err := LoadRules([]*Rule{
{
Resource: "abc1",
MetricType: Concurrency,
Threshold: 100,
},
})
assert.Nil(t, err)
assert.False(t, ok)
})
}