Skip to content

Commit bdd2897

Browse files
author
Shlomi Noach
authored
Merge pull request #500 from github/enable-extra-port
Enable extra port
2 parents dfc9f41 + 5db266e commit bdd2897

File tree

5 files changed

+56
-43
lines changed

5 files changed

+56
-43
lines changed

go/base/utils.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"regexp"
1212
"strings"
1313
"time"
14+
15+
gosql "database/sql"
16+
"github.com/github/gh-ost/go/mysql"
17+
"github.com/outbrain/golib/log"
1418
)
1519

1620
var (
@@ -50,3 +54,25 @@ func StringContainsAll(s string, substrings ...string) bool {
5054
}
5155
return nonEmptyStringsFound
5256
}
57+
58+
func ValidateConnection(db *gosql.DB, connectionConfig *mysql.ConnectionConfig) (string, error) {
59+
query := `select @@global.port, @@global.version`
60+
var port, extraPort int
61+
var version string
62+
if err := db.QueryRow(query).Scan(&port, &version); err != nil {
63+
return "", err
64+
}
65+
extraPortQuery := `select @@global.extra_port`
66+
if err := db.QueryRow(extraPortQuery).Scan(&extraPort); err != nil {
67+
// swallow this error. not all servers support extra_port
68+
}
69+
70+
if connectionConfig.Key.Port == port || (extraPort > 0 && connectionConfig.Key.Port == extraPort) {
71+
log.Infof("connection validated on %+v", connectionConfig.Key)
72+
return version, nil
73+
} else if extraPort == 0 {
74+
return "", fmt.Errorf("Unexpected database port reported: %+v", port)
75+
} else {
76+
return "", fmt.Errorf("Unexpected database port reported: %+v / extra_port: %+v", port, extraPort)
77+
}
78+
}

