-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathschema_changer.go
826 lines (761 loc) · 26.9 KB
/
schema_changer.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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
// 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: Vivek Menezes ([email protected])
package sql
import (
"bytes"
"fmt"
"math"
"time"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/gossip"
"github.com/cockroachdb/cockroach/internal/client"
"github.com/cockroachdb/cockroach/keys"
"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/retry"
"github.com/cockroachdb/cockroach/util/stop"
"github.com/cockroachdb/cockroach/util/timeutil"
"github.com/pkg/errors"
)
var (
// SchemaChangeLeaseDuration is the duration a lease will be acquired for.
// Exported for testing purposes only.
SchemaChangeLeaseDuration = 5 * time.Minute
// MinSchemaChangeLeaseDuration is the minimum duration a lease will have
// remaining upon acquisition. Exported for testing purposes only.
MinSchemaChangeLeaseDuration = time.Minute
)
// SchemaChanger is used to change the schema on a table.
type SchemaChanger struct {
tableID sqlbase.ID
mutationID sqlbase.MutationID
nodeID roachpb.NodeID
db client.DB
leaseMgr *LeaseManager
evalCtx parser.EvalContext
// The SchemaChangeManager can attempt to execute this schema
// changer after this time.
execAfter time.Time
}
func (sc *SchemaChanger) truncateAndDropTable(
ctx context.Context,
lease *sqlbase.TableDescriptor_SchemaChangeLease,
tableDesc *sqlbase.TableDescriptor) error {
l, err := sc.ExtendLease(*lease)
if err != nil {
return err
}
*lease = l
return truncateAndDropTable(ctx, tableDesc, &sc.db)
}
// NewSchemaChangerForTesting only for tests.
func NewSchemaChangerForTesting(
tableID sqlbase.ID,
mutationID sqlbase.MutationID,
nodeID roachpb.NodeID,
db client.DB,
leaseMgr *LeaseManager,
) SchemaChanger {
return SchemaChanger{
tableID: tableID,
mutationID: mutationID,
nodeID: nodeID,
db: db,
leaseMgr: leaseMgr}
}
func (sc *SchemaChanger) createSchemaChangeLease() sqlbase.TableDescriptor_SchemaChangeLease {
return sqlbase.TableDescriptor_SchemaChangeLease{
NodeID: sc.nodeID, ExpirationTime: timeutil.Now().Add(SchemaChangeLeaseDuration).UnixNano()}
}
var errExistingSchemaChangeLease = errors.New(
"an outstanding schema change lease exists")
// AcquireLease acquires a schema change lease on the table if
// an unexpired lease doesn't exist. It returns the lease.
func (sc *SchemaChanger) AcquireLease() (sqlbase.TableDescriptor_SchemaChangeLease, error) {
var lease sqlbase.TableDescriptor_SchemaChangeLease
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
txn.SetSystemConfigTrigger()
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
// A second to deal with the time uncertainty across nodes.
// It is perfectly valid for two or more goroutines to hold a valid
// lease and execute a schema change in parallel, because schema
// changes are executed using transactions that run sequentially.
// This just reduces the probability of a write collision.
expirationTimeUncertainty := time.Second
if tableDesc.Lease != nil {
if time.Unix(0, tableDesc.Lease.ExpirationTime).Add(expirationTimeUncertainty).After(timeutil.Now()) {
return errExistingSchemaChangeLease
}
log.Infof(txn.Context, "Overriding existing expired lease %v", tableDesc.Lease)
}
lease = sc.createSchemaChangeLease()
tableDesc.Lease = &lease
return txn.Put(sqlbase.MakeDescMetadataKey(tableDesc.ID), sqlbase.WrapDescriptor(tableDesc))
})
return lease, err
}
func (sc *SchemaChanger) findTableWithLease(
txn *client.Txn, lease sqlbase.TableDescriptor_SchemaChangeLease,
) (*sqlbase.TableDescriptor, error) {
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return nil, err
}
if tableDesc.Lease == nil {
return nil, errors.Errorf("no lease present for tableID: %d", sc.tableID)
}
if *tableDesc.Lease != lease {
return nil, errors.Errorf("table: %d has lease: %v, expected: %v", sc.tableID, tableDesc.Lease, lease)
}
return tableDesc, nil
}
// ReleaseLease the table lease if it is the one registered with
// the table descriptor.
func (sc *SchemaChanger) ReleaseLease(lease sqlbase.TableDescriptor_SchemaChangeLease) error {
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
tableDesc, err := sc.findTableWithLease(txn, lease)
if err != nil {
return err
}
tableDesc.Lease = nil
txn.SetSystemConfigTrigger()
return txn.Put(sqlbase.MakeDescMetadataKey(tableDesc.ID), sqlbase.WrapDescriptor(tableDesc))
})
return err
}
// ExtendLease for the current leaser. This needs to be called often while
// doing a schema change to prevent more than one node attempting to apply a
// schema change (which is still safe, but unwise).
func (sc *SchemaChanger) ExtendLease(
existingLease sqlbase.TableDescriptor_SchemaChangeLease,
) (sqlbase.TableDescriptor_SchemaChangeLease, error) {
// Check if there is still time on this lease.
minDesiredExpiration := timeutil.Now().Add(MinSchemaChangeLeaseDuration)
if time.Unix(0, existingLease.ExpirationTime).After(minDesiredExpiration) {
return existingLease, nil
}
// Update lease.
var lease sqlbase.TableDescriptor_SchemaChangeLease
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
tableDesc, err := sc.findTableWithLease(txn, existingLease)
if err != nil {
return err
}
lease = sc.createSchemaChangeLease()
tableDesc.Lease = &lease
txn.SetSystemConfigTrigger()
return txn.Put(sqlbase.MakeDescMetadataKey(tableDesc.ID), sqlbase.WrapDescriptor(tableDesc))
})
return lease, err
}
func isSchemaChangeRetryError(err error) bool {
switch err {
case sqlbase.ErrDescriptorNotFound:
return false
default:
return !sqlbase.IsIntegrityConstraintError(err)
}
}
// Execute the entire schema change in steps. startBackfillNotification is
// called before the backfill starts; it can be nil.
func (sc SchemaChanger) exec(
startBackfillNotification func() error,
oldNameNotInUseNotification func(),
) error {
// Acquire lease.
lease, err := sc.AcquireLease()
if err != nil {
return err
}
needRelease := true
// Always try to release lease.
defer func(l *sqlbase.TableDescriptor_SchemaChangeLease) {
// If the schema changer deleted the descriptor, there's no longer a lease to be
// released.
if !needRelease {
return
}
if err := sc.ReleaseLease(*l); err != nil {
log.Warning(context.TODO(), err)
}
}(&lease)
// Increment the version and unset tableDescriptor.UpVersion.
desc, err := sc.MaybeIncrementVersion()
if err != nil {
return err
}
table := desc.GetTable()
if table.Deleted() {
lease, err = sc.ExtendLease(lease)
if err != nil {
return err
}
// Wait for everybody to see the version with the deleted bit set. When
// this returns, nobody has any leases on the table, nor can get new leases,
// so the table will no longer be modified.
if err := sc.waitToUpdateLeases(sc.tableID); err != nil {
return err
}
// Truncate the table and delete the descriptor.
if err := sc.truncateAndDropTable(context.TODO(), &lease, table); err != nil {
return err
}
needRelease = false
return nil
}
if table.Adding() {
for _, idx := range table.AllNonDropIndexes() {
if idx.ForeignKey.IsSet() {
if err := sc.waitToUpdateLeases(idx.ForeignKey.Table); err != nil {
return err
}
}
}
if _, err := sc.leaseMgr.Publish(
table.ID,
func(tbl *sqlbase.TableDescriptor) error {
tbl.State = sqlbase.TableDescriptor_PUBLIC
return nil
},
func(txn *client.Txn) error { return nil },
); err != nil {
return err
}
}
if table.Renamed() {
lease, err = sc.ExtendLease(lease)
if err != nil {
return err
}
// Wait for everyone to see the version with the new name. When this
// returns, no new transactions will be using the old name for the table, so
// the old name can now be re-used (by CREATE).
if err := sc.waitToUpdateLeases(sc.tableID); err != nil {
return err
}
if oldNameNotInUseNotification != nil {
oldNameNotInUseNotification()
}
// Free up the old name(s).
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
b := txn.NewBatch()
for _, renameDetails := range table.Renames {
tbKey := tableKey{renameDetails.OldParentID, renameDetails.OldName}.Key()
b.Del(tbKey)
}
return txn.Run(b)
})
if err != nil {
return err
}
// Clean up - clear the descriptor's state.
_, err = sc.leaseMgr.Publish(sc.tableID, func(desc *sqlbase.TableDescriptor) error {
desc.Renames = nil
return nil
}, nil)
if err != nil {
return err
}
}
// Wait for the schema change to propagate to all nodes after this function
// returns, so that the new schema is live everywhere. This is not needed for
// correctness but is done to make the UI experience/tests predictable.
defer func() {
if err := sc.waitToUpdateLeases(sc.tableID); err != nil {
log.Warning(context.TODO(), err)
}
}()
if sc.mutationID == sqlbase.InvalidMutationID {
// Nothing more to do.
return nil
}
// Another transaction might set the up_version bit again,
// but we're no longer responsible for taking care of that.
// Run through mutation state machine and backfill.
err = sc.runStateMachineAndBackfill(&lease, startBackfillNotification)
// Purge the mutations if the application of the mutations failed due to
// an integrity constraint violation. All other errors are transient
// errors that are resolved by retrying the backfill.
if sqlbase.IsIntegrityConstraintError(err) {
log.Warningf(context.TODO(), "reversing schema change due to irrecoverable error: %s", err)
if errReverse := sc.reverseMutations(err); errReverse != nil {
// Although the backfill did hit an integrity constraint violation
// and made a decision to reverse the mutations,
// reverseMutations() failed. If exec() is called again the entire
// schema change will be retried.
return errReverse
}
// After this point the schema change has been reversed and any retry
// of the schema change will act upon the reversed schema change.
if errPurge := sc.runStateMachineAndBackfill(
&lease, startBackfillNotification,
); errPurge != nil {
// Don't return this error because we do want the caller to know
// that an integrity constraint was violated with the original
// schema change. The reversed schema change will be
// retried via the async schema change manager.
log.Warningf(context.TODO(), "error purging mutation: %s, after error: %s", errPurge, err)
}
}
return err
}
// MaybeIncrementVersion increments the version if needed.
// If the version is to be incremented, it also assures that all nodes are on
// the current (pre-increment) version of the descriptor.
// Returns the (potentially updated) descriptor.
func (sc *SchemaChanger) MaybeIncrementVersion() (*sqlbase.Descriptor, error) {
return sc.leaseMgr.Publish(sc.tableID, func(desc *sqlbase.TableDescriptor) error {
if !desc.UpVersion {
// Return error so that Publish() doesn't increment the version.
return errDidntUpdateDescriptor
}
desc.UpVersion = false
// Publish() will increment the version.
return nil
}, nil)
}
// RunStateMachineBeforeBackfill moves the state machine forward
// and wait to ensure that all nodes are seeing the latest version
// of the table.
func (sc *SchemaChanger) RunStateMachineBeforeBackfill() error {
if _, err := sc.leaseMgr.Publish(sc.tableID, func(desc *sqlbase.TableDescriptor) error {
var modified bool
// Apply mutations belonging to the same version.
for i, mutation := range desc.Mutations {
if mutation.MutationID != sc.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
}
switch mutation.Direction {
case sqlbase.DescriptorMutation_ADD:
switch mutation.State {
case sqlbase.DescriptorMutation_DELETE_ONLY:
// TODO(vivek): while moving up the state is appropriate,
// it will be better to run the backfill of a unique index
// twice: once in the DELETE_ONLY state to confirm that
// the index can indeed be created, and subsequently in the
// WRITE_ONLY state to fill in the missing elements of the
// index (INSERT and UPDATE that happened in the interim).
desc.Mutations[i].State = sqlbase.DescriptorMutation_WRITE_ONLY
modified = true
case sqlbase.DescriptorMutation_WRITE_ONLY:
// The state change has already moved forward.
}
case sqlbase.DescriptorMutation_DROP:
switch mutation.State {
case sqlbase.DescriptorMutation_DELETE_ONLY:
// The state change has already moved forward.
case sqlbase.DescriptorMutation_WRITE_ONLY:
desc.Mutations[i].State = sqlbase.DescriptorMutation_DELETE_ONLY
modified = true
}
}
}
if !modified {
// Return error so that Publish() doesn't increment the version.
return errDidntUpdateDescriptor
}
return nil
}, nil); err != nil {
return err
}
// wait for the state change to propagate to all leases.
return sc.waitToUpdateLeases(sc.tableID)
}
// Wait until the entire cluster has been updated to the latest version
// of the table descriptor.
func (sc *SchemaChanger) waitToUpdateLeases(tableID sqlbase.ID) error {
// Aggressively retry because there might be a user waiting for the
// schema change to complete.
retryOpts := retry.Options{
InitialBackoff: 20 * time.Millisecond,
MaxBackoff: 200 * time.Millisecond,
Multiplier: 2,
}
if log.V(2) {
log.Infof(context.TODO(), "waiting for a single version of table %d...", tableID)
}
_, err := sc.leaseMgr.waitForOneVersion(tableID, retryOpts)
if log.V(2) {
log.Infof(context.TODO(), "waiting for a single version of table %d... done", tableID)
}
return err
}
// done finalizes the mutations (adds new cols/indexes to the table).
// It ensures that all nodes are on the current (pre-update) version of the
// schema.
// Returns the updated of the descriptor.
func (sc *SchemaChanger) done() (*sqlbase.Descriptor, error) {
return sc.leaseMgr.Publish(sc.tableID, func(desc *sqlbase.TableDescriptor) error {
i := 0
for _, mutation := range desc.Mutations {
if mutation.MutationID != sc.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
}
desc.MakeMutationComplete(mutation)
i++
}
if i == 0 {
// The table descriptor is unchanged. Don't let Publish() increment
// the version.
return errDidntUpdateDescriptor
}
// Trim the executed mutations from the descriptor.
desc.Mutations = desc.Mutations[i:]
return nil
}, func(txn *client.Txn) error {
// Log "Finish Schema Change" event. Only the table ID and mutation ID
// are logged; this can be correlated with the DDL statement that
// initiated the change using the mutation id.
return MakeEventLogger(sc.leaseMgr).InsertEventRecord(txn,
EventLogFinishSchemaChange,
int32(sc.tableID),
int32(sc.evalCtx.NodeID),
struct {
MutationID uint32
}{uint32(sc.mutationID)},
)
})
}
// runStateMachineAndBackfill runs the schema change state machine followed by
// the backfill.
func (sc *SchemaChanger) runStateMachineAndBackfill(
lease *sqlbase.TableDescriptor_SchemaChangeLease, startBackfillNotification func() error,
) error {
// Run through mutation state machine before backfill.
if err := sc.RunStateMachineBeforeBackfill(); err != nil {
return err
}
if startBackfillNotification != nil {
if err := startBackfillNotification(); err != nil {
return err
}
}
// Run backfill.
if err := sc.runBackfill(lease); err != nil {
return err
}
// Mark the mutations as completed.
_, err := sc.done()
return err
}
// reverseMutations reverses the direction of all the mutations with the
// mutationID. This is called after hitting an irrecoverable error while
// applying a schema change. If a column being added is reversed and dropped,
// all new indexes referencing the column will also be dropped.
func (sc *SchemaChanger) reverseMutations(causingError error) error {
// Reverse the flow of the state machine.
_, err := sc.leaseMgr.Publish(sc.tableID, func(desc *sqlbase.TableDescriptor) error {
// Keep track of the column mutations being reversed so that indexes
// referencing them can be dropped.
columns := make(map[string]struct{})
for i, mutation := range desc.Mutations {
if mutation.MutationID != sc.mutationID {
// Only reverse the first set of mutations if they have the
// mutation ID we're looking for.
break
}
log.Warningf(context.TODO(), "reverse schema change mutation: %+v", mutation)
switch mutation.Direction {
case sqlbase.DescriptorMutation_ADD:
desc.Mutations[i].Direction = sqlbase.DescriptorMutation_DROP
// A column ADD being reversed gets placed in the map.
if col := mutation.GetColumn(); col != nil {
columns[col.Name] = struct{}{}
}
case sqlbase.DescriptorMutation_DROP:
desc.Mutations[i].Direction = sqlbase.DescriptorMutation_ADD
}
}
// Delete index mutations that reference any of the reversed columns.
if len(columns) > 0 {
sc.deleteIndexMutationsWithReversedColumns(desc, columns)
}
// Publish() will increment the version.
return nil
}, func(txn *client.Txn) error {
// Log "Reverse Schema Change" event. Only the causing error and the
// mutation ID are logged; this can be correlated with the DDL statement
// that initiated the change using the mutation id.
return MakeEventLogger(sc.leaseMgr).InsertEventRecord(txn,
EventLogReverseSchemaChange,
int32(sc.tableID),
int32(sc.evalCtx.NodeID),
struct {
Error string
MutationID uint32
}{fmt.Sprintf("%+v", causingError), uint32(sc.mutationID)},
)
})
return err
}
// deleteIndexMutationsWithReversedColumns deletes index mutations with a
// different mutationID than the schema changer and a reference to one of the
// reversed columns.
func (sc *SchemaChanger) deleteIndexMutationsWithReversedColumns(
desc *sqlbase.TableDescriptor, columns map[string]struct{},
) {
newMutations := make([]sqlbase.DescriptorMutation, 0, len(desc.Mutations))
for _, mutation := range desc.Mutations {
if mutation.MutationID != sc.mutationID {
if idx := mutation.GetIndex(); idx != nil {
deleteMutation := false
for _, name := range idx.ColumnNames {
if _, ok := columns[name]; ok {
// Such an index mutation has to be with direction ADD and
// in the DELETE_ONLY state. Live indexes referencing live
// columns cannot be deleted and thus never have direction
// DROP. All mutations with the ADD direction start off in
// the DELETE_ONLY state.
if mutation.Direction != sqlbase.DescriptorMutation_ADD ||
mutation.State != sqlbase.DescriptorMutation_DELETE_ONLY {
panic(fmt.Sprintf("mutation in bad state: %+v", mutation))
}
log.Warningf(context.TODO(), "delete schema change mutation: %+v", mutation)
deleteMutation = true
break
}
}
if deleteMutation {
continue
}
}
}
newMutations = append(newMutations, mutation)
}
// Reset mutations.
desc.Mutations = newMutations
}
// IsDone returns true if the work scheduled for the schema changer
// is complete.
func (sc *SchemaChanger) IsDone() (bool, error) {
var done bool
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
done = true
tableDesc, err := sqlbase.GetTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
if sc.mutationID == sqlbase.InvalidMutationID {
if tableDesc.UpVersion {
done = false
}
} else {
for _, mutation := range tableDesc.Mutations {
if mutation.MutationID == sc.mutationID {
done = false
break
}
}
}
return nil
})
return done, err
}
// SchemaChangeManagerTestingKnobs for the SchemaChangeManager.
type SchemaChangeManagerTestingKnobs struct {
// AsyncSchemaChangersExecNotification is a function called before running
// a schema change asynchronously. Returning an error will prevent the
// asynchronous execution path from running.
AsyncSchemaChangerExecNotification func() error
// AsyncSchemaChangerExecQuickly executes queued schema changes as soon as
// possible.
AsyncSchemaChangerExecQuickly bool
}
// ModuleTestingKnobs is part of the base.ModuleTestingKnobs interface.
func (*SchemaChangeManagerTestingKnobs) ModuleTestingKnobs() {}
// SchemaChangeManager processes pending schema changes seen in gossip
// updates. Most schema changes are executed synchronously by the node
// that created the schema change. If the node dies while
// processing the schema change this manager acts as a backup
// execution mechanism.
type SchemaChangeManager struct {
db client.DB
gossip *gossip.Gossip
leaseMgr *LeaseManager
testingKnobs *SchemaChangeManagerTestingKnobs
// Create a schema changer for every outstanding schema change seen.
schemaChangers map[sqlbase.ID]SchemaChanger
}
// NewSchemaChangeManager returns a new SchemaChangeManager.
func NewSchemaChangeManager(
testingKnobs *SchemaChangeManagerTestingKnobs,
db client.DB,
gossip *gossip.Gossip,
leaseMgr *LeaseManager,
) *SchemaChangeManager {
return &SchemaChangeManager{
db: db,
gossip: gossip,
leaseMgr: leaseMgr,
testingKnobs: testingKnobs,
schemaChangers: make(map[sqlbase.ID]SchemaChanger),
}
}
// Creates a timer that is used by the manager to decide on
// when to run the next schema changer.
func (s *SchemaChangeManager) newTimer() *time.Timer {
waitDuration := time.Duration(math.MaxInt64)
now := timeutil.Now()
for _, sc := range s.schemaChangers {
d := sc.execAfter.Sub(now)
if d < waitDuration {
waitDuration = d
}
}
// Create a timer if there is an existing schema changer.
if len(s.schemaChangers) > 0 {
return time.NewTimer(waitDuration)
}
return &time.Timer{}
}
// Start starts a goroutine that runs outstanding schema changes
// for tables received in the latest system configuration via gossip.
func (s *SchemaChangeManager) Start(stopper *stop.Stopper) {
stopper.RunWorker(func() {
descKeyPrefix := keys.MakeTablePrefix(uint32(sqlbase.DescriptorTable.ID))
gossipUpdateC := s.gossip.RegisterSystemConfigChannel()
timer := &time.Timer{}
delay := 360 * time.Second
if s.testingKnobs.AsyncSchemaChangerExecQuickly {
delay = 20 * time.Millisecond
}
for {
select {
case <-gossipUpdateC:
cfg, _ := s.gossip.GetSystemConfig()
// Read all tables and their versions
if log.V(2) {
log.Info(context.TODO(), "received a new config")
}
schemaChanger := SchemaChanger{
nodeID: roachpb.NodeID(s.leaseMgr.nodeID),
db: s.db,
leaseMgr: s.leaseMgr,
}
// Keep track of existing schema changers.
oldSchemaChangers := make(map[sqlbase.ID]struct{}, len(s.schemaChangers))
for k := range s.schemaChangers {
oldSchemaChangers[k] = struct{}{}
}
execAfter := timeutil.Now().Add(delay)
// Loop through the configuration to find all the tables.
for _, kv := range cfg.Values {
if !bytes.HasPrefix(kv.Key, descKeyPrefix) {
continue
}
// Attempt to unmarshal config into a table/database descriptor.
var descriptor sqlbase.Descriptor
if err := kv.Value.GetProto(&descriptor); err != nil {
log.Warningf(context.TODO(), "%s: unable to unmarshal descriptor %v", kv.Key, kv.Value)
continue
}
switch union := descriptor.Union.(type) {
case *sqlbase.Descriptor_Table:
table := union.Table
table.MaybeUpgradeFormatVersion()
if err := table.ValidateTable(); err != nil {
log.Errorf(context.TODO(), "%s: received invalid table descriptor: %v", kv.Key, table)
continue
}
// Keep track of outstanding schema changes.
// If all schema change commands always set UpVersion, why
// check for the presence of mutations?
// A schema change execution might fail soon after
// unsetting UpVersion, and we still want to process
// outstanding mutations. Similar with a table marked for deletion.
if table.UpVersion || table.Deleted() || table.Adding() ||
table.Renamed() || len(table.Mutations) > 0 {
if log.V(2) {
log.Infof(context.TODO(), "%s: queue up pending schema change; table: %d, version: %d",
kv.Key, table.ID, table.Version)
}
// Only track the first schema change. We depend on
// gossip to renotify us when a schema change has been
// completed.
schemaChanger.tableID = table.ID
if len(table.Mutations) == 0 {
schemaChanger.mutationID = sqlbase.InvalidMutationID
} else {
schemaChanger.mutationID = table.Mutations[0].MutationID
}
schemaChanger.execAfter = execAfter
// Keep track of this schema change.
// Remove from oldSchemaChangers map.
delete(oldSchemaChangers, table.ID)
if sc, ok := s.schemaChangers[table.ID]; ok {
if sc.mutationID == schemaChanger.mutationID {
// Ignore duplicate.
continue
}
}
s.schemaChangers[table.ID] = schemaChanger
}
case *sqlbase.Descriptor_Database:
// Ignore.
}
}
// Delete old schema changers.
for k := range oldSchemaChangers {
delete(s.schemaChangers, k)
}
timer = s.newTimer()
case <-timer.C:
if s.testingKnobs.AsyncSchemaChangerExecNotification != nil &&
s.testingKnobs.AsyncSchemaChangerExecNotification() != nil {
timer = s.newTimer()
continue
}
for tableID, sc := range s.schemaChangers {
if timeutil.Since(sc.execAfter) > 0 {
err := sc.exec(nil, nil)
if err != nil {
if err == errExistingSchemaChangeLease {
} else if err == sqlbase.ErrDescriptorNotFound {
// Someone deleted this table. Don't try to run the schema
// changer again. Note that there's no gossip update for the
// deletion which would remove this schemaChanger.
delete(s.schemaChangers, tableID)
} else {
// We don't need to act on integrity
// constraints violations because exec()
// purges mutations that violate integrity
// constraints.
log.Warningf(context.TODO(), "Error executing schema change: %s", err)
}
}
// Advance the execAfter time so that this schema
// changer doesn't get called again for a while.
sc.execAfter = timeutil.Now().Add(delay)
}
// Only attempt to run one schema changer.
break
}
timer = s.newTimer()
case <-stopper.ShouldStop():
return
}
}
})
}