forked from cockroachdb/cockroach
/
backfill.go
542 lines (498 loc) · 16.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
// 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 (
"bytes"
"sort"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/util/log"
"github.com/cockroachdb/cockroach/util/timeutil"
)
func makeColIDtoRowIndex(row planNode, desc *TableDescriptor) (map[ColumnID]int, error) {
columns := row.Columns()
colIDtoRowIndex := make(map[ColumnID]int, len(columns))
for i, column := range columns {
col, err := desc.FindActiveColumnByName(column.Name)
if err != nil {
return nil, err
}
colIDtoRowIndex[col.ID] = i
}
return colIDtoRowIndex, nil
}
var _ sort.Interface = columnsByID{}
var _ sort.Interface = indexesByID{}
type columnsByID []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 []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 *TableDescriptor, b *client.Batch, pErr *roachpb.Error,
) *roachpb.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, pErr)
}
var descriptorChangedVersionError = roachpb.NewErrorf("table descriptor has changed version")
// getTableDescAtVersion attempts to read a descriptor from the database at a
// specific version. It returns descriptorChangedVersionError if the current
// version is not the passed in version.
func getTableDescAtVersion(
txn *client.Txn, id ID, version DescriptorVersion,
) (*TableDescriptor, *roachpb.Error) {
tableDesc, pErr := getTableDescFromID(txn, id)
if pErr != nil {
return nil, pErr
}
if tableDesc == nil {
return nil, roachpb.NewError(&roachpb.DescriptorDeletedError{})
}
if version != tableDesc.Version {
return nil, descriptorChangedVersionError
}
return tableDesc, nil
}
// runBackfill runs the backfill for the schema changer. It runs the entire
// backfill at a specific version of the table descriptor, and re-attempts to
// run the schema change when the version changes.
func (sc *SchemaChanger) runBackfill(lease *TableDescriptor_SchemaChangeLease) *roachpb.Error {
for {
pErr := sc.runBackfillAtLatestVersion(lease)
if pErr == descriptorChangedVersionError {
continue
}
return pErr
}
}
// Run the backfill at the latest table descriptor version. It returns
// descriptorChangedVersionError when the table descriptor version changes
// while it is running the backfill.
func (sc *SchemaChanger) runBackfillAtLatestVersion(
lease *TableDescriptor_SchemaChangeLease,
) *roachpb.Error {
l, pErr := sc.ExtendLease(*lease)
if pErr != nil {
return pErr
}
*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 []ColumnDescriptor
var droppedIndexDescs []IndexDescriptor
var addedColumnDescs []ColumnDescriptor
var addedIndexDescs []IndexDescriptor
// Remember the version at the start of the backfill so we can ensure
// later that the table descriptor hasn't changed during the backfill
// process.
var version DescriptorVersion
if pErr := sc.db.Txn(func(txn *client.Txn) *roachpb.Error {
tableDesc, pErr := getTableDescFromID(txn, sc.tableID)
if pErr != nil {
return pErr
}
if tableDesc == nil {
return roachpb.NewError(&roachpb.DescriptorDeletedError{})
}
version = tableDesc.Version
for _, m := range tableDesc.Mutations {
if m.MutationID != sc.mutationID {
break
}
switch m.Direction {
case DescriptorMutation_ADD:
switch t := m.Descriptor_.(type) {
case *DescriptorMutation_Column:
addedColumnDescs = append(addedColumnDescs, *t.Column)
case *DescriptorMutation_Index:
addedIndexDescs = append(addedIndexDescs, *t.Index)
default:
return roachpb.NewErrorf("unsupported mutation: %+v", m)
}
case DescriptorMutation_DROP:
switch t := m.Descriptor_.(type) {
case *DescriptorMutation_Column:
droppedColumnDescs = append(droppedColumnDescs, *t.Column)
case *DescriptorMutation_Index:
droppedIndexDescs = append(droppedIndexDescs, *t.Index)
default:
return roachpb.NewErrorf("unsupported mutation: %+v", m)
}
}
}
return nil
}); pErr != nil {
return pErr
}
// Add and drop columns.
if pErr := sc.truncateAndBackfillColumns(
lease, addedColumnDescs, droppedColumnDescs, version,
); pErr != nil {
return pErr
}
// Drop indexes.
if pErr := sc.truncateIndexes(lease, droppedIndexDescs, version); pErr != nil {
return pErr
}
// Add new indexes.
if pErr := sc.backfillIndexes(lease, addedIndexDescs, version); pErr != nil {
return pErr
}
return nil
}
// getTableSpan returns a span containing the start and end key for a table.
// It also checks that the version hasn't changed.
func (sc *SchemaChanger) getTableSpan(version DescriptorVersion) (span, *roachpb.Error) {
var tableDesc *TableDescriptor
if pErr := sc.db.Txn(func(txn *client.Txn) *roachpb.Error {
var pErr *roachpb.Error
tableDesc, pErr = getTableDescAtVersion(txn, sc.tableID, version)
return pErr
}); pErr != nil {
return span{}, pErr
}
prefix := roachpb.Key(MakeIndexKeyPrefix(tableDesc.ID, tableDesc.PrimaryIndex.ID))
return span{
start: prefix,
end: 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 *TableDescriptor_SchemaChangeLease,
added []ColumnDescriptor,
dropped []ColumnDescriptor,
version DescriptorVersion,
) *roachpb.Error {
evalCtx := parser.EvalContext{}
// Set the eval context timestamps.
pTime := timeutil.Now()
evalCtx.SetTxnTimestamp(pTime)
evalCtx.SetStmtTimestamp(pTime)
defaultExprs, err := makeDefaultExprs(added, &parser.Parser{}, evalCtx)
if err != nil {
return roachpb.NewError(err)
}
marshalled := make([]roachpb.Value, len(defaultExprs))
// Remember any new non nullable column with no default value.
nonNullableColumn := ""
for _, columnDesc := range added {
if columnDesc.DefaultExpr == nil && !columnDesc.Nullable {
nonNullableColumn = columnDesc.Name
}
}
// Add or Drop a column.
if len(dropped) > 0 || nonNullableColumn != "" || len(defaultExprs) > 0 {
// First extend the schema change lease.
l, pErr := sc.ExtendLease(*lease)
if pErr != nil {
return pErr
}
*lease = l
// Initialize start and end to represent a span of keys.
sp, err := sc.getTableSpan(version)
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, pErr := sc.ExtendLease(*lease)
if pErr != nil {
return pErr
}
*lease = l
var currSentinel roachpb.Key
if pErr := sc.db.Txn(func(txn *client.Txn) *roachpb.Error {
tableDesc, pErr := getTableDescAtVersion(txn, sc.tableID, version)
if pErr != nil {
return pErr
}
// 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.
b := &client.Batch{}
b.Scan(sp.start, sp.end, ColumnTruncateAndBackfillChunkSize)
if pErr := txn.Run(b); pErr != nil {
return pErr
}
// Use a different batch to truncate/backfill columns.
writeBatch := &client.Batch{}
done = true
for _, result := range b.Results {
var sentinelKey roachpb.Key
for _, kv := range result.Rows {
// Still processing table.
done = false
if nonNullableColumn != "" {
return roachpb.NewErrorf("column %s contains null values", nonNullableColumn)
}
if sentinelKey == nil || !bytes.HasPrefix(kv.Key, sentinelKey) {
// Sentinel keys have a 0 suffix indicating 0
// bytes of column ID. Strip off that suffix to
// determine the prefix shared with the other keys
// for the row.
sentinelKey = stripColumnIDLength(kv.Key)
// Store away key for the next table row as the
// point from which to start from.
currSentinel = sentinelKey
// Delete the entire dropped columns. This used to
// use SQL UPDATE in the past to update the
// dropped column to NULL; but a column in the
// process of being dropped is placed in the table
// descriptor mutations, and a SQL UPDATE of a
// column in mutations will fail.
for _, columnDesc := range dropped {
// Delete the dropped column.
colKey := keys.MakeColumnKey(sentinelKey, uint32(columnDesc.ID))
if log.V(2) {
log.Infof("Del %s", colKey)
}
writeBatch.Del(colKey)
}
// Add the new columns and backfill the values.
for i, expr := range defaultExprs {
if expr == nil {
continue
}
col := added[i]
colKey := keys.MakeColumnKey(sentinelKey, uint32(col.ID))
d, err := expr.Eval(evalCtx)
if err != nil {
return roachpb.NewError(err)
}
marshalled[i], err = marshalColumnValue(col, d)
if err != nil {
return roachpb.NewError(err)
}
if log.V(2) {
log.Infof("Put %s -> %v", colKey, d)
}
// Insert default value into the column. If
// this row was recently added the default
// value might have already been populated,
// because the ColumnDescriptor is in the
// WRITE_ONLY state. Reinserting the default
// value is not a big deal.
//
// Note: a column in the WRITE_ONLY state
// cannot be populated directly through SQL. A
// SQL INSERT cannot directly reference the
// column, and the INSERT populates the column
// with the default value.
writeBatch.Put(colKey, &marshalled[i])
}
}
}
}
if pErr := txn.Run(writeBatch); pErr != nil {
return convertBackfillError(tableDesc, writeBatch, pErr)
}
return nil
}); pErr != nil {
return pErr
}
// Store away next starting point.
sp.start = currSentinel.PrefixEnd()
}
}
return nil
}
func (sc *SchemaChanger) truncateIndexes(
lease *TableDescriptor_SchemaChangeLease,
dropped []IndexDescriptor,
version DescriptorVersion,
) *roachpb.Error {
for _, desc := range dropped {
// First extend the schema change lease.
l, pErr := sc.ExtendLease(*lease)
if pErr != nil {
return pErr
}
*lease = l
if pErr := sc.db.Txn(func(txn *client.Txn) *roachpb.Error {
tableDesc, pErr := getTableDescAtVersion(txn, sc.tableID, version)
if pErr != nil {
return pErr
}
indexPrefix := MakeIndexKeyPrefix(tableDesc.ID, desc.ID)
// Delete the index.
indexStartKey := roachpb.Key(indexPrefix)
indexEndKey := indexStartKey.PrefixEnd()
if log.V(2) {
log.Infof("DelRange %s - %s", indexStartKey, indexEndKey)
}
b := &client.Batch{}
b.DelRange(indexStartKey, indexEndKey, false)
if pErr := txn.Run(b); pErr != nil {
return pErr
}
return nil
}); pErr != nil {
return pErr
}
}
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 *TableDescriptor_SchemaChangeLease,
added []IndexDescriptor,
version DescriptorVersion,
) *roachpb.Error {
if len(added) == 0 {
return nil
}
// First extend the schema change lease.
l, pErr := sc.ExtendLease(*lease)
if pErr != nil {
return pErr
}
*lease = l
// Initialize start and end to represent a span of keys.
sp, err := sc.getTableSpan(version)
if err != nil {
return err
}
// Backfill the index entries for all the rows.
for done := false; !done; {
// First extend the schema change lease.
l, pErr := sc.ExtendLease(*lease)
if pErr != nil {
return pErr
}
*lease = l
var nextKey roachpb.Key
pErr = sc.db.Txn(func(txn *client.Txn) *roachpb.Error {
tableDesc, pErr := getTableDescAtVersion(txn, sc.tableID, version)
if pErr != nil {
return pErr
}
// Get the next set of rows.
// TODO(tamird): Support partial indexes?
//
// Use a scanNode with SELECT to pass in a TableDescriptor to the
// SELECT without needing to use a parser.QualifiedName, 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.
scan := &scanNode{
planner: makePlanner(),
txn: txn,
desc: *tableDesc,
spans: []span{sp},
}
scan.initDescDefaults()
rows, err := selectIndex(scan, nil, false)
if err != nil {
return roachpb.NewError(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 roachpb.NewError(err)
}
b := &client.Batch{}
numRows := 0
for ; numRows < IndexBackfillChunkSize && rows.Next(); numRows++ {
rowVals := rows.Values()
for _, desc := range added {
secondaryIndexEntries, err := encodeSecondaryIndexes(
tableDesc.ID, []IndexDescriptor{desc}, colIDtoRowIndex, rowVals)
if err != nil {
return roachpb.NewError(err)
}
for _, secondaryIndexEntry := range secondaryIndexEntries {
if log.V(2) {
log.Infof("InitPut %s -> %v", secondaryIndexEntry.key,
secondaryIndexEntry.value)
}
b.InitPut(secondaryIndexEntry.key, secondaryIndexEntry.value)
}
}
}
if rows.PErr() != nil {
return rows.PErr()
}
// Write the new index values.
if pErr := txn.Run(b); pErr != nil {
return convertBackfillError(tableDesc, b, pErr)
}
// Have we processed all the table rows?
if numRows < IndexBackfillChunkSize {
done = true
return nil
}
// Update sp.start to the next possible key; sp.end remains the
// same.
nextKey = scan.fetcher.kv.Key
return nil
})
if pErr != nil {
return pErr
}
sp.start = nextKey
}
return nil
}