-
Notifications
You must be signed in to change notification settings - Fork 2
写数据库同步 binlog #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,12 +46,12 @@ type WriteEvent struct { | |
| type OutStatus struct { | ||
| err error | ||
| routineIndex int | ||
| logPos uint32 | ||
| logPos int32 | ||
| } | ||
|
|
||
| type Config struct { | ||
| ChCap int | ||
| ChCnt int | ||
| ChannelCapacity int | ||
|
||
| WriteThreadCount int | ||
|
||
|
|
||
| SourceConn Connection | ||
|
|
||
|
|
@@ -63,8 +63,11 @@ type Config struct { | |
| TableShard []string | ||
| TableIndex []string | ||
|
|
||
| // if set GTID, binlog file and pos will be ignored | ||
| GTID string | ||
|
|
||
| BinlogFile string | ||
| BinlogPos uint32 | ||
| BinlogPos int32 | ||
|
|
||
| TickCnt int64 | ||
| } | ||
|
|
@@ -111,25 +114,51 @@ func validRow(srcRow []interface{}) map[string]string { | |
| return row | ||
| } | ||
|
|
||
| func newBinlogReader(conn *Connection, binlogFile string, binlogPos uint32, serverID uint32) (*replication.BinlogStreamer, error) { | ||
|
|
||
| func newBinlogSyncer(conn *Connection, serverID int32) (*replication.BinlogSyncer, error) { | ||
| port, err := strconv.ParseInt(conn.Port, 10, 16) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| binlogCfg := replication.BinlogSyncerConfig{ | ||
| ServerID: serverID, | ||
| ServerID: uint32(serverID), | ||
| Flavor: "mysql", | ||
| Host: conn.Host, | ||
| Port: uint16(port), | ||
| User: conn.User, | ||
| Password: conn.Password, | ||
| } | ||
|
|
||
| syncer := replication.NewBinlogSyncer(binlogCfg) | ||
| return replication.NewBinlogSyncer(binlogCfg), nil | ||
| } | ||
|
|
||
| func newBinlogReaderByPosition(conn *Connection, binlogFile string, binlogPos int32, serverID int32) (*replication.BinlogStreamer, error) { | ||
|
|
||
| syncer, err := newBinlogSyncer(conn, serverID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| streamer, err := syncer.StartSync(mysql.Position{binlogFile, uint32(binlogPos)}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return streamer, nil | ||
| } | ||
|
|
||
| streamer, err := syncer.StartSync(mysql.Position{binlogFile, binlogPos}) | ||
| func newBinlogReaderByGTID(conn *Connection, GTID string, serverID int32) (*replication.BinlogStreamer, error) { | ||
| syncer, err := newBinlogSyncer(conn, serverID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| gtidSet, err := mysql.ParseMysqlGTIDSet(GTID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| streamer, err := syncer.StartSyncGTID(gtidSet) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -180,7 +209,7 @@ func writeToDB(chIdx int, inCh chan *WriteEvent, outCh chan *OutStatus) { | |
| stat := &OutStatus{ | ||
| err: err, | ||
| routineIndex: chIdx, | ||
| logPos: ev.event.Header.LogPos, | ||
| logPos: int32(ev.event.Header.LogPos), | ||
| } | ||
|
|
||
| FileLog.Printf("routin index: %d, event position: %d\n", chIdx, ev.event.Header.LogPos) | ||
|
|
@@ -294,18 +323,24 @@ func main() { | |
| ShellLog.Panicf("read config file failed: %v\n", err) | ||
| } | ||
|
|
||
| var writeChs = make([]chan *WriteEvent, conf.ChCnt) | ||
| var countCh = make(chan *OutStatus, conf.ChCnt*conf.ChCap) | ||
| var writeChs = make([]chan *WriteEvent, conf.WriteThreadCount) | ||
| var countCh = make(chan *OutStatus, conf.WriteThreadCount*conf.ChannelCapacity) | ||
|
|
||
| for i := 0; i < conf.ChCnt; i++ { | ||
| writeCh := make(chan *WriteEvent, conf.ChCap) | ||
| for i := 0; i < conf.WriteThreadCount; i++ { | ||
| writeCh := make(chan *WriteEvent, conf.ChannelCapacity) | ||
| writeChs[i] = writeCh | ||
| go writeToDB(i, writeCh, countCh) | ||
| } | ||
|
|
||
| go collector(countCh) | ||
|
|
||
| binlogReader, err := newBinlogReader(&conf.SourceConn, conf.BinlogFile, conf.BinlogPos, 9999) | ||
| var binlogReader *replication.BinlogStreamer | ||
| if conf.GTID != "" { | ||
| binlogReader, err = newBinlogReaderByGTID(&conf.SourceConn, conf.GTID, 9999) | ||
| } else { | ||
| binlogReader, err = newBinlogReaderByPosition(&conf.SourceConn, conf.BinlogFile, conf.BinlogPos, 9999) | ||
| } | ||
|
|
||
| if err != nil { | ||
| ShellLog.Panicf("make binlog reader failed: %v\n", err) | ||
| } | ||
|
|
@@ -343,7 +378,7 @@ func main() { | |
| ShellLog.Panicf("calculate hash failed: %v", err) | ||
| } | ||
|
|
||
| chIdx := rowSha1 % int64(conf.ChCnt) | ||
| chIdx := rowSha1 % int64(conf.WriteThreadCount) | ||
| writeChs[chIdx] <- writeEV | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "ChCap": 20, | ||
| "ChCnt": 2, | ||
| "ChannelCapacity": 20, | ||
| "WriteThreadCount": 2, | ||
|
|
||
| "SourceConn": { | ||
| "Addr": "106.14.46.83:3308", | ||
|
|
@@ -36,6 +36,7 @@ | |
| "TableShard": [ "bucket_id" , "scope", "key"], | ||
| "TableIndex": [ "bucket_id" , "scope", "key"], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. index 应该是 bucket_id, scope, key, ts |
||
|
|
||
| "GTID": "", | ||
| "BinlogFile": "mysql-bin.000004", | ||
| "BinlogPos": 4, | ||
| "TickCnt": 40 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
routine中文是什么意思?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goroutine 类似线程
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
叫goroutine吧...routine的解释有点多