/
mq.go
376 lines (332 loc) · 9.88 KB
/
mq.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2020 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 sink
import (
"context"
"net/url"
"strings"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/tikv/migration/cdc/cdc/model"
"github.com/tikv/migration/cdc/cdc/sink/codec"
"github.com/tikv/migration/cdc/cdc/sink/producer"
"github.com/tikv/migration/cdc/cdc/sink/producer/kafka"
"github.com/tikv/migration/cdc/pkg/config"
cerror "github.com/tikv/migration/cdc/pkg/errors"
"github.com/tikv/migration/cdc/pkg/notify"
"github.com/tikv/migration/cdc/pkg/security"
"github.com/twmb/murmur3"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
type mqEvent struct {
entry *model.RawKVEntry
resolvedTs uint64
}
const (
defaultPartitionInputChSize = 12800
)
type mqSink struct {
mqProducer producer.Producer
encoderBuilder codec.EncoderBuilder
protocol config.Protocol
partitionNum int32
partitionInput []chan mqEvent
partitionResolvedTs []uint64
checkpointTs uint64
resolvedNotifier *notify.Notifier
resolvedReceiver *notify.Receiver
statistics *Statistics
}
func newMqSink(
ctx context.Context, credential *security.Credential, mqProducer producer.Producer,
replicaConfig *config.ReplicaConfig, opts map[string]string, errCh chan error,
) (*mqSink, error) {
var protocol config.Protocol
err := protocol.FromString(replicaConfig.Sink.Protocol)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
encoderBuilder, err := codec.NewEventBatchEncoderBuilder(protocol, credential, opts)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
// pre-flight verification of encoder parameters
if _, err := encoderBuilder.Build(ctx); err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
partitionNum := mqProducer.GetPartitionNum()
partitionInput := make([]chan mqEvent, partitionNum)
for i := 0; i < int(partitionNum); i++ {
partitionInput[i] = make(chan mqEvent, defaultPartitionInputChSize)
}
notifier := new(notify.Notifier)
resolvedReceiver, err := notifier.NewReceiver(50 * time.Millisecond)
if err != nil {
return nil, errors.Trace(err)
}
s := &mqSink{
mqProducer: mqProducer,
encoderBuilder: encoderBuilder,
protocol: protocol,
partitionNum: partitionNum,
partitionInput: partitionInput,
partitionResolvedTs: make([]uint64, partitionNum),
resolvedNotifier: notifier,
resolvedReceiver: resolvedReceiver,
statistics: NewStatistics(ctx, "MQ", opts),
}
go func() {
if err := s.run(ctx); err != nil && errors.Cause(err) != context.Canceled {
select {
case <-ctx.Done():
return
case errCh <- err:
default:
log.Error("error channel is full", zap.Error(err))
}
}
}()
return s, nil
}
func (k *mqSink) dispatch(entry *model.RawKVEntry) uint32 {
hasher := murmur3.New32()
hasher.Write(entry.Key)
return hasher.Sum32() % uint32(k.partitionNum)
}
func (k *mqSink) EmitChangedEvents(ctx context.Context, rawKVEntries ...*model.RawKVEntry) error {
entriesCount := 0
for _, entry := range rawKVEntries {
partition := k.dispatch(entry)
select {
case <-ctx.Done():
return ctx.Err()
case k.partitionInput[partition] <- struct {
entry *model.RawKVEntry
resolvedTs uint64
}{entry: entry}:
}
entriesCount++
}
k.statistics.AddEntriesCount(entriesCount)
return nil
}
func (k *mqSink) FlushChangedEvents(ctx context.Context, keyspanID model.KeySpanID, resolvedTs uint64) (uint64, error) {
if resolvedTs <= k.checkpointTs {
return k.checkpointTs, nil
}
for i := 0; i < int(k.partitionNum); i++ {
select {
case <-ctx.Done():
return 0, ctx.Err()
case k.partitionInput[i] <- struct {
entry *model.RawKVEntry
resolvedTs uint64
}{resolvedTs: resolvedTs}:
}
}
// waiting for all events are sent to mq producer
flushLoop:
for {
select {
case <-ctx.Done():
return 0, ctx.Err()
case <-k.resolvedReceiver.C:
for i := 0; i < int(k.partitionNum); i++ {
if resolvedTs > atomic.LoadUint64(&k.partitionResolvedTs[i]) {
continue flushLoop
}
}
break flushLoop
}
}
err := k.mqProducer.Flush(ctx)
if err != nil {
return 0, errors.Trace(err)
}
k.checkpointTs = resolvedTs
k.statistics.PrintStatus(ctx)
return k.checkpointTs, nil
}
func (k *mqSink) EmitCheckpointTs(ctx context.Context, ts uint64) error {
encoder, err := k.encoderBuilder.Build(ctx)
if err != nil {
return errors.Trace(err)
}
msg, err := encoder.EncodeCheckpointEvent(ts)
if err != nil {
return errors.Trace(err)
}
if msg == nil {
return nil
}
err = k.writeToProducer(ctx, msg, codec.EncoderNeedSyncWrite, -1)
return errors.Trace(err)
}
func (k *mqSink) Close(ctx context.Context) error {
err := k.mqProducer.Close()
return errors.Trace(err)
}
func (k *mqSink) Barrier(cxt context.Context, keyspanID model.KeySpanID) error {
// Barrier does nothing because FlushChangedEvents in mq sink has flushed
// all buffered events by force.
return nil
}
func (k *mqSink) run(ctx context.Context) error {
defer k.resolvedReceiver.Stop()
wg, ctx := errgroup.WithContext(ctx)
for i := int32(0); i < k.partitionNum; i++ {
partition := i
wg.Go(func() error {
return k.runWorker(ctx, partition)
})
}
return wg.Wait()
}
const batchSizeLimit = 4 * 1024 * 1024 // 4MB
func (k *mqSink) runWorker(ctx context.Context, partition int32) error {
log.Info("mqSink worker start", zap.Int32("partition", partition))
input := k.partitionInput[partition]
encoder, err := k.encoderBuilder.Build(ctx)
if err != nil {
return errors.Trace(err)
}
tick := time.NewTicker(500 * time.Millisecond)
defer tick.Stop()
flushToProducer := func(op codec.EncoderResult) error {
return k.statistics.RecordBatchExecution(func() (int, error) {
messages := encoder.Build()
thisBatchSize := 0
if len(messages) == 0 {
return 0, nil
}
failpoint.Inject("SinkFlushEventPanic", func() {
time.Sleep(time.Second)
log.Fatal("kafka sink injected error")
})
for _, msg := range messages {
thisBatchSize += msg.GetEntriesCount()
err := k.writeToProducer(ctx, msg, codec.EncoderNeedAsyncWrite, partition)
if err != nil {
return 0, err
}
}
if op == codec.EncoderNeedSyncWrite {
err := k.mqProducer.Flush(ctx)
if err != nil {
return 0, err
}
}
log.Debug("MQSink flushed", zap.Int("thisBatchSize", thisBatchSize))
return thisBatchSize, nil
})
}
for {
var e mqEvent
select {
case <-ctx.Done():
return ctx.Err()
case <-tick.C:
if err := flushToProducer(codec.EncoderNeedAsyncWrite); err != nil {
return errors.Trace(err)
}
continue
case e = <-input:
}
if e.entry == nil {
if e.resolvedTs != 0 {
op, err := encoder.AppendResolvedEvent(e.resolvedTs)
if err != nil {
return errors.Trace(err)
}
if err := flushToProducer(op); err != nil {
return errors.Trace(err)
}
atomic.StoreUint64(&k.partitionResolvedTs[partition], e.resolvedTs)
k.resolvedNotifier.Notify()
}
continue
}
op, err := encoder.AppendChangedEvent(e.entry)
if err != nil {
return errors.Trace(err)
}
if encoder.Size() >= batchSizeLimit {
op = codec.EncoderNeedAsyncWrite
}
if encoder.Size() >= batchSizeLimit || op != codec.EncoderNoOperation {
if err := flushToProducer(op); err != nil {
return errors.Trace(err)
}
}
}
}
func (k *mqSink) writeToProducer(ctx context.Context, message *codec.MQMessage, op codec.EncoderResult, partition int32) error {
switch op {
case codec.EncoderNeedAsyncWrite:
if partition >= 0 {
return k.mqProducer.AsyncSendMessage(ctx, message, partition)
}
return cerror.ErrAsyncBroadcastNotSupport.GenWithStackByArgs()
case codec.EncoderNeedSyncWrite:
if partition >= 0 {
err := k.mqProducer.AsyncSendMessage(ctx, message, partition)
if err != nil {
return err
}
return k.mqProducer.Flush(ctx)
}
return k.mqProducer.SyncBroadcastMessage(ctx, message)
}
log.Warn("writeToProducer called with no-op",
zap.ByteString("key", message.Key),
zap.ByteString("value", message.Value),
zap.Int32("partition", partition))
return nil
}
func newKafkaSaramaSink(ctx context.Context, sinkURI *url.URL, replicaConfig *config.ReplicaConfig, opts map[string]string, errCh chan error) (*mqSink, error) {
producerConfig, topic, err := parseKafkaSinkConfig(sinkURI, replicaConfig, opts)
if err != nil {
return nil, err
}
sProducer, err := kafka.NewKafkaSaramaProducer(ctx, topic, producerConfig, opts, errCh)
if err != nil {
return nil, errors.Trace(err)
}
sink, err := newMqSink(ctx, producerConfig.Credential, sProducer, replicaConfig, opts, errCh)
if err != nil {
return nil, errors.Trace(err)
}
return sink, nil
}
func parseKafkaSinkConfig(sinkURI *url.URL, replicaConfig *config.ReplicaConfig, opts map[string]string) (producerConfig *kafka.Config, topic string, err error) {
producerConfig = kafka.NewConfig()
if err := kafka.CompleteConfigsAndOpts(sinkURI, producerConfig, replicaConfig, opts); err != nil {
return nil, "", cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
// NOTICE: Please check after the completion, as we may get the configuration from the sinkURI.
err = replicaConfig.Validate()
if err != nil {
return nil, "", err
}
topic = strings.TrimFunc(sinkURI.Path, func(r rune) bool {
return r == '/'
})
if topic == "" {
return nil, "", cerror.ErrKafkaInvalidConfig.GenWithStack("no topic is specified in sink-uri")
}
return producerConfig, topic, nil
}