-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
dependencies.go
185 lines (157 loc) · 6.77 KB
/
dependencies.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
// 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"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec/scmutationexec"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
)
// Dependencies contains all the dependencies required by the executor.
type Dependencies interface {
Catalog() Catalog
TransactionalJobCreator() TransactionalJobCreator
IndexBackfiller() IndexBackfiller
IndexValidator() IndexValidator
IndexSpanSplitter() IndexSpanSplitter
JobProgressTracker() JobProgressTracker
// TestingKnobs returns the testing knobs for the new schema changer.
TestingKnobs() *NewSchemaChangerTestingKnobs
// Statements returns the statements behind this schema change.
Statements() []string
// Phase returns the phase in which operations are to be executed.
Phase() scop.Phase
}
// Catalog encapsulates the catalog-related dependencies for the executor.
// This involves reading descriptors, as well as preparing batches of catalog
// changes.
type Catalog interface {
scmutationexec.CatalogReader
// MustReadMutableDescriptor the mutable equivalent to
// MustReadImmutableDescriptor in scmutationexec.CatalogReader.
// This method should be used carefully.
MustReadMutableDescriptor(ctx context.Context, id descpb.ID) (catalog.MutableDescriptor, error)
// NewCatalogChangeBatcher is equivalent to creating a new kv.Batch for the
// current kv.Txn.
NewCatalogChangeBatcher() CatalogChangeBatcher
}
// CatalogChangeBatcher encapsulates batched updates to the catalog: descriptor
// updates, namespace operations, etc.
type CatalogChangeBatcher interface {
// CreateOrUpdateDescriptor upserts a descriptor.
CreateOrUpdateDescriptor(ctx context.Context, desc catalog.MutableDescriptor) error
// DeleteName deletes a namespace entry.
DeleteName(ctx context.Context, nameInfo descpb.NameInfo, id descpb.ID) error
// ValidateAndRun executes the updates after validating them using
// catalog.Validate.
ValidateAndRun(ctx context.Context) error
}
// TransactionalJobCreator creates a job in the current transaction.
type TransactionalJobCreator interface {
// CreateJob creates a job in the current transaction and returns the
// id which was assigned to that job, or an error otherwise.
CreateJob(ctx context.Context, record jobs.Record) (jobspb.JobID, error)
}
// IndexBackfiller is an abstract index backfiller that performs index backfills
// when provided with a specification of tables and indexes and a way to track
// job progress.
type IndexBackfiller interface {
BackfillIndex(
ctx context.Context,
_ JobProgressTracker,
_ catalog.TableDescriptor,
source descpb.IndexID,
destinations ...descpb.IndexID,
) error
}
// CCLCallbacks provides an interface that implements CCL exclusive
// callbacks.
type CCLCallbacks interface {
CreatePartitioning(
ctx context.Context,
tableDesc *tabledesc.Mutable,
indexDesc descpb.IndexDescriptor,
partBy *tree.PartitionBy,
allowedNewColumnNames []tree.Name,
allowImplicitPartitioning bool,
) (newImplicitCols []catalog.Column, newPartitioning descpb.PartitioningDescriptor, err error)
}
// IndexValidator provides interfaces that allow indexes to be validated.
type IndexValidator interface {
ValidateForwardIndexes(
ctx context.Context,
tableDesc catalog.TableDescriptor,
indexes []catalog.Index,
withFirstMutationPublic bool,
gatherAllInvalid bool,
override sessiondata.InternalExecutorOverride,
) error
ValidateInvertedIndexes(
ctx context.Context,
tableDesc catalog.TableDescriptor,
indexes []catalog.Index,
gatherAllInvalid bool,
override sessiondata.InternalExecutorOverride,
) error
}
// IndexSpanSplitter can try to split an index span in the current transaction
// prior to backfilling.
type IndexSpanSplitter interface {
// MaybeSplitIndexSpans will attempt to split the backfilled index span.
MaybeSplitIndexSpans(ctx context.Context, table catalog.TableDescriptor, indexToBackfill catalog.Index) error
}
// JobProgressTracker abstracts the infrastructure to read and write backfill
// progress to job state.
type JobProgressTracker interface {
// This interface is implicitly implying that there is only one stage of
// index backfills for a given table in a schema change. It implies that
// because it assumes that it's safe and reasonable to just store one set of
// resume spans per table on the job.
//
// Potentially something close to interface could still work if there were
// multiple stages of backfills for a table if we tracked which stage this
// were somehow. Maybe we could do something like increment a stage counter
// per table after finishing the backfills.
//
// It definitely is possible that there are multiple index backfills on a
// table in the context of a single schema change that changes the set of
// columns (primary index) and adds secondary indexes.
//
// Really this complexity arises in the computation of the fraction completed.
// We'll want to know whether there are more index backfills to come.
//
// One idea is to index secondarily on the source index.
GetResumeSpans(ctx context.Context, tableID descpb.ID, indexID descpb.IndexID) ([]roachpb.Span, error)
SetResumeSpans(ctx context.Context, tableID descpb.ID, indexID descpb.IndexID, total, done []roachpb.Span) error
}
// NewSchemaChangerTestingKnobs are testing knobs for the executor.
type NewSchemaChangerTestingKnobs struct {
// BeforeStage is called before ops passed to the executor are executed.
// Errors returned are injected into the executor.
BeforeStage func(ops scop.Ops, m TestingKnobMetadata) error
// BeforeWaitingForConcurrentSchemaChanges is called at the start of waiting
// for concurrent schema changes to finish.
BeforeWaitingForConcurrentSchemaChanges func(stmts []string)
}
// ModuleTestingKnobs is part of the base.ModuleTestingKnobs interface.
func (*NewSchemaChangerTestingKnobs) ModuleTestingKnobs() {}
// TestingKnobMetadata holds additional information about the execution of the
// schema change that is used by the testing knobs.
type TestingKnobMetadata struct {
Statements []string
Phase scop.Phase
}