forked from pingcap/br
/
global.go
285 lines (258 loc) · 9.24 KB
/
global.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
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/BurntSushi/toml"
"github.com/carlmjohnson/flagext"
"github.com/pingcap/errors"
"github.com/pingcap/br/pkg/lightning/log"
"github.com/pingcap/br/pkg/version/build"
)
type GlobalLightning struct {
log.Config
StatusAddr string `toml:"status-addr" json:"status-addr"`
ServerMode bool `toml:"server-mode" json:"server-mode"`
CheckRequirements bool `toml:"check-requirements" json:"check-requirements"`
// The legacy alias for setting "status-addr". The value should always the
// same as StatusAddr, and will not be published in the JSON encoding.
PProfPort int `toml:"pprof-port" json:"-"`
}
type GlobalTiDB struct {
Host string `toml:"host" json:"host"`
Port int `toml:"port" json:"port"`
User string `toml:"user" json:"user"`
Psw string `toml:"password" json:"-"`
StatusPort int `toml:"status-port" json:"status-port"`
PdAddr string `toml:"pd-addr" json:"pd-addr"`
LogLevel string `toml:"log-level" json:"log-level"`
}
type GlobalMydumper struct {
SourceDir string `toml:"data-source-dir" json:"data-source-dir"`
// Deprecated
NoSchema bool `toml:"no-schema" json:"no-schema"`
Filter []string `toml:"filter" json:"filter"`
IgnoreColumns []*IgnoreColumns `toml:"ignore-columns" json:"ignore-columns"`
}
type GlobalImporter struct {
Addr string `toml:"addr" json:"addr"`
Backend string `toml:"backend" json:"backend"`
SortedKVDir string `toml:"sorted-kv-dir" json:"sorted-kv-dir"`
}
type GlobalConfig struct {
App GlobalLightning `toml:"lightning" json:"lightning"`
Checkpoint GlobalCheckpoint `toml:"checkpoint" json:"checkpoint"`
TiDB GlobalTiDB `toml:"tidb" json:"tidb"`
Mydumper GlobalMydumper `toml:"mydumper" json:"mydumper"`
TikvImporter GlobalImporter `toml:"tikv-importer" json:"tikv-importer"`
PostRestore GlobalPostRestore `toml:"post-restore" json:"post-restore"`
Security Security `toml:"security" json:"security"`
ConfigFileContent []byte
}
type GlobalCheckpoint struct {
Enable bool `toml:"enable" json:"enable"`
}
type GlobalPostRestore struct {
Checksum PostOpLevel `toml:"checksum" json:"checksum"`
Analyze PostOpLevel `toml:"analyze" json:"analyze"`
}
func NewGlobalConfig() *GlobalConfig {
return &GlobalConfig{
App: GlobalLightning{
ServerMode: false,
CheckRequirements: true,
},
Checkpoint: GlobalCheckpoint{
Enable: true,
},
TiDB: GlobalTiDB{
Host: "127.0.0.1",
User: "root",
StatusPort: 10080,
LogLevel: "error",
},
Mydumper: GlobalMydumper{
Filter: DefaultFilter,
},
TikvImporter: GlobalImporter{
Backend: "",
},
PostRestore: GlobalPostRestore{
Checksum: OpLevelRequired,
Analyze: OpLevelOptional,
},
}
}
// Must should be called after LoadGlobalConfig(). If LoadGlobalConfig() returns
// any error, this function will exit the program with an appropriate exit code.
func Must(cfg *GlobalConfig, err error) *GlobalConfig {
switch errors.Cause(err) {
case nil:
case flag.ErrHelp:
os.Exit(0)
default:
fmt.Println("Failed to parse command flags: ", err)
os.Exit(2)
}
return cfg
}
func timestampLogFileName() string {
return filepath.Join(os.TempDir(), time.Now().Format("lightning.log.2006-01-02T15.04.05Z0700"))
}
// LoadGlobalConfig reads the arguments and fills in the GlobalConfig.
func LoadGlobalConfig(args []string, extraFlags func(*flag.FlagSet)) (*GlobalConfig, error) {
cfg := NewGlobalConfig()
fs := flag.NewFlagSet("", flag.ContinueOnError)
// if both `-c` and `-config` are specified, the last one in the command line will take effect.
// the default value is assigned immediately after the StringVar() call,
// so it is fine to not give any default value for `-c`, to keep the `-h` page clean.
var configFilePath string
fs.StringVar(&configFilePath, "c", "", "(deprecated alias of -config)")
fs.StringVar(&configFilePath, "config", "", "tidb-lightning configuration file")
printVersion := fs.Bool("V", false, "print version of lightning")
logLevel := flagext.ChoiceVar(fs, "L", "", `log level: info, debug, warn, error, fatal (default info)`, "", "info", "debug", "warn", "warning", "error", "fatal")
logFilePath := fs.String("log-file", "", "log file path")
tidbHost := fs.String("tidb-host", "", "TiDB server host")
tidbPort := fs.Int("tidb-port", 0, "TiDB server port (default 4000)")
tidbUser := fs.String("tidb-user", "", "TiDB user name to connect")
tidbPsw := fs.String("tidb-password", "", "TiDB password to connect")
tidbStatusPort := fs.Int("tidb-status", 0, "TiDB server status port (default 10080)")
pdAddr := fs.String("pd-urls", "", "PD endpoint address")
dataSrcPath := fs.String("d", "", "Directory of the dump to import")
importerAddr := fs.String("importer", "", "address (host:port) to connect to tikv-importer")
backend := flagext.ChoiceVar(fs, "backend", "", `delivery backend: local, importer, tidb`, "", "local", "importer", "tidb")
sortedKVDir := fs.String("sorted-kv-dir", "", "path for KV pairs when local backend enabled")
enableCheckpoint := fs.Bool("enable-checkpoint", true, "whether to enable checkpoints")
noSchema := fs.Bool("no-schema", false, "ignore schema files, get schema directly from TiDB instead")
checksum := flagext.ChoiceVar(fs, "checksum", "", "compare checksum after importing.", "", "required", "optional", "off", "true", "false")
analyze := flagext.ChoiceVar(fs, "analyze", "", "analyze table after importing", "", "required", "optional", "off", "true", "false")
checkRequirements := fs.Bool("check-requirements", true, "check cluster version before starting")
tlsCAPath := fs.String("ca", "", "CA certificate path for TLS connection")
tlsCertPath := fs.String("cert", "", "certificate path for TLS connection")
tlsKeyPath := fs.String("key", "", "private key path for TLS connection")
redactInfoLog := fs.Bool("redact-info-log", false, "whether to redact sensitive info in log")
statusAddr := fs.String("status-addr", "", "the Lightning server address")
serverMode := fs.Bool("server-mode", false, "start Lightning in server mode, wait for multiple tasks instead of starting immediately")
var filter []string
flagext.StringsVar(fs, &filter, "f", "select tables to import")
if extraFlags != nil {
extraFlags(fs)
}
if err := fs.Parse(args); err != nil {
return nil, errors.Trace(err)
}
if *printVersion {
fmt.Println(build.Info())
return nil, flag.ErrHelp
}
if len(configFilePath) > 0 {
data, err := os.ReadFile(configFilePath)
if err != nil {
return nil, errors.Annotatef(err, "Cannot read config file `%s`", configFilePath)
}
if err = toml.Unmarshal(data, cfg); err != nil {
return nil, errors.Annotatef(err, "Cannot parse config file `%s`", configFilePath)
}
cfg.ConfigFileContent = data
}
if *logLevel != "" {
cfg.App.Config.Level = *logLevel
}
if *logFilePath != "" {
cfg.App.Config.File = *logFilePath
}
// "-" is a special config for log to stdout
if cfg.App.Config.File == "-" {
cfg.App.Config.File = ""
} else if cfg.App.Config.File == "" {
cfg.App.Config.File = timestampLogFileName()
}
if *tidbHost != "" {
cfg.TiDB.Host = *tidbHost
}
if *tidbPort != 0 {
cfg.TiDB.Port = *tidbPort
}
if *tidbStatusPort != 0 {
cfg.TiDB.StatusPort = *tidbStatusPort
}
if *tidbUser != "" {
cfg.TiDB.User = *tidbUser
}
if *tidbPsw != "" {
cfg.TiDB.Psw = *tidbPsw
}
if *pdAddr != "" {
cfg.TiDB.PdAddr = *pdAddr
}
if *dataSrcPath != "" {
cfg.Mydumper.SourceDir = *dataSrcPath
}
if *importerAddr != "" {
cfg.TikvImporter.Addr = *importerAddr
}
if *serverMode {
cfg.App.ServerMode = true
}
if *statusAddr != "" {
cfg.App.StatusAddr = *statusAddr
}
if *backend != "" {
cfg.TikvImporter.Backend = *backend
}
if *sortedKVDir != "" {
cfg.TikvImporter.SortedKVDir = *sortedKVDir
}
if !*enableCheckpoint {
cfg.Checkpoint.Enable = false
}
if *noSchema {
cfg.Mydumper.NoSchema = true
}
if *checksum != "" {
_ = cfg.PostRestore.Checksum.FromStringValue(*checksum)
}
if *analyze != "" {
_ = cfg.PostRestore.Analyze.FromStringValue(*analyze)
}
if cfg.App.StatusAddr == "" && cfg.App.PProfPort != 0 {
cfg.App.StatusAddr = fmt.Sprintf(":%d", cfg.App.PProfPort)
}
if !*checkRequirements {
cfg.App.CheckRequirements = false
}
if *tlsCAPath != "" {
cfg.Security.CAPath = *tlsCAPath
}
if *tlsCertPath != "" {
cfg.Security.CertPath = *tlsCertPath
}
if *tlsKeyPath != "" {
cfg.Security.KeyPath = *tlsKeyPath
}
if *redactInfoLog {
cfg.Security.RedactInfoLog = *redactInfoLog
}
if len(filter) > 0 {
cfg.Mydumper.Filter = filter
}
if cfg.App.StatusAddr == "" && cfg.App.ServerMode {
return nil, errors.New("If server-mode is enabled, the status-addr must be a valid listen address")
}
cfg.App.Config.Adjust()
return cfg, nil
}