-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathalter_changefeed_stmt.go
696 lines (614 loc) · 22.5 KB
/
alter_changefeed_stmt.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
// Copyright 2022 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package changefeedccl
import (
"context"
"net/url"
"github.com/cockroachdb/cockroach/pkg/ccl/backupccl/backupresolver"
"github.com/cockroachdb/cockroach/pkg/ccl/changefeedccl/changefeedbase"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
)
func init() {
sql.AddPlanHook("alter changefeed", alterChangefeedPlanHook)
}
const telemetryPath = `changefeed.alter`
// alterChangefeedPlanHook implements sql.PlanHookFn.
func alterChangefeedPlanHook(
ctx context.Context, stmt tree.Statement, p sql.PlanHookState,
) (sql.PlanHookRowFn, colinfo.ResultColumns, []sql.PlanNode, bool, error) {
alterChangefeedStmt, ok := stmt.(*tree.AlterChangefeed)
if !ok {
return nil, nil, nil, false, nil
}
header := colinfo.ResultColumns{
{Name: "job_id", Typ: types.Int},
{Name: "job_description", Typ: types.String},
}
lockForUpdate := false
fn := func(ctx context.Context, _ []sql.PlanNode, resultsCh chan<- tree.Datums) error {
if err := validateSettings(ctx, p); err != nil {
return err
}
typedExpr, err := alterChangefeedStmt.Jobs.TypeCheck(ctx, p.SemaCtx(), types.Int)
if err != nil {
return err
}
jobID := jobspb.JobID(tree.MustBeDInt(typedExpr))
job, err := p.ExecCfg().JobRegistry.LoadJobWithTxn(ctx, jobID, p.Txn())
if err != nil {
err = errors.Wrapf(err, `could not load job with job id %d`, jobID)
return err
}
prevDetails, ok := job.Details().(jobspb.ChangefeedDetails)
if !ok {
return errors.Errorf(`job %d is not changefeed job`, jobID)
}
if job.Status() != jobs.StatusPaused {
return errors.Errorf(`job %d is not paused`, jobID)
}
newChangefeedStmt := &tree.CreateChangefeed{}
prevOpts, err := getPrevOpts(job.Payload().Description, prevDetails.Opts)
if err != nil {
return err
}
newOptions, newSinkURI, err := generateNewOpts(ctx, p, alterChangefeedStmt.Cmds, prevOpts, prevDetails.SinkURI)
if err != nil {
return err
}
newTargets, newProgress, newStatementTime, originalSpecs, err := generateNewTargets(ctx,
p,
alterChangefeedStmt.Cmds,
newOptions,
prevDetails,
job.Progress(),
)
if err != nil {
return err
}
newChangefeedStmt.Targets = newTargets
for key, value := range newOptions {
opt := tree.KVOption{Key: tree.Name(key)}
if len(value) > 0 {
opt.Value = tree.NewDString(value)
}
newChangefeedStmt.Options = append(newChangefeedStmt.Options, opt)
}
newChangefeedStmt.SinkURI = tree.NewDString(newSinkURI)
annotatedStmt := &annotatedChangefeedStatement{
CreateChangefeed: newChangefeedStmt,
originalSpecs: originalSpecs,
}
jobRecord, err := createChangefeedJobRecord(
ctx,
p,
annotatedStmt,
newSinkURI,
newOptions,
jobID,
``,
)
if err != nil {
return errors.Wrap(err, `failed to alter changefeed`)
}
newDetails := jobRecord.Details.(jobspb.ChangefeedDetails)
newDetails.Opts[changefeedbase.OptInitialScan] = ``
// newStatementTime will either be the StatementTime of the job prior to the
// alteration, or it will be the high watermark of the job.
newDetails.StatementTime = newStatementTime
newPayload := job.Payload()
newPayload.Details = jobspb.WrapPayloadDetails(newDetails)
newPayload.Description = jobRecord.Description
newPayload.DescriptorIDs = jobRecord.DescriptorIDs
err = p.ExecCfg().JobRegistry.UpdateJobWithTxn(ctx, jobID, p.Txn(), lockForUpdate, func(
txn *kv.Txn, md jobs.JobMetadata, ju *jobs.JobUpdater,
) error {
ju.UpdatePayload(&newPayload)
if newProgress != nil {
ju.UpdateProgress(newProgress)
}
return nil
})
if err != nil {
return err
}
telemetry.Count(telemetryPath)
select {
case <-ctx.Done():
return ctx.Err()
case resultsCh <- tree.Datums{
tree.NewDInt(tree.DInt(jobID)),
tree.NewDString(jobRecord.Description),
}:
return nil
}
}
return fn, header, nil, false, nil
}
func getTargetDesc(
ctx context.Context,
p sql.PlanHookState,
descResolver *backupresolver.DescriptorResolver,
targetPattern tree.TablePattern,
) (catalog.Descriptor, bool, error) {
pattern, err := targetPattern.NormalizeTablePattern()
if err != nil {
return nil, false, err
}
targetName, ok := pattern.(*tree.TableName)
if !ok {
return nil, false, errors.Errorf(`CHANGEFEED cannot target %q`, tree.AsString(targetPattern))
}
found, _, desc, err := resolver.ResolveExisting(
ctx,
targetName.ToUnresolvedObjectName(),
descResolver,
tree.ObjectLookupFlags{},
p.CurrentDatabase(),
p.CurrentSearchPath(),
)
if err != nil {
return nil, false, err
}
return desc, found, nil
}
func generateNewOpts(
ctx context.Context,
p sql.PlanHookState,
alterCmds tree.AlterChangefeedCmds,
prevOpts map[string]string,
prevSinkURI string,
) (map[string]string, string, error) {
sinkURI := prevSinkURI
newOptions := prevOpts
for _, cmd := range alterCmds {
switch v := cmd.(type) {
case *tree.AlterChangefeedSetOptions:
optsFn, err := p.TypeAsStringOpts(ctx, v.Options, changefeedbase.AlterChangefeedOptionExpectValues)
if err != nil {
return nil, ``, err
}
opts, err := optsFn()
if err != nil {
return nil, ``, err
}
for key, value := range opts {
if _, ok := changefeedbase.AlterChangefeedUnsupportedOptions[key]; ok {
return nil, ``, pgerror.Newf(pgcode.InvalidParameterValue, `cannot alter option %q`, key)
}
if key == changefeedbase.OptSink {
newSinkURI, err := url.Parse(value)
if err != nil {
return nil, ``, err
}
prevSinkURI, err := url.Parse(sinkURI)
if err != nil {
return nil, ``, err
}
if newSinkURI.Scheme != prevSinkURI.Scheme {
return nil, ``, pgerror.Newf(
pgcode.InvalidParameterValue,
`New sink type %q does not match original sink type %q. `+
`Altering the sink type of a changefeed is disallowed, consider creating a new changefeed instead.`,
newSinkURI.Scheme,
prevSinkURI.Scheme,
)
}
sinkURI = value
} else {
newOptions[key] = value
}
}
telemetry.CountBucketed(telemetryPath+`.set_options`, int64(len(opts)))
case *tree.AlterChangefeedUnsetOptions:
optKeys := v.Options.ToStrings()
for _, key := range optKeys {
if key == changefeedbase.OptSink {
return nil, ``, pgerror.Newf(pgcode.InvalidParameterValue, `cannot unset option %q`, key)
}
if _, ok := changefeedbase.ChangefeedOptionExpectValues[key]; !ok {
return nil, ``, pgerror.Newf(pgcode.InvalidParameterValue, `invalid option %q`, key)
}
if _, ok := changefeedbase.AlterChangefeedUnsupportedOptions[key]; ok {
return nil, ``, pgerror.Newf(pgcode.InvalidParameterValue, `cannot alter option %q`, key)
}
delete(newOptions, key)
}
telemetry.CountBucketed(telemetryPath+`.unset_options`, int64(len(optKeys)))
}
}
return newOptions, sinkURI, nil
}
func generateNewTargets(
ctx context.Context,
p sql.PlanHookState,
alterCmds tree.AlterChangefeedCmds,
opts map[string]string,
prevDetails jobspb.ChangefeedDetails,
prevProgress jobspb.Progress,
) (
tree.ChangefeedTargets,
*jobspb.Progress,
hlc.Timestamp,
map[tree.ChangefeedTarget]jobspb.ChangefeedTargetSpecification,
error,
) {
type targetKey struct {
TableID descpb.ID
FamilyName tree.Name
}
newTargets := make(map[targetKey]tree.ChangefeedTarget)
droppedTargets := make(map[targetKey]tree.ChangefeedTarget)
newTableDescs := make(map[descpb.ID]catalog.Descriptor)
// originalSpecs provides a mapping between tree.ChangefeedTargets that
// existed prior to the alteration of the changefeed to their corresponding
// jobspb.ChangefeedTargetSpecification. The purpose of this mapping is to ensure
// that the StatementTimeName of the existing targets are not modified when the
// name of the target was modified.
originalSpecs := make(map[tree.ChangefeedTarget]jobspb.ChangefeedTargetSpecification)
// When we add new targets with or without initial scans, indicating
// initial_scan or no_initial_scan in the job description would lose its
// meaning. Hence, we will omit these details from the changefeed
// description. However, to ensure that we do perform the initial scan on
// newly added targets, we will introduce the initial_scan opt after the
// job record is created.
delete(opts, changefeedbase.OptNoInitialScan)
delete(opts, changefeedbase.OptInitialScan)
// the new progress and statement time will start from the progress and
// statement time of the job prior to the alteration of the changefeed. Each
// time we add a new set of targets we update the newJobProgress and
// newJobStatementTime accordingly.
newJobProgress := prevProgress
newJobStatementTime := prevDetails.StatementTime
statementTime := hlc.Timestamp{
WallTime: p.ExtendedEvalContext().GetStmtTimestamp().UnixNano(),
}
// we attempt to resolve the changefeed targets as of the current time to
// ensure that all targets exist. However, we also need to make sure that all
// targets can be resolved at the time in which the changefeed is resumed. We
// perform these validations in the validateNewTargets function.
allDescs, err := backupresolver.LoadAllDescs(ctx, p.ExecCfg(), statementTime)
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
descResolver, err := backupresolver.NewDescriptorResolver(allDescs)
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
for _, targetSpec := range AllTargets(prevDetails) {
k := targetKey{TableID: targetSpec.TableID, FamilyName: tree.Name(targetSpec.FamilyName)}
desc := descResolver.DescByID[targetSpec.TableID].(catalog.TableDescriptor)
tbName, err := getQualifiedTableNameObj(ctx, p.ExecCfg(), p.Txn(), desc)
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
tablePattern, err := tbName.NormalizeTablePattern()
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
newTarget := tree.ChangefeedTarget{
TableName: tablePattern,
FamilyName: tree.Name(targetSpec.FamilyName),
}
newTargets[k] = newTarget
newTableDescs[targetSpec.TableID] = descResolver.DescByID[targetSpec.TableID]
originalSpecs[newTarget] = targetSpec
}
for _, cmd := range alterCmds {
switch v := cmd.(type) {
case *tree.AlterChangefeedAddTarget:
targetOptsFn, err := p.TypeAsStringOpts(ctx, v.Options, changefeedbase.AlterChangefeedTargetOptions)
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
targetOpts, err := targetOptsFn()
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
_, withInitialScan := targetOpts[changefeedbase.OptInitialScan]
_, noInitialScan := targetOpts[changefeedbase.OptNoInitialScan]
if withInitialScan && noInitialScan {
return nil, nil, hlc.Timestamp{}, nil, pgerror.Newf(
pgcode.InvalidParameterValue,
`cannot specify both %q and %q`, changefeedbase.OptInitialScan,
changefeedbase.OptNoInitialScan,
)
}
var existingTargetDescs []catalog.Descriptor
for _, targetDesc := range newTableDescs {
existingTargetDescs = append(existingTargetDescs, targetDesc)
}
existingTargetSpans := fetchSpansForDescs(ctx, p, opts, statementTime, existingTargetDescs)
var newTargetDescs []catalog.Descriptor
for _, target := range v.Targets {
desc, found, err := getTargetDesc(ctx, p, descResolver, target.TableName)
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
if !found {
return nil, nil, hlc.Timestamp{}, nil, pgerror.Newf(
pgcode.InvalidParameterValue,
`target %q does not exist`,
tree.ErrString(&target),
)
}
k := targetKey{TableID: desc.GetID(), FamilyName: target.FamilyName}
newTargets[k] = target
newTableDescs[desc.GetID()] = desc
newTargetDescs = append(newTargetDescs, desc)
}
addedTargetSpans := fetchSpansForDescs(ctx, p, opts, statementTime, newTargetDescs)
// By default, we will not perform an initial scan on newly added
// targets. Hence, the user must explicitly state that they want an
// initial scan performed on the new targets.
newJobProgress, newJobStatementTime, err = generateNewProgress(
newJobProgress,
newJobStatementTime,
existingTargetSpans,
addedTargetSpans,
withInitialScan,
)
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
telemetry.CountBucketed(telemetryPath+`.added_targets`, int64(len(v.Targets)))
case *tree.AlterChangefeedDropTarget:
for _, target := range v.Targets {
desc, found, err := getTargetDesc(ctx, p, descResolver, target.TableName)
if err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
if !found {
return nil, nil, hlc.Timestamp{}, nil, pgerror.Newf(
pgcode.InvalidParameterValue,
`target %q does not exist`,
tree.ErrString(&target),
)
}
k := targetKey{TableID: desc.GetID(), FamilyName: target.FamilyName}
droppedTargets[k] = target
_, recognized := newTargets[k]
if !recognized {
return nil, nil, hlc.Timestamp{}, nil, pgerror.Newf(
pgcode.InvalidParameterValue,
`target %q already not watched by changefeed`,
tree.ErrString(&target),
)
}
delete(newTargets, k)
}
telemetry.CountBucketed(telemetryPath+`.dropped_targets`, int64(len(v.Targets)))
}
}
// Remove tables from the job progress if and only if the number of
// targets referencing them has fallen to zero. For example, we might
// drop one column family from a table and add another at the same time,
// and since we watch entire table spans the set of spans won't change.
if len(droppedTargets) > 0 {
stillThere := make(map[descpb.ID]bool)
for k := range newTargets {
stillThere[k.TableID] = true
}
for k := range droppedTargets {
if !stillThere[k.TableID] {
stillThere[k.TableID] = false
}
}
var droppedTargetDescs []catalog.Descriptor
for id, there := range stillThere {
if !there {
droppedTargetDescs = append(droppedTargetDescs, descResolver.DescByID[id])
}
}
if len(droppedTargetDescs) > 0 {
droppedTargetSpans := fetchSpansForDescs(ctx, p, opts, statementTime, droppedTargetDescs)
removeSpansFromProgress(newJobProgress, droppedTargetSpans)
}
}
newTargetList := tree.ChangefeedTargets{}
for _, target := range newTargets {
newTargetList = append(newTargetList, target)
}
if err := validateNewTargets(ctx, p, newTargetList, newJobProgress, newJobStatementTime); err != nil {
return nil, nil, hlc.Timestamp{}, nil, err
}
return newTargetList, &newJobProgress, newJobStatementTime, originalSpecs, nil
}
func validateNewTargets(
ctx context.Context,
p sql.PlanHookState,
newTargets tree.ChangefeedTargets,
jobProgress jobspb.Progress,
jobStatementTime hlc.Timestamp,
) error {
if len(newTargets) == 0 {
return pgerror.New(pgcode.InvalidParameterValue, "cannot drop all targets")
}
// when we resume the changefeed, we need to ensure that the newly added
// targets can be resolved at the time of the high watermark. If the high
// watermark is empty, then we need to ensure that the newly added targets can
// be resolved at the StatementTime of the changefeed job.
var resolveTime hlc.Timestamp
highWater := jobProgress.GetHighWater()
if highWater != nil && !highWater.IsEmpty() {
resolveTime = *highWater
} else {
resolveTime = jobStatementTime
}
allDescs, err := backupresolver.LoadAllDescs(ctx, p.ExecCfg(), resolveTime)
if err != nil {
return errors.Wrap(err, `error while validating new targets`)
}
descResolver, err := backupresolver.NewDescriptorResolver(allDescs)
if err != nil {
return errors.Wrap(err, `error while validating new targets`)
}
for _, target := range newTargets {
targetName := target.TableName
_, found, err := getTargetDesc(ctx, p, descResolver, targetName)
if err != nil {
return errors.Wrap(err, `error while validating new targets`)
}
if !found {
if highWater != nil && !highWater.IsEmpty() {
return errors.Errorf(`target %q cannot be resolved as of the high water mark. `+
`Please wait until the high water mark progresses past the creation time of this target in order to add it to the changefeed.`,
tree.ErrString(targetName),
)
}
return errors.Errorf(`target %q cannot be resolved as of the creation time of the changefeed. `+
`Please wait until the high water mark progresses past the creation time of this target in order to add it to the changefeed.`,
tree.ErrString(targetName),
)
}
}
return nil
}
// generateNewProgress determines if the progress of a changefeed job needs to
// be updated based on the targets that have been added, the options associated
// with each target we are adding/removing (i.e. with initial_scan or
// no_initial_scan), and the current status of the job. If the progress does not
// need to be updated, we will simply return the previous progress and statement
// time that is passed into the function.
func generateNewProgress(
prevProgress jobspb.Progress,
prevStatementTime hlc.Timestamp,
existingTargetSpans []roachpb.Span,
newSpans []roachpb.Span,
withInitialScan bool,
) (jobspb.Progress, hlc.Timestamp, error) {
prevHighWater := prevProgress.GetHighWater()
changefeedProgress := prevProgress.GetChangefeed()
haveHighwater := !(prevHighWater == nil || prevHighWater.IsEmpty())
haveCheckpoint := changefeedProgress != nil && changefeedProgress.Checkpoint != nil &&
len(changefeedProgress.Checkpoint.Spans) != 0
// Check if the progress does not need to be updated. The progress does not
// need to be updated if:
// * the high watermark is empty, and we would like to perform an initial scan.
// * the high watermark is non-empty, the checkpoint is empty, and we do not want to
// perform an initial scan.
if (!haveHighwater && withInitialScan) || (haveHighwater && !haveCheckpoint && !withInitialScan) {
return prevProgress, prevStatementTime, nil
}
// Check if the user is trying to perform an initial scan during a
// non-initial backfill.
if haveHighwater && haveCheckpoint && withInitialScan {
return prevProgress, prevStatementTime, errors.Errorf(
`cannot perform initial scan on newly added targets while the checkpoint is non-empty, `+
`please unpause the changefeed and wait until the high watermark progresses past the current value %s to add these targets.`,
tree.TimestampToDecimalDatum(*prevHighWater).Decimal.String(),
)
}
// Check if the user is trying to perform an initial scan while the high
// watermark is non-empty but the checkpoint is empty.
if haveHighwater && !haveCheckpoint && withInitialScan {
// If we would like to perform an initial scan on the new targets,
// we need to reset the high watermark. However, by resetting the high
// watermark, the initial scan will be performed on existing targets as well.
// To avoid this, we update the statement time of the job to the previous high
// watermark, and add all the existing targets to the checkpoint to skip the
// initial scan on these targets.
newStatementTime := *prevHighWater
newProgress := jobspb.Progress{
Progress: &jobspb.Progress_HighWater{},
Details: &jobspb.Progress_Changefeed{
Changefeed: &jobspb.ChangefeedProgress{
Checkpoint: &jobspb.ChangefeedProgress_Checkpoint{
Spans: existingTargetSpans,
},
},
},
}
return newProgress, newStatementTime, nil
}
// At this point, we are left with one of two cases:
// * the high watermark is empty, and we do not want to perform
// an initial scan on the new targets.
// * the high watermark is non-empty, the checkpoint is non-empty,
// and we do not want to perform an initial scan on the new targets.
// In either case, we need to update the checkpoint to include the spans
// of the newly added targets so that the changefeed will skip performing
// a backfill on these targets.
var mergedSpanGroup roachpb.SpanGroup
if haveCheckpoint {
mergedSpanGroup.Add(changefeedProgress.Checkpoint.Spans...)
}
mergedSpanGroup.Add(newSpans...)
newProgress := jobspb.Progress{
Progress: &jobspb.Progress_HighWater{},
Details: &jobspb.Progress_Changefeed{
Changefeed: &jobspb.ChangefeedProgress{
Checkpoint: &jobspb.ChangefeedProgress_Checkpoint{
Spans: mergedSpanGroup.Slice(),
},
},
},
}
return newProgress, prevStatementTime, nil
}
func removeSpansFromProgress(prevProgress jobspb.Progress, spansToRemove []roachpb.Span) {
changefeedProgress := prevProgress.GetChangefeed()
if changefeedProgress == nil {
return
}
changefeedCheckpoint := changefeedProgress.Checkpoint
if changefeedCheckpoint == nil {
return
}
prevSpans := changefeedCheckpoint.Spans
var spanGroup roachpb.SpanGroup
spanGroup.Add(prevSpans...)
spanGroup.Sub(spansToRemove...)
changefeedProgress.Checkpoint.Spans = spanGroup.Slice()
}
func fetchSpansForDescs(
ctx context.Context,
p sql.PlanHookState,
opts map[string]string,
statementTime hlc.Timestamp,
descs []catalog.Descriptor,
) (primarySpans []roachpb.Span) {
targets := make([]jobspb.ChangefeedTargetSpecification, len(descs))
for i, d := range descs {
targets[i] = jobspb.ChangefeedTargetSpecification{TableID: d.GetID()}
}
for _, d := range descs {
primarySpans = append(primarySpans, d.(catalog.TableDescriptor).PrimaryIndexSpan(p.ExtendedEvalContext().Codec))
}
return primarySpans
}
func getPrevOpts(prevDescription string, opts map[string]string) (map[string]string, error) {
prevStmt, err := parser.ParseOne(prevDescription)
if err != nil {
return nil, err
}
prevChangefeedStmt, ok := prevStmt.AST.(*tree.CreateChangefeed)
if !ok {
return nil, errors.Errorf(`could not parse job description`)
}
prevOpts := make(map[string]string, len(prevChangefeedStmt.Options))
for _, opt := range prevChangefeedStmt.Options {
prevOpts[opt.Key.String()] = opts[opt.Key.String()]
}
return prevOpts, nil
}