Skip to content

Commit 961d2c0

Browse files
authored
sync-diff-inspector: add default outputDir and support to export the template config (#553)
close #570
1 parent c5e06ff commit 961d2c0

File tree

3 files changed

+148
-2
lines changed

3 files changed

+148
-2
lines changed

sync_diff_inspector/config/config.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"path/filepath"
2424
"syscall"
25+
"time"
2526

2627
"github.com/BurntSushi/toml"
2728
"github.com/pingcap/errors"
@@ -179,9 +180,12 @@ func (t *TaskConfig) Init(
179180
return errors.Trace(err)
180181
}
181182

182-
// Create output Dir if not exists
183+
// Set default value when output is empty
183184
if t.OutputDir == "" {
184-
return errors.New("output-dir is missing from the task configuration")
185+
t.OutputDir = timestampOutputDir()
186+
if err := os.RemoveAll(t.OutputDir); err != nil && !os.IsNotExist(err) {
187+
log.Fatal("fail to remove the temp directory", zap.String("path", t.OutputDir), zap.String("error", err.Error()))
188+
}
185189
}
186190

187191
ok, err = pathExists(t.OutputDir)
@@ -292,6 +296,9 @@ type Config struct {
292296
// config file
293297
ConfigFile string
294298

299+
// export a template config file in the current directory
300+
Template string `toml:"-" json:"-"`
301+
295302
// print version if set true
296303
PrintVersion bool
297304
}
@@ -305,6 +312,7 @@ func NewConfig() *Config {
305312
fs.BoolVarP(&cfg.PrintVersion, "version", "V", false, "print version of sync_diff_inspector")
306313
fs.StringVarP(&cfg.LogLevel, "log-level", "L", "info", "log level: debug, info, warn, error, fatal")
307314
fs.StringVarP(&cfg.ConfigFile, "config", "C", "", "Config file")
315+
fs.StringVarP(&cfg.Template, "template", "T", "", "<dm|norm> export a template config file in the current directory")
308316
fs.StringVar(&cfg.DMAddr, "dm-addr", "", "the address of DM")
309317
fs.StringVar(&cfg.DMTask, "dm-task", "", "identifier of dm task")
310318
fs.IntVar(&cfg.CheckThreadCount, "check-thread-count", 1, "how many goroutines are created to check data")
@@ -327,6 +335,10 @@ func (c *Config) Parse(arguments []string) error {
327335
return nil
328336
}
329337

338+
if c.Template != "" {
339+
return nil
340+
}
341+
330342
// Load config file if specified.
331343
if c.ConfigFile == "" {
332344
return errors.Errorf("argument --config is required")
@@ -475,6 +487,10 @@ func (c *Config) CheckConfig() bool {
475487
return true
476488
}
477489

490+
func timestampOutputDir() string {
491+
return filepath.Join(os.TempDir(), time.Now().Format("sync-diff.output.2006-01-02T15.04.05Z0700"))
492+
}
493+
478494
func pathExists(_path string) (bool, error) {
479495
_, err := os.Stat(_path)
480496
if err != nil {
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright 2021 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package config
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/pingcap/errors"
20+
)
21+
22+
const (
23+
dmConfig = `# Diff Configuration.
24+
25+
######################### Global config #########################
26+
27+
check-thread-count = 4
28+
29+
export-fix-sql = true
30+
31+
check-struct-only = false
32+
33+
dm-addr = "http://127.0.0.1:8261"
34+
35+
dm-task = "test"
36+
37+
######################### Task config #########################
38+
[task]
39+
output-dir = "./output"
40+
41+
target-check-tables = ["hb_test.*"]
42+
43+
`
44+
45+
normConfig = `# Diff Configuration.
46+
47+
######################### Global config #########################
48+
49+
check-thread-count = 4
50+
51+
export-fix-sql = true
52+
53+
check-struct-only = false
54+
55+
56+
######################### Datasource config #########################
57+
[data-sources]
58+
[data-sources.mysql1]
59+
host = "127.0.0.1"
60+
port = 3306
61+
user = "root"
62+
password = ""
63+
64+
route-rules = ["rule1", "rule2"]
65+
66+
[data-sources.tidb0]
67+
host = "127.0.0.1"
68+
port = 4000
69+
user = "root"
70+
password = ""
71+
72+
# snapshot = "386902609362944000"
73+
74+
########################### Routes ###########################
75+
[routes]
76+
[routes.rule1]
77+
schema-pattern = "test_*"
78+
table-pattern = "t_*"
79+
target-schema = "test"
80+
target-table = "t"
81+
82+
[routes.rule2]
83+
schema-pattern = "test2_*"
84+
table-pattern = "t2_*"
85+
target-schema = "test2"
86+
target-table = "t2"
87+
88+
######################### Task config #########################
89+
[task]
90+
output-dir = "./output"
91+
92+
source-instances = ["mysql1"]
93+
94+
target-instance = "tidb0"
95+
96+
target-check-tables = ["schema*.table*", "!c.*", "test2.t2"]
97+
98+
target-configs = ["config1"]
99+
100+
######################### Table config #########################
101+
[table-configs.config1]
102+
target-tables = ["schema*.test*", "test2.t2"]
103+
range = "age > 10 AND age < 20"
104+
index-fields = ["col1","col2"]
105+
ignore-columns = ["",""]
106+
chunk-size = 0
107+
collation = ""
108+
109+
`
110+
)
111+
112+
func ExportTemplateConfig(configType string) error {
113+
switch configType {
114+
case "dm", "DM", "Dm", "dM":
115+
fmt.Print(dmConfig)
116+
case "norm", "normal", "Norm", "Normal":
117+
fmt.Print(normConfig)
118+
default:
119+
return errors.Errorf("Error: unexpect template name: %s\n-T dm: export a dm config\n-T norm: export a normal config\n", configType)
120+
}
121+
return nil
122+
}

sync_diff_inspector/main.go

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ func main() {
6464
return
6565
}
6666

67+
if cfg.Template != "" {
68+
if err := config.ExportTemplateConfig(cfg.Template); err != nil {
69+
fmt.Printf("%s\n", err.Error())
70+
os.Exit(2)
71+
}
72+
return
73+
}
74+
6775
conf := new(log.Config)
6876
conf.Level = cfg.LogLevel
6977

0 commit comments

Comments
 (0)