forked from pingcap/tiflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_changefeed.go
743 lines (693 loc) · 23.9 KB
/
client_changefeed.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// 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 cmd
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/google/uuid"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/cdc"
"github.com/pingcap/ticdc/cdc/model"
"github.com/pingcap/ticdc/pkg/config"
"github.com/pingcap/ticdc/pkg/cyclic"
"github.com/pingcap/ticdc/pkg/cyclic/mark"
cerror "github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/security"
"github.com/pingcap/ticdc/pkg/util"
"github.com/pingcap/ticdc/pkg/version"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/r3labs/diff"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
)
const (
// Use the empty string as the default to let the server local setting override the changefeed setting.
// TODO remove this when we change the changefeed `sort-dir` to no-op, which it currently is NOT.
defaultSortDir = ""
)
var forceEnableOldValueProtocols = []string{
"canal",
"maxwell",
}
func newChangefeedCommand() *cobra.Command {
command := &cobra.Command{
Use: "changefeed",
Short: "Manage changefeed (changefeed is a replication task)",
}
command.AddCommand(
newListChangefeedCommand(),
newQueryChangefeedCommand(),
newCreateChangefeedCommand(),
newUpdateChangefeedCommand(),
newStatisticsChangefeedCommand(),
newCreateChangefeedCyclicCommand(),
)
// Add pause, resume, remove changefeed
for _, cmd := range newAdminChangefeedCommand() {
command.AddCommand(cmd)
}
return command
}
func resumeChangefeedCheck(ctx context.Context, cmd *cobra.Command) error {
resp, err := applyOwnerChangefeedQuery(ctx, changefeedID, getCredential())
if err != nil {
return err
}
info := &cdc.ChangefeedResp{}
err = json.Unmarshal([]byte(resp), info)
if err != nil {
return err
}
return confirmLargeDataGap(ctx, cmd, info.TSO)
}
func newAdminChangefeedCommand() []*cobra.Command {
cmds := []*cobra.Command{
{
Use: "pause",
Short: "Pause a replicaiton task (changefeed)",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := defaultContext
job := model.AdminJob{
CfID: changefeedID,
Type: model.AdminStop,
}
return applyAdminChangefeed(ctx, job, getCredential())
},
},
{
Use: "resume",
Short: "Resume a paused replicaiton task (changefeed)",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := defaultContext
job := model.AdminJob{
CfID: changefeedID,
Type: model.AdminResume,
}
if err := resumeChangefeedCheck(ctx, cmd); err != nil {
return err
}
return applyAdminChangefeed(ctx, job, getCredential())
},
},
{
Use: "remove",
Short: "Remove a replicaiton task (changefeed)",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := defaultContext
job := model.AdminJob{
CfID: changefeedID,
Type: model.AdminRemove,
Opts: &model.AdminJobOption{
ForceRemove: optForceRemove,
},
}
return applyAdminChangefeed(ctx, job, getCredential())
},
},
}
for _, cmd := range cmds {
cmd.PersistentFlags().StringVarP(&changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID")
_ = cmd.MarkPersistentFlagRequired("changefeed-id")
if cmd.Use == "remove" {
cmd.PersistentFlags().BoolVarP(&optForceRemove, "force", "f", false, "remove all information of the changefeed")
}
if cmd.Use == "resume" {
cmd.PersistentFlags().BoolVar(&noConfirm, "no-confirm", false, "Don't ask user whether to ignore ineligible table")
}
}
return cmds
}
func newListChangefeedCommand() *cobra.Command {
command := &cobra.Command{
Use: "list",
Short: "List all replication tasks (changefeeds) in TiCDC cluster",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := defaultContext
_, raw, err := cdcEtcdCli.GetChangeFeeds(ctx)
if err != nil {
return err
}
changefeedIDs := make(map[string]struct{}, len(raw))
for id := range raw {
changefeedIDs[id] = struct{}{}
}
if changefeedListAll {
statuses, err := cdcEtcdCli.GetAllChangeFeedStatus(ctx)
if err != nil {
return err
}
for cid := range statuses {
changefeedIDs[cid] = struct{}{}
}
}
cfs := make([]*changefeedCommonInfo, 0, len(changefeedIDs))
for id := range changefeedIDs {
cfci := &changefeedCommonInfo{ID: id}
resp, err := applyOwnerChangefeedQuery(ctx, id, getCredential())
if err != nil {
// if no capture is available, the query will fail, just add a warning here
log.Warn("query changefeed info failed", zap.String("error", err.Error()))
} else {
info := &cdc.ChangefeedResp{}
err = json.Unmarshal([]byte(resp), info)
if err != nil {
return err
}
cfci.Summary = info
}
cfs = append(cfs, cfci)
}
return jsonPrint(cmd, cfs)
},
}
command.PersistentFlags().BoolVarP(&changefeedListAll, "all", "a", false, "List all replication tasks(including removed and finished)")
return command
}
func newQueryChangefeedCommand() *cobra.Command {
command := &cobra.Command{
Use: "query",
Short: "Query information and status of a replicaiton task (changefeed)",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := defaultContext
if simplified {
resp, err := applyOwnerChangefeedQuery(ctx, changefeedID, getCredential())
if err != nil {
return err
}
cmd.Println(resp)
return nil
}
info, err := cdcEtcdCli.GetChangeFeedInfo(ctx, changefeedID)
if err != nil && cerror.ErrChangeFeedNotExists.NotEqual(err) {
return err
}
status, _, err := cdcEtcdCli.GetChangeFeedStatus(ctx, changefeedID)
if err != nil && cerror.ErrChangeFeedNotExists.NotEqual(err) {
return err
}
taskPositions, err := cdcEtcdCli.GetAllTaskPositions(ctx, changefeedID)
if err != nil && cerror.ErrChangeFeedNotExists.NotEqual(err) {
return err
}
var count uint64
for _, pinfo := range taskPositions {
count += pinfo.Count
}
processorInfos, err := cdcEtcdCli.GetAllTaskStatus(ctx, changefeedID)
if err != nil {
return err
}
taskStatus := make([]captureTaskStatus, 0, len(processorInfos))
for captureID, status := range processorInfos {
taskStatus = append(taskStatus, captureTaskStatus{CaptureID: captureID, TaskStatus: status})
}
meta := &cfMeta{Info: info, Status: status, Count: count, TaskStatus: taskStatus}
if info == nil {
log.Warn("this changefeed has been deleted, the residual meta data will be completely deleted within 24 hours.")
}
return jsonPrint(cmd, meta)
},
}
command.PersistentFlags().BoolVarP(&simplified, "simple", "s", false, "Output simplified replication status")
command.PersistentFlags().StringVarP(&changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID")
_ = command.MarkPersistentFlagRequired("changefeed-id")
return command
}
func verifyChangefeedParamers(ctx context.Context, cmd *cobra.Command, isCreate bool, credential *security.Credential, captureInfos []*model.CaptureInfo) (*model.ChangeFeedInfo, error) {
if isCreate {
if sinkURI == "" {
return nil, errors.New("Creating chengfeed without a sink-uri")
}
if startTs == 0 {
ts, logical, err := pdCli.GetTS(ctx)
if err != nil {
return nil, err
}
startTs = oracle.ComposeTS(ts, logical)
}
if err := verifyStartTs(ctx, startTs); err != nil {
return nil, err
}
if err := confirmLargeDataGap(ctx, cmd, startTs); err != nil {
return nil, err
}
if err := verifyTargetTs(ctx, startTs, targetTs); err != nil {
return nil, err
}
}
cdcClusterVer, err := version.GetTiCDCClusterVersion(captureInfos)
if err != nil {
return nil, errors.Trace(err)
}
cfg := config.GetDefaultReplicaConfig()
sortEngineFlag := cmd.Flag("sort-engine")
if cdcClusterVer == version.TiCDCClusterVersion4_0 {
cfg.EnableOldValue = false
if !sortEngineFlag.Changed {
sortEngine = model.SortInMemory
}
log.Warn("The TiCDC cluster is built from 4.0-release branch, the old-value and unified-sorter are disabled by default.")
}
if len(configFile) > 0 {
if err := strictDecodeFile(configFile, "TiCDC changefeed", cfg); err != nil {
return nil, err
}
}
if disableGCSafePointCheck {
cfg.CheckGCSafePoint = false
}
if cyclicReplicaID != 0 || len(cyclicFilterReplicaIDs) != 0 {
if !(cyclicReplicaID != 0 && len(cyclicFilterReplicaIDs) != 0) {
return nil, errors.New("invaild cyclic config, please make sure using " +
"nonzero replica ID and specify filter replica IDs")
}
filter := make([]uint64, 0, len(cyclicFilterReplicaIDs))
for _, id := range cyclicFilterReplicaIDs {
filter = append(filter, uint64(id))
}
cfg.Cyclic = &config.CyclicConfig{
Enable: true,
ReplicaID: cyclicReplicaID,
FilterReplicaID: filter,
SyncDDL: cyclicSyncDDL,
// TODO(neil) enable ID bucket.
}
}
if !cfg.EnableOldValue {
sinkURIParsed, err := url.Parse(sinkURI)
if err != nil {
return nil, cerror.WrapError(cerror.ErrSinkURIInvalid, err)
}
protocol := sinkURIParsed.Query().Get("protocol")
for _, fp := range forceEnableOldValueProtocols {
if protocol == fp {
log.Warn("Attemping to replicate without old value enabled. CDC will enable old value and continue.", zap.String("protocol", protocol))
cfg.EnableOldValue = true
break
}
}
if cfg.ForceReplicate {
log.Error("if use force replicate, old value feature must be enabled")
return nil, cerror.ErrOldValueNotEnabled.GenWithStackByArgs()
}
}
for _, rules := range cfg.Sink.DispatchRules {
switch strings.ToLower(rules.Dispatcher) {
case "rowid", "index-value":
if cfg.EnableOldValue {
cmd.Printf("[WARN] This index-value distribution mode "+
"does not guarantee row-level orderliness when "+
"switching on the old value, so please use caution! dispatch-rules: %#v", rules)
}
}
}
switch sortEngine {
case model.SortUnified, model.SortInMemory, model.SortInFile:
default:
return nil, errors.Errorf("Creating chengfeed with an invalid sort engine(%s), `%s`,`%s` and `%s` are optional.", sortEngine, model.SortUnified, model.SortInMemory, model.SortInFile)
}
info := &model.ChangeFeedInfo{
SinkURI: sinkURI,
Opts: make(map[string]string),
CreateTime: time.Now(),
StartTs: startTs,
TargetTs: targetTs,
Config: cfg,
Engine: sortEngine,
SortDir: sortDir,
State: model.StateNormal,
SyncPointEnabled: syncPointEnabled,
SyncPointInterval: syncPointInterval,
CreatorVersion: version.ReleaseVersion,
}
if info.SortDir != "" {
cmd.Printf("[WARN] --sort-dir is deprecated in changefeed settings. "+
"Please use `cdc server --sort-dir` if possible. "+
"If you wish to continue, make sure %s is writable ON EACH SERVER where you run TiCDC", info.SortDir)
}
if info.Engine != model.SortInMemory && (info.SortDir == ".") {
cmd.Printf("[WARN] you are using the directory containing the cdc binary as sort-dir. " +
"make sure that is what you intend, and that the directory is writable. " +
"Adjust \"sort-dir\" accordingly if you'd like to use another directory. ")
}
if info.Engine == model.SortInFile {
cmd.Printf("[WARN] file sorter is deprecated. " +
"make sure that you DO NOT use it in production. " +
"Adjust \"sort-engine\" to make use of the right sorter.")
}
tz, err := util.GetTimezone(timezone)
if err != nil {
return nil, errors.Annotate(err, "can not load timezone, Please specify the time zone through environment variable `TZ` or command line parameters `--tz`")
}
if isCreate {
ctx = util.PutTimezoneInCtx(ctx, tz)
ineligibleTables, eligibleTables, err := verifyTables(ctx, credential, cfg, startTs)
if err != nil {
return nil, err
}
if len(ineligibleTables) != 0 {
if cfg.ForceReplicate {
cmd.Printf("[WARN] force to replicate some ineligible tables, %#v\n", ineligibleTables)
} else {
cmd.Printf("[WARN] some tables are not eligible to replicate, %#v\n", ineligibleTables)
if !noConfirm {
cmd.Printf("Could you agree to ignore those tables, and continue to replicate [Y/N]\n")
var yOrN string
_, err := fmt.Scan(&yOrN)
if err != nil {
return nil, err
}
if strings.ToLower(strings.TrimSpace(yOrN)) != "y" {
cmd.Printf("No changefeed is created because you don't want to ignore some tables.\n")
return nil, nil
}
}
}
}
if cfg.Cyclic.IsEnabled() && !cyclic.IsTablesPaired(eligibleTables) {
return nil, errors.New("normal tables and mark tables are not paired, " +
"please run `cdc cli changefeed cyclic create-marktables`")
}
}
for _, opt := range opts {
s := strings.SplitN(opt, "=", 2)
if len(s) <= 0 {
cmd.Printf("omit opt: %s", opt)
continue
}
var key string
var value string
key = s[0]
if len(s) > 1 {
value = s[1]
}
info.Opts[key] = value
}
err = verifySink(ctx, info.SinkURI, info.Config, info.Opts)
if err != nil {
return nil, err
}
return info, nil
}
func changefeedConfigVariables(command *cobra.Command) {
command.PersistentFlags().Uint64Var(&startTs, "start-ts", 0, "Start ts of changefeed")
command.PersistentFlags().Uint64Var(&targetTs, "target-ts", 0, "Target ts of changefeed")
command.PersistentFlags().StringVar(&sinkURI, "sink-uri", "", "sink uri")
command.PersistentFlags().StringVar(&configFile, "config", "", "Path of the configuration file")
command.PersistentFlags().StringSliceVar(&opts, "opts", nil, "Extra options, in the `key=value` format")
command.PersistentFlags().StringVar(&sortEngine, "sort-engine", model.SortUnified, "sort engine used for data sort")
command.PersistentFlags().StringVar(&sortDir, "sort-dir", defaultSortDir, "directory used for data sort")
command.PersistentFlags().StringVar(&timezone, "tz", "SYSTEM", "timezone used when checking sink uri (changefeed timezone is determined by cdc server)")
command.PersistentFlags().Uint64Var(&cyclicReplicaID, "cyclic-replica-id", 0, "(Expremental) Cyclic replication replica ID of changefeed")
command.PersistentFlags().UintSliceVar(&cyclicFilterReplicaIDs, "cyclic-filter-replica-ids", []uint{}, "(Expremental) Cyclic replication filter replica ID of changefeed")
command.PersistentFlags().BoolVar(&cyclicSyncDDL, "cyclic-sync-ddl", true, "(Expremental) Cyclic replication sync DDL of changefeed")
command.PersistentFlags().BoolVar(&syncPointEnabled, "sync-point", false, "(Expremental) Set and Record syncpoint in replication(default off)")
command.PersistentFlags().DurationVar(&syncPointInterval, "sync-interval", 10*time.Minute, "(Expremental) Set the interval for syncpoint in replication(default 10min)")
}
func newCreateChangefeedCommand() *cobra.Command {
command := &cobra.Command{
Use: "create",
Short: "Create a new replication task (changefeed)",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := defaultContext
id := changefeedID
if id == "" {
id = uuid.New().String()
}
_, captureInfos, err := cdcEtcdCli.GetCaptures(ctx)
if err != nil {
return err
}
info, err := verifyChangefeedParamers(ctx, cmd, true /* isCreate */, getCredential(), captureInfos)
if err != nil {
return err
}
if info == nil {
return nil
}
infoStr, err := info.Marshal()
if err != nil {
return err
}
err = cdcEtcdCli.CreateChangefeedInfo(ctx, info, id)
if err != nil {
return err
}
cmd.Printf("Create changefeed successfully!\nID: %s\nInfo: %s\n", id, infoStr)
return nil
},
}
changefeedConfigVariables(command)
command.PersistentFlags().BoolVar(&noConfirm, "no-confirm", false, "Don't ask user whether to ignore ineligible table")
command.PersistentFlags().StringVarP(&changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID")
command.PersistentFlags().BoolVarP(&disableGCSafePointCheck, "disable-gc-check", "", false, "Disable GC safe point check")
return command
}
func newUpdateChangefeedCommand() *cobra.Command {
command := &cobra.Command{
Use: "update",
Short: "Update config of an existing replication task (changefeed)",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx := defaultContext
old, err := cdcEtcdCli.GetChangeFeedInfo(ctx, changefeedID)
if err != nil {
return err
}
info, err := old.Clone()
if err != nil {
return err
}
cmd.Flags().Visit(func(flag *pflag.Flag) {
switch flag.Name {
case "target-ts":
info.TargetTs = targetTs
case "sink-uri":
info.SinkURI = sinkURI
case "config":
cfg := info.Config
if err = strictDecodeFile(configFile, "TiCDC changefeed", cfg); err != nil {
log.Error("decode config file error", zap.Error(err))
}
case "opts":
for _, opt := range opts {
s := strings.SplitN(opt, "=", 2)
if len(s) <= 0 {
cmd.Printf("omit opt: %s", opt)
continue
}
var key string
var value string
key = s[0]
if len(s) > 1 {
value = s[1]
}
info.Opts[key] = value
}
case "sort-engine":
info.Engine = sortEngine
case "sort-dir":
info.SortDir = sortDir
case "cyclic-replica-id":
filter := make([]uint64, 0, len(cyclicFilterReplicaIDs))
for _, id := range cyclicFilterReplicaIDs {
filter = append(filter, uint64(id))
}
info.Config.Cyclic.FilterReplicaID = filter
case "cyclic-sync-ddl":
info.Config.Cyclic.SyncDDL = cyclicSyncDDL
case "sync-point":
info.SyncPointEnabled = syncPointEnabled
case "sync-interval":
info.SyncPointInterval = syncPointInterval
case "pd", "tz", "start-ts", "changefeed-id", "no-confirm":
// do nothing
default:
// use this default branch to prevent new added parameter is not added
log.Warn("unsupported flag, please report a bug", zap.String("flagName", flag.Name))
}
})
if err != nil {
return err
}
resp, err := applyOwnerChangefeedQuery(ctx, changefeedID, getCredential())
// if no cdc owner exists, allow user to update changefeed config
if err != nil && errors.Cause(err) != errOwnerNotFound {
return err
}
// Note that the correctness of the logic here depends on the return value of `/capture/owner/changefeed/query` interface.
// TODO: Using error codes instead of string containing judgments
if err == nil && !strings.Contains(resp, `"state": "stopped"`) {
return errors.Errorf("can only update changefeed config when it is stopped\nstatus: %s", resp)
}
changelog, err := diff.Diff(old, info)
if err != nil {
return err
}
if len(changelog) == 0 {
cmd.Printf("changefeed config is the same with the old one, do nothing\n")
return nil
}
cmd.Printf("Diff of changefeed config:\n")
for _, change := range changelog {
cmd.Printf("%+v\n", change)
}
if !noConfirm {
cmd.Printf("Could you agree to apply changes above to changefeed [Y/N]\n")
var yOrN string
_, err = fmt.Scan(&yOrN)
if err != nil {
return err
}
if strings.ToLower(strings.TrimSpace(yOrN)) != "y" {
cmd.Printf("No upadte to changefeed.\n")
return nil
}
}
err = cdcEtcdCli.SaveChangeFeedInfo(ctx, info, changefeedID)
if err != nil {
return err
}
infoStr, err := info.Marshal()
if err != nil {
return err
}
cmd.Printf("Update changefeed config successfully! "+
"Will take effect only if the changefeed has been paused before this command"+
"\nID: %s\nInfo: %s\n", changefeedID, infoStr)
return nil
},
}
changefeedConfigVariables(command)
command.PersistentFlags().StringVarP(&changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID")
command.PersistentFlags().BoolVar(&noConfirm, "no-confirm", false, "Don't ask user whether to confirm update changefeed config")
_ = command.MarkPersistentFlagRequired("changefeed-id")
return command
}
func newStatisticsChangefeedCommand() *cobra.Command {
command := &cobra.Command{
Use: "statistics",
Short: "Periodically check and output the status of a replicaiton task (changefeed)",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := defaultContext
tick := time.NewTicker(time.Duration(interval) * time.Second)
lastTime := time.Now()
var lastCount uint64
for {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
return err
}
case <-tick.C:
now := time.Now()
status, _, err := cdcEtcdCli.GetChangeFeedStatus(ctx, changefeedID)
if err != nil {
return err
}
taskPositions, err := cdcEtcdCli.GetAllTaskPositions(ctx, changefeedID)
if err != nil {
return err
}
var count uint64
for _, pinfo := range taskPositions {
count += pinfo.Count
}
ts, _, err := pdCli.GetTS(ctx)
if err != nil {
return err
}
sinkGap := oracle.ExtractPhysical(status.ResolvedTs) - oracle.ExtractPhysical(status.CheckpointTs)
replicationGap := ts - oracle.ExtractPhysical(status.CheckpointTs)
statistics := profileStatus{
OPS: (count - lastCount) / uint64(now.Unix()-lastTime.Unix()),
SinkGap: fmt.Sprintf("%dms", sinkGap),
ReplicationGap: fmt.Sprintf("%dms", replicationGap),
Count: count,
}
jsonPrint(cmd, &statistics)
lastCount = count
lastTime = now
}
}
},
}
command.PersistentFlags().StringVarP(&changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID")
command.PersistentFlags().UintVarP(&interval, "interval", "I", 10, "Interval for outputing the latest statistics")
_ = command.MarkPersistentFlagRequired("changefeed-id")
return command
}
func newCreateChangefeedCyclicCommand() *cobra.Command {
command := &cobra.Command{
Use: "cyclic",
Short: "(Expremental) Utility about cyclic replication",
}
command.AddCommand(
&cobra.Command{
Use: "create-marktables",
Short: "Create cyclic replication mark tables",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := defaultContext
cfg := config.GetDefaultReplicaConfig()
if len(configFile) > 0 {
if err := strictDecodeFile(configFile, "TiCDC changefeed", cfg); err != nil {
return err
}
}
ts, logical, err := pdCli.GetTS(ctx)
if err != nil {
return err
}
startTs = oracle.ComposeTS(ts, logical)
_, eligibleTables, err := verifyTables(ctx, getCredential(), cfg, startTs)
if err != nil {
return err
}
tables := make([]mark.TableName, len(eligibleTables))
for i := range eligibleTables {
tables[i] = &eligibleTables[i]
}
err = mark.CreateMarkTables(ctx, cyclicUpstreamDSN, getUpstreamCredential(), tables...)
if err != nil {
return err
}
cmd.Printf("Create cyclic replication mark tables successfully! Total tables: %d\n", len(eligibleTables))
return nil
},
})
command.PersistentFlags().StringVar(&cyclicUpstreamDSN, "cyclic-upstream-dsn", "", "(Expremental) Upsteam TiDB DSN in the form of [user[:password]@][net[(addr)]]/")
command.PersistentFlags().StringVar(&upstreamSslCaPath, "cyclic-upstream-ssl-ca", "", "CA certificate path for TLS connection")
command.PersistentFlags().StringVar(&upstreamSslCertPath, "cyclic-upstream-ssl-cert", "", "Certificate path for TLS connection")
command.PersistentFlags().StringVar(&upstreamSslKeyPath, "cyclic-upstream-ssl-key", "", "Private key path for TLS connection")
return command
}
var (
upstreamSslCaPath string
upstreamSslCertPath string
upstreamSslKeyPath string
)
func getUpstreamCredential() *security.Credential {
return &security.Credential{
CAPath: upstreamSslCaPath,
CertPath: upstreamSslCertPath,
KeyPath: upstreamSslKeyPath,
}
}