-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
197 lines (162 loc) · 4.13 KB
/
connector.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package dcpsql
import (
"bytes"
"encoding/json"
"errors"
"os"
jsoniter "github.com/json-iterator/go"
dcpCouchbase "github.com/Trendyol/go-dcp/couchbase"
"github.com/Trendyol/go-dcp-sql/metric"
"github.com/Trendyol/go-dcp-sql/sql"
"github.com/Trendyol/go-dcp/helpers"
"github.com/Trendyol/go-dcp"
"github.com/Trendyol/go-dcp-sql/config"
"github.com/Trendyol/go-dcp-sql/couchbase"
"github.com/Trendyol/go-dcp-sql/sql/bulk"
"github.com/Trendyol/go-dcp/logger"
"github.com/Trendyol/go-dcp/models"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
type Connector interface {
Start()
Close()
GetDcpClient() dcpCouchbase.Client
}
type connector struct {
dcp dcp.Dcp
mapper Mapper
config *config.Connector
bulk *bulk.Bulk
}
func (c *connector) Start() {
go func() {
<-c.dcp.WaitUntilReady()
c.bulk.StartBulk()
}()
c.dcp.Start()
}
func (c *connector) Close() {
c.dcp.Close()
c.bulk.Close()
}
func (c *connector) GetDcpClient() dcpCouchbase.Client {
return c.dcp.GetClient()
}
func (c *connector) listener(ctx *models.ListenerContext) {
var e couchbase.Event
switch event := ctx.Event.(type) {
case models.DcpMutation:
e = couchbase.NewMutateEvent(event.Key, event.Value, event.CollectionName, event.EventTime, event.Cas, event.VbID)
case models.DcpExpiration:
e = couchbase.NewExpireEvent(event.Key, nil, event.CollectionName, event.EventTime, event.Cas, event.VbID)
case models.DcpDeletion:
e = couchbase.NewDeleteEvent(event.Key, nil, event.CollectionName, event.EventTime, event.Cas, event.VbID)
default:
return
}
actions := c.mapper(e)
if len(actions) == 0 {
ctx.Ack()
return
}
batchSizeLimit := c.config.SQL.BatchSizeLimit
if len(actions) > batchSizeLimit {
chunks := helpers.ChunkSliceWithSize[sql.Model](actions, batchSizeLimit)
lastChunkIndex := len(chunks) - 1
for idx, chunk := range chunks {
c.bulk.AddActions(ctx, e.EventTime, chunk, idx == lastChunkIndex)
}
} else {
c.bulk.AddActions(ctx, e.EventTime, actions, true)
}
}
type ConnectorBuilder struct {
mapper Mapper
config any
}
func newConnectorConfigFromPath(path string) (*config.Connector, error) {
file, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var c config.Connector
err = yaml.Unmarshal(file, &c)
if err != nil {
return nil, err
}
return &c, nil
}
func newConfig(cf any) (*config.Connector, error) {
switch v := cf.(type) {
case *config.Connector:
return v, nil
case config.Connector:
return &v, nil
case string:
return newConnectorConfigFromPath(v)
default:
return nil, errors.New("invalid config")
}
}
func newConnector(cf any, mapper Mapper) (Connector, error) {
cfg, err := newConfig(cf)
if err != nil {
return nil, err
}
cfg.ApplyDefaults()
connector := &connector{
mapper: mapper,
config: cfg,
}
dcp, err := dcp.NewDcp(&cfg.Dcp, connector.listener)
if err != nil {
logger.Log.Error("Dcp error: %v", err)
return nil, err
}
copyOfConfig := cfg.SQL
printConfiguration(copyOfConfig)
dcpConfig := dcp.GetConfig()
dcpConfig.Checkpoint.Type = "manual"
connector.dcp = dcp
connector.bulk, err = bulk.NewBulk(cfg, dcp.Commit)
if err != nil {
return nil, err
}
connector.dcp.SetEventHandler(
&DcpEventHandler{
bulk: connector.bulk,
})
metricCollector := metric.NewMetricCollector(connector.bulk)
dcp.SetMetricCollectors(metricCollector)
return connector, nil
}
func NewConnectorBuilder(config any) ConnectorBuilder {
return ConnectorBuilder{
config: config,
mapper: DefaultMapper,
}
}
func (c ConnectorBuilder) SetMapper(mapper Mapper) ConnectorBuilder {
c.mapper = mapper
return c
}
func (c ConnectorBuilder) Build() (Connector, error) {
return newConnector(c.config, c.mapper)
}
func (c ConnectorBuilder) SetLogger(logrus *logrus.Logger) ConnectorBuilder {
logger.Log = &logger.Loggers{
Logrus: logrus,
}
return c
}
func printConfiguration(config config.SQL) {
config.Password = "*****"
configJSON, _ := jsoniter.Marshal(config)
dst := &bytes.Buffer{}
if err := json.Compact(dst, configJSON); err != nil {
logger.Log.Error("error while print sql configuration, err: %v", err)
panic(err)
}
logger.Log.Info("using sql config: %v", dst.String())
}