-
Notifications
You must be signed in to change notification settings - Fork 0
/
backfill.go
621 lines (563 loc) · 18.6 KB
/
backfill.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
// Copyright 2015 The Cockroach Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package sql
import (
"sort"
"time"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/distsqlrun"
"github.com/cockroachdb/cockroach/pkg/sql/jobs"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
const (
// TODO(vivek): Replace these constants with a runtime budget for the
// operation chunk involved.
// columnTruncateAndBackfillChunkSize is the maximum number of columns
// processed per chunk during column truncate or backfill.
columnTruncateAndBackfillChunkSize = 200
// indexTruncateChunkSize is the maximum number of index entries truncated
// per chunk during an index truncation. This value is larger than the
// other chunk constants because the operation involves only running a
// DeleteRange().
indexTruncateChunkSize = 600
// indexBackfillChunkSize is the maximum number index entries backfilled
// per chunk during an index backfill. The index backfill involves a table
// scan, and a number of individual ops presented in a batch. This value
// is smaller than ColumnTruncateAndBackfillChunkSize, because it involves
// a number of individual index row updates that can be scattered over
// many ranges.
indexBackfillChunkSize = 100
// checkpointInterval is the interval after which a checkpoint of the
// schema change is posted.
checkpointInterval = 10 * time.Second
)
var _ sort.Interface = columnsByID{}
var _ sort.Interface = indexesByID{}
type columnsByID []sqlbase.ColumnDescriptor
func (cds columnsByID) Len() int {
return len(cds)
}
func (cds columnsByID) Less(i, j int) bool {
return cds[i].ID < cds[j].ID
}
func (cds columnsByID) Swap(i, j int) {
cds[i], cds[j] = cds[j], cds[i]
}
type indexesByID []sqlbase.IndexDescriptor
func (ids indexesByID) Len() int {
return len(ids)
}
func (ids indexesByID) Less(i, j int) bool {
return ids[i].ID < ids[j].ID
}
func (ids indexesByID) Swap(i, j int) {
ids[i], ids[j] = ids[j], ids[i]
}
func (sc *SchemaChanger) getChunkSize(chunkSize int64) int64 {
if sc.testingKnobs.BackfillChunkSize > 0 {
return sc.testingKnobs.BackfillChunkSize
}
return chunkSize
}
// runBackfill runs the backfill for the schema changer.
func (sc *SchemaChanger) runBackfill(
ctx context.Context, lease *sqlbase.TableDescriptor_SchemaChangeLease, evalCtx parser.EvalContext,
) error {
if sc.testingKnobs.RunBeforeBackfill != nil {
if err := sc.testingKnobs.RunBeforeBackfill(); err != nil {
return err
}
}
if err := sc.ExtendLease(ctx, lease); err != nil {
return err
}
// Mutations are applied in a FIFO order. Only apply the first set of
// mutations. Collect the elements that are part of the mutation.
var droppedIndexDescs []sqlbase.IndexDescriptor
var addedIndexDescs []sqlbase.IndexDescriptor
// Indexes within the Mutations slice for checkpointing.
mutationSentinel := -1
var droppedIndexMutationIdx int
var tableDesc *sqlbase.TableDescriptor
if err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
var err error
tableDesc, err = sqlbase.GetTableDescFromID(ctx, txn, sc.tableID)
return err
}); err != nil {
return err
}
// Short circuit the backfill if the table has been deleted.
if tableDesc.Dropped() {
return nil
}
version := tableDesc.Version
log.VEventf(ctx, 0, "Running backfill for %q, v=%d, m=%d",
tableDesc.Name, tableDesc.Version, sc.mutationID)
needColumnBackfill := false
for i, m := range tableDesc.Mutations {
if m.MutationID != sc.mutationID {
break
}
switch m.Direction {
case sqlbase.DescriptorMutation_ADD:
switch t := m.Descriptor_.(type) {
case *sqlbase.DescriptorMutation_Column:
desc := m.GetColumn()
if desc.DefaultExpr != nil || !desc.Nullable {
needColumnBackfill = true
}
case *sqlbase.DescriptorMutation_Index:
addedIndexDescs = append(addedIndexDescs, *t.Index)
default:
return errors.Errorf("unsupported mutation: %+v", m)
}
case sqlbase.DescriptorMutation_DROP:
switch t := m.Descriptor_.(type) {
case *sqlbase.DescriptorMutation_Column:
needColumnBackfill = true
case *sqlbase.DescriptorMutation_Index:
droppedIndexDescs = append(droppedIndexDescs, *t.Index)
if droppedIndexMutationIdx == mutationSentinel {
droppedIndexMutationIdx = i
}
default:
return errors.Errorf("unsupported mutation: %+v", m)
}
}
}
// First drop indexes, then add/drop columns, and only then add indexes.
// Drop indexes.
if err := sc.truncateIndexes(
ctx, lease, version, droppedIndexDescs, droppedIndexMutationIdx,
); err != nil {
return err
}
// Add and drop columns.
if needColumnBackfill {
if err := sc.truncateAndBackfillColumns(ctx, evalCtx, lease, version); err != nil {
return err
}
}
// Add new indexes.
if len(addedIndexDescs) > 0 {
if err := sc.backfillIndexes(ctx, evalCtx, lease, version); err != nil {
return err
}
}
return nil
}
func (sc *SchemaChanger) maybeWriteResumeSpan(
ctx context.Context,
txn *client.Txn,
version sqlbase.DescriptorVersion,
resume roachpb.Span,
mutationIdx int,
lastCheckpoint *time.Time,
) error {
checkpointInterval := checkpointInterval
if sc.testingKnobs.WriteCheckpointInterval > 0 {
checkpointInterval = sc.testingKnobs.WriteCheckpointInterval
}
if timeutil.Since(*lastCheckpoint) < checkpointInterval {
return nil
}
tableDesc, err := sqlbase.GetTableDescFromID(ctx, txn, sc.tableID)
if err != nil {
return err
}
if tableDesc.Version != version {
return errors.Errorf("table version mismatch: %d, expected: %d", tableDesc.Version, version)
}
mutationID := tableDesc.Mutations[mutationIdx].MutationID
jobID, err := sc.getJobIDForMutation(ctx, version, mutationID)
if err != nil {
return err
}
resumeSpanIndex := distsqlrun.GetResumeSpanIndexofMutationID(tableDesc, mutationIdx)
resumeSpans, err := distsqlrun.GetResumeSpansFromJob(ctx, sc.jobRegistry, txn, jobID, resumeSpanIndex)
if err != nil {
return err
}
if len(resumeSpans) > 0 {
resumeSpans[0] = resume
} else {
resumeSpans = append(resumeSpans, resume)
}
err = distsqlrun.SetResumeSpansInJob(ctx, resumeSpans, sc.jobRegistry, mutationIdx, txn, jobID)
if err != nil {
return err
}
*lastCheckpoint = timeutil.Now()
return nil
}
func (sc *SchemaChanger) getTableVersion(
ctx context.Context, txn *client.Txn, tc *TableCollection, version sqlbase.DescriptorVersion,
) (*sqlbase.TableDescriptor, error) {
tableDesc, err := tc.getTableVersionByID(ctx, txn, sc.tableID)
if err != nil {
return nil, err
}
if version != tableDesc.Version {
return nil, errors.Errorf("table version mismatch: %d, expected=%d", tableDesc.Version, version)
}
return tableDesc, nil
}
func (sc *SchemaChanger) truncateIndexes(
ctx context.Context,
lease *sqlbase.TableDescriptor_SchemaChangeLease,
version sqlbase.DescriptorVersion,
dropped []sqlbase.IndexDescriptor,
mutationIdx int,
) error {
chunkSize := sc.getChunkSize(indexTruncateChunkSize)
if sc.testingKnobs.BackfillChunkSize > 0 {
chunkSize = sc.testingKnobs.BackfillChunkSize
}
alloc := &sqlbase.DatumAlloc{}
for _, desc := range dropped {
var resume roachpb.Span
lastCheckpoint := timeutil.Now()
for row, done := int64(0), false; !done; row += chunkSize {
// First extend the schema change lease.
if err := sc.ExtendLease(ctx, lease); err != nil {
return err
}
resumeAt := resume
if log.V(2) {
log.Infof(ctx, "drop index (%d, %d) at row: %d, span: %s",
sc.tableID, sc.mutationID, row, resume)
}
if err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
if sc.testingKnobs.RunBeforeBackfillChunk != nil {
if err := sc.testingKnobs.RunBeforeBackfillChunk(resume); err != nil {
return err
}
}
if sc.testingKnobs.RunAfterBackfillChunk != nil {
defer sc.testingKnobs.RunAfterBackfillChunk()
}
// TODO(vivek): See comment in backfillIndexesChunk.
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
tc := &TableCollection{leaseMgr: sc.leaseMgr}
defer tc.releaseTables(ctx)
tableDesc, err := sc.getTableVersion(ctx, txn, tc, version)
if err != nil {
return err
}
rd, err := sqlbase.MakeRowDeleter(txn, tableDesc, nil, nil, false, alloc)
if err != nil {
return err
}
td := tableDeleter{rd: rd, alloc: alloc}
if err := td.init(txn); err != nil {
return err
}
resume, err = td.deleteIndex(
ctx, &desc, resumeAt, chunkSize, false, /* traceKV */
)
if err != nil {
return err
}
if err := sc.maybeWriteResumeSpan(ctx, txn, version, resume, mutationIdx, &lastCheckpoint); err != nil {
return err
}
done = resume.Key == nil
return nil
}); err != nil {
return err
}
}
}
return nil
}
type backfillType int
const (
_ backfillType = iota
columnBackfill
indexBackfill
)
// getMutationToBackfill returns the the first mutation enqueued on the table
// descriptor that passes the input mutationFilter. It also returns the index
// of that mutation in the table descriptor mutation list.
//
// Returns nil if the backfill is complete.
func (sc *SchemaChanger) getMutationToBackfill(
ctx context.Context,
version sqlbase.DescriptorVersion,
backfillType backfillType,
filter distsqlrun.MutationFilter,
) (*sqlbase.DescriptorMutation, int, error) {
var mutation *sqlbase.DescriptorMutation
var mutationIdx int
err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
mutation = nil
tableDesc, err := sqlbase.GetTableDescFromID(ctx, txn, sc.tableID)
if err != nil {
return err
}
if tableDesc.Version != version {
return errors.Errorf("table version mismatch: %d, expected: %d", tableDesc.Version, version)
}
if len(tableDesc.Mutations) > 0 {
mutationID := tableDesc.Mutations[0].MutationID
for i := range tableDesc.Mutations {
if tableDesc.Mutations[i].MutationID != mutationID {
break
}
if filter(tableDesc.Mutations[i]) {
mutation = &tableDesc.Mutations[i]
mutationIdx = i
break
}
}
}
return nil
})
return mutation, mutationIdx, err
}
// getJobIDForMutation returns the jobID associated with a mutationId.
func (sc *SchemaChanger) getJobIDForMutation(
ctx context.Context, version sqlbase.DescriptorVersion, mutationID sqlbase.MutationID,
) (int64, error) {
var jobID int64
err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
tableDesc, err := sqlbase.GetTableDescFromID(ctx, txn, sc.tableID)
if err != nil {
return err
}
if tableDesc.Version != version {
return errors.Errorf("table version mismatch: %d, expected: %d", tableDesc.Version, version)
}
if len(tableDesc.MutationJobs) > 0 {
for _, job := range tableDesc.MutationJobs {
if job.MutationID == mutationID {
jobID = job.JobID
break
}
}
}
return nil
})
return jobID, err
}
// getJobIDForMutationWithDescriptor returns a job id associated with a mutation given
// a table descriptor. Unlike getJobIDForMutation this doesn't need transaction.
func (sc *SchemaChanger) getJobIDForMutationWithDescriptor(
ctx context.Context, tableDesc *sqlbase.TableDescriptor, mutationID sqlbase.MutationID,
) (int64, error) {
if len(tableDesc.MutationJobs) > 0 {
for _, job := range tableDesc.MutationJobs {
if job.MutationID == mutationID {
return job.JobID, nil
}
}
}
return 0, errors.Errorf("mutation id not found %v", mutationID)
}
// nRanges returns the number of ranges that cover a set of spans.
func (sc *SchemaChanger) nRanges(
ctx context.Context, txn *client.Txn, spans []roachpb.Span,
) (int, error) {
spanResolver := sc.distSQLPlanner.spanResolver.NewSpanResolverIterator(txn)
rangeIds := make(map[int64]struct{})
for _, span := range spans {
// For each span, iterate the spanResolver until it's exhausted, storing
// the found range ids in the map to de-duplicate them.
spanResolver.Seek(ctx, span, kv.Ascending)
for {
if !spanResolver.Valid() {
return 0, spanResolver.Error()
}
rangeIds[int64(spanResolver.Desc().RangeID)] = struct{}{}
if !spanResolver.NeedAnother() {
break
}
spanResolver.Next(ctx)
}
}
return len(rangeIds), nil
}
// distBackfill runs (or continues) a backfill for the first mutation
// enqueued on the SchemaChanger's table descriptor that passes the input
// MutationFilter.
func (sc *SchemaChanger) distBackfill(
ctx context.Context,
evalCtx parser.EvalContext,
lease *sqlbase.TableDescriptor_SchemaChangeLease,
version sqlbase.DescriptorVersion,
backfillType backfillType,
backfillChunkSize int64,
filter distsqlrun.MutationFilter,
) error {
duration := checkpointInterval
if sc.testingKnobs.WriteCheckpointInterval > 0 {
duration = sc.testingKnobs.WriteCheckpointInterval
}
chunkSize := sc.getChunkSize(backfillChunkSize)
origNRanges := -1
origFractionCompleted := sc.job.Payload().FractionCompleted
fractionLeft := 1 - origFractionCompleted
for {
// Repeat until getMutationToBackfill returns a mutation with no remaining
// ResumeSpans, indicating that the backfill is complete.
mutation, mutationIdx, err := sc.getMutationToBackfill(ctx, version, backfillType, filter)
if err != nil {
return err
}
jobID, err := sc.getJobIDForMutation(ctx, version, mutation.MutationID)
var tableDesc *sqlbase.TableDescriptor
err = sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
tableDesc, err = sqlbase.GetTableDescFromID(ctx, txn, sc.tableID)
return err
})
if err != nil {
return err
}
resumeSpanIndex := distsqlrun.GetResumeSpanIndexofMutationID(tableDesc, mutationIdx)
spans, err := distsqlrun.GetResumeSpansFromJob(ctx, sc.jobRegistry, nil, jobID, resumeSpanIndex)
if err != nil {
return err
}
if len(spans) <= 0 {
break
}
if err := sc.ExtendLease(ctx, lease); err != nil {
return err
}
log.VEventf(ctx, 2, "backfill: process %+v spans", spans)
if err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
// Report schema change progress. We define progress at this point
// as the the fraction of fully-backfilled ranges of the primary index of
// the table being scanned. Since we may have already modified the
// fraction completed of our job from the 10% allocated to completing the
// schema change state machine or from a previous backfill attempt,
// we scale that fraction of ranges completed by the remaining fraction
// of the job's progress bar.
nRanges, err := sc.nRanges(ctx, txn, spans)
if err != nil {
return err
}
if origNRanges == -1 {
origNRanges = nRanges
}
if nRanges < origNRanges {
fractionRangesFinished := float32(origNRanges-nRanges) / float32(origNRanges)
fractionCompleted := origFractionCompleted + fractionLeft*fractionRangesFinished
if err := sc.job.Progressed(ctx, fractionCompleted, jobs.Noop); err != nil {
log.Infof(ctx, "Ignoring error reporting progress %f for job %d: %v", fractionCompleted, *sc.job.ID(), err)
}
}
tc := &TableCollection{leaseMgr: sc.leaseMgr}
// Use a leased table descriptor for the backfill.
defer tc.releaseTables(ctx)
tableDesc, err := sc.getTableVersion(ctx, txn, tc, version)
if err != nil {
return err
}
// otherTableDescs contains any other table descriptors required by the
// backfiller processor.
var otherTableDescs []sqlbase.TableDescriptor
if backfillType == columnBackfill {
fkTables := sqlbase.TablesNeededForFKs(*tableDesc, sqlbase.CheckUpdates)
for k := range fkTables {
table, err := tc.getTableVersionByID(ctx, txn, k)
if err != nil {
return err
}
otherTableDescs = append(otherTableDescs, *table)
}
}
// TODO(andrei): pass the right caches. I think this will crash without
// them.
recv, err := makeDistSQLReceiver(
ctx,
nil, /* sink */
nil, /* rangeCache */
nil, /* leaseCache */
nil, /* txn - the flow does not run wholly in a txn */
// updateClock - the flow will not generate errors with time signal.
// TODO(andrei): plumb a clock update handler here regardless of whether
// it will actually be used or not.
nil,
)
if err != nil {
return err
}
planCtx := sc.distSQLPlanner.NewPlanningCtx(ctx, txn)
plan, err := sc.distSQLPlanner.CreateBackfiller(
&planCtx, backfillType, *tableDesc, duration, chunkSize, spans, otherTableDescs, sc.readAsOf,
)
if err != nil {
return err
}
if err := sc.distSQLPlanner.Run(&planCtx, txn, &plan, &recv, evalCtx); err != nil {
return err
}
return recv.err
}); err != nil {
return err
}
}
return nil
}
func (sc *SchemaChanger) backfillIndexes(
ctx context.Context,
evalCtx parser.EvalContext,
lease *sqlbase.TableDescriptor_SchemaChangeLease,
version sqlbase.DescriptorVersion,
) error {
// Pick a read timestamp for our index backfill, or reuse the previously
// stored one.
if err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
details := *sc.job.WithTxn(txn).Payload().Details.(*jobs.Payload_SchemaChange).SchemaChange
if details.ReadAsOf == (hlc.Timestamp{}) {
details.ReadAsOf = txn.OrigTimestamp()
if err := sc.job.WithTxn(txn).SetDetails(ctx, details); err != nil {
log.Warningf(ctx, "failed to store readAsOf on job %v after completing state machine: %v",
sc.job.ID(), err)
}
}
sc.readAsOf = details.ReadAsOf
return nil
}); err != nil {
return err
}
if fn := sc.testingKnobs.RunBeforeIndexBackfill; fn != nil {
fn()
}
return sc.distBackfill(
ctx, evalCtx, lease, version, indexBackfill, indexBackfillChunkSize,
distsqlrun.IndexMutationFilter)
}
func (sc *SchemaChanger) truncateAndBackfillColumns(
ctx context.Context,
evalCtx parser.EvalContext,
lease *sqlbase.TableDescriptor_SchemaChangeLease,
version sqlbase.DescriptorVersion,
) error {
return sc.distBackfill(
ctx, evalCtx,
lease, version, columnBackfill, columnTruncateAndBackfillChunkSize,
distsqlrun.ColumnMutationFilter)
}