Skip to content

Commit f482356

Browse files
authored
Merge branch 'master' into parse-alter-statement
2 parents 6c7b473 + c07d08f commit f482356

File tree

11 files changed

+317
-230
lines changed

11 files changed

+317
-230
lines changed

go/base/context.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/github/gh-ost/go/mysql"
2121
"github.com/github/gh-ost/go/sql"
22+
"github.com/outbrain/golib/log"
2223

2324
"gopkg.in/gcfg.v1"
2425
gcfgscanner "gopkg.in/gcfg.v1/scanner"
@@ -217,6 +218,25 @@ type MigrationContext struct {
217218
ForceTmpTableName string
218219

219220
recentBinlogCoordinates mysql.BinlogCoordinates
221+
222+
Log Logger
223+
}
224+
225+
type Logger interface {
226+
Debug(args ...interface{})
227+
Debugf(format string, args ...interface{})
228+
Info(args ...interface{})
229+
Infof(format string, args ...interface{})
230+
Warning(args ...interface{}) error
231+
Warningf(format string, args ...interface{}) error
232+
Error(args ...interface{}) error
233+
Errorf(format string, args ...interface{}) error
234+
Errore(err error) error
235+
Fatal(args ...interface{}) error
236+
Fatalf(format string, args ...interface{}) error
237+
Fatale(err error) error
238+
SetLevel(level log.LogLevel)
239+
SetPrintStackTrace(printStackTraceFlag bool)
220240
}
221241

222242
type ContextConfig struct {
@@ -251,6 +271,7 @@ func NewMigrationContext() *MigrationContext {
251271
pointOfInterestTimeMutex: &sync.Mutex{},
252272
ColumnRenameMap: make(map[string]string),
253273
PanicAbort: make(chan error),
274+
Log: NewDefaultLogger(),
254275
}
255276
}
256277

go/base/default_logger.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package base
2+
3+
import (
4+
"github.com/outbrain/golib/log"
5+
)
6+
7+
type simpleLogger struct{}
8+
9+
func NewDefaultLogger() *simpleLogger {
10+
return &simpleLogger{}
11+
}
12+
13+
func (*simpleLogger) Debug(args ...interface{}) {
14+
log.Debug(args[0].(string), args[1:])
15+
return
16+
}
17+
18+
func (*simpleLogger) Debugf(format string, args ...interface{}) {
19+
log.Debugf(format, args...)
20+
return
21+
}
22+
23+
func (*simpleLogger) Info(args ...interface{}) {
24+
log.Info(args[0].(string), args[1:])
25+
return
26+
}
27+
28+
func (*simpleLogger) Infof(format string, args ...interface{}) {
29+
log.Infof(format, args...)
30+
return
31+
}
32+
33+
func (*simpleLogger) Warning(args ...interface{}) error {
34+
return log.Warning(args[0].(string), args[1:])
35+
}
36+
37+
func (*simpleLogger) Warningf(format string, args ...interface{}) error {
38+
return log.Warningf(format, args...)
39+
}
40+
41+
func (*simpleLogger) Error(args ...interface{}) error {
42+
return log.Error(args[0].(string), args[1:])
43+
}
44+
45+
func (*simpleLogger) Errorf(format string, args ...interface{}) error {
46+
return log.Errorf(format, args...)
47+
}
48+
49+
func (*simpleLogger) Errore(err error) error {
50+
return log.Errore(err)
51+
}
52+
53+
func (*simpleLogger) Fatal(args ...interface{}) error {
54+
return log.Fatal(args[0].(string), args[1:])
55+
}
56+
57+
func (*simpleLogger) Fatalf(format string, args ...interface{}) error {
58+
return log.Fatalf(format, args...)
59+
}
60+
61+
func (*simpleLogger) Fatale(err error) error {
62+
return log.Fatale(err)
63+
}
64+
65+
func (*simpleLogger) SetLevel(level log.LogLevel) {
66+
log.SetLevel(level)
67+
return
68+
}
69+
70+
func (*simpleLogger) SetPrintStackTrace(printStackTraceFlag bool) {
71+
log.SetPrintStackTrace(printStackTraceFlag)
72+
return
73+
}

go/base/utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
gosql "database/sql"
1616
"github.com/github/gh-ost/go/mysql"
17-
"github.com/outbrain/golib/log"
1817
)
1918

