forked from Trendyol/go-dcp-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
228 lines (188 loc) · 5.03 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package dcpkafka
import (
"errors"
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/Trendyol/go-dcp"
"github.com/Trendyol/go-dcp-kafka/config"
"github.com/Trendyol/go-dcp-kafka/couchbase"
"github.com/Trendyol/go-dcp-kafka/kafka"
"github.com/Trendyol/go-dcp-kafka/kafka/metadata"
"github.com/Trendyol/go-dcp-kafka/kafka/producer"
"github.com/Trendyol/go-dcp-kafka/metric"
dcpConfig "github.com/Trendyol/go-dcp/config"
"github.com/Trendyol/go-dcp/logger"
"github.com/Trendyol/go-dcp/models"
sKafka "github.com/segmentio/kafka-go"
"gopkg.in/yaml.v3"
)
var MetadataTypeKafka = "kafka"
type Connector interface {
Start()
Close()
}
type connector struct {
dcp dcp.Dcp
mapper Mapper
producer producer.Producer
config *config.Connector
}
func (c *connector) Start() {
go func() {
<-c.dcp.WaitUntilReady()
c.producer.StartBatch()
}()
c.dcp.Start()
}
func (c *connector) Close() {
c.dcp.Close()
err := c.producer.Close()
if err != nil {
logger.Log.Error("error | %v", err)
}
}
func (c *connector) produce(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)
case models.DcpExpiration:
e = couchbase.NewExpireEvent(event.Key, nil, event.CollectionName, event.EventTime)
case models.DcpDeletion:
e = couchbase.NewDeleteEvent(event.Key, nil, event.CollectionName, event.EventTime)
default:
return
}
kafkaMessages := c.mapper(e)
if len(kafkaMessages) == 0 {
ctx.Ack()
return
}
messages := make([]sKafka.Message, 0, len(kafkaMessages))
for _, message := range kafkaMessages {
messages = append(messages, sKafka.Message{
Topic: c.getTopicName(e.CollectionName, message.Topic),
Key: message.Key,
Value: message.Value,
Headers: message.Headers,
})
}
c.producer.Produce(ctx, e.EventTime, messages)
}
func (c *connector) getTopicName(collectionName string, messageTopic string) string {
if messageTopic != "" {
return messageTopic
}
topic := c.config.Kafka.CollectionTopicMapping[collectionName]
if topic == "" {
panic(fmt.Sprintf("there is no topic mapping for collection: %s on your configuration", collectionName))
}
return topic
}
func newConnector(cfg any, mapper Mapper) (Connector, error) {
c, err := newConfig(cfg)
if err != nil {
return nil, err
}
c.ApplyDefaults()
connector := &connector{
mapper: mapper,
config: c,
}
dcpClient, err := dcp.NewDcp(&c.Dcp, connector.produce)
if err != nil {
logger.Log.Error("dcp error: %v", err)
return nil, err
}
conf := dcpClient.GetConfig()
conf.Checkpoint.Type = "manual"
kafkaClient, err := createKafkaClient(c)
if err != nil {
return nil, err
}
if conf.Metadata.Type == MetadataTypeKafka {
setKafkaMetadata(kafkaClient, conf, dcpClient)
}
connector.dcp = dcpClient
connector.producer, err = producer.NewProducer(kafkaClient, c, dcpClient.Commit)
if err != nil {
logger.Log.Error("kafka error: %v", err)
return nil, err
}
connector.dcp.SetEventHandler(&DcpEventHandler{
producerBatch: connector.producer.ProducerBatch,
})
initializeMetricCollector(connector, dcpClient)
return connector, 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 createKafkaClient(cc *config.Connector) (kafka.Client, error) {
kafkaClient := kafka.NewClient(cc)
var topics []string
for _, topic := range cc.Kafka.CollectionTopicMapping {
topics = append(topics, topic)
}
if !cc.Kafka.AllowAutoTopicCreation {
if err := kafkaClient.CheckTopics(topics); err != nil {
logger.Log.Error("collection topic mapping error: %v", err)
return nil, err
}
}
return kafkaClient, nil
}
func setKafkaMetadata(kafkaClient kafka.Client, dcpConfig *dcpConfig.Dcp, dcp dcp.Dcp) {
kafkaMetadata := metadata.NewKafkaMetadata(kafkaClient, dcpConfig.Metadata.Config)
dcp.SetMetadata(kafkaMetadata)
}
func initializeMetricCollector(connector *connector, dcp dcp.Dcp) {
metricCollector := metric.NewMetricCollector(connector.producer)
dcp.SetMetricCollectors(metricCollector)
}
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
}
c.ApplyDefaults()
return &c, nil
}
type ConnectorBuilder struct {
mapper Mapper
config any
}
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(l *logrus.Logger) ConnectorBuilder {
logger.Log = &logger.Loggers{
Logrus: l,
}
return c
}