-
Notifications
You must be signed in to change notification settings - Fork 218
/
config.go
321 lines (279 loc) · 7.91 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package config
import (
"encoding/json"
"io/ioutil"
"xbase"
"github.com/pkg/errors"
)
const (
NormalBackend = 0
AttachBackend = 1
)
// ProxyConfig tuple.
type ProxyConfig struct {
IPS []string `json:"allowip,omitempty"`
MetaDir string `json:"meta-dir"`
Endpoint string `json:"endpoint"`
TwopcEnable bool `json:"twopc-enable"`
MaxConnections int `json:"max-connections"`
MaxResultSize int `json:"max-result-size"`
MaxJoinRows int `json:"max-join-rows"`
DDLTimeout int `json:"ddl-timeout"`
QueryTimeout int `json:"query-timeout"`
PeerAddress string `json:"peer-address,omitempty"`
LongQueryTime int `json:"long-query-time"`
StreamBufferSize int `json:"stream-buffer-size"`
IdleTxnTimeout uint32 `json:"kill-idle-transaction"` //is consistent with the official 8.0 kill_idle_transaction
AutocommitFalseIsTxn bool `json:"autocommit-false-is-txn"`
}
// DefaultProxyConfig returns default proxy config.
func DefaultProxyConfig() *ProxyConfig {
return &ProxyConfig{
MetaDir: "./radon-meta",
Endpoint: "127.0.0.1:3308",
MaxConnections: 1024,
MaxResultSize: 1024 * 1024 * 1024, // 1GB
MaxJoinRows: 32768,
DDLTimeout: 10 * 3600 * 1000, // 10hours
QueryTimeout: 5 * 60 * 1000, // 5minutes
PeerAddress: "127.0.0.1:8080",
LongQueryTime: 5, // 5 seconds
StreamBufferSize: 1024 * 1024 * 32, // 32MB
IdleTxnTimeout: 60, // 60 seconds
}
}
// UnmarshalJSON interface on ProxyConfig.
func (c *ProxyConfig) UnmarshalJSON(b []byte) error {
type confAlias *ProxyConfig
conf := confAlias(DefaultProxyConfig())
if err := json.Unmarshal(b, conf); err != nil {
return err
}
*c = ProxyConfig(*conf)
return nil
}
// AuditConfig tuple.
type AuditConfig struct {
Mode string `json:"mode"`
LogDir string `json:"audit-dir"`
MaxSize int `json:"max-size"`
ExpireHours int `json:"expire-hours"`
}
// DefaultAuditConfig returns default audit config.
func DefaultAuditConfig() *AuditConfig {
return &AuditConfig{
Mode: "N",
LogDir: "/tmp/auditlog",
MaxSize: 1024 * 1024 * 256, // 256MB
ExpireHours: 1, // 1hours
}
}
// UnmarshalJSON interface on AuditConfig.
func (c *AuditConfig) UnmarshalJSON(b []byte) error {
type confAlias *AuditConfig
conf := confAlias(DefaultAuditConfig())
if err := json.Unmarshal(b, conf); err != nil {
return err
}
*c = AuditConfig(*conf)
return nil
}
// LogConfig tuple.
type LogConfig struct {
Level string `json:"level"`
}
// DefaultLogConfig returns default log config.
func DefaultLogConfig() *LogConfig {
return &LogConfig{
Level: "ERROR",
}
}
// UnmarshalJSON interface on LogConfig.
func (c *LogConfig) UnmarshalJSON(b []byte) error {
type confAlias *LogConfig
conf := confAlias(DefaultLogConfig())
if err := json.Unmarshal(b, conf); err != nil {
return err
}
*c = LogConfig(*conf)
return nil
}
// MonitorConfig tuple
type MonitorConfig struct {
MonitorAddress string `json:"monitor-address"`
}
// DefaultMonitorConfig returns default monitor config.
func DefaultMonitorConfig() *MonitorConfig {
return &MonitorConfig{
MonitorAddress: "0.0.0.0:13380",
}
}
// UnmarshalJSON interface on MonitorConfig.
func (c *MonitorConfig) UnmarshalJSON(b []byte) error {
type confAlias *MonitorConfig
conf := confAlias(DefaultMonitorConfig())
if err := json.Unmarshal(b, conf); err != nil {
return err
}
*c = MonitorConfig(*conf)
return nil
}
// BackendConfig tuple.
type BackendConfig struct {
Name string `json:"name"`
Address string `json:"address"`
User string `json:"user"`
Password string `json:"password"`
DBName string `json:"database"`
Charset string `json:"charset"`
MaxConnections int `json:"max-connections"`
Role int `json:"role"`
}
// BackendsConfig tuple.
type BackendsConfig struct {
Backends []*BackendConfig `json:"backends"`
}
// PartitionConfig tuple.
type PartitionConfig struct {
Table string `json:"table"`
Segment string `json:"segment"`
Backend string `json:"backend"`
}
// AutoIncrement tuple.
type AutoIncrement struct {
Column string `json:"column"`
}
// TableConfig tuple.
type TableConfig struct {
Name string `json:"name"`
Slots int `json:"slots-readonly"`
Blocks int `json:"blocks-readonly"`
ShardType string `json:"shardtype"`
ShardKey string `json:"shardkey"`
Partitions []*PartitionConfig `json:"partitions"`
AutoIncrement *AutoIncrement `json:"auto-increment,omitempty"`
}
// SchemaConfig tuple.
type SchemaConfig struct {
DB string `json:"database"`
Tables []*TableConfig `json:"tables"`
}
// RouterConfig tuple.
type RouterConfig struct {
Slots int `json:"slots-readonly"`
Blocks int `json:"blocks-readonly"`
}
// DefaultRouterConfig returns the default router config.
func DefaultRouterConfig() *RouterConfig {
return &RouterConfig{
Slots: 4096,
Blocks: 64,
}
}
// UnmarshalJSON interface on RouterConfig.
func (c *RouterConfig) UnmarshalJSON(b []byte) error {
type confAlias *RouterConfig
conf := confAlias(DefaultRouterConfig())
if err := json.Unmarshal(b, conf); err != nil {
return err
}
*c = RouterConfig(*conf)
return nil
}
// ScatterConfig tuple.
type ScatterConfig struct {
XaCheckInterval int `json:"xa-check-interval"`
XaCheckDir string `json:"xa-check-dir"`
XaCheckRetrys int `json:"xa-check-retrys`
}
// DefaultScatterConfig returns default ScatterConfig config.
func DefaultScatterConfig() *ScatterConfig {
return &ScatterConfig{
XaCheckInterval: 10,
XaCheckDir: "./xacheck", //In the production environment, don't set the tmp dir
XaCheckRetrys: 10,
}
}
// UnmarshalJSON interface on XaCheckConfig.
func (c *ScatterConfig) UnmarshalJSON(b []byte) error {
type confAlias *ScatterConfig
conf := confAlias(DefaultScatterConfig())
if err := json.Unmarshal(b, conf); err != nil {
return err
}
*c = ScatterConfig(*conf)
return nil
}
// Config tuple.
type Config struct {
Proxy *ProxyConfig `json:"proxy"`
Audit *AuditConfig `json:"audit"`
Router *RouterConfig `json:"router"`
Log *LogConfig `json:"log"`
Monitor *MonitorConfig `json:"monitor"`
Scatter *ScatterConfig `json:"scatter"`
}
func checkConfig(conf *Config) {
if conf.Proxy == nil {
conf.Proxy = DefaultProxyConfig()
}
if conf.Audit == nil {
conf.Audit = DefaultAuditConfig()
}
if conf.Router == nil {
conf.Router = DefaultRouterConfig()
}
if conf.Log == nil {
conf.Log = DefaultLogConfig()
}
if conf.Monitor == nil {
conf.Monitor = DefaultMonitorConfig()
}
if conf.Scatter == nil {
conf.Scatter = DefaultScatterConfig()
}
}
// LoadConfig used to load the config from file.
func LoadConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.WithStack(err)
}
conf := &Config{}
if err := json.Unmarshal([]byte(data), conf); err != nil {
return nil, errors.WithStack(err)
}
checkConfig(conf)
return conf, nil
}
// ReadTableConfig used to read the table config from the data.
func ReadTableConfig(data string) (*TableConfig, error) {
conf := &TableConfig{}
if err := json.Unmarshal([]byte(data), conf); err != nil {
return nil, errors.WithStack(err)
}
return conf, nil
}
// ReadBackendsConfig used to read the backend config from the data.
func ReadBackendsConfig(data string) (*BackendsConfig, error) {
conf := &BackendsConfig{}
if err := json.Unmarshal([]byte(data), conf); err != nil {
return nil, errors.WithStack(err)
}
return conf, nil
}
// WriteConfig used to write the conf to file.
func WriteConfig(path string, conf interface{}) error {
b, err := json.MarshalIndent(conf, "", "\t")
if err != nil {
return errors.WithStack(err)
}
return xbase.WriteFile(path, b)
}