forked from vitessio/vitess
/
max_replication_lag_module_config.go
142 lines (125 loc) · 5.11 KB
/
max_replication_lag_module_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package throttler
import (
"fmt"
"time"
"github.com/youtube/vitess/go/vt/proto/throttlerdata"
)
// MaxReplicationLagModuleConfig stores all configuration parameters for
// MaxReplicationLagModule. Internally, the parameters are represented by a
// protobuf message. This message is also used to update the parameters.
type MaxReplicationLagModuleConfig struct {
throttlerdata.Configuration
}
// Most of the values are based on the assumption that vttablet is started
// with the flag --health_check_interval=20s.
const healthCheckInterval = 20
var defaultMaxReplicationLagModuleConfig = MaxReplicationLagModuleConfig{
throttlerdata.Configuration{
TargetReplicationLagSec: 2,
MaxReplicationLagSec: ReplicationLagModuleDisabled,
InitialRate: 100,
// 1 means 100% i.e. double rates by default.
MaxIncrease: 1,
EmergencyDecrease: 0.5,
// Wait for two health broadcast rounds. Otherwise, the "decrease" mode
// has less than 2 lag records available to calculate the actual slave rate.
MinDurationBetweenIncreasesSec: 2 * healthCheckInterval,
// MaxDurationBetweenIncreasesSec defaults to 60+2 seconds because this
// corresponds to 3 broadcasts.
// The 2 extra seconds give us headroom to account for delay in the process.
MaxDurationBetweenIncreasesSec: 3*healthCheckInterval + 2,
MinDurationBetweenDecreasesSec: healthCheckInterval,
SpreadBacklogAcrossSec: healthCheckInterval,
AgeBadRateAfterSec: 3 * 60,
BadRateIncrease: 0.10,
MaxRateApproachThreshold: 0.90,
},
}
// DefaultMaxReplicationLagModuleConfig returns a copy of the default config object.
func DefaultMaxReplicationLagModuleConfig() MaxReplicationLagModuleConfig {
return defaultMaxReplicationLagModuleConfig
}
// NewMaxReplicationLagModuleConfig returns a default configuration where
// only "maxReplicationLag" is set.
func NewMaxReplicationLagModuleConfig(maxReplicationLag int64) MaxReplicationLagModuleConfig {
config := defaultMaxReplicationLagModuleConfig
config.MaxReplicationLagSec = maxReplicationLag
return config
}
// TODO(mberlin): Add method which updates the config using a (partially) filled
// in protobuf.
// Verify returns an error if the config is invalid.
func (c MaxReplicationLagModuleConfig) Verify() error {
if c.TargetReplicationLagSec < 1 {
return fmt.Errorf("target_replication_lag_sec must be >= 1")
}
if c.MaxReplicationLagSec < 2 {
return fmt.Errorf("max_replication_lag_sec must be >= 2")
}
if c.TargetReplicationLagSec > c.MaxReplicationLagSec {
return fmt.Errorf("target_replication_lag_sec must not be higher than max_replication_lag_sec: invalid: %v > %v",
c.TargetReplicationLagSec, c.MaxReplicationLagSec)
}
if c.InitialRate < 1 {
return fmt.Errorf("initial_rate must be >= 1")
}
if c.MaxIncrease <= 0 {
return fmt.Errorf("max_increase must be > 0")
}
if c.EmergencyDecrease <= 0 {
return fmt.Errorf("emergency_decrease must be > 0")
}
if c.MinDurationBetweenIncreasesSec < 1 {
return fmt.Errorf("min_duration_between_increases_sec must be >= 1")
}
if c.MaxDurationBetweenIncreasesSec < 1 {
return fmt.Errorf("max_duration_between_increases_sec must be >= 1")
}
if c.MinDurationBetweenDecreasesSec < 1 {
return fmt.Errorf("min_duration_between_decreases_sec must be >= 1")
}
if c.SpreadBacklogAcrossSec < 1 {
return fmt.Errorf("spread_backlog_across_sec must be >= 1")
}
if c.IgnoreNSlowestReplicas < 0 {
return fmt.Errorf("ignore_n_slowest_replicas must be >= 0")
}
if c.IgnoreNSlowestRdonlys < 0 {
return fmt.Errorf("ignore_n_slowest_rdonlys must be >= 0")
}
if c.AgeBadRateAfterSec < 1 {
return fmt.Errorf("age_bad_rate_after_sec must be >= 1")
}
if c.MaxRateApproachThreshold < 0 {
return fmt.Errorf("max_rate_approach_threshold must be >=0")
}
if c.MaxRateApproachThreshold > 1 {
return fmt.Errorf("max_rate_approach_threshold must be <=1")
}
return nil
}
// MinDurationBetweenIncreases is a helper function which returns the respective
// protobuf field as native Go type.
func (c MaxReplicationLagModuleConfig) MinDurationBetweenIncreases() time.Duration {
return time.Duration(c.MinDurationBetweenIncreasesSec) * time.Second
}
// MaxDurationBetweenIncreases is a helper function which returns the respective
// protobuf field as native Go type.
func (c MaxReplicationLagModuleConfig) MaxDurationBetweenIncreases() time.Duration {
return time.Duration(c.MaxDurationBetweenIncreasesSec) * time.Second
}
// MinDurationBetweenDecreases is a helper function which returns the respective
// protobuf field as native Go type.
func (c MaxReplicationLagModuleConfig) MinDurationBetweenDecreases() time.Duration {
return time.Duration(c.MinDurationBetweenDecreasesSec) * time.Second
}
// SpreadBacklogAcross is a helper function which returns the respective
// protobuf field as native Go type.
func (c MaxReplicationLagModuleConfig) SpreadBacklogAcross() time.Duration {
return time.Duration(c.SpreadBacklogAcrossSec) * time.Second
}
// AgeBadRateAfter is a helper function which returns the respective
// protobuf field as native Go type.
func (c MaxReplicationLagModuleConfig) AgeBadRateAfter() time.Duration {
return time.Duration(c.AgeBadRateAfterSec) * time.Second
}