Skip to content

Commit 5acc5cc

Browse files
timvaillancourtRainbowDashy
authored andcommitted
Use switch statements for readability, simplify .NewGoMySQLReader() (github#1135)
* Use `switch` statements for readability * Simplify initBinlogReader()
1 parent e270633 commit 5acc5cc

File tree

3 files changed

+32
-40
lines changed

3 files changed

+32
-40
lines changed

go/binlog/gomysql_reader.go

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,24 @@ type GoMySQLReader struct {
2828
LastAppliedRowsEventHint mysql.BinlogCoordinates
2929
}
3030

31-
func NewGoMySQLReader(migrationContext *base.MigrationContext) (binlogReader *GoMySQLReader, err error) {
32-
binlogReader = &GoMySQLReader{
31+
func NewGoMySQLReader(migrationContext *base.MigrationContext) *GoMySQLReader {
32+
connectionConfig := migrationContext.InspectorConnectionConfig
33+
return &GoMySQLReader{
3334
migrationContext: migrationContext,
34-
connectionConfig: migrationContext.InspectorConnectionConfig,
35+
connectionConfig: connectionConfig,
3536
currentCoordinates: mysql.BinlogCoordinates{},
3637
currentCoordinatesMutex: &sync.Mutex{},
37-
binlogSyncer: nil,
38-
binlogStreamer: nil,
38+
binlogSyncer: replication.NewBinlogSyncer(replication.BinlogSyncerConfig{
39+
ServerID: uint32(migrationContext.ReplicaServerId),
40+
Flavor: gomysql.MySQLFlavor,
41+
Host: connectionConfig.Key.Hostname,
42+
Port: uint16(connectionConfig.Key.Port),
43+
User: connectionConfig.User,
44+
Password: connectionConfig.Password,
45+
TLSConfig: connectionConfig.TLSConfig(),
46+
UseDecimal: true,
47+
}),
3948
}
40-
41-
serverId := uint32(migrationContext.ReplicaServerId)
42-
43-
binlogSyncerConfig := replication.BinlogSyncerConfig{
44-
ServerID: serverId,
45-
Flavor: "mysql",
46-
Host: binlogReader.connectionConfig.Key.Hostname,
47-
Port: uint16(binlogReader.connectionConfig.Key.Port),
48-
User: binlogReader.connectionConfig.User,
49-
Password: binlogReader.connectionConfig.Password,
50-
TLSConfig: binlogReader.connectionConfig.TLSConfig(),
51-
UseDecimal: true,
52-
}
53-
binlogReader.binlogSyncer = replication.NewBinlogSyncer(binlogSyncerConfig)
54-
55-
return binlogReader, err
5649
}
5750

5851
// ConnectBinlogStreamer
@@ -145,15 +138,17 @@ func (this *GoMySQLReader) StreamEvents(canStopStreaming func() bool, entriesCha
145138
defer this.currentCoordinatesMutex.Unlock()
146139
this.currentCoordinates.LogPos = int64(ev.Header.LogPos)
147140
}()
148-
if rotateEvent, ok := ev.Event.(*replication.RotateEvent); ok {
141+
142+
switch binlogEvent := ev.Event.(type) {
143+
case *replication.RotateEvent:
149144
func() {
150145
this.currentCoordinatesMutex.Lock()
151146
defer this.currentCoordinatesMutex.Unlock()
152-
this.currentCoordinates.LogFile = string(rotateEvent.NextLogName)
147+
this.currentCoordinates.LogFile = string(binlogEvent.NextLogName)
153148
}()
154-
this.migrationContext.Log.Infof("rotate to next log from %s:%d to %s", this.currentCoordinates.LogFile, int64(ev.Header.LogPos), rotateEvent.NextLogName)
155-
} else if rowsEvent, ok := ev.Event.(*replication.RowsEvent); ok {
156-
if err := this.handleRowsEvent(ev, rowsEvent, entriesChannel); err != nil {
149+
this.migrationContext.Log.Infof("rotate to next log from %s:%d to %s", this.currentCoordinates.LogFile, int64(ev.Header.LogPos), binlogEvent.NextLogName)
150+
case *replication.RowsEvent:
151+
if err := this.handleRowsEvent(ev, binlogEvent, entriesChannel); err != nil {
157152
return err
158153
}
159154
}

go/logic/migrator.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -541,19 +541,19 @@ func (this *Migrator) cutOver() (err error) {
541541
}
542542
}
543543
}
544-
if this.migrationContext.CutOverType == base.CutOverAtomic {
544+
545+
switch this.migrationContext.CutOverType {
546+
case base.CutOverAtomic:
545547
// Atomic solution: we use low timeout and multiple attempts. But for
546548
// each failed attempt, we throttle until replication lag is back to normal
547-
err := this.atomicCutOver()
548-
this.handleCutOverResult(err)
549-
return err
550-
}
551-
if this.migrationContext.CutOverType == base.CutOverTwoStep {
552-
err := this.cutOverTwoStep()
553-
this.handleCutOverResult(err)
554-
return err
549+
err = this.atomicCutOver()
550+
case base.CutOverTwoStep:
551+
err = this.cutOverTwoStep()
552+
default:
553+
return this.migrationContext.Log.Errorf("Unknown cut-over type: %d; should never get here!", this.migrationContext.CutOverType)
555554
}
556-
return this.migrationContext.Log.Errorf("Unknown cut-over type: %d; should never get here!", this.migrationContext.CutOverType)
555+
this.handleCutOverResult(err)
556+
return err
557557
}
558558

559559
// Inject the "AllEventsUpToLockProcessed" state hint, wait for it to appear in the binary logs,

go/logic/streamer.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ func (this *EventsStreamer) InitDBConnections() (err error) {
124124

125125
// initBinlogReader creates and connects the reader: we hook up to a MySQL server as a replica
126126
func (this *EventsStreamer) initBinlogReader(binlogCoordinates *mysql.BinlogCoordinates) error {
127-
goMySQLReader, err := binlog.NewGoMySQLReader(this.migrationContext)
128-
if err != nil {
129-
return err
130-
}
127+
goMySQLReader := binlog.NewGoMySQLReader(this.migrationContext)
131128
if err := goMySQLReader.ConnectBinlogStreamer(*binlogCoordinates); err != nil {
132129
return err
133130
}

0 commit comments

Comments
 (0)