forked from cockroachdb/cockroach
/
backfill.go
552 lines (502 loc) · 15.8 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
// 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"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/internal/client"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/sqlbase"
"github.com/cockroachdb/cockroach/util/log"
"github.com/cockroachdb/cockroach/util/timeutil"
"github.com/pkg/errors"
)
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(sqlbase.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)
}
// 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
if err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
for _, 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)
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:
droppedColumnDescs = append(droppedColumnDescs, *t.Column)
case *sqlbase.DescriptorMutation_Index:
droppedIndexDescs = append(droppedIndexDescs, *t.Index)
default:
return errors.Errorf("unsupported mutation: %+v", m)
}
}
}
return nil
}); err != nil {
return err
}
// First drop indexes, then add/drop columns, and only then add indexes.
// Drop indexes.
if err := sc.truncateIndexes(lease, droppedIndexDescs); err != nil {
return err
}
// Add and drop columns.
if err := sc.truncateAndBackfillColumns(
lease, addedColumnDescs, droppedColumnDescs,
); err != nil {
return err
}
// Add new indexes.
if err := sc.backfillIndexes(lease, addedIndexDescs); err != nil {
return err
}
return nil
}
// getTableSpan returns a span containing the start and end key for a table.
func (sc *SchemaChanger) getTableSpan() (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
}
prefix := roachpb.Key(sqlbase.MakeIndexKeyPrefix(tableDesc, tableDesc.PrimaryIndex.ID))
return roachpb.Span{
Key: prefix,
EndKey: prefix.PrefixEnd(),
}, nil
}
// ColumnTruncateAndBackfillChunkSize is the maximum number of rows of keys
// processed per chunk during the column truncate or backfill.
//
// TODO(vivek): Run some experiments to set this value to something sensible
// or adjust it dynamically. Also add in a sleep after every chunk is
// processed to slow down the backfill and reduce its CPU usage.
const ColumnTruncateAndBackfillChunkSize = 600
func (sc *SchemaChanger) truncateAndBackfillColumns(
lease *sqlbase.TableDescriptor_SchemaChangeLease,
added []sqlbase.ColumnDescriptor,
dropped []sqlbase.ColumnDescriptor,
) 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 start and end to represent a span of keys.
sp, err := sc.getTableSpan()
if err != nil {
return err
}
// Run through the entire table key space adding and deleting columns.
for done := false; !done; {
// First extend the schema change lease.
l, err := sc.ExtendLease(*lease)
if err != nil {
return err
}
*lease = l
// Add and delete columns for a chunk of the key space.
sp.Key, done, err = sc.truncateAndBackfillColumnsChunk(
added, dropped, defaultExprs, &sc.evalCtx, sp,
)
if err != nil {
return err
}
}
}
return nil
}
func (sc *SchemaChanger) truncateAndBackfillColumnsChunk(
added []sqlbase.ColumnDescriptor,
dropped []sqlbase.ColumnDescriptor,
defaultExprs []parser.TypedExpr,
evalCtx *parser.EvalContext,
sp roachpb.Span,
) (roachpb.Key, bool, error) {
var curIndexKey roachpb.Key
done := false
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
// Short circuit the backfill if the table has been deleted.
if tableDesc.Deleted() {
done = true
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]
}
err = rf.Init(tableDesc, colIDtoRowIndex, &tableDesc.PrimaryIndex, false, false,
tableDesc.Columns, valNeededForCol)
if err != nil {
return err
}
err = rf.StartScan(txn, roachpb.Spans{sp}, true /* limit batches */, 0 /* no limit hint */)
if err != nil {
return err
}
indexKeyPrefix := sqlbase.MakeIndexKeyPrefix(tableDesc, tableDesc.PrimaryIndex.ID)
oldValues := make(parser.DTuple, len(ru.fetchCols))
updateValues := make(parser.DTuple, len(updateCols))
writeBatch := &client.Batch{}
var i int
for ; i < ColumnTruncateAndBackfillChunkSize; i++ {
row, err := rf.NextRow()
if err != nil {
return err
}
if row == nil {
break // Done
}
curIndexKey, _, err = sqlbase.EncodeIndexKey(
tableDesc, &tableDesc.PrimaryIndex, colIDtoRowIndex, row, indexKeyPrefix)
for j, col := range added {
if defaultExprs == nil || defaultExprs[j] == nil {
updateValues[j] = parser.DNull
} else {
updateValues[j], err = defaultExprs[j].Eval(evalCtx)
if err != nil {
return err
}
}
if !col.Nullable && updateValues[j].Compare(parser.DNull) == 0 {
return sqlbase.NewNonNullViolationError(col.Name)
}
}
for j := range dropped {
updateValues[j+len(added)] = parser.DNull
}
copy(oldValues, row)
for j := len(row); j < len(oldValues); j++ {
oldValues[j] = parser.DNull
}
if _, err := ru.updateRow(context.TODO(), writeBatch, oldValues, updateValues); err != nil {
return err
}
}
if i < ColumnTruncateAndBackfillChunkSize {
done = true
}
if err := txn.Run(writeBatch); err != nil {
return convertBackfillError(tableDesc, writeBatch)
}
return nil
})
return curIndexKey.PrefixEnd(), done, err
}
func (sc *SchemaChanger) truncateIndexes(
lease *sqlbase.TableDescriptor_SchemaChangeLease,
dropped []sqlbase.IndexDescriptor,
) error {
for _, desc := range dropped {
var resume roachpb.Span
for done := false; !done; {
// First extend the schema change lease.
l, err := sc.ExtendLease(*lease)
if err != nil {
return err
}
*lease = l
resumeAt := resume
if err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
// Short circuit the truncation if the table has been deleted.
if tableDesc.Deleted() {
done = true
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(
context.TODO(), &desc, resumeAt, IndexBackfillChunkSize,
)
done = resume.Key == nil
return err
}); err != nil {
return err
}
}
}
return nil
}
// IndexBackfillChunkSize is the maximum number of rows processed per chunk
// during the index backfill.
//
// TODO(vivek) Run some experiments to set this value to something sensible or
// adjust it dynamically. Also add in a sleep after every chunk is processed,
// to slow down the backfill and not have it interfere with OLTP commands.
const IndexBackfillChunkSize = 100
func (sc *SchemaChanger) backfillIndexes(
lease *sqlbase.TableDescriptor_SchemaChangeLease,
added []sqlbase.IndexDescriptor,
) error {
if len(added) == 0 {
return nil
}
// Initialize start and end to represent a span of keys.
sp, err := sc.getTableSpan()
if err != nil {
return err
}
// Backfill the index entries for all the rows.
for done := false; !done; {
// First extend the schema change lease.
l, err := sc.ExtendLease(*lease)
if err != nil {
return err
}
*lease = l
sp.Key, done, err = sc.backfillIndexesChunk(added, sp)
if err != nil {
return err
}
}
return nil
}
func (sc *SchemaChanger) backfillIndexesChunk(
added []sqlbase.IndexDescriptor,
sp roachpb.Span,
) (roachpb.Key, bool, error) {
var nextKey roachpb.Key
done := false
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
// Short circuit the backfill if the table has been deleted.
if tableDesc.Deleted() {
done = true
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()
planner.setTxn(txn)
scan := planner.Scan()
scan.desc = *tableDesc
scan.spans = []roachpb.Span{sp}
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 := 0
for ; numRows < IndexBackfillChunkSize; numRows++ {
if next, err := rows.Next(); !next {
if err != nil {
return err
}
break
}
rowVals := rows.Values()
for _, desc := range added {
secondaryIndexEntries := make([]sqlbase.IndexEntry, 1)
err := sqlbase.EncodeSecondaryIndexes(
tableDesc, []sqlbase.IndexDescriptor{desc}, colIDtoRowIndex,
rowVals, secondaryIndexEntries)
if err != nil {
return err
}
for _, secondaryIndexEntry := range secondaryIndexEntries {
if log.V(2) {
log.Infof(context.TODO(), "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 numRows < IndexBackfillChunkSize {
done = true
return nil
}
// Keep track of the next key.
nextKey = scan.fetcher.Key()
return nil
})
return nextKey, done, err
}