-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_changer.go
1153 lines (1057 loc) · 38.6 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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 (
"bytes"
"fmt"
"math"
"time"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"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/retry"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
)
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
// The SchemaChangeManager can attempt to execute this schema
// changer after this time.
execAfter time.Time
readAsOf hlc.Timestamp
testingKnobs *SchemaChangerTestingKnobs
distSQLPlanner *distSQLPlanner
jobRegistry *jobs.Registry
job *jobs.Job
}
// NewSchemaChangerForTesting only for tests.
func NewSchemaChangerForTesting(
tableID sqlbase.ID,
mutationID sqlbase.MutationID,
nodeID roachpb.NodeID,
db client.DB,
leaseMgr *LeaseManager,
jobRegistry *jobs.Registry,
) SchemaChanger {
return SchemaChanger{
tableID: tableID,
mutationID: mutationID,
nodeID: nodeID,
db: db,
leaseMgr: leaseMgr,
jobRegistry: jobRegistry,
}
}
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")
var errSchemaChangeNotFirstInLine = errors.New(
"schema change not first in line")
func shouldLogSchemaChangeError(err error) bool {
return err != errExistingSchemaChangeLease && err != errSchemaChangeNotFirstInLine
}
// AcquireLease acquires a schema change lease on the table if
// an unexpired lease doesn't exist. It returns the lease.
func (sc *SchemaChanger) AcquireLease(
ctx context.Context,
) (sqlbase.TableDescriptor_SchemaChangeLease, error) {
var lease sqlbase.TableDescriptor_SchemaChangeLease
err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
tableDesc, err := sqlbase.GetTableDescFromID(ctx, 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 timeutil.Unix(0, tableDesc.Lease.ExpirationTime).Add(expirationTimeUncertainty).After(timeutil.Now()) {
return errExistingSchemaChangeLease
}
log.Infof(ctx, "Overriding existing expired lease %v", tableDesc.Lease)
}
lease = sc.createSchemaChangeLease()
tableDesc.Lease = &lease
return txn.Put(ctx, sqlbase.MakeDescMetadataKey(tableDesc.ID), sqlbase.WrapDescriptor(tableDesc))
})
return lease, err
}
func (sc *SchemaChanger) findTableWithLease(
ctx context.Context, txn *client.Txn, lease sqlbase.TableDescriptor_SchemaChangeLease,
) (*sqlbase.TableDescriptor, error) {
tableDesc, err := sqlbase.GetTableDescFromID(ctx, 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 releases the table lease if it is the one registered with
// the table descriptor.
func (sc *SchemaChanger) ReleaseLease(
ctx context.Context, lease sqlbase.TableDescriptor_SchemaChangeLease,
) error {
return sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
tableDesc, err := sc.findTableWithLease(ctx, txn, lease)
if err != nil {
return err
}
tableDesc.Lease = nil
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
return txn.Put(ctx, sqlbase.MakeDescMetadataKey(tableDesc.ID), sqlbase.WrapDescriptor(tableDesc))
})
}
// 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). It updates existingLease
// with the new lease.
func (sc *SchemaChanger) ExtendLease(
ctx context.Context, existingLease *sqlbase.TableDescriptor_SchemaChangeLease,
) error {
// Check if there is still time on this lease.
minDesiredExpiration := timeutil.Now().Add(MinSchemaChangeLeaseDuration)
if timeutil.Unix(0, existingLease.ExpirationTime).After(minDesiredExpiration) {
return nil
}
// Update lease.
var lease sqlbase.TableDescriptor_SchemaChangeLease
if err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
tableDesc, err := sc.findTableWithLease(ctx, txn, *existingLease)
if err != nil {
return err
}
lease = sc.createSchemaChangeLease()
tableDesc.Lease = &lease
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
return txn.Put(ctx, sqlbase.MakeDescMetadataKey(tableDesc.ID), sqlbase.WrapDescriptor(tableDesc))
}); err != nil {
return err
}
*existingLease = lease
return nil
}
// DropTableName removes a mapping from name to ID from the KV database.
func DropTableName(
ctx context.Context, tableDesc *sqlbase.TableDescriptor, db *client.DB, traceKV bool,
) error {
_, nameKey, _ := GetKeysForTableDescriptor(tableDesc)
// The table name is no longer in use across the entire cluster.
// Delete the namekey so that it can be used by another table.
// We do this before truncating the table because the table truncation
// takes too much time.
return db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
b := &client.Batch{}
// Use CPut because we want to remove a specific name -> id map.
if traceKV {
log.VEventf(ctx, 2, "CPut %s -> nil", nameKey)
}
b.CPut(nameKey, nil, tableDesc.ID)
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
err := txn.Run(ctx, b)
if _, ok := err.(*roachpb.ConditionFailedError); ok {
return nil
}
return err
})
}
// DropTableDesc removes a descriptor from the KV database.
func DropTableDesc(
ctx context.Context, tableDesc *sqlbase.TableDescriptor, db *client.DB, traceKV bool,
) error {
descKey := sqlbase.MakeDescMetadataKey(tableDesc.ID)
zoneKeyPrefix := sqlbase.MakeZoneKeyPrefix(tableDesc.ID)
// Finished deleting all the table data, now delete the table meta data.
return db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
// Delete table descriptor
b := &client.Batch{}
if traceKV {
log.VEventf(ctx, 2, "Del %s", descKey)
log.VEventf(ctx, 2, "DelRange %s", zoneKeyPrefix)
}
// Delete the descriptor.
b.Del(descKey)
// Delete the zone config entry for this table.
b.DelRange(zoneKeyPrefix, zoneKeyPrefix.PrefixEnd(), false /* returnKeys */)
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
return txn.Run(ctx, b)
})
}
// maybe Add/Drop/Rename a table depending on the state of a table descriptor.
// This method returns true if the table is deleted.
func (sc *SchemaChanger) maybeAddDropRename(
ctx context.Context,
inSession bool,
lease *sqlbase.TableDescriptor_SchemaChangeLease,
table *sqlbase.TableDescriptor,
) (bool, error) {
if table.Dropped() {
if err := sc.ExtendLease(ctx, lease); err != nil {
return false, 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(ctx, sc.tableID); err != nil {
return false, err
}
if err := DropTableName(ctx, table /* false */, &sc.db, false /* traceKV */); err != nil {
return false, err
}
if inSession {
return false, nil
}
// Do all the hard work of deleting the table data and the table ID.
if err := truncateTableInChunks(ctx, table, &sc.db, false /* traceKV */); err != nil {
return false, err
}
return true, DropTableDesc(ctx, table, &sc.db, false /* traceKV */)
}
if table.Adding() {
for _, idx := range table.AllNonDropIndexes() {
if idx.ForeignKey.IsSet() {
if err := sc.waitToUpdateLeases(ctx, idx.ForeignKey.Table); err != nil {
return false, err
}
}
}
if _, err := sc.leaseMgr.Publish(
ctx,
table.ID,
func(tbl *sqlbase.TableDescriptor) error {
tbl.State = sqlbase.TableDescriptor_PUBLIC
return nil
},
func(txn *client.Txn) error { return nil },
); err != nil {
return false, err
}
}
if table.Renamed() {
if err := sc.ExtendLease(ctx, lease); err != nil {
return false, 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(ctx, sc.tableID); err != nil {
return false, err
}
if sc.testingKnobs.RenameOldNameNotInUseNotification != nil {
sc.testingKnobs.RenameOldNameNotInUseNotification()
}
// Free up the old name(s).
if err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
b := txn.NewBatch()
for _, renameDetails := range table.Renames {
tbKey := tableKey{renameDetails.OldParentID, renameDetails.OldName}.Key()
b.Del(tbKey)
}
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
return txn.Run(ctx, b)
}); err != nil {
return false, err
}
// Clean up - clear the descriptor's state.
if _, err := sc.leaseMgr.Publish(ctx, sc.tableID, func(desc *sqlbase.TableDescriptor) error {
desc.Renames = nil
return nil
}, nil); err != nil {
return false, err
}
}
return false, nil
}
// Execute the entire schema change in steps.
// inSession is set to false when this is called from the asynchronous
// schema change execution path.
//
// If the txn that queued the schema changer did not commit, this will be a
// no-op, as we'll fail to find the job for our mutation in the jobs registry.
func (sc *SchemaChanger) exec(
ctx context.Context, inSession bool, evalCtx parser.EvalContext,
) error {
// Acquire lease.
lease, err := sc.AcquireLease(ctx)
if err != nil {
return err
}
needRelease := true
// Always try to release lease.
defer func() {
// If the schema changer deleted the descriptor, there's no longer a lease to be
// released.
if !needRelease {
return
}
if err := sc.ReleaseLease(ctx, lease); err != nil {
log.Warning(ctx, err)
}
}()
notFirst, err := sc.notFirstInLine(ctx)
if err != nil {
return err
}
if notFirst {
return errSchemaChangeNotFirstInLine
}
// Increment the version and unset tableDescriptor.UpVersion.
desc, err := sc.MaybeIncrementVersion(ctx)
if err != nil {
return err
}
tableDesc := desc.GetTable()
if drop, err := sc.maybeAddDropRename(ctx, inSession, &lease, tableDesc); err != nil {
return err
} else if drop {
needRelease = false
return nil
}
// 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(ctx, sc.tableID); err != nil {
log.Warning(ctx, err)
}
}()
if sc.mutationID == sqlbase.InvalidMutationID {
// Nothing more to do.
return nil
}
// Find our job.
foundJobID := false
for _, g := range tableDesc.MutationJobs {
if g.MutationID == sc.mutationID {
job, err := sc.jobRegistry.LoadJob(ctx, g.JobID)
if err != nil {
return err
}
sc.job = job
foundJobID = true
break
}
}
if !foundJobID {
// No job means we've already run and completed this schema change
// successfully, so we can just exit.
return nil
}
if err := sc.job.Started(ctx); err != nil {
if log.V(2) {
log.Infof(ctx, "Failed to mark job %d as started: %v", *sc.job.ID(), err)
}
}
// 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(ctx, &lease, evalCtx)
// Purge the mutations if the application of the mutations failed due to
// a permanent error. All other errors are transient errors that are
// resolved by retrying the backfill.
if sqlbase.IsPermanentSchemaChangeError(err) {
if err := sc.rollbackSchemaChange(ctx, err, &lease, evalCtx); err != nil {
return err
}
}
return err
}
func (sc *SchemaChanger) rollbackSchemaChange(
ctx context.Context,
err error,
lease *sqlbase.TableDescriptor_SchemaChangeLease,
evalCtx parser.EvalContext,
) error {
log.Warningf(ctx, "reversing schema change %d due to irrecoverable error: %s", *sc.job.ID(), err)
sc.job.Failed(ctx, err)
if errReverse := sc.reverseMutations(ctx, 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(ctx, lease, evalCtx); 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(ctx, "error purging mutation: %s, after error: %s", errPurge, err)
}
return nil
}
// 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(ctx context.Context) (*sqlbase.Descriptor, error) {
return sc.leaseMgr.Publish(ctx, 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(ctx context.Context) error {
if _, err := sc.leaseMgr.Publish(ctx, 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
// DELETE_AND_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_DELETE_AND_WRITE_ONLY
modified = true
case sqlbase.DescriptorMutation_DELETE_AND_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_DELETE_AND_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(ctx, sc.tableID)
}
// Wait until the entire cluster has been updated to the latest version
// of the table descriptor.
func (sc *SchemaChanger) waitToUpdateLeases(ctx context.Context, 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(ctx, "waiting for a single version of table %d...", tableID)
}
_, err := sc.leaseMgr.WaitForOneVersion(ctx, tableID, retryOpts)
if log.V(2) {
log.Infof(ctx, "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(ctx context.Context) (*sqlbase.Descriptor, error) {
return sc.leaseMgr.Publish(ctx, 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:]
for i, g := range desc.MutationJobs {
if g.MutationID == sc.mutationID {
// Trim the executed mutation group from the descriptor.
desc.MutationJobs = append(desc.MutationJobs[:i], desc.MutationJobs[i+1:]...)
break
}
}
return nil
}, func(txn *client.Txn) error {
if err := sc.job.WithTxn(txn).Succeeded(ctx); err != nil {
log.Warningf(ctx, "schema change ignoring error while marking job %d as successful: %+v",
sc.job.ID(), err)
}
// 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(
ctx,
txn,
EventLogFinishSchemaChange,
int32(sc.tableID),
int32(sc.nodeID),
struct {
MutationID uint32
}{uint32(sc.mutationID)},
)
})
}
// notFirstInLine returns true whenever the schema change has been queued
// up for execution after another schema change.
func (sc *SchemaChanger) notFirstInLine(ctx context.Context) (bool, error) {
var notFirst bool
err := sc.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
notFirst = false
desc, err := sqlbase.GetTableDescFromID(ctx, txn, sc.tableID)
if err != nil {
return err
}
for i, mutation := range desc.Mutations {
if mutation.MutationID == sc.mutationID {
notFirst = i != 0
break
}
}
return nil
})
return notFirst, err
}
// runStateMachineAndBackfill runs the schema change state machine followed by
// the backfill.
func (sc *SchemaChanger) runStateMachineAndBackfill(
ctx context.Context, lease *sqlbase.TableDescriptor_SchemaChangeLease, evalCtx parser.EvalContext,
) error {
if fn := sc.testingKnobs.RunBeforePublishWriteAndDelete; fn != nil {
fn()
}
// Run through mutation state machine before backfill.
if err := sc.RunStateMachineBeforeBackfill(ctx); err != nil {
return err
}
if err := sc.job.Progressed(ctx, .1, jobs.Noop); err != nil {
log.Warningf(ctx, "failed to log progress on job %v after completing state machine: %v",
sc.job.ID(), err)
}
// Run backfill(s).
if err := sc.runBackfill(ctx, lease, evalCtx); err != nil {
return err
}
// Mark the mutations as completed.
_, err := sc.done(ctx)
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(ctx context.Context, causingError error) error {
// Reverse the flow of the state machine.
_, err := sc.leaseMgr.Publish(ctx, 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
}
jobID, err := sc.getJobIDForMutationWithDescriptor(ctx, desc, mutation.MutationID)
if err != nil {
return err
}
job, err := sc.jobRegistry.LoadJob(ctx, jobID)
if err != nil {
return err
}
details, ok := job.Record.Details.(jobs.SchemaChangeDetails)
if !ok {
return errors.Errorf("expected SchemaChangeDetails job type, got %T", sc.job.Record.Details)
}
details.ResumeSpanList[i].ResumeSpans = nil
err = job.SetDetails(ctx, details)
if err != nil {
return err
}
log.Warningf(ctx, "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
}
}
for i := range desc.MutationJobs {
if desc.MutationJobs[i].MutationID == sc.mutationID {
// Create a roll back job.
record := sc.job.Record
record.Description = "ROLL BACK " + record.Description
job := sc.jobRegistry.NewJob(record)
if err := job.Created(ctx, jobs.WithoutCancel); err != nil {
return err
}
if err := job.Started(ctx); err != nil {
return err
}
desc.MutationJobs[i].JobID = *job.ID()
sc.job = job
break
}
}
// Delete index mutations that reference any of the reversed columns.
if len(columns) > 0 {
sc.deleteIndexMutationsWithReversedColumns(ctx, 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(
ctx,
txn,
EventLogReverseSchemaChange,
int32(sc.tableID),
int32(sc.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(
ctx context.Context, 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(ctx, "delete schema change mutation: %+v", mutation)
deleteMutation = true
break
}
}
if deleteMutation {
continue
}
}
}
newMutations = append(newMutations, mutation)
}
// Reset mutations.
desc.Mutations = newMutations
}
// TestingSchemaChangerCollection is an exported (for testing) version of
// schemaChangerCollection.
// TODO(andrei): get rid of this type once we can have tests internal to the sql
// package (as of April 2016 we can't because sql can't import server).
type TestingSchemaChangerCollection struct {
scc *schemaChangerCollection
}
// ClearSchemaChangers clears the schema changers from the collection.
// If this is called from a SyncSchemaChangersFilter, no schema changer will be
// run.
func (tscc TestingSchemaChangerCollection) ClearSchemaChangers() {
tscc.scc.schemaChangers = tscc.scc.schemaChangers[:0]
}
// SyncSchemaChangersFilter is the type of a hook to be installed through the
// ExecutorContext for blocking or otherwise manipulating schema changers run
// through the sync schema changers path.
type SyncSchemaChangersFilter func(TestingSchemaChangerCollection)
// SchemaChangerTestingKnobs for testing the schema change execution path
// through both the synchronous and asynchronous paths.
type SchemaChangerTestingKnobs struct {
// SyncFilter is called before running schema changers synchronously (at
// the end of a txn). The function can be used to clear the schema
// changers (if the test doesn't want them run using the synchronous path)
// or to temporarily block execution. Note that this has nothing to do
// with the async path for running schema changers. To block that, set
// AsyncExecNotification.
SyncFilter SyncSchemaChangersFilter
// RunBeforePublishWriteAndDelete is called just before publishing the
// write+delete state for the schema change.
RunBeforePublishWriteAndDelete func()
// RunBeforeBackfill is called just before starting the backfill.
RunBeforeBackfill func() error
// RunBeforeBackfill is called just before starting the index backfill, after
// fixing the index backfill scan timestamp.
RunBeforeIndexBackfill func()
// RunBeforeBackfillChunk is called before executing each chunk of a
// backfill during a schema change operation. It is called with the
// current span and returns an error which eventually is returned to the
// caller of SchemaChanger.exec(). It is called at the start of the
// backfill function passed into the transaction executing the chunk.
RunBeforeBackfillChunk func(sp roachpb.Span) error
// RunAfterBackfillChunk is called after executing each chunk of a
// backfill during a schema change operation. It is called just before
// returning from the backfill function passed into the transaction
// executing the chunk. It is always called even when the backfill
// function returns an error, or if the table has already been dropped.
RunAfterBackfillChunk func()
// RenameOldNameNotInUseNotification is called during a rename schema
// change, after all leases on the version of the descriptor with the old
// name are gone, and just before the mapping of the old name to the
// descriptor id is about to be deleted.
RenameOldNameNotInUseNotification func()
// AsyncExecNotification is a function called before running a schema
// change asynchronously. Returning an error will prevent the asynchronous
// execution path from running.
AsyncExecNotification func() error
// AsyncExecQuickly executes queued schema changes as soon as possible.
AsyncExecQuickly bool
// WriteCheckpointInterval is the interval after which a checkpoint is
// written.
WriteCheckpointInterval time.Duration
// BackfillChunkSize is to be used for all backfill chunked operations.
BackfillChunkSize int64
}
// ModuleTestingKnobs is part of the base.ModuleTestingKnobs interface.
func (*SchemaChangerTestingKnobs) 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 {
ambientCtx log.AmbientContext
db client.DB
gossip *gossip.Gossip
leaseMgr *LeaseManager
testingKnobs *SchemaChangerTestingKnobs
// Create a schema changer for every outstanding schema change seen.
schemaChangers map[sqlbase.ID]SchemaChanger
distSQLPlanner *distSQLPlanner
clock *hlc.Clock
jobRegistry *jobs.Registry
}
// NewSchemaChangeManager returns a new SchemaChangeManager.
func NewSchemaChangeManager(
st *cluster.Settings,
ambientCtx log.AmbientContext,
testingKnobs *SchemaChangerTestingKnobs,
db client.DB,
nodeDesc roachpb.NodeDescriptor,
rpcContext *rpc.Context,
distSQLServ *distsqlrun.ServerImpl,
distSender *kv.DistSender,
gossip *gossip.Gossip,
leaseMgr *LeaseManager,
clock *hlc.Clock,
jobRegistry *jobs.Registry,
) *SchemaChangeManager {
return &SchemaChangeManager{
ambientCtx: ambientCtx,
db: db,
gossip: gossip,
leaseMgr: leaseMgr,
testingKnobs: testingKnobs,
schemaChangers: make(map[sqlbase.ID]SchemaChanger),
// TODO(radu): investigate using the same distSQLPlanner from the executor.
distSQLPlanner: newDistSQLPlanner(
distsqlrun.Version,
st,
nodeDesc,
rpcContext,
distSQLServ,
distSender,
gossip,
leaseMgr.stopper,
// TODO(radu): pass these knobs
DistSQLPlannerTestingKnobs{},
),
jobRegistry: jobRegistry,
clock: clock,
}
}
// 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(s.ambientCtx.AnnotateCtx(context.Background()), func(ctx context.Context) {
descKeyPrefix := keys.MakeTablePrefix(uint32(sqlbase.DescriptorTable.ID))
gossipUpdateC := s.gossip.RegisterSystemConfigChannel()
timer := &time.Timer{}
// TODO(tschottdorf): it is not clear that inserting this delay is
// useful.
//
// Vivek wrote: I believe the only reason to pace these is to allow
// different schema changes to execute on different nodes and distribute
// the load. But that was a thought a long time ago. You can safely move
// the break inside the if statement.
delay := 360 * time.Second
if s.testingKnobs.AsyncExecQuickly {
delay = 20 * time.Millisecond
}
for {
select {
case <-gossipUpdateC:
cfg, _ := s.gossip.GetSystemConfig()
// Read all tables and their versions
if log.V(2) {
log.Info(ctx, "received a new config")
}
schemaChanger := SchemaChanger{
nodeID: s.leaseMgr.nodeID.Get(),
db: s.db,
leaseMgr: s.leaseMgr,