Skip to content

Commit b4566de

Browse files
timvaillancourtdm-2
authored andcommitted
Use switch statements for readability, simplify .NewGoMySQLReader() (#1135)
* Use `switch` statements for readability * Simplify initBinlogReader()
1 parent 68b4085 commit b4566de

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
@@ -537,19 +537,19 @@ func (this *Migrator) cutOver() (err error) {
537537
}
538538
}
539539
}
540-
if this.migrationContext.CutOverType == base.CutOverAtomic {
540+
541+
switch this.migrationContext.CutOverType {
542+
case base.CutOverAtomic:
541543
// Atomic solution: we use low timeout and multiple attempts. But for
542544
// each failed attempt, we throttle until replication lag is back to normal
543-
err := this.atomicCutOver()
544-
this.handleCutOverResult(err)
545-
return err
546-
}
547-
if this.migrationContext.CutOverType == base.CutOverTwoStep {
548-
err := this.cutOverTwoStep()
549-
this.handleCutOverResult(err)
550-
return err
545+
err = this.atomicCutOver()
546+
case base.CutOverTwoStep:
547+
err = this.cutOverTwoStep()
548+
default:
549+
return this.migrationContext.Log.Fatalf("Unknown cut-over type: %d; should never get here!", this.migrationContext.CutOverType)
551550
}
552-
return this.migrationContext.Log.Fatalf("Unknown cut-over type: %d; should never get here!", this.migrationContext.CutOverType)
551+
this.handleCutOverResult(err)
552+
return err
553553
}
554554

555555
// 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
@@ -123,10 +123,7 @@ func (this *EventsStreamer) InitDBConnections() (err error) {
123123

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

0 commit comments

Comments
 (0)