-
Notifications
You must be signed in to change notification settings - Fork 131
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
Drainer support plugin framework #977
Open
tsthght
wants to merge
3
commits into
pingcap:master
Choose a base branch
from
tsthght:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+299
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package sync | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pingcap/log" | ||
"github.com/pingcap/tidb-binlog/drainer/loopbacksync" | ||
"github.com/pingcap/tidb-binlog/drainer/relay" | ||
"github.com/pingcap/tidb-binlog/drainer/translator" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"go.uber.org/zap" | ||
) | ||
|
||
//SyncerDemo is a syncer demo | ||
type SyncerDemo struct { | ||
*baseSyncer | ||
} | ||
|
||
//Sync is the method that interface must implement | ||
func (sd *SyncerDemo) Sync(item *Item) error { | ||
//demo | ||
log.Info("item", zap.String("%s", fmt.Sprintf("%v", item))) | ||
sd.success <- item | ||
return nil | ||
} | ||
|
||
//Close is the method that interface must implement | ||
func (sd *SyncerDemo) Close() error { | ||
return nil | ||
} | ||
|
||
//SetSafeMode is the method that interface must implement | ||
func (sd *SyncerDemo) SetSafeMode(mode bool) bool { | ||
return false | ||
} | ||
|
||
//NewSyncerDemo is a syncer demo | ||
func NewSyncerDemo( | ||
cfg *DBConfig, | ||
file string, | ||
tableInfoGetter translator.TableInfoGetter, | ||
worker int, | ||
batchSize int, | ||
queryHistogramVec *prometheus.HistogramVec, | ||
sqlMode *string, | ||
destDBType string, | ||
relayer relay.Relayer, | ||
info *loopbacksync.LoopBackSync, | ||
enableDispatch bool, | ||
enableCausility bool, | ||
) (Syncer, error) { | ||
log.Info("call NewSyncerDemo()") | ||
executor := &SyncerDemo{} | ||
executor.baseSyncer = newBaseSyncer(tableInfoGetter) | ||
return executor, nil | ||
} |
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,76 @@ | ||
package syncplg | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"plugin" | ||
|
||
"github.com/pingcap/tidb-binlog/drainer/loopbacksync" | ||
"github.com/pingcap/tidb-binlog/drainer/relay" | ||
"github.com/pingcap/tidb-binlog/drainer/sync" | ||
"github.com/pingcap/tidb-binlog/drainer/translator" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const ( | ||
//NewPlugin is the name of exported function by syncer plugin | ||
NewPlugin = "NewPluginFactory" | ||
) | ||
|
||
//FactoryInterface is interface of Factory | ||
type FactoryInterface interface { | ||
NewSyncerPlugin( | ||
cfg *sync.DBConfig, | ||
file string, | ||
tableInfoGetter translator.TableInfoGetter, | ||
worker int, | ||
batchSize int, | ||
queryHistogramVec *prometheus.HistogramVec, | ||
sqlMode *string, | ||
destDBType string, | ||
relayer relay.Relayer, | ||
info *loopbacksync.LoopBackSync, | ||
enableDispatch bool, | ||
enableCausility bool, | ||
) (sync.Syncer, error) | ||
} | ||
|
||
//NewSyncerFunc is a function type which syncer plugin must implement | ||
type NewSyncerFunc func( | ||
cfg *sync.DBConfig, | ||
file string, | ||
tableInfoGetter translator.TableInfoGetter, | ||
worker int, | ||
batchSize int, | ||
queryHistogramVec *prometheus.HistogramVec, | ||
sqlMode *string, | ||
destDBType string, | ||
relayer relay.Relayer, | ||
info *loopbacksync.LoopBackSync, | ||
enableDispatch bool, | ||
enableCausility bool, | ||
) (sync.Syncer, error) | ||
|
||
//LoadPlugin load syncer plugin | ||
func LoadPlugin(path, name string) (NewSyncerFunc, error) { | ||
fp := path + "/" + name | ||
p, err := plugin.Open(fp) | ||
if err != nil { | ||
return nil, fmt.Errorf("faile to Open %s . err: %s", fp, err.Error()) | ||
} | ||
|
||
sym, err := p.Lookup(NewPlugin) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newFactory, ok := sym.(func() interface{}) | ||
if !ok { | ||
return nil, errors.New("function type is incorrect") | ||
} | ||
fac := newFactory() | ||
plg, ok := fac.(FactoryInterface) | ||
if !ok { | ||
return nil, errors.New("not implement FactoryInterface") | ||
} | ||
return plg.NewSyncerPlugin, nil | ||
} |
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,4 @@ | ||
plugin: | ||
go build -o syncerdemo.so -buildmode=plugin syncerdemo.go | ||
clean: | ||
rm -rf syncerdemo.so |
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,84 @@ | ||
### Drainer支持插件的用法 | ||
#### 1.1 为什么支持插件 | ||
目前Drainer已经支持file, MySQL, TiDB, Kafka等多种通用下游组件。但是在实际使用中,一些用户所在的公司对通用组件进行了定制化或者使用了公司内部的组件,此时Drainer便无法满足要求。 | ||
|
||
针对上述问题,Drainer提供了插件的形式,供用户针对特定的下游组件进行定制化开发,从而满足公司内部业务场景的需求。 | ||
#### 1.2 插件原理 | ||
Drainer会调用`Syncer`接口,将解析出的Binlog同步到各个下游组件中。用户只需要定制化实现`Syncer`接口即可。 | ||
|
||
定制化`Syncer`时候,需要实现`Syncer`的如下接口: | ||
|
||
``` | ||
// Syncer sync binlog item to downstream | ||
type Syncer interface { | ||
// Sync the binlog item to downstream | ||
Sync(item *Item) error | ||
// will be close if Close normally or meet error, call Error() to check it | ||
Successes() <-chan *Item | ||
// Return not nil if fail to sync data to downstream or nil if closed normally | ||
Error() <-chan error | ||
// Close the Syncer, no more item can be added by `Sync` | ||
// will drain all items and return nil if all successfully sync into downstream | ||
Close() error | ||
// SetSafeMode make the Syncer to use safe mode or not. If no need to handle, it should return false | ||
SetSafeMode(mode bool) bool | ||
} | ||
``` | ||
|
||
用户需要将上述接口进行实现,并编译成动态库(*.so)形式,通过Drainer的启动参数进行加载。 | ||
|
||
我们已经实现了插件框架,用户在定制化插件时只需要关注各个`Syncer`接口的实现即可。 | ||
|
||
#### 1.3 Demo介绍 | ||
为了向用户展示Drainer插件的用法,我们在源码中编写了一个Demo。Demo涉及的文件如下: | ||
|
||
``` | ||
# 需要实现 Syncer 接口函数,主要是业务逻辑相关的内容 | ||
./drainer/sync/plugin_demo.go | ||
# 胶水代码,用来将 业务逻辑代码与插件代码进行耦合,基本不需要用户修改 | ||
./drainer/syncplg/syncerdemo/syncerdemo.go | ||
# 用来将 业务代码 编译成 插件(动态库),不需要用户修改 | ||
./drainer/syncplg/syncerdemo/Makefile | ||
``` | ||
|
||
编写一个插件的**步骤如下**: | ||
|
||
- 步骤一:plugin_demo.go文件实现各个接口 | ||
|
||
该文件中主要是需要用户实现的 `Syncer`接口的各个函数,例如例子中,我们只对binlog进行简单打印,核心代码集中在 `Sync(item *Item)`函数中,如下所示: | ||
|
||
``` | ||
func (sd *SyncerDemo) Sync(item *Item) error { | ||
//demo | ||
log.Info("item", zap.String("%s", fmt.Sprintf("%v", item))) | ||
sd.success <- item | ||
return nil | ||
} | ||
``` | ||
- 步骤二:编译 | ||
|
||
``` | ||
cd ./drainer/syncplg/syncerdemo/ | ||
make | ||
``` | ||
|
||
如果没有报错,会生成插件,如下: | ||
|
||
``` | ||
syncerdemo.so | ||
``` | ||
|
||
- 步骤三:配置启动参数 | ||
|
||
``` | ||
# 指定Syncer使用插件 | ||
dest-db-type = "plugin" | ||
# 需要加载的插件的名称 | ||
plugin-name = "syncerdemo.so" | ||
# 插件所在路径 | ||
plugin-path = "./drainer/syncplg/syncerdemo/" | ||
# 插件内部可以用的配置文件,如果使用不到,可以不配置 | ||
plugin-cfg-path = "/drainer/syncplg/syncerdemo/plgcfg.toml" | ||
``` | ||
|
||
加载插件后,可以通过Drainer的运行日志(`./conf/drainer.log`)来查看参数是否已经正常加载;也可以用来查看插件是否加载成功。 |
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,41 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/pingcap/log" | ||
"github.com/pingcap/tidb-binlog/drainer/loopbacksync" | ||
"github.com/pingcap/tidb-binlog/drainer/relay" | ||
"github.com/pingcap/tidb-binlog/drainer/sync" | ||
"github.com/pingcap/tidb-binlog/drainer/translator" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
//PluginFactory is the Factory struct | ||
type PluginFactory struct{} | ||
|
||
//NewPluginFactory is factory function of plugin | ||
func NewPluginFactory() interface{} { | ||
log.Info("call NewPluginFactory") | ||
return PluginFactory{} | ||
} | ||
|
||
//NewSyncerPlugin return A syncer instance which implemented interface of sync.Syncer | ||
func (pf PluginFactory) NewSyncerPlugin( | ||
cfg *sync.DBConfig, | ||
file string, | ||
tableInfoGetter translator.TableInfoGetter, | ||
worker int, | ||
batchSize int, | ||
queryHistogramVec *prometheus.HistogramVec, | ||
sqlMode *string, | ||
destDBType string, | ||
relayer relay.Relayer, | ||
info *loopbacksync.LoopBackSync, | ||
enableDispatch bool, | ||
enableCausility bool, | ||
) (sync.Syncer, error) { | ||
return sync.NewSyncerDemo(cfg, file, tableInfoGetter, worker, batchSize, queryHistogramVec, sqlMode, | ||
destDBType, relayer, info, enableDispatch, enableCausility) | ||
} | ||
|
||
var _ PluginFactory | ||
var _ = NewPluginFactory() |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
how about adding some comments for how to use these arguments? For example, I don't know what the
file
used for.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.
Thanks, I had writen a README.md. PTAL @WangXiangUSTC