go/logic/applier.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ func (this *Applier) InitDBConnections() (err error) {
5353
return err
5454
}
5555
this.singletonDB.SetMaxOpenConns(1)
56-
if err := this.validateConnection(this.db); err != nil {
56+
version, err := base.ValidateConnection(this.db, this.connectionConfig)
57+
if err != nil {
5758
return err
5859
}
59-
if err := this.validateConnection(this.singletonDB); err != nil {
60+
if _, err := base.ValidateConnection(this.singletonDB, this.connectionConfig); err != nil {
6061
return err
6162
}
63+
this.migrationContext.ApplierMySQLVersion = version
6264
if err := this.validateAndReadTimeZone(); err != nil {
6365
return err
6466
}
@@ -74,20 +76,6 @@ func (this *Applier) InitDBConnections() (err error) {
7476
return nil
7577
}
7678

77-
// validateConnection issues a simple can-connect to MySQL
78-
func (this *Applier) validateConnection(db *gosql.DB) error {
79-
query := `select @@global.port, @@global.version`
80-
var port int
81-
if err := db.QueryRow(query).Scan(&port, &this.migrationContext.ApplierMySQLVersion); err != nil {
82-
return err
83-
}
84-
if port != this.connectionConfig.Key.Port {
85-
return fmt.Errorf("Unexpected database port reported: %+v", port)
86-
}
87-
log.Infof("connection validated on %+v", this.connectionConfig.Key)
88-
return nil
89-
}
90-
9179
// validateAndReadTimeZone potentially reads server time-zone
9280
func (this *Applier) validateAndReadTimeZone() error {
9381
query := `select @@global.time_zone`

go/logic/inspect.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,10 @@ func (this *Inspector) validateConnection() error {
195195
if len(this.connectionConfig.Password) > mysql.MaxReplicationPasswordLength {
196196
return fmt.Errorf("MySQL replication length limited to 32 characters. See https://dev.mysql.com/doc/refman/5.7/en/assigning-passwords.html")
197197
}
198-
query := `select @@global.port, @@global.version`
199-
var port int
200-
if err := this.db.QueryRow(query).Scan(&port, &this.migrationContext.InspectorMySQLVersion); err != nil {
201-
return err
202-
}
203-
if port != this.connectionConfig.Key.Port {
204-
return fmt.Errorf("Unexpected database port reported: %+v", port)
205-
}
206-
log.Infof("connection validated on %+v", this.connectionConfig.Key)
207-
return nil
198+
199+
version, err := base.ValidateConnection(this.db, this.connectionConfig)
200+
this.migrationContext.InspectorMySQLVersion = version
201+
return err
208202
}
209203

210204
// validateGrants verifies the user by which we're executing has necessary grants

go/logic/streamer.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (this *EventsStreamer) InitDBConnections() (err error) {
107107
if this.db, _, err = sqlutils.GetDB(EventsStreamerUri); err != nil {
108108
return err
109109
}
110-
if err := this.validateConnection(); err != nil {
110+
if _, err := base.ValidateConnection(this.db, this.connectionConfig); err != nil {
111111
return err
112112
}
113113
if err := this.readCurrentBinlogCoordinates(); err != nil {
@@ -133,20 +133,6 @@ func (this *EventsStreamer) initBinlogReader(binlogCoordinates *mysql.BinlogCoor
133133
return nil
134134
}
135135

136-
// validateConnection issues a simple can-connect to MySQL
137-
func (this *EventsStreamer) validateConnection() error {
138-
query := `select @@global.port`
139-
var port int
140-
if err := this.db.QueryRow(query).Scan(&port); err != nil {
141-
return err
142-
}
143-
if port != this.connectionConfig.Key.Port {
144-
return fmt.Errorf("Unexpected database port reported: %+v", port)
145-
}
146-
log.Infof("connection validated on %+v", this.connectionConfig.Key)
147-
return nil
148-
}
149-
150136
func (this *EventsStreamer) GetCurrentBinlogCoordinates() *mysql.BinlogCoordinates {
151137
return this.binlogReader.GetCurrentBinlogCoordinates()
152138
}

localtests/test.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ verify_master_and_replica() {
2929
echo "Cannot verify gh-ost-test-mysql-replica"
3030
exit 1
3131
fi
32+
if [ "$(gh-ost-test-mysql-replica -e "select @@global.binlog_format" -ss)" != "ROW" ] ; then
33+
echo "Expecting test replica to have binlog_format=ROW"
34+
exit 1
35+
fi
3236
read replica_host replica_port <<< $(gh-ost-test-mysql-replica -e "select @@hostname, @@port" -ss)
3337
}
3438

@@ -42,14 +46,29 @@ echo_dot() {
4246
echo -n "."
4347
}
4448

49+
start_replication() {
50+
gh-ost-test-mysql-replica -e "stop slave; start slave;"
51+
num_attempts=0
52+
while gh-ost-test-mysql-replica -e "show slave status\G" | grep Seconds_Behind_Master | grep -q NULL ; do
53+
((num_attempts=num_attempts+1))
54+
if [ $num_attempts -gt 10 ] ; then
55+
echo
56+
echo "ERROR replication failure"
57+
exit 1
58+
fi
59+
echo_dot
60+
sleep 1
61+
done
62+
}
63+
4564
test_single() {
4665
local test_name
4766
test_name="$1"
4867

4968
echo -n "Testing: $test_name"
5069

5170
echo_dot
52-
gh-ost-test-mysql-replica -e "stop slave; start slave; do sleep(1)"
71+
start_replication
5372
echo_dot
5473
gh-ost-test-mysql-master --default-character-set=utf8mb4 test < $tests_path/$test_name/create.sql
5574

@@ -82,7 +101,7 @@ test_single() {
82101
--table=gh_ost_test \
83102
--alter='engine=innodb' \
84103
--exact-rowcount \
85-
--switch-to-rbr \
104+
--assume-rbr \
86105
--initially-drop-old-table \
87106
--initially-drop-ghost-table \
88107
--throttle-query='select timestampdiff(second, min(last_update), now()) < 5 from _gh_ost_test_ghc' \

0 commit comments

Comments
 (0)