@@ -43,7 +43,11 @@ type MigrationContext struct {
43
43
AllowedRunningOnMaster bool
44
44
SwitchToRowBinlogFormat bool
45
45
46
- ConfigFile string
46
+ config ContextConfig
47
+ configMutex * sync.Mutex
48
+ ConfigFile string
49
+ CliUser string
50
+ CliPassword string
47
51
48
52
ChunkSize int64
49
53
MaxLagMillisecondsThrottleThreshold int64
@@ -96,6 +100,19 @@ type MigrationContext struct {
96
100
CanStopStreaming func () bool
97
101
}
98
102
103
+ type ContextConfig struct {
104
+ Client struct {
105
+ User string
106
+ Password string
107
+ }
108
+ Osc struct {
109
+ Chunk_Size int64
110
+ Max_Lag_Millis int64
111
+ Replication_Lag_Query string
112
+ Max_Load string
113
+ }
114
+ }
115
+
99
116
var context * MigrationContext
100
117
101
118
func init () {
@@ -112,6 +129,7 @@ func newMigrationContext() *MigrationContext {
112
129
MaxLoad : make (map [string ]int64 ),
113
130
throttleMutex : & sync.Mutex {},
114
131
ThrottleControlReplicaKeys : mysql .NewInstanceKeyMap (),
132
+ configMutex : & sync.Mutex {},
115
133
}
116
134
}
117
135
@@ -210,6 +228,8 @@ func (this *MigrationContext) IsThrottled() (bool, string) {
210
228
return this .isThrottled , this .throttleReason
211
229
}
212
230
231
+ // ReadMaxLoad parses the `--max-load` flag, which is in multiple key-value format,
232
+ // such as: 'Threads_running=100,Threads_connected=500'
213
233
func (this * MigrationContext ) ReadMaxLoad (maxLoadList string ) error {
214
234
if maxLoadList == "" {
215
235
return nil
@@ -232,31 +252,37 @@ func (this *MigrationContext) ReadMaxLoad(maxLoadList string) error {
232
252
return nil
233
253
}
234
254
255
+ // ApplyCredentials sorts out the credentials between the config file and the CLI flags
256
+ func (this * MigrationContext ) ApplyCredentials () {
257
+ this .configMutex .Lock ()
258
+ defer this .configMutex .Unlock ()
259
+
260
+ if this .config .Client .User != "" {
261
+ this .InspectorConnectionConfig .User = this .config .Client .User
262
+ }
263
+ if this .CliUser != "" {
264
+ // Override
265
+ this .InspectorConnectionConfig .User = this .CliUser
266
+ }
267
+ if this .config .Client .Password != "" {
268
+ this .InspectorConnectionConfig .Password = this .config .Client .Password
269
+ }
270
+ if this .CliPassword != "" {
271
+ // Override
272
+ this .InspectorConnectionConfig .Password = this .CliPassword
273
+ }
274
+ }
275
+
276
+ // ReadConfigFile attempts to read the config file, if it exists
235
277
func (this * MigrationContext ) ReadConfigFile () error {
278
+ this .configMutex .Lock ()
279
+ defer this .configMutex .Unlock ()
280
+
236
281
if this .ConfigFile == "" {
237
282
return nil
238
283
}
239
- conf := struct {
240
- Client struct {
241
- User string
242
- Password string
243
- }
244
- Osc struct {
245
- Chunk_Size int64
246
- Max_Lag_Millis int64
247
- Replication_Lag_Query string
248
- Max_Load string
249
- }
250
- }{}
251
- if err := gcfg .ReadFileInto (& conf , this .ConfigFile ); err != nil {
284
+ if err := gcfg .ReadFileInto (& this .config , this .ConfigFile ); err != nil {
252
285
return err
253
286
}
254
- if this .InspectorConnectionConfig .User == "" {
255
- this .InspectorConnectionConfig .User = conf .Client .User
256
- }
257
- if this .InspectorConnectionConfig .Password == "" {
258
- this .InspectorConnectionConfig .Password = conf .Client .Password
259
- }
260
-
261
287
return nil
262
288
}
0 commit comments