-
Notifications
You must be signed in to change notification settings - Fork 173
/
input.go
179 lines (142 loc) · 4.18 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package tidb_kafka
import (
"context"
"time"
"github.com/moiot/gravity/pkg/core"
"github.com/moiot/gravity/pkg/position_store"
"github.com/mitchellh/mapstructure"
"github.com/moiot/gravity/pkg/registry"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
"github.com/moiot/gravity/pkg/mysql_test"
"github.com/moiot/gravity/pkg/config"
"github.com/moiot/gravity/pkg/inputs/helper/binlog_checker"
"github.com/moiot/gravity/pkg/utils"
)
var (
BinlogCheckInterval = time.Second
)
type tidbKafkaStreamInputPlugin struct {
pipelineName string
emitter core.Emitter
gravityServerID uint32
cfg *config.SourceTiDBConfig
ctx context.Context
cancel context.CancelFunc
positionCache position_store.PositionCacheInterface
binlogTailer *BinlogTailer
binlogChecker binlog_checker.BinlogChecker
}
func init() {
registry.RegisterPlugin(registry.InputPlugin, "tidbkafka", &tidbKafkaStreamInputPlugin{}, false)
}
func (plugin *tidbKafkaStreamInputPlugin) Configure(pipelineName string, data map[string]interface{}) error {
plugin.pipelineName = pipelineName
cfg := config.SourceTiDBConfig{}
if err := mapstructure.Decode(data, &cfg); err != nil {
return errors.Trace(err)
}
if cfg.SourceDB == nil {
return errors.Errorf("source-db must be configured")
}
if cfg.SourceKafka == nil {
return errors.Errorf("source-kafka must be configured")
}
if cfg.OffsetStoreConfig == nil {
return errors.Errorf("offset-positionCache must be configured")
}
plugin.cfg = &cfg
return nil
}
func (plugin *tidbKafkaStreamInputPlugin) NewPositionCache() (position_store.PositionCacheInterface, error) {
positionRepo, err := position_store.NewMySQLRepo(
plugin.cfg.OffsetStoreConfig.SourceMySQL,
plugin.cfg.OffsetStoreConfig.Annotation)
if err != nil {
return nil, errors.Trace(err)
}
positionCache, err := position_store.NewPositionCache(
plugin.pipelineName,
positionRepo,
KafkaPositionValueEncoder,
KafkaPositionValueDecoder,
position_store.DefaultFlushPeriod)
if err != nil {
return nil, errors.Trace(err)
}
return positionCache, nil
}
func (plugin *tidbKafkaStreamInputPlugin) Start(emitter core.Emitter, positionCache position_store.PositionCacheInterface) error {
plugin.emitter = emitter
plugin.gravityServerID = utils.GenerateRandomServerID()
plugin.positionCache = positionCache
plugin.ctx, plugin.cancel = context.WithCancel(context.Background())
cfg := plugin.cfg
binlogChecker, err := binlog_checker.NewBinlogChecker(
plugin.pipelineName,
cfg.SourceDB,
"",
5*time.Second,
false,
)
if err != nil {
return errors.Trace(err)
}
plugin.binlogChecker = binlogChecker
binlogTailer, err := NewBinlogTailer(
plugin.pipelineName,
plugin.gravityServerID,
positionCache,
cfg,
emitter,
binlogChecker,
)
if err != nil {
return errors.Trace(err)
}
plugin.binlogTailer = binlogTailer
if err := plugin.binlogTailer.Start(); err != nil {
return errors.Trace(err)
}
if err := plugin.binlogChecker.Start(); err != nil {
return errors.Trace(err)
}
return nil
}
func (plugin *tidbKafkaStreamInputPlugin) Stage() config.InputMode {
return config.Stream
}
func (plugin *tidbKafkaStreamInputPlugin) Done() chan position_store.Position {
c := make(chan position_store.Position)
go func() {
plugin.binlogTailer.Wait()
position, exist, err := plugin.positionCache.Get()
if err != nil && exist {
c <- position
} else {
log.Fatalf("[tidbKafkaStreamInputPlugin] failed to get position, exist: %v, err: %v", exist, errors.ErrorStack(err))
}
close(c)
}()
return c
}
func (plugin *tidbKafkaStreamInputPlugin) SendDeadSignal() error {
db, err := utils.CreateDBConnection(plugin.cfg.SourceDB)
if err != nil {
return errors.Trace(err)
}
return mysql_test.SendDeadSignal(db, plugin.pipelineName)
}
func (plugin *tidbKafkaStreamInputPlugin) Wait() {
plugin.binlogTailer.Wait()
}
func (plugin *tidbKafkaStreamInputPlugin) Identity() uint32 {
return plugin.gravityServerID
}
func (plugin *tidbKafkaStreamInputPlugin) Close() {
log.Infof("[mysql_binlog_server] stop...")
plugin.binlogTailer.Close()
log.Infof("[mysql_binlog_server] stopped binlogTailer")
plugin.binlogChecker.Stop()
log.Infof("[mysql_binlog_server] stopped binlogChecker")
}