Skip to content

Instantly share code, notes, and snippets.

@shuhaowu
Created July 23, 2018 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuhaowu/5c48465040bda9d4143363f06c600c59 to your computer and use it in GitHub Desktop.
Save shuhaowu/5c48465040bda9d4143363f06c600c59 to your computer and use it in GitHub Desktop.
package main
import (
"sync"
"github.com/Shopify/ghostferry"
"github.com/Shopify/ghostferry/copydb"
"github.com/sirupsen/logrus"
)
func main() {
var err error
logrus.SetLevel(logrus.DebugLevel)
ferry := &ghostferry.Ferry{
Config: &ghostferry.Config{
Source: ghostferry.DatabaseConfig{
Host: "localhost",
Port: uint16(29291),
User: "root",
Pass: "",
},
Target: ghostferry.DatabaseConfig{
Host: "localhost",
Port: uint16(29292),
User: "root",
Pass: "",
},
TableFilter: &copydb.StaticTableFilter{
Dbs: []string{"abc"},
Tables: []string{},
TablesIsBlacklist: true,
},
AutomaticCutover: true,
},
}
err = ferry.ValidateConfig()
if err != nil {
panic(err)
}
err = ferry.Initialize()
if err != nil {
panic(err)
}
err = ferry.Start()
if err != nil {
panic(err)
}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
ferry.Run()
}()
ferry.WaitUntilRowCopyIsComplete()
logrus.Warn("setting source read_only = 1")
_, err = ferry.SourceDB.Exec("FLUSH TABLES WITH READ LOCK; SET GLOBAL read_only = 1;")
if err != nil {
panic(err)
}
ferry.FlushBinlogAndStopStreaming()
wg.Wait()
logrus.Warn("setting source read_only = 0")
_, err = ferry.SourceDB.Exec("SET GLOBAL read_only = 0")
if err != nil {
panic(err)
}
verifier := &ghostferry.ChecksumTableVerifier{
Tables: ferry.Tables.AsSlice(),
SourceDB: ferry.SourceDB,
TargetDB: ferry.TargetDB,
}
result, err := verifier.Verify()
if err != nil {
panic(err)
}
logrus.Infof("Verification match: %v, Message: %s", result.DataCorrect, result.Message)
logrus.Info("completed ghostferry run")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment