-
Notifications
You must be signed in to change notification settings - Fork 269
/
mq.go
538 lines (482 loc) · 14.4 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// 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"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/cdc/model"
"github.com/pingcap/ticdc/cdc/sink/codec"
"github.com/pingcap/ticdc/cdc/sink/dispatcher"
"github.com/pingcap/ticdc/cdc/sink/producer"
"github.com/pingcap/ticdc/cdc/sink/producer/kafka"
"github.com/pingcap/ticdc/cdc/sink/producer/pulsar"
"github.com/pingcap/ticdc/pkg/config"
cerror "github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/filter"
"github.com/pingcap/ticdc/pkg/notify"
"github.com/pingcap/ticdc/pkg/security"
"github.com/pingcap/ticdc/pkg/util"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
type mqSink struct {
mqProducer producer.Producer
dispatcher dispatcher.Dispatcher
newEncoder func() codec.EventBatchEncoder
filter *filter.Filter
protocol codec.Protocol
partitionNum int32
partitionInput []chan struct {
row *model.RowChangedEvent
resolvedTs uint64
}
partitionResolvedTs []uint64
checkpointTs uint64
resolvedNotifier *notify.Notifier
resolvedReceiver *notify.Receiver
statistics *Statistics
}
func newMqSink(
ctx context.Context, credential *security.Credential, mqProducer producer.Producer,
filter *filter.Filter, config *config.ReplicaConfig, opts map[string]string, errCh chan error,
) (*mqSink, error) {
partitionNum := mqProducer.GetPartitionNum()
partitionInput := make([]chan struct {
row *model.RowChangedEvent
resolvedTs uint64
}, partitionNum)
for i := 0; i < int(partitionNum); i++ {
partitionInput[i] = make(chan struct {
row *model.RowChangedEvent
resolvedTs uint64
}, 12800)
}
d, err := dispatcher.NewDispatcher(config, mqProducer.GetPartitionNum())
if err != nil {
return nil, errors.Trace(err)
}
notifier := new(notify.Notifier)
var protocol codec.Protocol
protocol.FromString(config.Sink.Protocol)
newEncoder := codec.NewEventBatchEncoder(protocol)
if protocol == codec.ProtocolAvro {
registryURI, ok := opts["registry"]
if !ok {
return nil, cerror.ErrPrepareAvroFailed.GenWithStack(`Avro protocol requires parameter "registry"`)
}
keySchemaManager, err := codec.NewAvroSchemaManager(ctx, credential, registryURI, "-key")
if err != nil {
return nil, errors.Annotate(
cerror.WrapError(cerror.ErrPrepareAvroFailed, err),
"Could not create Avro schema manager for message keys")
}
valueSchemaManager, err := codec.NewAvroSchemaManager(ctx, credential, registryURI, "-value")
if err != nil {
return nil, errors.Annotate(
cerror.WrapError(cerror.ErrPrepareAvroFailed, err),
"Could not create Avro schema manager for message values")
}
newEncoder1 := newEncoder
newEncoder = func() codec.EventBatchEncoder {
avroEncoder := newEncoder1().(*codec.AvroEventBatchEncoder)
avroEncoder.SetKeySchemaManager(keySchemaManager)
avroEncoder.SetValueSchemaManager(valueSchemaManager)
avroEncoder.SetTimeZone(util.TimezoneFromCtx(ctx))
return avroEncoder
}
} else if (protocol == codec.ProtocolCanal || protocol == codec.ProtocolCanalJSON) && !config.EnableOldValue {
log.Error("Old value is not enabled when using Canal protocol. Please update changefeed config")
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, errors.New("Canal requires old value to be enabled"))
}
// pre-flight verification of encoder parameters
if err := newEncoder().SetParams(opts); err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
newEncoder1 := newEncoder
newEncoder = func() codec.EventBatchEncoder {
ret := newEncoder1()
err := ret.SetParams(opts)
if err != nil {
log.Panic("MQ Encoder could not parse parameters", zap.Error(err))
}
return ret
}
resolvedReceiver, err := notifier.NewReceiver(50 * time.Millisecond)
if err != nil {
return nil, err
}
k := &mqSink{
mqProducer: mqProducer,
dispatcher: d,
newEncoder: newEncoder,
filter: filter,
protocol: protocol,
partitionNum: partitionNum,
partitionInput: partitionInput,
partitionResolvedTs: make([]uint64, partitionNum),
resolvedNotifier: notifier,
resolvedReceiver: resolvedReceiver,
statistics: NewStatistics(ctx, "MQ", opts),
}
go func() {
if err := k.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 k, nil
}
func (k *mqSink) EmitRowChangedEvents(ctx context.Context, rows ...*model.RowChangedEvent) error {
rowsCount := 0
for _, row := range rows {
if k.filter.ShouldIgnoreDMLEvent(row.StartTs, row.Table.Schema, row.Table.Table) {
log.Info("Row changed event ignored", zap.Uint64("start-ts", row.StartTs))
continue
}
partition := k.dispatcher.Dispatch(row)
select {
case <-ctx.Done():
return ctx.Err()
case k.partitionInput[partition] <- struct {
row *model.RowChangedEvent
resolvedTs uint64
}{row: row}:
}
rowsCount++
}
k.statistics.AddRowsCount(rowsCount)
return nil
}
func (k *mqSink) FlushRowChangedEvents(ctx context.Context, 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 {
row *model.RowChangedEvent
resolvedTs uint64
}{resolvedTs: resolvedTs}:
}
}
// waiting for all row 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 := k.newEncoder()
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) EmitDDLEvent(ctx context.Context, ddl *model.DDLEvent) error {
if k.filter.ShouldIgnoreDDLEvent(ddl.StartTs, ddl.Type, ddl.TableInfo.Schema, ddl.TableInfo.Table) {
log.Info(
"DDL event ignored",
zap.String("query", ddl.Query),
zap.Uint64("startTs", ddl.StartTs),
zap.Uint64("commitTs", ddl.CommitTs),
)
return cerror.ErrDDLEventIgnored.GenWithStackByArgs()
}
encoder := k.newEncoder()
msg, err := encoder.EncodeDDLEvent(ddl)
if err != nil {
return errors.Trace(err)
}
if msg == nil {
return nil
}
k.statistics.AddDDLCount()
log.Debug("emit ddl event", zap.String("query", ddl.Query), zap.Uint64("commit-ts", ddl.CommitTs))
err = k.writeToProducer(ctx, msg, codec.EncoderNeedSyncWrite, -1)
return errors.Trace(err)
}
// Initialize registers Avro schemas for all tables
func (k *mqSink) Initialize(ctx context.Context, tableInfo []*model.SimpleTableInfo) error {
// No longer need it for now
return nil
}
func (k *mqSink) Close(ctx context.Context) error {
err := k.mqProducer.Close()
return errors.Trace(err)
}
func (k *mqSink) Barrier(cxt context.Context) error {
// Barrier does nothing because FlushRowChangedEvents in mq sink has flushed
// all buffered events forcedlly.
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 {
input := k.partitionInput[partition]
encoder := k.newEncoder()
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 := len(messages)
if thisBatchSize == 0 {
return 0, nil
}
for _, msg := range messages {
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 struct {
row *model.RowChangedEvent
resolvedTs uint64
}
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.row == 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.AppendRowChangedEvent(e.row)
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.SendMessage(ctx, message, partition)
}
return cerror.ErrAsyncBroadcastNotSupport.GenWithStackByArgs()
case codec.EncoderNeedSyncWrite:
if partition >= 0 {
err := k.mqProducer.SendMessage(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, filter *filter.Filter, replicaConfig *config.ReplicaConfig, opts map[string]string, errCh chan error) (*mqSink, error) {
config := kafka.NewKafkaConfig()
scheme := strings.ToLower(sinkURI.Scheme)
if scheme != "kafka" && scheme != "kafka+ssl" {
return nil, cerror.ErrKafkaInvalidConfig.GenWithStack("can't create MQ sink with unsupported scheme: %s", scheme)
}
s := sinkURI.Query().Get("partition-num")
if s != "" {
c, err := strconv.Atoi(s)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
config.PartitionNum = int32(c)
}
s = sinkURI.Query().Get("replication-factor")
if s != "" {
c, err := strconv.Atoi(s)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
config.ReplicationFactor = int16(c)
}
s = sinkURI.Query().Get("kafka-version")
if s != "" {
config.Version = s
}
s = sinkURI.Query().Get("max-message-bytes")
if s != "" {
c, err := strconv.Atoi(s)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
config.MaxMessageBytes = c
opts["max-message-bytes"] = s
}
s = sinkURI.Query().Get("max-batch-size")
if s != "" {
opts["max-batch-size"] = s
}
s = sinkURI.Query().Get("compression")
if s != "" {
config.Compression = s
}
config.ClientID = sinkURI.Query().Get("kafka-client-id")
s = sinkURI.Query().Get("protocol")
if s != "" {
replicaConfig.Sink.Protocol = s
}
s = sinkURI.Query().Get("ca")
if s != "" {
config.Credential.CAPath = s
}
s = sinkURI.Query().Get("cert")
if s != "" {
config.Credential.CertPath = s
}
s = sinkURI.Query().Get("key")
if s != "" {
config.Credential.KeyPath = s
}
s = sinkURI.Query().Get("sasl-user")
if s != "" {
config.SaslScram.SaslUser = s
}
s = sinkURI.Query().Get("sasl-password")
if s != "" {
config.SaslScram.SaslPassword = s
}
s = sinkURI.Query().Get("sasl-mechanism")
if s != "" {
config.SaslScram.SaslMechanism = s
}
s = sinkURI.Query().Get("auto-create-topic")
if s != "" {
autoCreate, err := strconv.ParseBool(s)
if err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaInvalidConfig, err)
}
config.TopicPreProcess = autoCreate
}
topic := strings.TrimFunc(sinkURI.Path, func(r rune) bool {
return r == '/'
})
producer, err := kafka.NewKafkaSaramaProducer(ctx, sinkURI.Host, topic, config, errCh)
if err != nil {
return nil, errors.Trace(err)
}
sink, err := newMqSink(ctx, config.Credential, producer, filter, replicaConfig, opts, errCh)
if err != nil {
return nil, errors.Trace(err)
}
return sink, nil
}
func newPulsarSink(ctx context.Context, sinkURI *url.URL, filter *filter.Filter, replicaConfig *config.ReplicaConfig, opts map[string]string, errCh chan error) (*mqSink, error) {
producer, err := pulsar.NewProducer(sinkURI, errCh)
if err != nil {
return nil, errors.Trace(err)
}
s := sinkURI.Query().Get("protocol")
if s != "" {
replicaConfig.Sink.Protocol = s
}
// These two options are not used by Pulsar producer itself, but the encoders
s = sinkURI.Query().Get("max-message-bytes")
if s != "" {
opts["max-message-bytes"] = s
}
s = sinkURI.Query().Get("max-batch-size")
if s != "" {
opts["max-batch-size"] = s
}
// For now, it's a place holder. Avro format have to make connection to Schema Registery,
// and it may needs credential.
credential := &security.Credential{}
sink, err := newMqSink(ctx, credential, producer, filter, replicaConfig, opts, errCh)
if err != nil {
return nil, errors.Trace(err)
}
return sink, nil
}