Skip to content

Commit 4c29041

Browse files
authored
config, balance: add configs to specify the balanced conn ratio (#1045)
1 parent 6682575 commit 4c29041

6 files changed

Lines changed: 53 additions & 31 deletions

File tree

lib/config/balance.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,29 @@ const (
1313
RoutingPolicyPreferIdle = "prefer-idle"
1414
RoutingPolicyRandom = "random"
1515
RoutingPolicyIdlest = "idlest"
16+
17+
// MatchClientCIDRStr is used for MatchClientCIDR.
18+
MatchClientCIDRStr = "client_cidr"
19+
// MatchProxyCIDRStr is used for MatchProxyCIDR.
20+
MatchProxyCIDRStr = "proxy_cidr"
1621
)
1722

1823
type Balance struct {
19-
LabelName string `yaml:"label-name,omitempty" toml:"label-name,omitempty" json:"label-name,omitempty" reloadable:"true"`
20-
RoutingRule string `yaml:"routing-rule,omitempty" toml:"routing-rule,omitempty" json:"routing-rule,omitempty" reloadable:"false"`
21-
Policy string `yaml:"policy,omitempty" toml:"policy,omitempty" json:"policy,omitempty" reloadable:"true"`
22-
RoutingPolicy string `yaml:"routing-policy,omitempty" toml:"routing-policy,omitempty" json:"routing-policy,omitempty" reloadable:"true"`
23-
Status Factor `yaml:"status,omitempty" toml:"status,omitempty" json:"status,omitempty" reloadable:"true"`
24-
Health Factor `yaml:"health,omitempty" toml:"health,omitempty" json:"health,omitempty" reloadable:"true"`
25-
Memory Factor `yaml:"memory,omitempty" toml:"memory,omitempty" json:"memory,omitempty" reloadable:"true"`
26-
CPU Factor `yaml:"cpu,omitempty" toml:"cpu,omitempty" json:"cpu,omitempty" reloadable:"true"`
27-
Location Factor `yaml:"location,omitempty" toml:"location,omitempty" json:"location,omitempty" reloadable:"true"`
28-
ConnCount Factor `yaml:"conn-count,omitempty" toml:"conn-count,omitempty" json:"conn-count,omitempty" reloadable:"true"`
24+
LabelName string `yaml:"label-name,omitempty" toml:"label-name,omitempty" json:"label-name,omitempty" reloadable:"true"`
25+
RoutingRule string `yaml:"routing-rule,omitempty" toml:"routing-rule,omitempty" json:"routing-rule,omitempty" reloadable:"false"`
26+
Policy string `yaml:"policy,omitempty" toml:"policy,omitempty" json:"policy,omitempty" reloadable:"true"`
27+
RoutingPolicy string `yaml:"routing-policy,omitempty" toml:"routing-policy,omitempty" json:"routing-policy,omitempty" reloadable:"true"`
28+
Status Factor `yaml:"status,omitempty" toml:"status,omitempty" json:"status,omitempty" reloadable:"true"`
29+
Health Factor `yaml:"health,omitempty" toml:"health,omitempty" json:"health,omitempty" reloadable:"true"`
30+
Memory Factor `yaml:"memory,omitempty" toml:"memory,omitempty" json:"memory,omitempty" reloadable:"true"`
31+
CPU Factor `yaml:"cpu,omitempty" toml:"cpu,omitempty" json:"cpu,omitempty" reloadable:"true"`
32+
Location Factor `yaml:"location,omitempty" toml:"location,omitempty" json:"location,omitempty" reloadable:"true"`
33+
ConnCount ConnCountFactor `yaml:"conn-count,omitempty" toml:"conn-count,omitempty" json:"conn-count,omitempty" reloadable:"true"`
34+
}
35+
36+
type ConnCountFactor struct {
37+
Factor `yaml:",inline" toml:",inline" json:",inline"`
38+
CountRatioThreshold float64 `yaml:"count-ratio-threshold,omitempty" toml:"count-ratio-threshold,omitempty" json:"count-ratio-threshold,omitempty" reloadable:"true"`
2939
}
3040

3141
type Factor struct {
@@ -35,20 +45,26 @@ type Factor struct {
3545
func (b *Balance) Check() error {
3646
switch b.Policy {
3747
case BalancePolicyResource, BalancePolicyLocation, BalancePolicyConnection:
38-
return nil
3948
case "":
4049
b.Policy = BalancePolicyResource
4150
default:
4251
return errors.Wrapf(ErrInvalidConfigValue, "invalid balance.policy")
4352
}
53+
54+
switch b.RoutingRule {
55+
case MatchClientCIDRStr, MatchProxyCIDRStr, "":
56+
default:
57+
return errors.Wrapf(ErrInvalidConfigValue, "invalid balance.routing-rule")
58+
}
59+
4460
switch b.RoutingPolicy {
4561
case RoutingPolicyPreferIdle, RoutingPolicyRandom:
46-
return nil
4762
case "":
4863
b.RoutingPolicy = RoutingPolicyPreferIdle
4964
default:
5065
return errors.Wrapf(ErrInvalidConfigValue, "invalid balance.route-policy")
5166
}
67+
5268
if b.Status.MigrationsPerSecond < 0 {
5369
return errors.Wrapf(ErrInvalidConfigValue, "invalid balance.status.migrations-per-second")
5470
}
@@ -67,6 +83,9 @@ func (b *Balance) Check() error {
6783
if b.ConnCount.MigrationsPerSecond < 0 {
6884
return errors.Wrapf(ErrInvalidConfigValue, "invalid balance.conn-count.migrations-per-second")
6985
}
86+
if b.ConnCount.CountRatioThreshold != 0 && b.ConnCount.CountRatioThreshold <= 1 {
87+
return errors.Wrapf(ErrInvalidConfigValue, "invalid balance.conn-count.count-ratio-threshold")
88+
}
7089
return nil
7190
}
7291

pkg/balance/factor/factor_conn.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
)
1010

1111
const (
12-
// connBalancedRatio is the threshold of ratio of the most connection count and least count.
12+
// countRatioThreshold is the threshold of ratio of the most connection count and least count.
1313
// If the ratio exceeds the threshold, we migrate connections.
14-
connBalancedRatio = 1.2
14+
countRatioThreshold = 1.2
1515
// Narrow it to 20% in 120s, but the speed is slower and slower because the difference is getting smaller.
1616
// The original difference is 30%: after 120s, the difference is 23.5%.
1717
// The original difference is 100%: after 120s, the difference is 43.9%.
@@ -27,11 +27,13 @@ var _ Factor = (*FactorConnCount)(nil)
2727
type FactorConnCount struct {
2828
bitNum int
2929
migrationsPerSecond float64
30+
countRatioThreshold float64
3031
}
3132

3233
func NewFactorConnCount() *FactorConnCount {
3334
return &FactorConnCount{
34-
bitNum: 16,
35+
bitNum: 16,
36+
countRatioThreshold: countRatioThreshold,
3537
}
3638
}
3739

@@ -56,13 +58,13 @@ func (fcc *FactorConnCount) ScoreBitNum() int {
5658
}
5759

5860
func (fcc *FactorConnCount) BalanceCount(from, to scoredBackend) (BalanceAdvice, float64, []zap.Field) {
59-
if float64(from.ConnScore()) <= float64(to.ConnScore()+1)*connBalancedRatio {
61+
if float64(from.ConnScore()) <= float64(to.ConnScore()+1)*fcc.countRatioThreshold {
6062
return AdviceNeutral, 0, nil
6163
}
6264
if fcc.migrationsPerSecond > 0 {
6365
return AdvicePositive, fcc.migrationsPerSecond, nil
6466
}
65-
targetTo := float64(from.ConnScore()+to.ConnScore()+1) / (1 + connBalancedRatio)
67+
targetTo := float64(from.ConnScore()+to.ConnScore()+1) / (1 + fcc.countRatioThreshold)
6668
count := (targetTo - float64(to.ConnScore()+1)) / balanceSeconds4Conn
6769
if count < 0 {
6870
count = 0
@@ -72,6 +74,10 @@ func (fcc *FactorConnCount) BalanceCount(from, to scoredBackend) (BalanceAdvice,
7274

7375
func (fcc *FactorConnCount) SetConfig(cfg *config.Config) {
7476
fcc.migrationsPerSecond = cfg.Balance.ConnCount.MigrationsPerSecond
77+
fcc.countRatioThreshold = cfg.Balance.ConnCount.CountRatioThreshold
78+
if fcc.countRatioThreshold <= 1 {
79+
fcc.countRatioThreshold = countRatioThreshold
80+
}
7581
}
7682

7783
func (fcc *FactorConnCount) CanBeRouted(_ uint64) bool {

pkg/balance/factor/factor_conn_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,24 @@ func TestFactorConnConfig(t *testing.T) {
103103
speed float64
104104
}{
105105
{
106-
score1: 120,
106+
score1: 150,
107107
score2: 100,
108108
speed: 0,
109109
},
110110
{
111-
score1: 150,
111+
score1: 300,
112112
score2: 100,
113113
speed: 10,
114114
},
115115
}
116116

117117
factor := NewFactorConnCount()
118-
factor.SetConfig(&config.Config{Balance: config.Balance{ConnCount: config.Factor{MigrationsPerSecond: 10}}})
118+
cfg := config.Config{}
119+
cfg.Balance.ConnCount.MigrationsPerSecond = 10
120+
cfg.Balance.ConnCount.CountRatioThreshold = 2
121+
factor.SetConfig(&cfg)
119122
require.EqualValues(t, 10, factor.migrationsPerSecond)
123+
require.EqualValues(t, 2, factor.countRatioThreshold)
120124
backend1 := newMockBackend(true, 0)
121125
backend2 := newMockBackend(true, 0)
122126
scoredBackend1 := newScoredBackend(backend1, zap.NewNop())
@@ -125,6 +129,6 @@ func TestFactorConnConfig(t *testing.T) {
125129
backend1.connScore = test.score1
126130
backend2.connScore = test.score2
127131
_, balanceCount, _ := factor.BalanceCount(scoredBackend1, scoredBackend2)
128-
require.EqualValues(t, balanceCount, test.speed, "case id: %d", i)
132+
require.EqualValues(t, test.speed, balanceCount, "case id: %d", i)
129133
}
130134
}

pkg/balance/router/group.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ const (
3232
MatchProxyCIDR
3333
)
3434

35-
const (
36-
// MatchClientCIDRStr is used for MatchClientCIDR.
37-
MatchClientCIDRStr = "client_cidr"
38-
// MatchProxyCIDRStr is used for MatchProxyCIDR.
39-
MatchProxyCIDRStr = "proxy_cidr"
40-
)
41-
4235
var _ ConnEventReceiver = (*Group)(nil)
4336

4437
// Group is used for one backend group that can be matched by CIDR, username, database, or resource group list.

pkg/balance/router/router_score.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ func (r *ScoreBasedRouter) Init(ctx context.Context, ob observer.BackendObserver
7070

7171
r.matchType = MatchAll
7272
switch strings.ToLower(cfg.Balance.RoutingRule) {
73-
case MatchClientCIDRStr:
73+
case config.MatchClientCIDRStr:
7474
r.matchType = MatchClientCIDR
75-
case MatchProxyCIDRStr:
75+
case config.MatchProxyCIDRStr:
7676
r.matchType = MatchProxyCIDR
7777
case "":
7878
default:

pkg/balance/router/router_score_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ func TestGroupBackends(t *testing.T) {
10101010
cfgCh := make(chan *config.Config)
10111011
cfg := &config.Config{
10121012
Balance: config.Balance{
1013-
RoutingRule: MatchClientCIDRStr,
1013+
RoutingRule: config.MatchClientCIDRStr,
10141014
},
10151015
}
10161016
cfgGetter := newMockConfigGetter(cfg)

0 commit comments

Comments
 (0)