forked from cockroachdb/cockroach
/
backfill.go
709 lines (655 loc) · 21.2 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
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
// 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.
//
// Author: Tamir Duberstein (tamird@gmail.com)
package sql
import (
"sort"
"time"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/pkg/errors"
)
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
)
func makeColIDtoRowIndex(
row planNode, desc *sqlbase.TableDescriptor,
) (map[sqlbase.ColumnID]int, error) {
columns := row.Columns()
colIDtoRowIndex := make(map[sqlbase.ColumnID]int, len(columns))
for i, column := range columns {
s, idx, err := desc.FindColumnByNormalizedName(parser.ReNormalizeName(column.Name))
if err != nil {
return nil, err
}
switch s {
case sqlbase.DescriptorActive:
colIDtoRowIndex[desc.Columns[idx].ID] = i
case sqlbase.DescriptorIncomplete:
colIDtoRowIndex[desc.Mutations[idx].GetColumn().ID] = i
default:
panic("unreachable")
}
}
return colIDtoRowIndex, nil
}
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 convertBackfillError(tableDesc *sqlbase.TableDescriptor, b *client.Batch) error {
// A backfill on a new schema element has failed and the batch contains
// information useful in printing a sensible error. However
// convertBatchError() will only work correctly if the schema elements are
// "live" in the tableDesc. Apply the mutations belonging to the same
// mutationID to make all the mutations live in tableDesc. Note: this
// tableDesc is not written to the k:v store.
mutationID := tableDesc.Mutations[0].MutationID
for _, mutation := range tableDesc.Mutations {
if mutation.MutationID != mutationID {
// Mutations are applied in a FIFO order. Only apply the first set
// of mutations if they have the mutation ID we're looking for.
break
}
tableDesc.MakeMutationComplete(mutation)
}
return convertBatchError(tableDesc, b)
}
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(lease *sqlbase.TableDescriptor_SchemaChangeLease) error {
l, err := sc.ExtendLease(*lease)
if err != nil {
return err
}
*lease = l
// Mutations are applied in a FIFO order. Only apply the first set of
// mutations. Collect the elements that are part of the mutation.
var droppedColumnDescs []sqlbase.ColumnDescriptor
var droppedIndexDescs []sqlbase.IndexDescriptor
var addedColumnDescs []sqlbase.ColumnDescriptor
var addedIndexDescs []sqlbase.IndexDescriptor
// Indexes within the Mutations slice for checkpointing.
mutationSentinel := -1
var columnMutationIdx, addedIndexMutationIdx, droppedIndexMutationIdx int
var tableDesc *sqlbase.TableDescriptor
if err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
tableDesc, err = sqlbase.GetTableDescFromID(txn, sc.tableID)
return err
}); err != nil {
return err
}
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:
addedColumnDescs = append(addedColumnDescs, *t.Column)
if columnMutationIdx == mutationSentinel {
columnMutationIdx = i
}
case *sqlbase.DescriptorMutation_Index:
addedIndexDescs = append(addedIndexDescs, *t.Index)
if addedIndexMutationIdx == mutationSentinel {
addedIndexMutationIdx = i
}
default:
return errors.Errorf("unsupported mutation: %+v", m)
}
case sqlbase.DescriptorMutation_DROP:
switch t := m.Descriptor_.(type) {
case *sqlbase.DescriptorMutation_Column:
droppedColumnDescs = append(droppedColumnDescs, *t.Column)
if columnMutationIdx == mutationSentinel {
columnMutationIdx = i
}
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(lease, droppedIndexDescs, droppedIndexMutationIdx); err != nil {
return err
}
// Add and drop columns.
if err := sc.truncateAndBackfillColumns(
lease, addedColumnDescs, droppedColumnDescs, columnMutationIdx,
); err != nil {
return err
}
// Add new indexes.
if err := sc.backfillIndexes(lease, addedIndexDescs, addedIndexMutationIdx); err != nil {
return err
}
return nil
}
// getTableSpan returns a span stored at a checkpoint idx, or in the absence
// of a checkpoint, the span over all keys within a table.
func (sc *SchemaChanger) getTableSpan(mutationIdx int) (roachpb.Span, error) {
var tableDesc *sqlbase.TableDescriptor
if err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
var err error
tableDesc, err = sqlbase.GetTableDescFromID(txn, sc.tableID)
return err
}); err != nil {
return roachpb.Span{}, err
}
if len(tableDesc.Mutations) < mutationIdx {
return roachpb.Span{},
errors.Errorf("cannot find idx %d among %d mutations", mutationIdx, len(tableDesc.Mutations))
}
if mutationID := tableDesc.Mutations[mutationIdx].MutationID; mutationID != sc.mutationID {
return roachpb.Span{},
errors.Errorf("mutation index pointing to the wrong schema change, %d vs expected %d", mutationID, sc.mutationID)
}
resumeSpan := tableDesc.Mutations[mutationIdx].ResumeSpan
if resumeSpan.Key != nil {
return resumeSpan, nil
}
prefix := roachpb.Key(sqlbase.MakeIndexKeyPrefix(tableDesc, tableDesc.PrimaryIndex.ID))
return roachpb.Span{
Key: prefix,
EndKey: prefix.PrefixEnd(),
}, nil
}
func (sc *SchemaChanger) maybeWriteResumeSpan(
txn *client.Txn,
tableDesc *sqlbase.TableDescriptor,
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.Mutations[mutationIdx].ResumeSpan = resume
txn.SetSystemConfigTrigger()
if err := txn.Put(sqlbase.MakeDescMetadataKey(tableDesc.GetID()),
sqlbase.WrapDescriptor(tableDesc)); err != nil {
return err
}
*lastCheckpoint = timeutil.Now()
return nil
}
func (sc *SchemaChanger) truncateAndBackfillColumns(
lease *sqlbase.TableDescriptor_SchemaChangeLease,
added []sqlbase.ColumnDescriptor,
dropped []sqlbase.ColumnDescriptor,
mutationIdx int,
) error {
// Set the eval context timestamps.
pTime := timeutil.Now()
sc.evalCtx = parser.EvalContext{}
sc.evalCtx.SetTxnTimestamp(pTime)
sc.evalCtx.SetStmtTimestamp(pTime)
defaultExprs, err := makeDefaultExprs(added, &parser.Parser{}, &sc.evalCtx)
if err != nil {
return err
}
// Note if there is a new non nullable column with no default value.
addingNonNullableColumn := false
for _, columnDesc := range added {
if columnDesc.DefaultExpr == nil && !columnDesc.Nullable {
addingNonNullableColumn = true
break
}
}
// Add or Drop a column.
if len(dropped) > 0 || addingNonNullableColumn || len(defaultExprs) > 0 {
// Initialize a span of keys.
sp, err := sc.getTableSpan(mutationIdx)
if err != nil {
return err
}
// Run through the entire table key space adding and deleting columns.
chunkSize := sc.getChunkSize(columnTruncateAndBackfillChunkSize)
// Evaluate default values.
updateCols := append(added, dropped...)
updateValues := make(parser.DTuple, len(updateCols))
var nonNullViolationColumnName string
for j, col := range added {
if defaultExprs == nil || defaultExprs[j] == nil {
updateValues[j] = parser.DNull
} else {
updateValues[j], err = defaultExprs[j].Eval(&sc.evalCtx)
if err != nil {
return err
}
}
if !col.Nullable && updateValues[j].Compare(parser.DNull) == 0 {
nonNullViolationColumnName = col.Name
}
}
for j := range dropped {
updateValues[j+len(added)] = parser.DNull
}
lastCheckpoint := timeutil.Now()
for row, done := int64(0), false; !done; row += chunkSize {
// First extend the schema change lease.
l, err := sc.ExtendLease(*lease)
if err != nil {
return err
}
*lease = l
if log.V(2) {
log.Infof(context.TODO(), "column schema change (%d, %d) at row: %d, span: %s",
sc.tableID, sc.mutationID, row, sp)
}
// Add and delete columns for a chunk of the key space.
sp.Key, done, err = sc.truncateAndBackfillColumnsChunk(
added, dropped, defaultExprs, sp,
updateValues, nonNullViolationColumnName, chunkSize, mutationIdx, &lastCheckpoint)
if err != nil {
return err
}
}
}
return nil
}
// truncateAndBackfillColumnsChunk returns the next-key, done and an error.
// next-key and done are invalid if error != nil. next-key is invalid if done
// is true.
func (sc *SchemaChanger) truncateAndBackfillColumnsChunk(
added []sqlbase.ColumnDescriptor,
dropped []sqlbase.ColumnDescriptor,
defaultExprs []parser.TypedExpr,
sp roachpb.Span,
updateValues parser.DTuple,
nonNullViolationColumnName string,
chunkSize int64,
mutationIdx int,
lastCheckpoint *time.Time,
) (roachpb.Key, bool, error) {
done := false
var nextKey roachpb.Key
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
if sc.testingKnobs.RunBeforeBackfillChunk != nil {
if err := sc.testingKnobs.RunBeforeBackfillChunk(sp); err != nil {
return err
}
}
if sc.testingKnobs.RunAfterBackfillChunk != nil {
defer sc.testingKnobs.RunAfterBackfillChunk()
}
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
// Short circuit the backfill if the table has been deleted.
if done = tableDesc.Dropped(); done {
return nil
}
updateCols := append(added, dropped...)
fkTables := tablesNeededForFKs(*tableDesc, CheckUpdates)
for k := range fkTables {
table, err := sqlbase.GetTableDescFromID(txn, k)
if err != nil {
return err
}
fkTables[k] = tableLookup{table: table}
}
// TODO(dan): Tighten up the bound on the requestedCols parameter to
// makeRowUpdater.
requestedCols := make([]sqlbase.ColumnDescriptor, 0, len(tableDesc.Columns)+len(added))
requestedCols = append(requestedCols, tableDesc.Columns...)
requestedCols = append(requestedCols, added...)
ru, err := makeRowUpdater(
txn, tableDesc, fkTables, updateCols, requestedCols, rowUpdaterOnlyColumns,
)
if err != nil {
return err
}
// TODO(dan): This check is an unfortunate bleeding of the internals of
// rowUpdater. Extract the sql row to k/v mapping logic out into something
// usable here.
if !ru.isColumnOnlyUpdate() {
panic("only column data should be modified, but the rowUpdater is configured otherwise")
}
// Run a scan across the table using the primary key. Running
// the scan and applying the changes in many transactions is
// fine because the schema change is in the correct state to
// handle intermediate OLTP commands which delete and add
// values during the scan.
var rf sqlbase.RowFetcher
colIDtoRowIndex := colIDtoRowIndexFromCols(tableDesc.Columns)
valNeededForCol := make([]bool, len(tableDesc.Columns))
for i := range valNeededForCol {
_, valNeededForCol[i] = ru.fetchColIDtoRowIndex[tableDesc.Columns[i].ID]
}
if err := rf.Init(
tableDesc, colIDtoRowIndex, &tableDesc.PrimaryIndex, false, false,
tableDesc.Columns, valNeededForCol,
); err != nil {
return err
}
if err := rf.StartScan(
txn, roachpb.Spans{sp}, true /* limit batches */, chunkSize,
); err != nil {
return err
}
oldValues := make(parser.DTuple, len(ru.fetchCols))
writeBatch := txn.NewBatch()
rowLength := 0
var lastRowSeen parser.DTuple
i := int64(0)
for ; i < chunkSize; i++ {
row, err := rf.NextRowDecoded()
if err != nil {
return err
}
if row == nil {
break
}
lastRowSeen = row
if nonNullViolationColumnName != "" {
return sqlbase.NewNonNullViolationError(nonNullViolationColumnName)
}
copy(oldValues, row)
// Update oldValues with NULL values where values weren't found;
// only update when necessary.
if rowLength != len(row) {
rowLength = len(row)
for j := rowLength; j < len(oldValues); j++ {
oldValues[j] = parser.DNull
}
}
if _, err := ru.updateRow(txn.Context, writeBatch, oldValues, updateValues); err != nil {
return err
}
}
if err := txn.Run(writeBatch); err != nil {
return convertBackfillError(tableDesc, writeBatch)
}
if done = i < chunkSize; done {
return nil
}
curIndexKey, _, err := sqlbase.EncodeIndexKey(
tableDesc, &tableDesc.PrimaryIndex, colIDtoRowIndex, lastRowSeen,
sqlbase.MakeIndexKeyPrefix(tableDesc, tableDesc.PrimaryIndex.ID))
if err != nil {
return err
}
resume := roachpb.Span{Key: roachpb.Key(curIndexKey).PrefixEnd(), EndKey: sp.EndKey}
if err := sc.maybeWriteResumeSpan(txn, tableDesc, resume, mutationIdx, lastCheckpoint); err != nil {
return err
}
nextKey = resume.Key
return nil
})
return nextKey, done, err
}
func (sc *SchemaChanger) truncateIndexes(
lease *sqlbase.TableDescriptor_SchemaChangeLease,
dropped []sqlbase.IndexDescriptor,
mutationIdx int,
) error {
chunkSize := sc.getChunkSize(indexTruncateChunkSize)
if sc.testingKnobs.BackfillChunkSize > 0 {
chunkSize = sc.testingKnobs.BackfillChunkSize
}
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.
l, err := sc.ExtendLease(*lease)
if err != nil {
return err
}
*lease = l
resumeAt := resume
if log.V(2) {
log.Infof(context.TODO(), "drop index (%d, %d) at row: %d, span: %s",
sc.tableID, sc.mutationID, row, resume)
}
if err := sc.db.Txn(context.TODO(), func(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()
}
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
// Short circuit the truncation if the table has been deleted.
if done = tableDesc.Dropped(); done {
return nil
}
rd, err := makeRowDeleter(txn, tableDesc, nil, nil, false)
if err != nil {
return err
}
td := tableDeleter{rd: rd}
if err := td.init(txn); err != nil {
return err
}
resume, err = td.deleteIndex(
txn.Context, &desc, resumeAt, chunkSize,
)
if err != nil {
return err
}
if err := sc.maybeWriteResumeSpan(txn, tableDesc, resume, mutationIdx, &lastCheckpoint); err != nil {
return err
}
done = resume.Key == nil
return nil
}); err != nil {
return err
}
}
}
return nil
}
func (sc *SchemaChanger) backfillIndexes(
lease *sqlbase.TableDescriptor_SchemaChangeLease,
added []sqlbase.IndexDescriptor,
mutationIdx int,
) error {
if len(added) == 0 {
return nil
}
// Initialize a span of keys.
sp, err := sc.getTableSpan(mutationIdx)
if err != nil {
return err
}
// Backfill the index entries for all the rows.
chunkSize := sc.getChunkSize(indexBackfillChunkSize)
lastCheckpoint := timeutil.Now()
for row, done := int64(0), false; !done; row += chunkSize {
// First extend the schema change lease.
l, err := sc.ExtendLease(*lease)
if err != nil {
return err
}
*lease = l
if log.V(2) {
log.Infof(context.TODO(), "index add (%d, %d) at row: %d, span: %s",
sc.tableID, sc.mutationID, row, sp)
}
sp.Key, done, err = sc.backfillIndexesChunk(added, sp, chunkSize, mutationIdx, &lastCheckpoint)
if err != nil {
return err
}
}
return nil
}
// backfillIndexesChunk returns the next-key, done and an error. next-key and
// done are invalid if error != nil. next-key is invalid if done is true.
func (sc *SchemaChanger) backfillIndexesChunk(
added []sqlbase.IndexDescriptor,
sp roachpb.Span,
chunkSize int64,
mutationIdx int,
lastCheckpoint *time.Time,
) (roachpb.Key, bool, error) {
var nextKey roachpb.Key
done := false
secondaryIndexEntries := make([]sqlbase.IndexEntry, len(added))
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
if sc.testingKnobs.RunBeforeBackfillChunk != nil {
if err := sc.testingKnobs.RunBeforeBackfillChunk(sp); err != nil {
return err
}
}
if sc.testingKnobs.RunAfterBackfillChunk != nil {
defer sc.testingKnobs.RunAfterBackfillChunk()
}
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
// Short circuit the backfill if the table has been deleted.
if done = tableDesc.Dropped(); done {
return nil
}
// Get the next set of rows.
// TODO(tamird): Support partial indexes?
//
// Use a scanNode with SELECT to pass in a sqlbase.TableDescriptor
// to the SELECT without needing to go through table name
// resolution, because we want to run schema changes from a gossip
// feed of table IDs. Running the scan and applying the changes in
// many transactions is fine because the schema change is in the
// correct state to handle intermediate OLTP commands which delete
// and add values during the scan.
planner := makePlanner("backfill")
planner.setTxn(txn)
scan := planner.Scan()
scan.desc = *tableDesc
scan.spans = []roachpb.Span{sp}
scan.SetLimitHint(chunkSize, false)
scan.initDescDefaults(publicAndNonPublicColumns)
rows, err := selectIndex(scan, nil, false)
if err != nil {
return err
}
if err := rows.Start(); err != nil {
return err
}
// Construct a map from column ID to the index the value appears at
// within a row.
colIDtoRowIndex, err := makeColIDtoRowIndex(rows, tableDesc)
if err != nil {
return err
}
b := &client.Batch{}
numRows := int64(0)
for ; numRows < chunkSize; numRows++ {
if next, err := rows.Next(); !next {
if err != nil {
return err
}
break
}
rowVals := rows.Values()
err := sqlbase.EncodeSecondaryIndexes(
tableDesc, added, colIDtoRowIndex,
rowVals, secondaryIndexEntries)
if err != nil {
return err
}
for _, secondaryIndexEntry := range secondaryIndexEntries {
if log.V(2) {
log.Infof(txn.Context, "InitPut %s -> %v", secondaryIndexEntry.Key,
secondaryIndexEntry.Value)
}
b.InitPut(secondaryIndexEntry.Key, &secondaryIndexEntry.Value)
}
}
// Write the new index values.
if err := txn.Run(b); err != nil {
return convertBackfillError(tableDesc, b)
}
// Have we processed all the table rows?
if done = numRows < chunkSize; done {
return nil
}
// Keep track of the next key.
resume := roachpb.Span{Key: scan.fetcher.Key(), EndKey: sp.EndKey}
if err := sc.maybeWriteResumeSpan(txn, tableDesc, resume, mutationIdx, lastCheckpoint); err != nil {
return err
}
nextKey = resume.Key
return nil
})
return nextKey, done, err
}