-
Notifications
You must be signed in to change notification settings - Fork 65
/
config.go
192 lines (148 loc) · 3.53 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
package ghostferry
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"github.com/go-sql-driver/mysql"
)
type TLSConfig struct {
CertPath string
ServerName string
tlsConfig *tls.Config
}
func (this *TLSConfig) BuildConfig() (*tls.Config, error) {
if this.tlsConfig == nil {
certPool := x509.NewCertPool()
pem, err := ioutil.ReadFile(this.CertPath)
if err != nil {
return nil, err
}
if ok := certPool.AppendCertsFromPEM(pem); !ok {
return nil, errors.New("unable to append pem")
}
this.tlsConfig = &tls.Config{
RootCAs: certPool,
ServerName: this.ServerName,
}
}
return this.tlsConfig, nil
}
type DatabaseConfig struct {
Host string
Port uint16
User string
Pass string
Collation string
Params map[string]string
TLS *TLSConfig
}
func (c *DatabaseConfig) MySQLConfig() (*mysql.Config, error) {
cfg := &mysql.Config{
User: c.User,
Passwd: c.Pass,
Net: "tcp",
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
Collation: c.Collation,
Params: c.Params,
MultiStatements: true,
}
if c.TLS != nil {
tlsConfig, err := c.TLS.BuildConfig()
if err != nil {
return nil, fmt.Errorf("failed to build TLS config: %v", err)
}
cfgName := fmt.Sprintf("%s@%s:%s", c.User, c.Host, c.Port)
err = mysql.RegisterTLSConfig(cfgName, tlsConfig)
if err != nil {
return nil, fmt.Errorf("failed to register TLS config: %v", err)
}
cfg.TLSConfig = cfgName
}
return cfg, nil
}
func (c *DatabaseConfig) Validate() error {
if c.Host == "" {
return fmt.Errorf("host is empty")
}
if c.Port == 0 {
return fmt.Errorf("port is not specified")
}
if c.User == "" {
return fmt.Errorf("user is empty")
}
err := c.assertParamSet("time_zone", "'+00:00'")
if err != nil {
return err
}
err = c.assertParamSet("sql_mode", "'STRICT_ALL_TABLES,NO_BACKSLASH_ESCAPES'")
if err != nil {
return err
}
return nil
}
func (c *DatabaseConfig) assertParamSet(param, value string) error {
if c.Params == nil {
c.Params = make(map[string]string)
}
if c.Params[param] != "" && c.Params[param] != value {
return fmt.Errorf("%s must be set to %s", param, value)
}
c.Params[param] = value
return nil
}
type Config struct {
Source DatabaseConfig
Target DatabaseConfig
DatabaseRewrites map[string]string
TableRewrites map[string]string
// Config for Ferry
MaxWriteRetriesOnTargetDBError int
TableFilter TableFilter
CopyFilter CopyFilter
// Config for BinlogStreamer
MyServerId uint32
BinlogEventBatchSize int
// Config for DataIterator
IterateChunksize uint64
MaxIterationReadRetries int
NumberOfTableIterators int
AutomaticCutover bool
// Config for the ControlServer
ServerBindAddr string
WebBasedir string
}
func (c *Config) ValidateConfig() error {
if err := c.Source.Validate(); err != nil {
return fmt.Errorf("source: %s", err)
}
if err := c.Target.Validate(); err != nil {
return fmt.Errorf("target: %s", err)
}
if c.TableFilter == nil {
return fmt.Errorf("Table filter function must be provided")
}
if c.MaxWriteRetriesOnTargetDBError == 0 {
c.MaxWriteRetriesOnTargetDBError = 5
}
if c.IterateChunksize == 0 {
c.IterateChunksize = 200
}
if c.BinlogEventBatchSize == 0 {
c.BinlogEventBatchSize = 100
}
if c.NumberOfTableIterators == 0 {
c.NumberOfTableIterators = 4
}
if c.MaxIterationReadRetries == 0 {
c.MaxIterationReadRetries = 5
}
if c.ServerBindAddr == "" {
c.ServerBindAddr = "0.0.0.0:8000"
}
if c.WebBasedir == "" {
c.WebBasedir = "."
}
return nil
}