-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
exec_mutation.go
644 lines (601 loc) · 19.7 KB
/
exec_mutation.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
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package scexec
import (
"context"
"sort"
"strings"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec/scmutationexec"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/cockroach/pkg/util/log/logpb"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)
// executeDescriptorMutationOps will visit each operation, accumulating
// side effects into a mutationVisitorState object, and then writing out
// those side effects using the provided deps.
func executeDescriptorMutationOps(ctx context.Context, deps Dependencies, ops []scop.Op) error {
mvs := newMutationVisitorState(deps.Catalog())
v := scmutationexec.NewMutationVisitor(mvs, deps.Catalog(), deps.Clock())
for _, op := range ops {
if err := op.(scop.MutationOp).Visit(ctx, v); err != nil {
return errors.Wrapf(err, "%T: %v", op, op)
}
}
// Note that we perform the catalog writes first in order to acquire locks
// on the descriptors in question as early as possible. If a restart is
// encountered, these locks will be retained in subsequent epochs (assuming
// that the transaction is not aborted due to, say, a deadlock). If we were
// to lock the eventlog or jobs tables first, they would not provide any
// liveness benefit because their entries are non-deterministic. The jobs
// writes are particularly bad because that table is constantly being
// scanned.
dbZoneConfigsToDelete, gcJobRecords := mvs.gcJobs.makeRecords(
deps.TransactionalJobRegistry().MakeJobID,
!deps.TransactionalJobRegistry().UseLegacyGCJob(ctx),
)
if err := performBatchedCatalogWrites(
ctx,
mvs.descriptorsToDelete,
dbZoneConfigsToDelete,
mvs.modifiedDescriptors,
mvs.drainedNames,
deps.Catalog(),
); err != nil {
return err
}
if err := logEvents(ctx, mvs, deps.EventLogger()); err != nil {
return err
}
if err := updateDescriptorMetadata(
ctx, mvs, deps.DescriptorMetadataUpdater(ctx),
); err != nil {
return err
}
if err := refreshStatsForDescriptors(
ctx,
mvs,
deps.StatsRefresher()); err != nil {
return err
}
return manageJobs(
ctx,
gcJobRecords,
mvs.schemaChangerJob,
mvs.schemaChangerJobUpdates,
deps.TransactionalJobRegistry(),
)
}
func performBatchedCatalogWrites(
ctx context.Context,
descriptorsToDelete catalog.DescriptorIDSet,
dbZoneConfigsToDelete catalog.DescriptorIDSet,
modifiedDescriptors nstree.Map,
drainedNames map[descpb.ID][]descpb.NameInfo,
cat Catalog,
) error {
b := cat.NewCatalogChangeBatcher()
descriptorsToDelete.ForEach(func(id descpb.ID) {
modifiedDescriptors.Remove(id)
})
err := modifiedDescriptors.IterateByID(func(entry catalog.NameEntry) error {
return b.CreateOrUpdateDescriptor(ctx, entry.(catalog.MutableDescriptor))
})
if err != nil {
return err
}
for _, id := range descriptorsToDelete.Ordered() {
if err := b.DeleteDescriptor(ctx, id); err != nil {
return err
}
}
for id, drainedNames := range drainedNames {
for _, name := range drainedNames {
if err := b.DeleteName(ctx, name, id); err != nil {
return err
}
}
}
// Any databases being GCed should have an entry even if none of its tables
// are being dropped. This entry will be used to generate the GC jobs below.
{
var err error
dbZoneConfigsToDelete.ForEach(func(id descpb.ID) {
if err == nil {
err = b.DeleteZoneConfig(ctx, id)
}
})
if err != nil {
return err
}
}
return b.ValidateAndRun(ctx)
}
func logEvents(ctx context.Context, mvs *mutationVisitorState, el EventLogger) error {
statementIDs := make([]uint32, 0, len(mvs.eventsByStatement))
for statementID := range mvs.eventsByStatement {
statementIDs = append(statementIDs, statementID)
}
sort.Slice(statementIDs, func(i, j int) bool {
return statementIDs[i] < statementIDs[j]
})
for _, statementID := range statementIDs {
entries := eventLogEntriesForStatement(mvs.eventsByStatement[statementID])
for _, e := range entries {
// TODO(postamar): batch these
switch e.event.(type) {
case eventpb.EventWithCommonSQLPayload:
details := e.details
details.DescriptorID = uint32(e.id)
if err := el.LogEvent(ctx, details, e.event); err != nil {
return err
}
case eventpb.EventWithCommonSchemaChangePayload:
if err := el.LogEventForSchemaChange(ctx, e.event); err != nil {
return err
}
}
}
}
return nil
}
func eventLogEntriesForStatement(statementEvents []eventPayload) (logEntries []eventPayload) {
// A dependent event is one which is generated because of a
// dependency getting modified from the source object. An example
// of this is a DROP TABLE will be the source event, which will track
// any dependent views dropped.
var dependentEvents = make(map[uint32][]eventPayload)
var sourceEvents = make(map[uint32]eventPayload)
// First separate out events, where the first event generated will always
// be the source and everything else before will be dependencies if they have
// the same subtask ID.
for _, event := range statementEvents {
dependentEvents[event.SubWorkID] = append(dependentEvents[event.SubWorkID], event)
}
// Split of the source events.
orderedSubWorkID := make([]uint32, 0, len(dependentEvents))
for subWorkID := range dependentEvents {
elems := dependentEvents[subWorkID]
sort.SliceStable(elems, func(i, j int) bool {
return elems[i].SourceElementID < elems[j].SourceElementID
})
sourceEvents[subWorkID] = elems[0]
dependentEvents[subWorkID] = elems[1:]
orderedSubWorkID = append(orderedSubWorkID, subWorkID)
}
// Store an ordered list of sub-work IDs for deterministic
// event order.
sort.SliceStable(orderedSubWorkID, func(i, j int) bool {
return orderedSubWorkID[i] < orderedSubWorkID[j]
})
// Collect the dependent objects for each
// source event, and generate an event log entry.
for _, subWorkID := range orderedSubWorkID {
// Determine which objects we should collect.
collectDependentViewNames := false
collectDependentTables := false
collectDependentSequences := false
sourceEvent := sourceEvents[subWorkID]
switch sourceEvent.event.(type) {
case *eventpb.DropDatabase:
// Log each of the objects that are dropped.
collectDependentViewNames = true
collectDependentTables = true
collectDependentSequences = true
case *eventpb.DropView, *eventpb.DropTable:
// Drop view and drop tables only cares about
// dependent views
collectDependentViewNames = true
}
var dependentObjects []string
for _, dependentEvent := range dependentEvents[subWorkID] {
switch ev := dependentEvent.event.(type) {
case *eventpb.DropSequence:
if collectDependentSequences {
dependentObjects = append(dependentObjects, ev.SequenceName)
}
case *eventpb.DropTable:
if collectDependentTables {
dependentObjects = append(dependentObjects, ev.TableName)
}
case *eventpb.DropView:
if collectDependentViewNames {
dependentObjects = append(dependentObjects, ev.ViewName)
}
}
}
// Add anything that we determined based
// on the dependencies.
switch ev := sourceEvent.event.(type) {
case *eventpb.DropTable:
ev.CascadeDroppedViews = dependentObjects
case *eventpb.DropView:
ev.CascadeDroppedViews = dependentObjects
case *eventpb.DropDatabase:
ev.DroppedSchemaObjects = dependentObjects
}
// Generate event log entries for the source event only. The dependent
// events will be ignored.
logEntries = append(logEntries, sourceEvent)
}
return logEntries
}
// updateDescriptorMetadata performs the portions of the side effects of the
// operations delegated to the DescriptorMetadataUpdater.
func updateDescriptorMetadata(
ctx context.Context, mvs *mutationVisitorState, m DescriptorMetadataUpdater,
) error {
for _, comment := range mvs.commentsToUpdate {
if len(comment.comment) > 0 {
if err := m.UpsertDescriptorComment(
comment.id, comment.subID, comment.commentType, comment.comment); err != nil {
return err
}
} else {
if err := m.DeleteDescriptorComment(
comment.id, comment.subID, comment.commentType); err != nil {
return err
}
}
}
for _, comment := range mvs.constraintCommentsToUpdate {
if len(comment.comment) > 0 {
if err := m.UpsertConstraintComment(
comment.tblID, comment.constraintID, comment.comment); err != nil {
return err
}
} else {
if err := m.DeleteConstraintComment(
comment.tblID, comment.constraintID); err != nil {
return err
}
}
}
if !mvs.tableCommentsToDelete.Empty() {
if err := m.DeleteAllCommentsForTables(mvs.tableCommentsToDelete); err != nil {
return err
}
}
for _, dbRoleSetting := range mvs.databaseRoleSettingsToDelete {
err := m.DeleteDatabaseRoleSettings(ctx, dbRoleSetting.dbID)
if err != nil {
return err
}
}
for _, scheduleID := range mvs.scheduleIDsToDelete {
if err := m.DeleteSchedule(ctx, scheduleID); err != nil {
return err
}
}
return nil
}
func refreshStatsForDescriptors(
_ context.Context, mvs *mutationVisitorState, statsRefresher StatsRefreshQueue,
) error {
for descriptorID := range mvs.statsToRefresh {
statsRefresher.AddTableForStatsRefresh(descriptorID)
}
return nil
}
func manageJobs(
ctx context.Context,
gcJobs []jobs.Record,
scJob *jobs.Record,
scJobUpdates map[jobspb.JobID]schemaChangerJobUpdate,
jr TransactionalJobRegistry,
) error {
// TODO(ajwerner): Batch job creation. Should be easy, the registry has
// the needed API.
for _, j := range gcJobs {
if err := jr.CreateJob(ctx, j); err != nil {
return err
}
}
if scJob != nil {
if err := jr.CreateJob(ctx, *scJob); err != nil {
return err
}
}
for id, update := range scJobUpdates {
if err := jr.UpdateSchemaChangeJob(ctx, id, func(
md jobs.JobMetadata, updateProgress func(*jobspb.Progress), updatePayload func(*jobspb.Payload),
) error {
s := schemaChangeJobUpdateState{md: md}
defer s.doUpdate(updateProgress, updatePayload)
s.updatedProgress().RunningStatus = update.runningStatus
if !md.Payload.Noncancelable && update.isNonCancelable {
s.updatedPayload().Noncancelable = true
}
oldIDs := catalog.MakeDescriptorIDSet(md.Payload.DescriptorIDs...)
newIDs := oldIDs.Difference(update.descriptorIDsToRemove)
if newIDs.Len() < oldIDs.Len() {
s.updatedPayload().DescriptorIDs = newIDs.Ordered()
}
return nil
}); err != nil {
return err
}
}
return nil
}
// schemaChangeJobUpdateState is a helper struct for managing the state in the
// callback passed to TransactionalJobRegistry.UpdateSchemaChangeJob in
// manageJobs.
type schemaChangeJobUpdateState struct {
md jobs.JobMetadata
maybeUpdatedPayload *jobspb.Payload
maybeUpdatedProgress *jobspb.Progress
}
func (s *schemaChangeJobUpdateState) updatedProgress() *jobspb.Progress {
if s.maybeUpdatedProgress == nil {
clone := *s.md.Progress
s.maybeUpdatedProgress = &clone
}
return s.maybeUpdatedProgress
}
func (s *schemaChangeJobUpdateState) updatedPayload() *jobspb.Payload {
if s.maybeUpdatedPayload == nil {
clone := *s.md.Payload
s.maybeUpdatedPayload = &clone
}
return s.maybeUpdatedPayload
}
func (s *schemaChangeJobUpdateState) doUpdate(
updateProgress func(*jobspb.Progress), updatePayload func(*jobspb.Payload),
) {
if s.maybeUpdatedProgress != nil {
updateProgress(s.maybeUpdatedProgress)
}
if s.maybeUpdatedPayload != nil {
updatePayload(s.maybeUpdatedPayload)
}
}
type mutationVisitorState struct {
c Catalog
modifiedDescriptors nstree.Map
drainedNames map[descpb.ID][]descpb.NameInfo
descriptorsToDelete catalog.DescriptorIDSet
commentsToUpdate []commentToUpdate
tableCommentsToDelete catalog.DescriptorIDSet
constraintCommentsToUpdate []constraintCommentToUpdate
databaseRoleSettingsToDelete []databaseRoleSettingToDelete
schemaChangerJob *jobs.Record
schemaChangerJobUpdates map[jobspb.JobID]schemaChangerJobUpdate
eventsByStatement map[uint32][]eventPayload
scheduleIDsToDelete []int64
statsToRefresh map[descpb.ID]struct{}
gcJobs
}
type constraintCommentToUpdate struct {
tblID catid.DescID
constraintID descpb.ConstraintID
comment string
}
type commentToUpdate struct {
id int64
subID int64
commentType keys.CommentType
comment string
}
type databaseRoleSettingToDelete struct {
dbID catid.DescID
}
type eventPayload struct {
id descpb.ID
scpb.TargetMetadata
details eventpb.CommonSQLEventDetails
event logpb.EventPayload
}
type schemaChangerJobUpdate struct {
isNonCancelable bool
runningStatus string
descriptorIDsToRemove catalog.DescriptorIDSet
}
func (mvs *mutationVisitorState) UpdateSchemaChangerJob(
jobID jobspb.JobID,
isNonCancelable bool,
runningStatus string,
descriptorIDsToRemove catalog.DescriptorIDSet,
) error {
if mvs.schemaChangerJobUpdates == nil {
mvs.schemaChangerJobUpdates = make(map[jobspb.JobID]schemaChangerJobUpdate)
} else if _, exists := mvs.schemaChangerJobUpdates[jobID]; exists {
return errors.AssertionFailedf("cannot update job %d more than once", jobID)
}
mvs.schemaChangerJobUpdates[jobID] = schemaChangerJobUpdate{
isNonCancelable: isNonCancelable,
runningStatus: runningStatus,
descriptorIDsToRemove: descriptorIDsToRemove,
}
return nil
}
func newMutationVisitorState(c Catalog) *mutationVisitorState {
return &mutationVisitorState{
c: c,
drainedNames: make(map[descpb.ID][]descpb.NameInfo),
eventsByStatement: make(map[uint32][]eventPayload),
statsToRefresh: make(map[descpb.ID]struct{}),
}
}
var _ scmutationexec.MutationVisitorStateUpdater = (*mutationVisitorState)(nil)
func (mvs *mutationVisitorState) GetDescriptor(
ctx context.Context, id descpb.ID,
) (catalog.Descriptor, error) {
if entry := mvs.modifiedDescriptors.GetByID(id); entry != nil {
return entry.(catalog.Descriptor), nil
}
descs, err := mvs.c.MustReadImmutableDescriptors(ctx, id)
if err != nil {
return nil, err
}
return descs[0], nil
}
func (mvs *mutationVisitorState) CheckOutDescriptor(
ctx context.Context, id descpb.ID,
) (catalog.MutableDescriptor, error) {
entry := mvs.modifiedDescriptors.GetByID(id)
if entry != nil {
return entry.(catalog.MutableDescriptor), nil
}
mut, err := mvs.c.MustReadMutableDescriptor(ctx, id)
if err != nil {
return nil, err
}
mut.MaybeIncrementVersion()
mvs.modifiedDescriptors.Upsert(mut, mut.SkipNamespace())
return mut, nil
}
func (mvs *mutationVisitorState) DeleteDescriptor(id descpb.ID) {
mvs.descriptorsToDelete.Add(id)
}
func (mvs *mutationVisitorState) DeleteAllTableComments(id descpb.ID) {
mvs.tableCommentsToDelete.Add(id)
}
func (mvs *mutationVisitorState) AddComment(
id descpb.ID, subID int, commentType keys.CommentType, comment string,
) {
mvs.commentsToUpdate = append(mvs.commentsToUpdate,
commentToUpdate{
id: int64(id),
subID: int64(subID),
commentType: commentType,
comment: comment,
})
}
func (mvs *mutationVisitorState) DeleteComment(
id descpb.ID, subID int, commentType keys.CommentType,
) {
mvs.commentsToUpdate = append(mvs.commentsToUpdate,
commentToUpdate{
id: int64(id),
subID: int64(subID),
commentType: commentType,
})
}
func (mvs *mutationVisitorState) DeleteConstraintComment(
ctx context.Context, tblID descpb.ID, constraintID descpb.ConstraintID,
) error {
mvs.constraintCommentsToUpdate = append(mvs.constraintCommentsToUpdate,
constraintCommentToUpdate{
tblID: tblID,
constraintID: constraintID,
})
return nil
}
func (mvs *mutationVisitorState) DeleteDatabaseRoleSettings(
ctx context.Context, dbID descpb.ID,
) error {
mvs.databaseRoleSettingsToDelete = append(mvs.databaseRoleSettingsToDelete,
databaseRoleSettingToDelete{
dbID: dbID,
})
return nil
}
func (mvs *mutationVisitorState) DeleteSchedule(scheduleID int64) {
mvs.scheduleIDsToDelete = append(mvs.scheduleIDsToDelete, scheduleID)
}
func (mvs *mutationVisitorState) RefreshStats(descriptorID descpb.ID) {
mvs.statsToRefresh[descriptorID] = struct{}{}
}
func (mvs *mutationVisitorState) AddDrainedName(id descpb.ID, nameInfo descpb.NameInfo) {
mvs.drainedNames[id] = append(mvs.drainedNames[id], nameInfo)
}
func (mvs *mutationVisitorState) AddNewSchemaChangerJob(
jobID jobspb.JobID,
stmts []scpb.Statement,
isNonCancelable bool,
auth scpb.Authorization,
descriptorIDs catalog.DescriptorIDSet,
runningStatus string,
) error {
if mvs.schemaChangerJob != nil {
return errors.AssertionFailedf("cannot create more than one new schema change job")
}
mvs.schemaChangerJob = MakeDeclarativeSchemaChangeJobRecord(
jobID,
stmts,
isNonCancelable,
auth,
descriptorIDs,
runningStatus,
)
return nil
}
// MakeDeclarativeSchemaChangeJobRecord is used to construct a declarative
// schema change job. The state of the schema change is stored in the descriptors
// themselves rather than the job state. During execution, the only state which
// is stored in the job itself pertains to backfill progress.
//
// Note that there's no way to construct a job in the reverting state. If the
// state of the schema change according to the descriptors is InRollback, then
// at the outset of the job, an error will be returned to move the job into
// the reverting state.
func MakeDeclarativeSchemaChangeJobRecord(
jobID jobspb.JobID,
stmts []scpb.Statement,
isNonCancelable bool,
auth scpb.Authorization,
descriptorIDs catalog.DescriptorIDSet,
runningStatus string,
) *jobs.Record {
stmtStrs := make([]string, len(stmts))
for i, stmt := range stmts {
// Use the redactable string because it's been normalized and
// fully-qualified. The regular statement is exactly the user input
// but that's a possibly ambiguous value and not what the old
// schema changer used. It's probably that the right thing to use
// is the redactable string with the redaction markers.
stmtStrs[i] = redact.RedactableString(stmt.RedactedStatement).StripMarkers()
}
// The description being all the statements might seem a bit suspect, but
// it's what the old schema changer does, so it's what we'll do.
description := strings.Join(stmtStrs, "; ")
rec := &jobs.Record{
JobID: jobID,
Description: description,
Statements: stmtStrs,
Username: username.MakeSQLUsernameFromPreNormalizedString(auth.UserName),
DescriptorIDs: descriptorIDs.Ordered(),
Details: jobspb.NewSchemaChangeDetails{},
Progress: jobspb.NewSchemaChangeProgress{},
RunningStatus: jobs.RunningStatus(runningStatus),
NonCancelable: isNonCancelable,
}
return rec
}
// EnqueueEvent implements the scmutationexec.MutationVisitorStateUpdater
// interface.
func (mvs *mutationVisitorState) EnqueueEvent(
id descpb.ID,
metadata scpb.TargetMetadata,
details eventpb.CommonSQLEventDetails,
event logpb.EventPayload,
) error {
mvs.eventsByStatement[metadata.StatementID] = append(
mvs.eventsByStatement[metadata.StatementID],
eventPayload{
id: id,
event: event,
TargetMetadata: metadata,
details: details,
},
)
return nil
}