Skip to content

Commit 0834aad

Browse files
sczyh30louyuting
authored andcommitted
Polish comments and logs regarding parameter flow control and util pkg
- Rename field in hotspot.Rule: Behavior -> ControlBehavior Signed-off-by: Eric Zhao <[email protected]>
1 parent 46ea824 commit 0834aad

File tree

14 files changed

+102
-103
lines changed

14 files changed

+102
-103
lines changed

core/base/result.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const (
1111
BlockTypeFlow
1212
BlockTypeCircuitBreaking
1313
BlockTypeSystemFlow
14-
BlockTypeFreqParamsFlow
14+
BlockTypeHotSpotParamFlow
1515
)
1616

1717
func (t BlockType) String() string {
@@ -24,6 +24,8 @@ func (t BlockType) String() string {
2424
return "CircuitBreaking"
2525
case BlockTypeSystemFlow:
2626
return "System"
27+
case BlockTypeHotSpotParamFlow:
28+
return "HotSpotParamFlow"
2729
default:
2830
return fmt.Sprintf("%d", t)
2931
}

core/hotspot/params_metric.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ const (
88
ParamsMaxCapacity = 20000
99
)
1010

11-
// ParamsMetric cache the frequent(hot spot) parameters for each value.
12-
// ParamsMetric is used for pair <resource, TrafficShapingController>.
11+
// ParamsMetric carries real-time counters for frequent ("hot spot") parameters.
12+
//
13+
// For each cache map, the key is the parameter value, while the value is the counter.
1314
type ParamsMetric struct {
14-
// cache's key is the hot value
15-
// cache's value is the counter
16-
// RuleTimeCounter record the last add token time
15+
// RuleTimeCounter records the last added token timestamp.
1716
RuleTimeCounter cache.ConcurrentCounterCache
18-
// RuleTokenCounter record the number of token
17+
// RuleTokenCounter records the number of tokens.
1918
RuleTokenCounter cache.ConcurrentCounterCache
20-
// ConcurrencyCounter record the number of goroutine
19+
// ConcurrencyCounter records the real-time concurrency.
2120
ConcurrencyCounter cache.ConcurrentCounterCache
2221
}

core/hotspot/rule.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ type Rule struct {
8787
// Id is the unique id
8888
Id string
8989
// Resource is the resource name
90-
Resource string
91-
MetricType MetricType
92-
Behavior ControlBehavior
90+
Resource string
91+
MetricType MetricType
92+
ControlBehavior ControlBehavior
9393
// ParamIndex is the index in context arguments slice.
9494
ParamIndex int
9595
Threshold float64
@@ -102,27 +102,27 @@ type Rule struct {
102102
}
103103

104104
func (r *Rule) String() string {
105-
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}",
106-
r.Id, r.Resource, r.MetricType, r.Behavior, r.ParamIndex, r.Threshold, r.MaxQueueingTimeMs, r.BurstCount, r.DurationInSec, r.ParamsMaxCapacity, r.SpecificItems)
105+
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}",
106+
r.Id, r.Resource, r.MetricType, r.ControlBehavior, r.ParamIndex, r.Threshold, r.MaxQueueingTimeMs, r.BurstCount, r.DurationInSec, r.ParamsMaxCapacity, r.SpecificItems)
107107
}
108108
func (r *Rule) ResourceName() string {
109109
return r.Resource
110110
}
111111

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

