Skip to content
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

Optimize the performance of bidirectional synchronous update table of _drainer_repl_mark #903

Merged
merged 11 commits into from
Feb 24, 2020
2 changes: 2 additions & 0 deletions drainer/loopbacksync/loopbacksync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package loopbacksync
const (
//MarkTableName mark table name
MarkTableName = "retl._drainer_repl_mark"
//ID syncer worker thread id
ID = "id"
WangXiangUSTC marked this conversation as resolved.
Show resolved Hide resolved
//ChannelID channel id
ChannelID = "channel_id"
//Val val
Expand Down
76 changes: 69 additions & 7 deletions pkg/loader/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
gosql "database/sql"
"fmt"
"strings"
"sync/atomic"
"time"

"github.com/pingcap/tidb-binlog/drainer/loopbacksync"
Expand All @@ -33,19 +34,23 @@ import (
)

var defaultBatchSize = 128
var defaultWorkerCount = 16
var index int64
WangXiangUSTC marked this conversation as resolved.
Show resolved Hide resolved

type executor struct {
db *gosql.DB
batchSize int
workerCount int
info *loopbacksync.LoopBackSync
queryHistogramVec *prometheus.HistogramVec
refreshTableInfo func(schema string, table string) (info *tableInfo, err error)
}

func newExecutor(db *gosql.DB) *executor {
exe := &executor{
db: db,
batchSize: defaultBatchSize,
db: db,
batchSize: defaultBatchSize,
workerCount: defaultWorkerCount,
}

return exe
Expand All @@ -65,6 +70,10 @@ func (e *executor) setSyncInfo(info *loopbacksync.LoopBackSync) {
e.info = info
}

func (e *executor) setWorkerCount(workerCount int) {
e.workerCount = workerCount
}

func (e *executor) withQueryHistogramVec(queryHistogramVec *prometheus.HistogramVec) *executor {
e.queryHistogramVec = queryHistogramVec
return e
Expand Down Expand Up @@ -118,19 +127,72 @@ func (tx *tx) commit() error {
}

func (e *executor) updateMark(channel string, tx *tx) error {
if e.info == nil {
return nil
}
index = e.getIndex(index)
WangXiangUSTC marked this conversation as resolved.
Show resolved Hide resolved
var args []interface{}
sql := fmt.Sprintf("update %s set %s=%s+1 where %s=? and %s=? limit 1;", loopbacksync.MarkTableName, loopbacksync.Val, loopbacksync.Val, loopbacksync.ID, loopbacksync.ChannelID)
args = append(args, index, e.info.ChannelID)
_, err1 := tx.autoRollbackExec(sql, args...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can check the affect rows to make sure we do update some row?

if err1 != nil {
return errors.Trace(err1)
}
return nil
}

func (e *executor) initMarkTable() error {
if e.info == nil {
return nil
}
status := 1
columns := fmt.Sprintf("(%s,%s,%s) VALUES(?,?,?)", loopbacksync.ChannelID, loopbacksync.Val, loopbacksync.ChannelInfo)
channel := ""
var builder strings.Builder
holder := "(?,?,?,?)"
columns := fmt.Sprintf("(%s,%s,%s,%s) ", loopbacksync.ID, loopbacksync.ChannelID, loopbacksync.Val, loopbacksync.ChannelInfo)
builder.WriteString("REPLACE INTO " + loopbacksync.MarkTableName + columns + " VALUES ")
for i := 0; i < e.workerCount; i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString(holder)
}
var args []interface{}
sql := fmt.Sprintf("INSERT INTO %s%s on duplicate key update %s=%s+1;", loopbacksync.MarkTableName, columns, loopbacksync.Val, loopbacksync.Val)
args = append(args, e.info.ChannelID, status, channel)
_, err := tx.autoRollbackExec(sql, args...)
for id := 0; id < e.workerCount; id++ {
args = append(args, id, e.info.ChannelID, status, channel)
}
tx, err := e.begin()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in e.begin() it will updateMark it's confusing doing this.
suggest writing a func to accepte a *sql.DB directly to initMarkTable

if err != nil {
return errors.Trace(err)
}
return nil
_, err1 := tx.autoRollbackExec(builder.String(), args...)
if err1 != nil {
return errors.Trace(err1)
}
err2 := tx.commit()
return errors.Trace(err2)
}

func (e *executor) cleanChannelInfo() error {
if e.info == nil {
return nil
}
tx, err := e.begin()
if err != nil {
return errors.Trace(err)
}
var args []interface{}
sql := fmt.Sprintf("delete from %s where %s=? ", loopbacksync.MarkTableName, loopbacksync.ChannelID)
args = append(args, e.info.ChannelID)
_, err1 := tx.autoRollbackExec(sql, args...)
if err1 != nil {
return errors.Trace(err1)
}
err2 := tx.commit()
return errors.Trace(err2)
}
func (e *executor) getIndex(index int64) int64 {
return atomic.AddInt64(&index, 1) % ((int64)(e.workerCount))
}

// return a wrap of sql.Tx
Expand Down
17 changes: 16 additions & 1 deletion pkg/loader/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,16 +511,30 @@ func (s *loaderImpl) createMarkTable() error {
return nil
}

func (s *loaderImpl) initMarkTable() error {
if err := s.createMarkTable(); err != nil {
return errors.Trace(err)
}
executor := s.getExecutor()
return executor.initMarkTable()
}

func (s *loaderImpl) cleanChannelInfo() {
executor := s.getExecutor()
_ = executor.cleanChannelInfo()
}

// Run will quit when meet any error, or all the txn are drained
func (s *loaderImpl) Run() error {
if s.loopBackSyncInfo != nil && s.loopBackSyncInfo.LoopbackControl {
if err := s.createMarkTable(); err != nil {
if err := s.initMarkTable(); err != nil {
return errors.Trace(err)
}
}
txnManager := newTxnManager(1024, s.input)
defer func() {
log.Info("Run()... in Loader quit")
s.cleanChannelInfo()
close(s.successTxn)
txnManager.Close()
}()
Expand Down Expand Up @@ -630,6 +644,7 @@ func (s *loaderImpl) getExecutor() *executor {
e = e.withRefreshTableInfo(s.refreshTableInfo)
}
e.setSyncInfo(s.loopBackSyncInfo)
e.setWorkerCount(s.workerCount)
if s.metrics != nil && s.metrics.QueryHistogramVec != nil {
e = e.withQueryHistogramVec(s.metrics.QueryHistogramVec)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/loader/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (dml *DML) updateSQL() (sql string, args []interface{}) {
}

func createMarkTableDDL() string {
sql := fmt.Sprintf("CREATE TABLE If Not Exists %s ( %s bigint primary key, %s bigint DEFAULT 0, %s varchar(64));", loopbacksync.MarkTableName, loopbacksync.ChannelID, loopbacksync.Val, loopbacksync.ChannelInfo)
sql := fmt.Sprintf("CREATE TABLE If Not Exists %s (%s bigint not null,%s bigint not null DEFAULT 0, %s bigint DEFAULT 0, %s varchar(64) ,PRIMARY KEY (%s,%s));", loopbacksync.MarkTableName, loopbacksync.ID, loopbacksync.ChannelID, loopbacksync.Val, loopbacksync.ChannelInfo, loopbacksync.ID, loopbacksync.ChannelID)
return sql
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/loader/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,10 @@ func (s *SQLSuite) TestUpdateMarkSQL(c *check.C) {
db, mock, err := sqlmock.New()
c.Assert(err, check.IsNil)
defer db.Close()
columns := fmt.Sprintf("(%s,%s,%s) VALUES(?,?,?)", loopbacksync.ChannelID, loopbacksync.Val, loopbacksync.ChannelInfo)
sql := fmt.Sprintf("INSERT INTO %s%s on duplicate key update %s=%s+1;", loopbacksync.MarkTableName, columns, loopbacksync.Val, loopbacksync.Val)
sql := fmt.Sprintf("update %s set %s=%s+1 where %s=? and %s=? limit 1;", loopbacksync.MarkTableName, loopbacksync.Val, loopbacksync.Val, loopbacksync.ID, loopbacksync.ChannelID)
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(sql)).
WithArgs(100, 1, "").WillReturnResult(sqlmock.NewResult(1, 1))
WithArgs(1, 100).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
e := newExecutor(db)
tx, err := e.begin()
Expand All @@ -261,6 +260,6 @@ func (s *SQLSuite) TestUpdateMarkSQL(c *check.C) {
}
func (s *SQLSuite) TestCreateMarkTable(c *check.C) {
sql := createMarkTableDDL()
sql1 := fmt.Sprintf("CREATE TABLE If Not Exists %s ( %s bigint primary key, %s bigint DEFAULT 0, %s varchar(64));", loopbacksync.MarkTableName, loopbacksync.ChannelID, loopbacksync.Val, loopbacksync.ChannelInfo)
sql1 := fmt.Sprintf("CREATE TABLE If Not Exists %s (%s bigint not null,%s bigint not null DEFAULT 0, %s bigint DEFAULT 0, %s varchar(64) ,PRIMARY KEY (%s,%s));", loopbacksync.MarkTableName, loopbacksync.ID, loopbacksync.ChannelID, loopbacksync.Val, loopbacksync.ChannelInfo, loopbacksync.ID, loopbacksync.ChannelID)
c.Assert(sql, check.Equals, sql1)
}