Skip to content

Commit c399119

Browse files
author
Shlomi Noach
authored
Merge pull request #263 from github/implied-key-fix
assume-master-host now applies ImpliedKey
2 parents 4d903d0 + c280197 commit c399119

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

go/logic/migrator.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -616,16 +616,22 @@ func (this *Migrator) initiateInspector() (err error) {
616616
}
617617
// So far so good, table is accessible and valid.
618618
// Let's get master connection config
619-
if this.migrationContext.ApplierConnectionConfig, err = this.inspector.getMasterConnectionConfig(); err != nil {
620-
return err
621-
}
622-
if this.migrationContext.AssumeMasterHostname != "" {
623-
if key, err := mysql.ParseRawInstanceKeyLoose(this.migrationContext.AssumeMasterHostname); err != nil {
619+
if this.migrationContext.AssumeMasterHostname == "" {
620+
// No forced master host; detect master
621+
if this.migrationContext.ApplierConnectionConfig, err = this.inspector.getMasterConnectionConfig(); err != nil {
622+
return err
623+
}
624+
log.Infof("Master found to be %+v", *this.migrationContext.ApplierConnectionConfig.ImpliedKey)
625+
} else {
626+
// Forced master host.
627+
key, err := mysql.ParseRawInstanceKeyLoose(this.migrationContext.AssumeMasterHostname)
628+
if err != nil {
624629
return err
625-
} else {
626-
this.migrationContext.ApplierConnectionConfig.Key = *key
627630
}
631+
this.migrationContext.ApplierConnectionConfig = this.migrationContext.InspectorConnectionConfig.DuplicateCredentials(*key)
632+
log.Infof("Master forced to be %+v", *this.migrationContext.ApplierConnectionConfig.ImpliedKey)
628633
}
634+
// validate configs
629635
if this.migrationContext.TestOnReplica || this.migrationContext.MigrateOnReplica {
630636
if this.migrationContext.InspectorIsAlsoApplier() {
631637
return fmt.Errorf("Instructed to --test-on-replica or --migrate-on-replica, but the server we connect to doesn't seem to be a replica")
@@ -638,13 +644,12 @@ func (this *Migrator) initiateInspector() (err error) {
638644
this.migrationContext.AddThrottleControlReplicaKey(this.migrationContext.InspectorConnectionConfig.Key)
639645
}
640646
} else if this.migrationContext.InspectorIsAlsoApplier() && !this.migrationContext.AllowedRunningOnMaster {
641-
return fmt.Errorf("It seems like this migration attempt to run directly on master. Preferably it would be executed on a replica (and this reduces load from the master). To proceed please provide --allow-on-master")
647+
return fmt.Errorf("It seems like this migration attempt to run directly on master. Preferably it would be executed on a replica (and this reduces load from the master). To proceed please provide --allow-on-master. Inspector config=%+v, applier config=%+v", this.migrationContext.InspectorConnectionConfig, this.migrationContext.ApplierConnectionConfig)
642648
}
643649
if err := this.inspector.validateLogSlaveUpdates(); err != nil {
644650
return err
645651
}
646652

647-
log.Infof("Master found to be %+v", *this.migrationContext.ApplierConnectionConfig.ImpliedKey)
648653
return nil
649654
}
650655

go/mysql/connection.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ func NewConnectionConfig() *ConnectionConfig {
2626
return config
2727
}
2828

29-
func (this *ConnectionConfig) Duplicate() *ConnectionConfig {
29+
// DuplicateCredentials creates a new connection config with given key and with same credentials as this config
30+
func (this *ConnectionConfig) DuplicateCredentials(key InstanceKey) *ConnectionConfig {
3031
config := &ConnectionConfig{
31-
Key: InstanceKey{
32-
Hostname: this.Key.Hostname,
33-
Port: this.Key.Port,
34-
},
32+
Key: key,
3533
User: this.User,
3634
Password: this.Password,
3735
}
3836
config.ImpliedKey = &config.Key
3937
return config
4038
}
4139

40+
func (this *ConnectionConfig) Duplicate() *ConnectionConfig {
41+
return this.DuplicateCredentials(this.Key)
42+
}
43+
4244
func (this *ConnectionConfig) String() string {
4345
return fmt.Sprintf("%s, user=%s", this.Key.DisplayString(), this.User)
4446
}

go/mysql/connection_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2016 GitHub Inc.
3+
See https://github.com/github/gh-ost/blob/master/LICENSE
4+
*/
5+
6+
package mysql
7+
8+
import (
9+
"testing"
10+
11+
"github.com/outbrain/golib/log"
12+
test "github.com/outbrain/golib/tests"
13+
)
14+
15+
func init() {
16+
log.SetLevel(log.ERROR)
17+
}
18+
19+
func TestNewConnectionConfig(t *testing.T) {
20+
c := NewConnectionConfig()
21+
test.S(t).ExpectEquals(c.Key.Hostname, "")
22+
test.S(t).ExpectEquals(c.Key.Port, 0)
23+
test.S(t).ExpectEquals(c.ImpliedKey.Hostname, "")
24+
test.S(t).ExpectEquals(c.ImpliedKey.Port, 0)
25+
test.S(t).ExpectEquals(c.User, "")
26+
test.S(t).ExpectEquals(c.Password, "")
27+
}
28+
29+
func TestDuplicateCredentials(t *testing.T) {
30+
c := NewConnectionConfig()
31+
c.Key = InstanceKey{Hostname: "myhost", Port: 3306}
32+
c.User = "gromit"
33+
c.Password = "penguin"
34+
35+
dup := c.DuplicateCredentials(InstanceKey{Hostname: "otherhost", Port: 3310})
36+
test.S(t).ExpectEquals(dup.Key.Hostname, "otherhost")
37+
test.S(t).ExpectEquals(dup.Key.Port, 3310)
38+
test.S(t).ExpectEquals(dup.ImpliedKey.Hostname, "otherhost")
39+
test.S(t).ExpectEquals(dup.ImpliedKey.Port, 3310)
40+
test.S(t).ExpectEquals(dup.User, "gromit")
41+
test.S(t).ExpectEquals(dup.Password, "penguin")
42+
}
43+
44+
func TestDuplicate(t *testing.T) {
45+
c := NewConnectionConfig()
46+
c.Key = InstanceKey{Hostname: "myhost", Port: 3306}
47+
c.User = "gromit"
48+
c.Password = "penguin"
49+
50+
dup := c.Duplicate()
51+
test.S(t).ExpectEquals(dup.Key.Hostname, "myhost")
52+
test.S(t).ExpectEquals(dup.Key.Port, 3306)
53+
test.S(t).ExpectEquals(dup.ImpliedKey.Hostname, "myhost")
54+
test.S(t).ExpectEquals(dup.ImpliedKey.Port, 3306)
55+
test.S(t).ExpectEquals(dup.User, "gromit")
56+
test.S(t).ExpectEquals(dup.Password, "penguin")
57+
}

0 commit comments

Comments
 (0)