117117
// IsEqualsTo checks whether current rule is consistent with the given rule.
118118
func (r *Rule) Equals(newRule *Rule) bool {
119-
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)
119+
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)
120120
if !baseCheck {
121121
return false
122122
}
123-
if r.Behavior == Reject {
123+
if r.ControlBehavior == Reject {
124124
return r.BurstCount == newRule.BurstCount
125-
} else if r.Behavior == Throttling {
125+
} else if r.ControlBehavior == Throttling {
126126
return r.MaxQueueingTimeMs == newRule.MaxQueueingTimeMs
127127
} else {
128128
return false
@@ -140,7 +140,7 @@ func parseSpecificItems(source map[SpecificValue]int64) map[interface{}]int64 {
140140
case KindInt:
141141
realVal, err := strconv.Atoi(k.ValStr)
142142
if err != nil {
143-
logger.Errorf("Fail to parse value for int specific item. paramKind: %+v, value: %s, err: %+v", k.ValKind, k.ValStr, err)
143+
logger.Errorf("Failed to parse value for int specific item. paramKind: %+v, value: %s, err: %+v", k.ValKind, k.ValStr, err)
144144
continue
145145
}
146146
ret[realVal] = v
@@ -151,26 +151,25 @@ func parseSpecificItems(source map[SpecificValue]int64) map[interface{}]int64 {
151151
case KindBool:
152152
realVal, err := strconv.ParseBool(k.ValStr)
153153
if err != nil {
154-
logger.Errorf("Fail to parse value for int specific item. value: %s, err: %+v", k.ValStr, err)
154+
logger.Errorf("Failed to parse value for bool specific item. value: %s, err: %+v", k.ValStr, err)
155155
continue
156156
}
157157
ret[realVal] = v
158158

159159
case KindFloat64:
160160
realVal, err := strconv.ParseFloat(k.ValStr, 64)
161161
if err != nil {
162-
logger.Errorf("Fail to parse value for int specific item. value: %s, err: %+v", k.ValStr, err)
162+
logger.Errorf("Failed to parse value for float specific item. value: %s, err: %+v", k.ValStr, err)
163163
continue
164164
}
165165
realVal, err = strconv.ParseFloat(fmt.Sprintf("%.5f", realVal), 64)
166166
if err != nil {
167-
logger.Errorf("Fail to parse value for int specific item. value: %s, err: %+v", k.ValStr, err)
167+
logger.Errorf("Failed to parse value for float specific item. value: %s, err: %+v", k.ValStr, err)
168168
continue
169169
}
170170
ret[realVal] = v
171-
172171
default:
173-
logger.Errorf("Unsupported kind(%d) for specific item.", k.ValKind)
172+
logger.Errorf("Unsupported kind for specific item: %d", k.ValKind)
174173
}
175174
}
176175
return ret

core/hotspot/rule_manager.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,16 @@ func getTrafficControllersFor(res string) []TrafficShapingController {
6060
return tcMap[res]
6161
}
6262

63-
// LoadRules replaces old rules with the given frequency parameters flow control rules.
64-
// return value:
63+
// LoadRules replaces old rules with the given frequent parameter flow control rules. Return value:
6564
//
66-
// bool: was designed to indicate whether the internal map has been changed
67-
// error: was designed to indicate whether occurs the error.
65+
// bool: indicates whether the internal map has been changed;
66+
// error: indicates whether occurs the error.
6867
func LoadRules(rules []*Rule) (bool, error) {
6968
err := onRuleUpdate(rules)
7069
return true, err
7170
}
7271

73-
// GetRules return the res's rules
72+
// GetRules returns existing rules of the given resource.
7473
func GetRules(res string) []*Rule {
7574
tcMux.RLock()
7675
defer tcMux.RUnlock()
@@ -82,7 +81,7 @@ func GetRules(res string) []*Rule {
8281
return ret
8382
}
8483

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

112111
func logRuleUpdate(m trafficControllerMap) {
113112
sb := strings.Builder{}
114-
sb.WriteString("Frequency parameters flow control rules loaded:[")
113+
sb.WriteString("Frequent parameter flow control rules loaded: [")
115114

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

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

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

203202
// generate new traffic shaping controller
204-
generator, supported := tcGenFuncMap[r.Behavior]
203+
generator, supported := tcGenFuncMap[r.ControlBehavior]
205204
if !supported {
206-
logger.Warnf("Ignoring the frequency params Rule due to unsupported control strategy: %+v", r)
205+
logger.Warnf("Ignoring the frequent param flow rule due to unsupported control behavior: %v", r)
207206
continue
208207
}
209208
var tc TrafficShapingController
@@ -214,7 +213,7 @@ func buildTcMap(rules []*Rule) trafficControllerMap {
214213
tc = generator(r, nil)
215214
}
216215
if tc == nil {
217-
logger.Debugf("Ignoring the frequency params Rule due to bad generated traffic controller: %+v", r)
216+
logger.Debugf("Ignoring the frequent param flow rule due to bad generated traffic controller: %v", r)
218217
continue
219218
}
220219

@@ -240,7 +239,7 @@ func IsValidRule(rule *Rule) error {
240239
if rule.MetricType < 0 {
241240
return errors.New("invalid metric type")
242241
}
243-
if rule.Behavior < 0 {
242+
if rule.ControlBehavior < 0 {
244243
return errors.New("invalid control strategy")
245244
}
246245
if rule.ParamIndex < 0 {
@@ -253,7 +252,7 @@ func IsValidRule(rule *Rule) error {
253252
}
254253

255254
func checkControlBehaviorField(rule *Rule) error {
256-
switch rule.Behavior {
255+
switch rule.ControlBehavior {
257256
case Reject:
258257
if rule.BurstCount < 0 {
259258
return errors.New("invalid BurstCount")

core/hotspot/rule_manager_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ func Test_tcGenFuncMap(t *testing.T) {
2323
Id: "abc",
2424
Resource: "abc",
2525
MetricType: Concurrency,
26-
Behavior: Reject,
26+
ControlBehavior: Reject,
2727
ParamIndex: 0,
2828
Threshold: 110,
2929
MaxQueueingTimeMs: 0,
3030
BurstCount: 10,
3131
DurationInSec: 1,
3232
SpecificItems: m,
3333
}
34-
generator, supported := tcGenFuncMap[r1.Behavior]
34+
generator, supported := tcGenFuncMap[r1.ControlBehavior]
3535
assert.True(t, supported && generator != nil)
3636
tc := generator(r1, nil)
3737
assert.True(t, tc.BoundMetric() != nil && tc.BoundRule() == r1 && tc.BoundParamIndex() == 0)
@@ -54,15 +54,15 @@ func Test_tcGenFuncMap(t *testing.T) {
5454
Id: "abc",
5555
Resource: "abc",
5656
MetricType: Concurrency,
57-
Behavior: Reject,
57+
ControlBehavior: Reject,
5858
ParamIndex: 0,
5959
Threshold: 110,
6060
MaxQueueingTimeMs: 0,
6161
BurstCount: 10,
6262
DurationInSec: 1,
6363
SpecificItems: m,
6464
}
65-
generator, supported := tcGenFuncMap[r1.Behavior]
65+
generator, supported := tcGenFuncMap[r1.ControlBehavior]
6666
assert.True(t, supported && generator != nil)
6767

6868
size := int(math.Min(float64(ParamsMaxCapacity), float64(ParamsCapacityBase*r1.DurationInSec)))
@@ -100,7 +100,7 @@ func Test_IsValidRule(t *testing.T) {
100100
Id: "abc",
101101
Resource: "abc",
102102
MetricType: Concurrency,
103-
Behavior: Reject,
103+
ControlBehavior: Reject,
104104
ParamIndex: 0,
105105
Threshold: 110,
106106
MaxQueueingTimeMs: 0,
@@ -124,7 +124,7 @@ func Test_IsValidRule(t *testing.T) {
124124
Id: "",
125125
Resource: "",
126126
MetricType: Concurrency,
127-
Behavior: Reject,
127+
ControlBehavior: Reject,
128128
ParamIndex: 0,
129129
Threshold: 110,
130130
MaxQueueingTimeMs: 0,
@@ -150,7 +150,7 @@ func Test_buildTcMap(t *testing.T) {
150150
Id: "1",
151151
Resource: "abc",
152152
MetricType: Concurrency,
153-
Behavior: Reject,
153+
ControlBehavior: Reject,
154154
ParamIndex: 0,
155155
Threshold: 100,
156156
MaxQueueingTimeMs: 0,
@@ -172,7 +172,7 @@ func Test_buildTcMap(t *testing.T) {
172172
Id: "2",
173173
Resource: "abc",
174174
MetricType: QPS,
175-
Behavior: Throttling,
175+
ControlBehavior: Throttling,
176176
ParamIndex: 1,
177177
Threshold: 100,
178178
MaxQueueingTimeMs: 20,
@@ -194,7 +194,7 @@ func Test_buildTcMap(t *testing.T) {
194194
Id: "3",
195195
Resource: "abc",
196196
MetricType: Concurrency,
197-
Behavior: Throttling,
197+
ControlBehavior: Throttling,
198198
ParamIndex: 2,
199199
Threshold: 100,
200200
MaxQueueingTimeMs: 20,
@@ -207,7 +207,7 @@ func Test_buildTcMap(t *testing.T) {
207207
Id: "4",
208208
Resource: "abc",
209209
MetricType: Concurrency,
210-
Behavior: Throttling,
210+
ControlBehavior: Throttling,
211211
ParamIndex: 2,
212212
Threshold: 100,
213213
MaxQueueingTimeMs: 20,
@@ -226,7 +226,7 @@ func Test_buildTcMap(t *testing.T) {
226226
Id: "21",
227227
Resource: "abc",
228228
MetricType: Concurrency,
229-
Behavior: Reject,
229+
ControlBehavior: Reject,
230230
ParamIndex: 0,
231231
Threshold: 100,
232232
MaxQueueingTimeMs: 0,
@@ -238,7 +238,7 @@ func Test_buildTcMap(t *testing.T) {
238238
Id: "22",
239239
Resource: "abc",
240240
MetricType: QPS,
241-
Behavior: Throttling,
241+
ControlBehavior: Throttling,
242242
ParamIndex: 1,
243243
Threshold: 101,
244244
MaxQueueingTimeMs: 20,
@@ -250,7 +250,7 @@ func Test_buildTcMap(t *testing.T) {
250250
Id: "23",
251251
Resource: "abc",
252252
MetricType: Concurrency,
253-
Behavior: Throttling,
253+
ControlBehavior: Throttling,
254254
ParamIndex: 2,
255255
Threshold: 100,
256256
MaxQueueingTimeMs: 20,

core/hotspot/rule_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func Test_Rule_String(t *testing.T) {
7474
Id: "abc",
7575
Resource: "abc",
7676
MetricType: Concurrency,
77-
Behavior: Reject,
77+
ControlBehavior: Reject,
7878
ParamIndex: 0,
7979
Threshold: 110,
8080
MaxQueueingTimeMs: 5,
@@ -84,7 +84,7 @@ func Test_Rule_String(t *testing.T) {
8484
SpecificItems: m,
8585
}
8686
fmt.Println(fmt.Sprintf("%+v", []*Rule{r}))
87-
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]}]")
87+
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]}]")
8888
})
8989
}
9090

@@ -103,7 +103,7 @@ func Test_Rule_Equals(t *testing.T) {
103103
Id: "abc",
104104
Resource: "abc",
105105
MetricType: Concurrency,
106-
Behavior: Reject,
106+
ControlBehavior: Reject,
107107
ParamIndex: 0,
108108
Threshold: 110,
109109
MaxQueueingTimeMs: 5,
@@ -126,7 +126,7 @@ func Test_Rule_Equals(t *testing.T) {
126126
Id: "abc",
127127
Resource: "abc",
128128
MetricType: Concurrency,
129-
Behavior: Reject,
129+
ControlBehavior: Reject,
130130
ParamIndex: 0,
131131
Threshold: 110,
132132
MaxQueueingTimeMs: 5,

0 commit comments

Comments
 (0)