-
Notifications
You must be signed in to change notification settings - Fork 711
/
config.go
244 lines (213 loc) · 7.57 KB
/
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package config
import (
"sync/atomic"
"github.com/BurntSushi/toml"
"github.com/pingcap/errors"
"github.com/pingcap/log"
flag "github.com/spf13/pflag"
"github.com/tikv/pd/pkg/utils/configutil"
"go.uber.org/zap"
)
const (
defaultStoreCount = 50
defaultRegionCount = 1000000
defaultHotStoreCount = 0
defaultReplica = 3
defaultLeaderUpdateRatio = 0.06
defaultEpochUpdateRatio = 0.0
defaultSpaceUpdateRatio = 0.0
defaultFlowUpdateRatio = 0.0
defaultReportRatio = 1
defaultRound = 0
defaultSample = false
defaultInitialVersion = 1
defaultLogFormat = "text"
)
// Config is the heartbeat-bench configuration.
type Config struct {
flagSet *flag.FlagSet
configFile string
PDAddr string
StatusAddr string
Log log.Config `toml:"log" json:"log"`
Logger *zap.Logger
LogProps *log.ZapProperties
Security configutil.SecurityConfig `toml:"security" json:"security"`
InitEpochVer uint64 `toml:"epoch-ver" json:"epoch-ver"`
StoreCount int `toml:"store-count" json:"store-count"`
HotStoreCount int `toml:"hot-store-count" json:"hot-store-count"`
RegionCount int `toml:"region-count" json:"region-count"`
Replica int `toml:"replica" json:"replica"`
LeaderUpdateRatio float64 `toml:"leader-update-ratio" json:"leader-update-ratio"`
EpochUpdateRatio float64 `toml:"epoch-update-ratio" json:"epoch-update-ratio"`
SpaceUpdateRatio float64 `toml:"space-update-ratio" json:"space-update-ratio"`
FlowUpdateRatio float64 `toml:"flow-update-ratio" json:"flow-update-ratio"`
ReportRatio float64 `toml:"report-ratio" json:"report-ratio"`
Sample bool `toml:"sample" json:"sample"`
Round int `toml:"round" json:"round"`
}
// NewConfig return a set of settings.
func NewConfig() *Config {
cfg := &Config{}
cfg.flagSet = flag.NewFlagSet("heartbeat-bench", flag.ContinueOnError)
fs := cfg.flagSet
fs.ParseErrorsWhitelist.UnknownFlags = true
fs.StringVar(&cfg.configFile, "config", "", "config file")
fs.StringVar(&cfg.PDAddr, "pd-endpoints", "127.0.0.1:2379", "pd address")
fs.StringVar(&cfg.Log.File.Filename, "log-file", "", "log file path")
fs.StringVar(&cfg.StatusAddr, "status-addr", "127.0.0.1:20180", "status address")
fs.StringVar(&cfg.Security.CAPath, "cacert", "", "path of file that contains list of trusted TLS CAs")
fs.StringVar(&cfg.Security.CertPath, "cert", "", "path of file that contains X509 certificate in PEM format")
fs.StringVar(&cfg.Security.KeyPath, "key", "", "path of file that contains X509 key in PEM format")
fs.Uint64Var(&cfg.InitEpochVer, "epoch-ver", 1, "the initial epoch version value")
return cfg
}
// Parse parses flag definitions from the argument list.
func (c *Config) Parse(arguments []string) error {
// Parse first to get config file.
err := c.flagSet.Parse(arguments)
if err != nil {
return errors.WithStack(err)
}
// Load config file if specified.
var meta *toml.MetaData
if c.configFile != "" {
meta, err = configutil.ConfigFromFile(c, c.configFile)
if err != nil {
return err
}
}
// Parse again to replace with command line options.
err = c.flagSet.Parse(arguments)
if err != nil {
return errors.WithStack(err)
}
if len(c.flagSet.Args()) != 0 {
return errors.Errorf("'%s' is an invalid flag", c.flagSet.Arg(0))
}
c.Adjust(meta)
return c.Validate()
}
// Adjust is used to adjust configurations
func (c *Config) Adjust(meta *toml.MetaData) {
if len(c.Log.Format) == 0 {
c.Log.Format = defaultLogFormat
}
if !meta.IsDefined("round") {
configutil.AdjustInt(&c.Round, defaultRound)
}
if !meta.IsDefined("store-count") {
configutil.AdjustInt(&c.StoreCount, defaultStoreCount)
}
if !meta.IsDefined("region-count") {
configutil.AdjustInt(&c.RegionCount, defaultRegionCount)
}
if !meta.IsDefined("hot-store-count") {
configutil.AdjustInt(&c.HotStoreCount, defaultHotStoreCount)
}
if !meta.IsDefined("replica") {
configutil.AdjustInt(&c.Replica, defaultReplica)
}
if !meta.IsDefined("leader-update-ratio") {
configutil.AdjustFloat64(&c.LeaderUpdateRatio, defaultLeaderUpdateRatio)
}
if !meta.IsDefined("epoch-update-ratio") {
configutil.AdjustFloat64(&c.EpochUpdateRatio, defaultEpochUpdateRatio)
}
if !meta.IsDefined("space-update-ratio") {
configutil.AdjustFloat64(&c.SpaceUpdateRatio, defaultSpaceUpdateRatio)
}
if !meta.IsDefined("flow-update-ratio") {
configutil.AdjustFloat64(&c.FlowUpdateRatio, defaultFlowUpdateRatio)
}
if !meta.IsDefined("report-ratio") {
configutil.AdjustFloat64(&c.ReportRatio, defaultReportRatio)
}
if !meta.IsDefined("sample") {
c.Sample = defaultSample
}
if !meta.IsDefined("epoch-ver") {
c.InitEpochVer = defaultInitialVersion
}
}
// Validate is used to validate configurations
func (c *Config) Validate() error {
if c.HotStoreCount < 0 || c.HotStoreCount > c.StoreCount {
return errors.Errorf("hot-store-count must be in [0, store-count]")
}
if c.ReportRatio < 0 || c.ReportRatio > 1 {
return errors.Errorf("report-ratio must be in [0, 1]")
}
if c.LeaderUpdateRatio > c.ReportRatio || c.LeaderUpdateRatio < 0 {
return errors.Errorf("leader-update-ratio can not be negative or larger than report-ratio")
}
if c.EpochUpdateRatio > c.ReportRatio || c.EpochUpdateRatio < 0 {
return errors.Errorf("epoch-update-ratio can not be negative or larger than report-ratio")
}
if c.SpaceUpdateRatio > c.ReportRatio || c.SpaceUpdateRatio < 0 {
return errors.Errorf("space-update-ratio can not be negative or larger than report-ratio")
}
if c.FlowUpdateRatio > c.ReportRatio || c.FlowUpdateRatio < 0 {
return errors.Errorf("flow-update-ratio can not be negative or larger than report-ratio")
}
return nil
}
// Clone creates a copy of current config.
func (c *Config) Clone() *Config {
cfg := &Config{}
*cfg = *c
return cfg
}
// Options is the option of the heartbeat-bench.
type Options struct {
HotStoreCount atomic.Value
ReportRatio atomic.Value
LeaderUpdateRatio atomic.Value
EpochUpdateRatio atomic.Value
SpaceUpdateRatio atomic.Value
FlowUpdateRatio atomic.Value
}
// NewOptions creates a new option.
func NewOptions(cfg *Config) *Options {
o := &Options{}
o.HotStoreCount.Store(cfg.HotStoreCount)
o.LeaderUpdateRatio.Store(cfg.LeaderUpdateRatio)
o.EpochUpdateRatio.Store(cfg.EpochUpdateRatio)
o.SpaceUpdateRatio.Store(cfg.SpaceUpdateRatio)
o.FlowUpdateRatio.Store(cfg.FlowUpdateRatio)
o.ReportRatio.Store(cfg.ReportRatio)
return o
}
// GetHotStoreCount returns the hot store count.
func (o *Options) GetHotStoreCount() int {
return o.HotStoreCount.Load().(int)
}
// GetLeaderUpdateRatio returns the leader update ratio.
func (o *Options) GetLeaderUpdateRatio() float64 {
return o.LeaderUpdateRatio.Load().(float64)
}
// GetEpochUpdateRatio returns the epoch update ratio.
func (o *Options) GetEpochUpdateRatio() float64 {
return o.EpochUpdateRatio.Load().(float64)
}
// GetSpaceUpdateRatio returns the space update ratio.
func (o *Options) GetSpaceUpdateRatio() float64 {
return o.SpaceUpdateRatio.Load().(float64)
}
// GetFlowUpdateRatio returns the flow update ratio.
func (o *Options) GetFlowUpdateRatio() float64 {
return o.FlowUpdateRatio.Load().(float64)
}
// GetReportRatio returns the report ratio.
func (o *Options) GetReportRatio() float64 {
return o.ReportRatio.Load().(float64)
}
// SetOptions sets the option.
func (o *Options) SetOptions(cfg *Config) {
o.HotStoreCount.Store(cfg.HotStoreCount)
o.LeaderUpdateRatio.Store(cfg.LeaderUpdateRatio)
o.EpochUpdateRatio.Store(cfg.EpochUpdateRatio)
o.SpaceUpdateRatio.Store(cfg.SpaceUpdateRatio)
o.FlowUpdateRatio.Store(cfg.FlowUpdateRatio)
o.ReportRatio.Store(cfg.ReportRatio)
}