-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drainer: refactor executor & add self-description pb output (#121)
*: add output self-description protobuf binlog
- Loading branch information
1 parent
92dd34f
commit 70ca906
Showing
21 changed files
with
1,894 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package executor | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
) | ||
|
||
// Executor is the interface for execute TiDB binlog's sql | ||
type Executor interface { | ||
// Execute executes TiDB binlogs | ||
Execute([]string, [][]interface{}, []int64, bool) error | ||
// Close closes the executor | ||
Close() error | ||
} | ||
|
||
// New returns the an Executor instance by given name | ||
func New(name string, cfg *DBConfig) (Executor, error) { | ||
switch name { | ||
case "mysql": | ||
return newMysql(cfg) | ||
case "pb": | ||
return newPB(cfg) | ||
default: | ||
return nil, errors.Errorf("unsupport executor type %s", name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package executor | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/juju/errors" | ||
) | ||
|
||
type mysqlExecutor struct { | ||
db *sql.DB | ||
} | ||
|
||
func newMysql(cfg *DBConfig) (Executor, error) { | ||
db, err := openDB("mysql", cfg.Host, cfg.Port, cfg.User, cfg.Password) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
return &mysqlExecutor{ | ||
db: db, | ||
}, nil | ||
} | ||
|
||
func (m *mysqlExecutor) Execute(sqls []string, args [][]interface{}, commitTSs []int64, isDDL bool) error { | ||
return executeSQLs(m.db, sqls, args, isDDL) | ||
} | ||
|
||
func (m *mysqlExecutor) Close() error { | ||
return m.db.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package executor | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb-binlog/pkg/file" | ||
pb "github.com/pingcap/tidb-binlog/proto/binlog" | ||
"github.com/pingcap/tidb-binlog/pump" | ||
) | ||
|
||
type pbExecutor struct { | ||
dir string | ||
binlogger pump.Binlogger | ||
} | ||
|
||
func newPB(cfg *DBConfig) (Executor, error) { | ||
var ( | ||
binlogger pump.Binlogger | ||
err error | ||
) | ||
dirPath := cfg.BinlogFileDir | ||
names, err := file.ReadDir(dirPath) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
if len(names) > 0 { | ||
binlogger, err = pump.OpenBinlogger(dirPath) | ||
} else { | ||
binlogger, err = pump.CreateBinlogger(dirPath) | ||
} | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
return &pbExecutor{ | ||
dir: cfg.BinlogFileDir, | ||
binlogger: binlogger, | ||
}, nil | ||
} | ||
|
||
func (p *pbExecutor) Execute(sqls []string, args [][]interface{}, commitTSs []int64, isDDL bool) error { | ||
binlog := &pb.Binlog{CommitTs: commitTSs[0]} | ||
if isDDL { | ||
binlog.Tp = pb.BinlogType_DDL | ||
binlog.DdlQuery = []byte(sqls[0]) | ||
return p.saveBinlog(binlog) | ||
} | ||
|
||
binlog.Tp = pb.BinlogType_DML | ||
binlog.DmlData = new(pb.DMLData) | ||
for i := range sqls { | ||
// event can be only pb.Event, otherwise need to panic | ||
event := args[i][0].(*pb.Event) | ||
binlog.DmlData.Events = append(binlog.DmlData.Events, *event) | ||
} | ||
return p.saveBinlog(binlog) | ||
} | ||
|
||
func (p *pbExecutor) Close() error { | ||
return p.binlogger.Close() | ||
} | ||
|
||
func (p *pbExecutor) saveBinlog(binlog *pb.Binlog) error { | ||
data, err := binlog.Marshal() | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
return p.binlogger.WriteTail(data) | ||
} |
Oops, something went wrong.