Skip to content

Commit

Permalink
Polish comments and logs regarding parameter flow control and util pkg
Browse files Browse the repository at this point in the history
- Rename field in hotspot.Rule: Behavior -> ControlBehavior

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 authored and louyuting committed Jun 18, 2020
1 parent 46ea824 commit 0834aad
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 103 deletions.
4 changes: 3 additions & 1 deletion core/base/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
BlockTypeFlow
BlockTypeCircuitBreaking
BlockTypeSystemFlow
BlockTypeFreqParamsFlow
BlockTypeHotSpotParamFlow
)

func (t BlockType) String() string {
Expand All @@ -24,6 +24,8 @@ func (t BlockType) String() string {
return "CircuitBreaking"
case BlockTypeSystemFlow:
return "System"
case BlockTypeHotSpotParamFlow:
return "HotSpotParamFlow"
default:
return fmt.Sprintf("%d", t)
}
Expand Down
13 changes: 6 additions & 7 deletions core/hotspot/params_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ const (
ParamsMaxCapacity = 20000
)

// ParamsMetric cache the frequent(hot spot) parameters for each value.
// ParamsMetric is used for pair <resource, TrafficShapingController>.
// ParamsMetric carries real-time counters for frequent ("hot spot") parameters.
//
// For each cache map, the key is the parameter value, while the value is the counter.
type ParamsMetric struct {
// cache's key is the hot value
// cache's value is the counter
// RuleTimeCounter record the last add token time
// RuleTimeCounter records the last added token timestamp.
RuleTimeCounter cache.ConcurrentCounterCache
// RuleTokenCounter record the number of token
// RuleTokenCounter records the number of tokens.
RuleTokenCounter cache.ConcurrentCounterCache
// ConcurrencyCounter record the number of goroutine
// ConcurrencyCounter records the real-time concurrency.
ConcurrencyCounter cache.ConcurrentCounterCache
}
29 changes: 14 additions & 15 deletions core/hotspot/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ type Rule struct {
// Id is the unique id
Id string
// Resource is the resource name
Resource string
MetricType MetricType
Behavior ControlBehavior
Resource string
MetricType MetricType
ControlBehavior ControlBehavior
// ParamIndex is the index in context arguments slice.
ParamIndex int
Threshold float64
Expand All @@ -102,27 +102,27 @@ type Rule struct {
}

func (r *Rule) String() string {
return fmt.Sprintf("{Id:%s, Resource:%s, MetricType:%+v, Behavior:%+v, ParamIndex:%d, Threshold:%f, MaxQueueingTimeMs:%d, BurstCount:%d, DurationInSec:%d, ParamsMaxCapacity:%d, SpecificItems:%+v}",
r.Id, r.Resource, r.MetricType, r.Behavior, r.ParamIndex, r.Threshold, r.MaxQueueingTimeMs, r.BurstCount, r.DurationInSec, r.ParamsMaxCapacity, r.SpecificItems)
return fmt.Sprintf("{Id:%s, Resource:%s, MetricType:%+v, ControlBehavior:%+v, ParamIndex:%d, Threshold:%f, MaxQueueingTimeMs:%d, BurstCount:%d, DurationInSec:%d, ParamsMaxCapacity:%d, SpecificItems:%+v}",
r.Id, r.Resource, r.MetricType, r.ControlBehavior, r.ParamIndex, r.Threshold, r.MaxQueueingTimeMs, r.BurstCount, r.DurationInSec, r.ParamsMaxCapacity, r.SpecificItems)
}
func (r *Rule) ResourceName() string {
return r.Resource
}

// IsStatReusable checks whether current rule is "statistically" equal to the given rule.
func (r *Rule) IsStatReusable(newRule *Rule) bool {
return r.Resource == newRule.Resource && r.Behavior == newRule.Behavior && r.ParamsMaxCapacity == newRule.ParamsMaxCapacity && r.DurationInSec == newRule.DurationInSec
return r.Resource == newRule.Resource && r.ControlBehavior == newRule.ControlBehavior && r.ParamsMaxCapacity == newRule.ParamsMaxCapacity && r.DurationInSec == newRule.DurationInSec
}

// IsEqualsTo checks whether current rule is consistent with the given rule.
func (r *Rule) Equals(newRule *Rule) bool {
baseCheck := r.Resource == newRule.Resource && r.MetricType == newRule.MetricType && r.Behavior == newRule.Behavior && r.ParamsMaxCapacity == newRule.ParamsMaxCapacity && r.ParamIndex == newRule.ParamIndex && r.Threshold == newRule.Threshold && r.DurationInSec == newRule.DurationInSec && reflect.DeepEqual(r.SpecificItems, newRule.SpecificItems)
baseCheck := r.Resource == newRule.Resource && r.MetricType == newRule.MetricType && r.ControlBehavior == newRule.ControlBehavior && r.ParamsMaxCapacity == newRule.ParamsMaxCapacity && r.ParamIndex == newRule.ParamIndex && r.Threshold == newRule.Threshold && r.DurationInSec == newRule.DurationInSec && reflect.DeepEqual(r.SpecificItems, newRule.SpecificItems)
if !baseCheck {
return false
}
if r.Behavior == Reject {
if r.ControlBehavior == Reject {
return r.BurstCount == newRule.BurstCount
} else if r.Behavior == Throttling {
} else if r.ControlBehavior == Throttling {
return r.MaxQueueingTimeMs == newRule.MaxQueueingTimeMs
} else {
return false
Expand All @@ -140,7 +140,7 @@ func parseSpecificItems(source map[SpecificValue]int64) map[interface{}]int64 {
case KindInt:
realVal, err := strconv.Atoi(k.ValStr)
if err != nil {
logger.Errorf("Fail to parse value for int specific item. paramKind: %+v, value: %s, err: %+v", k.ValKind, k.ValStr, err)
logger.Errorf("Failed to parse value for int specific item. paramKind: %+v, value: %s, err: %+v", k.ValKind, k.ValStr, err)
continue
}
ret[realVal] = v
Expand All @@ -151,26 +151,25 @@ func parseSpecificItems(source map[SpecificValue]int64) map[interface{}]int64 {
case KindBool:
realVal, err := strconv.ParseBool(k.ValStr)
if err != nil {
logger.Errorf("Fail to parse value for int specific item. value: %s, err: %+v", k.ValStr, err)
logger.Errorf("Failed to parse value for bool specific item. value: %s, err: %+v", k.ValStr, err)
continue
}
ret[realVal] = v

case KindFloat64:
realVal, err := strconv.ParseFloat(k.ValStr, 64)
if err != nil {
logger.Errorf("Fail to parse value for int specific item. value: %s, err: %+v", k.ValStr, err)
logger.Errorf("Failed to parse value for float specific item. value: %s, err: %+v", k.ValStr, err)
continue
}
realVal, err = strconv.ParseFloat(fmt.Sprintf("%.5f", realVal), 64)
if err != nil {
logger.Errorf("Fail to parse value for int specific item. value: %s, err: %+v", k.ValStr, err)
logger.Errorf("Failed to parse value for float specific item. value: %s, err: %+v", k.ValStr, err)
continue
}
ret[realVal] = v

default:
logger.Errorf("Unsupported kind(%d) for specific item.", k.ValKind)
logger.Errorf("Unsupported kind for specific item: %d", k.ValKind)
}
}
return ret
Expand Down
25 changes: 12 additions & 13 deletions core/hotspot/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ func getTrafficControllersFor(res string) []TrafficShapingController {
return tcMap[res]
}

// LoadRules replaces old rules with the given frequency parameters flow control rules.
// return value:
// LoadRules replaces old rules with the given frequent parameter flow control rules. Return value:
//
// bool: was designed to indicate whether the internal map has been changed
// error: was designed to indicate whether occurs the error.
// bool: indicates whether the internal map has been changed;
// error: indicates whether occurs the error.
func LoadRules(rules []*Rule) (bool, error) {
err := onRuleUpdate(rules)
return true, err
}

// GetRules return the res's rules
// GetRules returns existing rules of the given resource.
func GetRules(res string) []*Rule {
tcMux.RLock()
defer tcMux.RUnlock()
Expand All @@ -82,7 +81,7 @@ func GetRules(res string) []*Rule {
return ret
}

// ClearRules clears all rules in frequency parameters flow control components
// ClearRules clears all parameter flow rules.
func ClearRules() error {
_, err := LoadRules(nil)
return err
Expand Down Expand Up @@ -111,7 +110,7 @@ func onRuleUpdate(rules []*Rule) (err error) {

func logRuleUpdate(m trafficControllerMap) {
sb := strings.Builder{}
sb.WriteString("Frequency parameters flow control rules loaded:[")
sb.WriteString("Frequent parameter flow control rules loaded: [")

for _, r := range rulesFrom(m) {
sb.WriteString(r.String() + ",")
Expand Down Expand Up @@ -183,7 +182,7 @@ func buildTcMap(rules []*Rule) trafficControllerMap {

for _, r := range rules {
if err := IsValidRule(r); err != nil {
logger.Warnf("Ignoring invalid frequency params Rule: %+v, reason: %s", r, err.Error())
logger.Warnf("Ignoring invalid frequent param flow rule: %v, reason: %s", r.String(), err.Error())
continue
}

Expand All @@ -201,9 +200,9 @@ func buildTcMap(rules []*Rule) trafficControllerMap {
}

// generate new traffic shaping controller
generator, supported := tcGenFuncMap[r.Behavior]
generator, supported := tcGenFuncMap[r.ControlBehavior]
if !supported {
logger.Warnf("Ignoring the frequency params Rule due to unsupported control strategy: %+v", r)
logger.Warnf("Ignoring the frequent param flow rule due to unsupported control behavior: %v", r)
continue
}
var tc TrafficShapingController
Expand All @@ -214,7 +213,7 @@ func buildTcMap(rules []*Rule) trafficControllerMap {
tc = generator(r, nil)
}
if tc == nil {
logger.Debugf("Ignoring the frequency params Rule due to bad generated traffic controller: %+v", r)
logger.Debugf("Ignoring the frequent param flow rule due to bad generated traffic controller: %v", r)
continue
}

Expand All @@ -240,7 +239,7 @@ func IsValidRule(rule *Rule) error {
if rule.MetricType < 0 {
return errors.New("invalid metric type")
}
if rule.Behavior < 0 {
if rule.ControlBehavior < 0 {
return errors.New("invalid control strategy")
}
if rule.ParamIndex < 0 {
Expand All @@ -253,7 +252,7 @@ func IsValidRule(rule *Rule) error {
}

func checkControlBehaviorField(rule *Rule) error {
switch rule.Behavior {
switch rule.ControlBehavior {
case Reject:
if rule.BurstCount < 0 {
return errors.New("invalid BurstCount")
Expand Down
26 changes: 13 additions & 13 deletions core/hotspot/rule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func Test_tcGenFuncMap(t *testing.T) {
Id: "abc",
Resource: "abc",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 110,
MaxQueueingTimeMs: 0,
BurstCount: 10,
DurationInSec: 1,
SpecificItems: m,
}
generator, supported := tcGenFuncMap[r1.Behavior]
generator, supported := tcGenFuncMap[r1.ControlBehavior]
assert.True(t, supported && generator != nil)
tc := generator(r1, nil)
assert.True(t, tc.BoundMetric() != nil && tc.BoundRule() == r1 && tc.BoundParamIndex() == 0)
Expand All @@ -54,15 +54,15 @@ func Test_tcGenFuncMap(t *testing.T) {
Id: "abc",
Resource: "abc",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 110,
MaxQueueingTimeMs: 0,
BurstCount: 10,
DurationInSec: 1,
SpecificItems: m,
}
generator, supported := tcGenFuncMap[r1.Behavior]
generator, supported := tcGenFuncMap[r1.ControlBehavior]
assert.True(t, supported && generator != nil)

size := int(math.Min(float64(ParamsMaxCapacity), float64(ParamsCapacityBase*r1.DurationInSec)))
Expand Down Expand Up @@ -100,7 +100,7 @@ func Test_IsValidRule(t *testing.T) {
Id: "abc",
Resource: "abc",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 110,
MaxQueueingTimeMs: 0,
Expand All @@ -124,7 +124,7 @@ func Test_IsValidRule(t *testing.T) {
Id: "",
Resource: "",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 110,
MaxQueueingTimeMs: 0,
Expand All @@ -150,7 +150,7 @@ func Test_buildTcMap(t *testing.T) {
Id: "1",
Resource: "abc",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 100,
MaxQueueingTimeMs: 0,
Expand All @@ -172,7 +172,7 @@ func Test_buildTcMap(t *testing.T) {
Id: "2",
Resource: "abc",
MetricType: QPS,
Behavior: Throttling,
ControlBehavior: Throttling,
ParamIndex: 1,
Threshold: 100,
MaxQueueingTimeMs: 20,
Expand All @@ -194,7 +194,7 @@ func Test_buildTcMap(t *testing.T) {
Id: "3",
Resource: "abc",
MetricType: Concurrency,
Behavior: Throttling,
ControlBehavior: Throttling,
ParamIndex: 2,
Threshold: 100,
MaxQueueingTimeMs: 20,
Expand All @@ -207,7 +207,7 @@ func Test_buildTcMap(t *testing.T) {
Id: "4",
Resource: "abc",
MetricType: Concurrency,
Behavior: Throttling,
ControlBehavior: Throttling,
ParamIndex: 2,
Threshold: 100,
MaxQueueingTimeMs: 20,
Expand All @@ -226,7 +226,7 @@ func Test_buildTcMap(t *testing.T) {
Id: "21",
Resource: "abc",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 100,
MaxQueueingTimeMs: 0,
Expand All @@ -238,7 +238,7 @@ func Test_buildTcMap(t *testing.T) {
Id: "22",
Resource: "abc",
MetricType: QPS,
Behavior: Throttling,
ControlBehavior: Throttling,
ParamIndex: 1,
Threshold: 101,
MaxQueueingTimeMs: 20,
Expand All @@ -250,7 +250,7 @@ func Test_buildTcMap(t *testing.T) {
Id: "23",
Resource: "abc",
MetricType: Concurrency,
Behavior: Throttling,
ControlBehavior: Throttling,
ParamIndex: 2,
Threshold: 100,
MaxQueueingTimeMs: 20,
Expand Down
8 changes: 4 additions & 4 deletions core/hotspot/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Test_Rule_String(t *testing.T) {
Id: "abc",
Resource: "abc",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 110,
MaxQueueingTimeMs: 5,
Expand All @@ -84,7 +84,7 @@ func Test_Rule_String(t *testing.T) {
SpecificItems: m,
}
fmt.Println(fmt.Sprintf("%+v", []*Rule{r}))
assert.True(t, fmt.Sprintf("%+v", []*Rule{r}) == "[{Id:abc, Resource:abc, MetricType:Concurrency, Behavior:Reject, ParamIndex:0, Threshold:110.000000, MaxQueueingTimeMs:5, BurstCount:10, DurationInSec:1, ParamsMaxCapacity:10000, SpecificItems:map[{ValKind:KindString ValStr:sss}:1 {ValKind:KindFloat64 ValStr:1.123}:3]}]")
assert.True(t, fmt.Sprintf("%+v", []*Rule{r}) == "[{Id:abc, Resource:abc, MetricType:Concurrency, ControlBehavior:Reject, ParamIndex:0, Threshold:110.000000, MaxQueueingTimeMs:5, BurstCount:10, DurationInSec:1, ParamsMaxCapacity:10000, SpecificItems:map[{ValKind:KindString ValStr:sss}:1 {ValKind:KindFloat64 ValStr:1.123}:3]}]")
})
}

Expand All @@ -103,7 +103,7 @@ func Test_Rule_Equals(t *testing.T) {
Id: "abc",
Resource: "abc",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 110,
MaxQueueingTimeMs: 5,
Expand All @@ -126,7 +126,7 @@ func Test_Rule_Equals(t *testing.T) {
Id: "abc",
Resource: "abc",
MetricType: Concurrency,
Behavior: Reject,
ControlBehavior: Reject,
ParamIndex: 0,
Threshold: 110,
MaxQueueingTimeMs: 5,
Expand Down
Loading

0 comments on commit 0834aad

Please sign in to comment.