2019
var (
@@ -86,7 +85,7 @@ func ValidateConnection(db *gosql.DB, connectionConfig *mysql.ConnectionConfig,
8685
}
8786

8887
if connectionConfig.Key.Port == port || (extraPort > 0 && connectionConfig.Key.Port == extraPort) {
89-
log.Infof("connection validated on %+v", connectionConfig.Key)
88+
migrationContext.Log.Infof("connection validated on %+v", connectionConfig.Key)
9089
return version, nil
9190
} else if extraPort == 0 {
9291
return "", fmt.Errorf("Unexpected database port reported: %+v", port)

go/binlog/gomysql_reader.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
"github.com/github/gh-ost/go/mysql"
1414
"github.com/github/gh-ost/go/sql"
1515

16-
"github.com/outbrain/golib/log"
1716
gomysql "github.com/siddontang/go-mysql/mysql"
1817
"github.com/siddontang/go-mysql/replication"
1918
"golang.org/x/net/context"
2019
)
2120

2221
type GoMySQLReader struct {
22+
migrationContext *base.MigrationContext
2323
connectionConfig *mysql.ConnectionConfig
2424
binlogSyncer *replication.BinlogSyncer
2525
binlogStreamer *replication.BinlogStreamer
@@ -30,6 +30,7 @@ type GoMySQLReader struct {
3030

3131
func NewGoMySQLReader(migrationContext *base.MigrationContext) (binlogReader *GoMySQLReader, err error) {
3232
binlogReader = &GoMySQLReader{
33+
migrationContext: migrationContext,
3334
connectionConfig: migrationContext.InspectorConnectionConfig,
3435
currentCoordinates: mysql.BinlogCoordinates{},
3536
currentCoordinatesMutex: &sync.Mutex{},
@@ -57,11 +58,11 @@ func NewGoMySQLReader(migrationContext *base.MigrationContext) (binlogReader *Go
5758
// ConnectBinlogStreamer
5859
func (this *GoMySQLReader) ConnectBinlogStreamer(coordinates mysql.BinlogCoordinates) (err error) {
5960
if coordinates.IsEmpty() {
60-
return log.Errorf("Empty coordinates at ConnectBinlogStreamer()")
61+
return this.migrationContext.Log.Errorf("Empty coordinates at ConnectBinlogStreamer()")
6162
}
6263

6364
this.currentCoordinates = coordinates
64-
log.Infof("Connecting binlog streamer at %+v", this.currentCoordinates)
65+
this.migrationContext.Log.Infof("Connecting binlog streamer at %+v", this.currentCoordinates)
6566
// Start sync with specified binlog file and position
6667
this.binlogStreamer, err = this.binlogSyncer.StartSync(gomysql.Position{this.currentCoordinates.LogFile, uint32(this.currentCoordinates.LogPos)})
6768

@@ -78,7 +79,7 @@ func (this *GoMySQLReader) GetCurrentBinlogCoordinates() *mysql.BinlogCoordinate
7879
// StreamEvents
7980
func (this *GoMySQLReader) handleRowsEvent(ev *replication.BinlogEvent, rowsEvent *replication.RowsEvent, entriesChannel chan<- *BinlogEntry) error {
8081
if this.currentCoordinates.SmallerThanOrEquals(&this.LastAppliedRowsEventHint) {
81-
log.Debugf("Skipping handled query at %+v", this.currentCoordinates)
82+
this.migrationContext.Log.Debugf("Skipping handled query at %+v", this.currentCoordinates)
8283
return nil
8384
}
8485

@@ -147,14 +148,14 @@ func (this *GoMySQLReader) StreamEvents(canStopStreaming func() bool, entriesCha
147148
defer this.currentCoordinatesMutex.Unlock()
148149
this.currentCoordinates.LogFile = string(rotateEvent.NextLogName)
149150
}()
150-
log.Infof("rotate to next log from %s:%d to %s", this.currentCoordinates.LogFile, int64(ev.Header.LogPos), rotateEvent.NextLogName)
151+
this.migrationContext.Log.Infof("rotate to next log from %s:%d to %s", this.currentCoordinates.LogFile, int64(ev.Header.LogPos), rotateEvent.NextLogName)
151152
} else if rowsEvent, ok := ev.Event.(*replication.RowsEvent); ok {
152153
if err := this.handleRowsEvent(ev, rowsEvent, entriesChannel); err != nil {
153154
return err
154155
}
155156
}
156157
}
157-
log.Debugf("done streaming events")
158+
this.migrationContext.Log.Debugf("done streaming events")
158159

159160
return nil
160161
}

go/cmd/gh-ost/main.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func acceptSignals(migrationContext *base.MigrationContext) {
3232
for sig := range c {
3333
switch sig {
3434
case syscall.SIGHUP:
35-
log.Infof("Received SIGHUP. Reloading configuration")
35+
migrationContext.Log.Infof("Received SIGHUP. Reloading configuration")
3636
if err := migrationContext.ReadConfigFile(); err != nil {
3737
log.Errore(err)
3838
} else {
@@ -158,19 +158,19 @@ func main() {
158158
return
159159
}
160160

161-
log.SetLevel(log.ERROR)
161+
migrationContext.Log.SetLevel(log.ERROR)
162162
if *verbose {
163-
log.SetLevel(log.INFO)
163+
migrationContext.Log.SetLevel(log.INFO)
164164
}
165165
if *debug {
166-
log.SetLevel(log.DEBUG)
166+
migrationContext.Log.SetLevel(log.DEBUG)
167167
}
168168
if *stack {
169-
log.SetPrintStackTrace(*stack)
169+
migrationContext.Log.SetPrintStackTrace(*stack)
170170
}
171171
if *quiet {
172172
// Override!!
173-
log.SetLevel(log.ERROR)
173+
migrationContext.Log.SetLevel(log.ERROR)
174174
}
175175

176176
if migrationContext.AlterStatement == "" {
@@ -194,43 +194,43 @@ func main() {
194194
}
195195
migrationContext.Noop = !(*executeFlag)
196196
if migrationContext.AllowedRunningOnMaster && migrationContext.TestOnReplica {
197-
log.Fatalf("--allow-on-master and --test-on-replica are mutually exclusive")
197+
migrationContext.Log.Fatalf("--allow-on-master and --test-on-replica are mutually exclusive")
198198
}
199199
if migrationContext.AllowedRunningOnMaster && migrationContext.MigrateOnReplica {
200-
log.Fatalf("--allow-on-master and --migrate-on-replica are mutually exclusive")
200+
migrationContext.Log.Fatalf("--allow-on-master and --migrate-on-replica are mutually exclusive")
201201
}
202202
if migrationContext.MigrateOnReplica && migrationContext.TestOnReplica {
203-
log.Fatalf("--migrate-on-replica and --test-on-replica are mutually exclusive")
203+
migrationContext.Log.Fatalf("--migrate-on-replica and --test-on-replica are mutually exclusive")
204204
}
205205
if migrationContext.SwitchToRowBinlogFormat && migrationContext.AssumeRBR {
206-
log.Fatalf("--switch-to-rbr and --assume-rbr are mutually exclusive")
206+
migrationContext.Log.Fatalf("--switch-to-rbr and --assume-rbr are mutually exclusive")
207207
}
208208
if migrationContext.TestOnReplicaSkipReplicaStop {
209209
if !migrationContext.TestOnReplica {
210-
log.Fatalf("--test-on-replica-skip-replica-stop requires --test-on-replica to be enabled")
210+
migrationContext.Log.Fatalf("--test-on-replica-skip-replica-stop requires --test-on-replica to be enabled")
211211
}
212-
log.Warning("--test-on-replica-skip-replica-stop enabled. We will not stop replication before cut-over. Ensure you have a plugin that does this.")
212+
migrationContext.Log.Warning("--test-on-replica-skip-replica-stop enabled. We will not stop replication before cut-over. Ensure you have a plugin that does this.")
213213
}
214214
if migrationContext.CliMasterUser != "" && migrationContext.AssumeMasterHostname == "" {
215-
log.Fatalf("--master-user requires --assume-master-host")
215+
migrationContext.Log.Fatalf("--master-user requires --assume-master-host")
216216
}
217217
if migrationContext.CliMasterPassword != "" && migrationContext.AssumeMasterHostname == "" {
218-
log.Fatalf("--master-password requires --assume-master-host")
218+
migrationContext.Log.Fatalf("--master-password requires --assume-master-host")
219219
}
220220
if migrationContext.TLSCACertificate != "" && !migrationContext.UseTLS {
221-
log.Fatalf("--ssl-ca requires --ssl")
221+
migrationContext.Log.Fatalf("--ssl-ca requires --ssl")
222222
}
223223
if migrationContext.TLSCertificate != "" && !migrationContext.UseTLS {
224-
log.Fatalf("--ssl-cert requires --ssl")
224+
migrationContext.Log.Fatalf("--ssl-cert requires --ssl")
225225
}
226226
if migrationContext.TLSKey != "" && !migrationContext.UseTLS {
227-
log.Fatalf("--ssl-key requires --ssl")
227+
migrationContext.Log.Fatalf("--ssl-key requires --ssl")
228228
}
229229
if migrationContext.TLSAllowInsecure && !migrationContext.UseTLS {
230-
log.Fatalf("--ssl-allow-insecure requires --ssl")
230+
migrationContext.Log.Fatalf("--ssl-allow-insecure requires --ssl")
231231
}
232232
if *replicationLagQuery != "" {
233-
log.Warningf("--replication-lag-query is deprecated")
233+
migrationContext.Log.Warningf("--replication-lag-query is deprecated")
234234
}
235235

236236
switch *cutOver {
@@ -239,19 +239,19 @@ func main() {
239239
case "two-step":
240240
migrationContext.CutOverType = base.CutOverTwoStep
241241
default:
242-
log.Fatalf("Unknown cut-over: %s", *cutOver)
242+
migrationContext.Log.Fatalf("Unknown cut-over: %s", *cutOver)
243243
}
244244
if err := migrationContext.ReadConfigFile(); err != nil {
245-
log.Fatale(err)
245+
migrationContext.Log.Fatale(err)
246246
}
247247
if err := migrationContext.ReadThrottleControlReplicaKeys(*throttleControlReplicas); err != nil {
248-
log.Fatale(err)
248+
migrationContext.Log.Fatale(err)
249249
}
250250
if err := migrationContext.ReadMaxLoad(*maxLoad); err != nil {
251-
log.Fatale(err)
251+
migrationContext.Log.Fatale(err)
252252
}
253253
if err := migrationContext.ReadCriticalLoad(*criticalLoad); err != nil {
254-
log.Fatale(err)
254+
migrationContext.Log.Fatale(err)
255255
}
256256
if migrationContext.ServeSocketFile == "" {
257257
migrationContext.ServeSocketFile = fmt.Sprintf("/tmp/gh-ost.%s.%s.sock", migrationContext.DatabaseName, migrationContext.OriginalTableName)
@@ -260,7 +260,7 @@ func main() {
260260
fmt.Println("Password:")
261261
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
262262
if err != nil {
263-
log.Fatale(err)
263+
migrationContext.Log.Fatale(err)
264264
}
265265
migrationContext.CliPassword = string(bytePassword)
266266
}
@@ -275,13 +275,13 @@ func main() {
275275
migrationContext.SetDefaultNumRetries(*defaultRetries)
276276
migrationContext.ApplyCredentials()
277277
if err := migrationContext.SetupTLS(); err != nil {
278-
log.Fatale(err)
278+
migrationContext.Log.Fatale(err)
279279
}
280280
if err := migrationContext.SetCutOverLockTimeoutSeconds(*cutOverLockTimeoutSeconds); err != nil {
281-
log.Errore(err)
281+
migrationContext.Log.Errore(err)
282282
}
283283
if err := migrationContext.SetExponentialBackoffMaxInterval(*exponentialBackoffMaxInterval); err != nil {
284-
log.Errore(err)
284+
migrationContext.Log.Errore(err)
285285
}
286286

287287
log.Infof("starting gh-ost %+v", AppVersion)
@@ -291,7 +291,7 @@ func main() {
291291
err := migrator.Migrate()
292292
if err != nil {
293293
migrator.ExecOnFailureHook()
294-
log.Fatale(err)
294+
migrationContext.Log.Fatale(err)
295295
}
296296
fmt.Fprintf(os.Stdout, "# Done\n")
297297
}

0 commit comments

Comments
 (0)