/
kafka_dml_sink.go
132 lines (115 loc) · 4.15 KB
/
kafka_dml_sink.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
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package mq
import (
"context"
"net/url"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/cdc/sink/dmlsink/mq/dispatcher"
"github.com/pingcap/tiflow/cdc/sink/dmlsink/mq/dmlproducer"
"github.com/pingcap/tiflow/cdc/sink/dmlsink/mq/transformer/columnselector"
"github.com/pingcap/tiflow/cdc/sink/util"
"github.com/pingcap/tiflow/pkg/config"
cerror "github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/sink"
"github.com/pingcap/tiflow/pkg/sink/codec"
"github.com/pingcap/tiflow/pkg/sink/codec/builder"
"github.com/pingcap/tiflow/pkg/sink/kafka"
tiflowutil "github.com/pingcap/tiflow/pkg/util"
"go.uber.org/zap"
)
// NewKafkaDMLSink will verify the config and create a KafkaSink.
func NewKafkaDMLSink(
ctx context.Context,
changefeedID model.ChangeFeedID,
sinkURI *url.URL,
replicaConfig *config.ReplicaConfig,
errCh chan error,
factoryCreator kafka.FactoryCreator,
producerCreator dmlproducer.Factory,
) (_ *dmlSink, err error) {
topic, err := util.GetTopic(sinkURI)
if err != nil {
return nil, errors.Trace(err)
}
options := kafka.NewOptions()
if err := options.Apply(changefeedID, sinkURI, replicaConfig); err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
factory, err := factoryCreator(options, changefeedID)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaNewProducer, err)
}
adminClient, err := factory.AdminClient(ctx)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaNewProducer, err)
}
// We must close adminClient when this func return cause by an error
// otherwise the adminClient will never be closed and lead to a goroutine leak.
defer func() {
if err != nil && adminClient != nil {
adminClient.Close()
}
}()
// adjust the option configuration before creating the kafka client
if err = kafka.AdjustOptions(ctx, adminClient, options, topic); err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaNewProducer, err)
}
protocol, err := util.GetProtocol(tiflowutil.GetOrZero(replicaConfig.Sink.Protocol))
if err != nil {
return nil, errors.Trace(err)
}
topicManager, err := util.GetTopicManagerAndTryCreateTopic(
ctx,
changefeedID,
topic,
options.DeriveTopicConfig(),
adminClient,
)
if err != nil {
return nil, errors.Trace(err)
}
scheme := sink.GetScheme(sinkURI)
eventRouter, err := dispatcher.NewEventRouter(replicaConfig, protocol, topic, scheme)
if err != nil {
return nil, errors.Trace(err)
}
trans, err := columnselector.New(replicaConfig)
if err != nil {
return nil, errors.Trace(err)
}
encoderConfig, err := util.GetEncoderConfig(changefeedID, sinkURI, protocol, replicaConfig, options.MaxMessageBytes)
if err != nil {
return nil, errors.Trace(err)
}
encoderBuilder, err := builder.NewRowEventEncoderBuilder(ctx, encoderConfig)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaNewProducer, err)
}
failpointCh := make(chan error, 1)
asyncProducer, err := factory.AsyncProducer(ctx, failpointCh)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaNewProducer, err)
}
metricsCollector := factory.MetricsCollector(tiflowutil.RoleProcessor, adminClient)
dmlProducer := producerCreator(ctx, changefeedID, asyncProducer, metricsCollector, errCh, failpointCh)
encoderGroup := codec.NewEncoderGroup(replicaConfig.Sink, encoderBuilder, changefeedID)
s := newDMLSink(ctx, changefeedID, dmlProducer, adminClient, topicManager,
eventRouter, trans, encoderGroup, protocol, scheme, errCh)
log.Info("DML sink producer created",
zap.String("namespace", changefeedID.Namespace),
zap.String("changefeedID", changefeedID.ID))
return s, nil
}