-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathscrun.go
289 lines (275 loc) · 9.04 KB
/
scrun.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
// 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 scrun
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scerrors"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scplan"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
// RunStatementPhase executes in-transaction schema changes for the targeted
// state. These are the immediate changes which take place at DDL statement
// execution time (scop.StatementPhase).
func RunStatementPhase(
ctx context.Context,
knobs *scexec.TestingKnobs,
deps scexec.Dependencies,
state scpb.CurrentState,
) (scpb.CurrentState, jobspb.JobID, error) {
return runTransactionPhase(ctx, knobs, deps, state, scop.StatementPhase)
}
// RunPreCommitPhase executes in-transaction schema changes for the targeted
// state. These are run when executing COMMIT (scop.PreCommitPhase), rather
// than the asynchronous changes which are done by the schema changer job
// after the transaction commits.
func RunPreCommitPhase(
ctx context.Context,
knobs *scexec.TestingKnobs,
deps scexec.Dependencies,
state scpb.CurrentState,
) (scpb.CurrentState, jobspb.JobID, error) {
return runTransactionPhase(ctx, knobs, deps, state, scop.PreCommitPhase)
}
func runTransactionPhase(
ctx context.Context,
knobs *scexec.TestingKnobs,
deps scexec.Dependencies,
state scpb.CurrentState,
phase scop.Phase,
) (scpb.CurrentState, jobspb.JobID, error) {
if len(state.Current) == 0 {
return scpb.CurrentState{}, jobspb.InvalidJobID, nil
}
sc, err := scplan.MakePlan(state, scplan.Params{
ExecutionPhase: phase,
SchemaChangerJobIDSupplier: deps.TransactionalJobRegistry().SchemaChangerJobID,
})
if err != nil {
return scpb.CurrentState{}, jobspb.InvalidJobID, err
}
after := state.Current
if len(after) == 0 {
return scpb.CurrentState{}, jobspb.InvalidJobID, nil
}
stages := sc.StagesForCurrentPhase()
for i := range stages {
if err := executeStage(ctx, knobs, deps, sc, i, stages[i]); err != nil {
return scpb.CurrentState{}, jobspb.InvalidJobID, err
}
after = stages[i].After
}
return scpb.CurrentState{TargetState: state.TargetState, Current: after}, sc.JobID, nil
}
// RunSchemaChangesInJob contains the business logic for the Resume method of a
// declarative schema change job, with the dependencies abstracted away.
func RunSchemaChangesInJob(
ctx context.Context,
knobs *scexec.TestingKnobs,
settings *cluster.Settings,
deps JobRunDependencies,
jobID jobspb.JobID,
descriptorIDs []descpb.ID,
rollback bool,
) error {
state, err := makeState(ctx, jobID, descriptorIDs, rollback, func(
ctx context.Context, f catalogFunc,
) error {
return deps.WithTxnInJob(ctx, func(
ctx context.Context, txnDeps scexec.Dependencies,
) error {
return f(ctx, txnDeps.Catalog())
})
})
if err != nil {
if knobs != nil && knobs.OnPostCommitPlanError != nil {
return knobs.OnPostCommitPlanError(nil, err)
}
return errors.Wrapf(err, "failed to construct state for job %d", jobID)
}
sc, err := scplan.MakePlan(state, scplan.Params{
ExecutionPhase: scop.PostCommitPhase,
SchemaChangerJobIDSupplier: func() jobspb.JobID { return jobID },
})
if err != nil {
if knobs != nil && knobs.OnPostCommitPlanError != nil {
return knobs.OnPostCommitPlanError(&state, err)
}
return err
}
for i := range sc.Stages {
// Execute each stage in its own transaction.
if err := deps.WithTxnInJob(ctx, func(ctx context.Context, td scexec.Dependencies) error {
if err := td.TransactionalJobRegistry().CheckPausepoint(
pausepointName(state, i),
); err != nil {
return err
}
return executeStage(ctx, knobs, td, sc, i, sc.Stages[i])
}); err != nil {
if knobs != nil && knobs.OnPostCommitError != nil {
return knobs.OnPostCommitError(sc, i, err)
}
return err
}
}
return nil
}
// pausepointName construct a name for the job execution phase pausepoint.
func pausepointName(state scpb.CurrentState, i int) string {
return fmt.Sprintf(
"schemachanger.%s.%s.%d",
state.Authorization.UserName, state.Authorization.AppName, i,
)
}
func executeStage(
ctx context.Context,
knobs *scexec.TestingKnobs,
deps scexec.Dependencies,
p scplan.Plan,
stageIdx int,
stage scplan.Stage,
) (err error) {
if knobs != nil && knobs.BeforeStage != nil {
if err := knobs.BeforeStage(p, stageIdx); err != nil {
return err
}
}
log.Infof(ctx, "executing %s (rollback=%v)", stage, p.InRollback)
start := timeutil.Now()
defer func() {
if log.ExpensiveLogEnabled(ctx, 2) {
log.Infof(ctx, "executing %s (rollback=%v) took %v: err = %v",
stage, p.InRollback, timeutil.Since(start), err)
}
}()
if err := scexec.ExecuteStage(ctx, deps, stage.Ops()); err != nil {
// Don't go through the effort to wrap the error if it's a retry or it's a
// cancelation.
if !errors.HasType(err, (*roachpb.TransactionRetryWithProtoRefreshError)(nil)) &&
!errors.Is(err, context.Canceled) &&
!scerrors.HasSchemaChangerUserError(err) {
err = p.DecorateErrorWithPlanDetails(err)
}
// Certain errors are aimed to be user consumable and should never be
// wrapped.
if scerrors.HasSchemaChangerUserError(err) {
return errors.Unwrap(err)
}
return errors.Wrapf(err, "error executing %s", stage)
}
if knobs != nil && knobs.AfterStage != nil {
if err := knobs.AfterStage(p, stageIdx); err != nil {
return err
}
}
return nil
}
type (
catalogFunc = func(context.Context, scexec.Catalog) error
withCatalogFunc = func(context.Context, catalogFunc) error
)
func makeState(
ctx context.Context,
jobID jobspb.JobID,
descriptorIDs []descpb.ID,
rollback bool,
withCatalog withCatalogFunc,
) (scpb.CurrentState, error) {
descError := func(desc catalog.Descriptor, err error) error {
return errors.Wrapf(err, "descriptor %q (%d)", desc.GetName(), desc.GetID())
}
validateJobID := func(fromDesc jobspb.JobID) error {
switch {
case fromDesc == jobspb.InvalidJobID:
return errors.New("missing job ID in schema changer state")
case fromDesc != jobID:
return errors.Errorf("job ID mismatch: expected %d, got %d",
jobID, fromDesc)
default:
return nil
}
}
var authorization scpb.Authorization
validateAuthorization := func(fromDesc scpb.Authorization) error {
switch {
case fromDesc == (scpb.Authorization{}):
return errors.New("missing authorization in schema changer state")
case authorization == (scpb.Authorization{}):
authorization = fromDesc
case authorization != fromDesc:
return errors.Errorf("authorization mismatch: expected %v, got %v",
authorization, fromDesc)
}
return nil
}
var descriptorStates []*scpb.DescriptorState
addDescriptorState := func(desc catalog.Descriptor) error {
cs := desc.GetDeclarativeSchemaChangerState()
if cs == nil {
return errors.New("missing schema changer state")
}
if err := validateJobID(cs.JobID); err != nil {
return err
}
if err := validateAuthorization(cs.Authorization); err != nil {
return err
}
descriptorStates = append(descriptorStates, cs)
return nil
}
if err := withCatalog(ctx, func(
ctx context.Context, cat scexec.Catalog,
) error {
descriptorStates = nil // reset for restarts
descs, err := cat.MustReadImmutableDescriptors(ctx, descriptorIDs...)
if err != nil {
// TODO(ajwerner): It seems possible that a descriptor could be deleted
// and the schema change is in a happy place. Ideally we'd enforce that
// descriptors may only be deleted on the very last step of the schema
// change.
return err
}
for _, desc := range descs {
if err := addDescriptorState(desc); err != nil {
return descError(desc, err)
}
}
return nil
}); err != nil {
return scpb.CurrentState{}, err
}
state, err := scpb.MakeCurrentStateFromDescriptors(descriptorStates)
if err != nil {
return scpb.CurrentState{}, err
}
if !rollback && state.InRollback {
// If we do not mark the error as permanent, but we've configured the job to
// be non-cancelable, we'll never make it to the reverting state.
return scpb.CurrentState{}, jobs.MarkAsPermanentJobError(errors.Errorf(
"job in running state but schema change in rollback, " +
"returning an error to restart in the reverting state"))
}
if rollback && !state.InRollback {
state.Rollback()
}
return state, nil
}