-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
index_garbage_collection.go
260 lines (240 loc) · 8.58 KB
/
index_garbage_collection.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
// Copyright 2020 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 gcjob
import (
"context"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// deleteIndexData is used to issue range deletion tombstones over all indexes
// being gc'd.
func deleteIndexData(
ctx context.Context,
execCfg *sql.ExecutorConfig,
parentID descpb.ID,
progress *jobspb.SchemaChangeGCProgress,
) error {
droppedIndexes := progress.Indexes
if log.V(2) {
log.Infof(ctx, "GC is being considered on table %d for indexes indexes: %+v", parentID, droppedIndexes)
}
// Before deleting any indexes, ensure that old versions of the table descriptor
// are no longer in use. This is necessary in the case of truncate, where we
// schedule a GC Job in the transaction that commits the truncation.
parentDesc, err := sql.WaitToUpdateLeases(ctx, execCfg.LeaseManager, parentID)
if errors.Is(err, catalog.ErrDescriptorNotFound) {
handleTableDescriptorDeleted(ctx, parentID, progress)
return nil
}
if err != nil {
return err
}
parentTable, isTable := parentDesc.(catalog.TableDescriptor)
if !isTable {
return errors.AssertionFailedf("expected descriptor %d to be a table, not %T", parentID, parentDesc)
}
for _, index := range droppedIndexes {
// TODO(ajwerner): Is there any reason to check on the current status of
// the index? At time of writing, we don't checkpoint between operating on
// individual indexes, and we always delete the data before moving on to
// waiting, so it seems like there's nothing to check for.
if err := clearIndex(
ctx, execCfg, parentTable, index.IndexID, deleteAllSpanData,
); err != nil {
return errors.Wrapf(err, "deleting index %d from table %d", index.IndexID, parentTable.GetID())
}
markIndexGCed(
ctx, index.IndexID, progress,
jobspb.SchemaChangeGCProgress_WAITING_FOR_MVCC_GC,
)
}
return nil
}
// gcIndexes find the indexes that need to be GC'd, GC's them, and then updates
// the cleans up the table descriptor, zone configs and job payload to indicate
// the work that it did.
func gcIndexes(
ctx context.Context,
execCfg *sql.ExecutorConfig,
parentID descpb.ID,
progress *jobspb.SchemaChangeGCProgress,
) error {
droppedIndexes := progress.Indexes
if log.V(2) {
log.Infof(ctx, "GC is being considered on table %d for indexes indexes: %+v", parentID, droppedIndexes)
}
// Before deleting any indexes, ensure that old versions of the table descriptor
// are no longer in use. This is necessary in the case of truncate, where we
// schedule a GC Job in the transaction that commits the truncation.
parentDesc, err := sql.WaitToUpdateLeases(ctx, execCfg.LeaseManager, parentID)
if errors.Is(err, catalog.ErrDescriptorNotFound) {
handleTableDescriptorDeleted(ctx, parentID, progress)
return nil
}
if err != nil {
return err
}
parentTable, isTable := parentDesc.(catalog.TableDescriptor)
if !isTable {
return errors.AssertionFailedf("expected descriptor %d to be a table, not %T", parentID, parentDesc)
}
for _, index := range droppedIndexes {
if index.Status != jobspb.SchemaChangeGCProgress_CLEARING {
continue
}
if err := clearIndex(
ctx, execCfg, parentTable, index.IndexID, clearSpanData,
); err != nil {
return errors.Wrapf(err, "clearing index %d from table %d", index.IndexID, parentTable.GetID())
}
// All the data chunks have been removed. Now also removed the
// zone configs for the dropped indexes, if any.
removeIndexZoneConfigs := func(
ctx context.Context, txn *kv.Txn, descriptors *descs.Collection,
) error {
freshParentTableDesc, err := descriptors.GetMutableTableByID(
ctx, txn, parentID, tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{
AvoidLeased: true,
Required: true,
IncludeDropped: true,
IncludeOffline: true,
},
})
if err != nil {
return err
}
return sql.RemoveIndexZoneConfigs(
ctx, txn, execCfg, descriptors, freshParentTableDesc, []uint32{uint32(index.IndexID)},
)
}
err := sql.DescsTxn(ctx, execCfg, removeIndexZoneConfigs)
if errors.Is(err, catalog.ErrDescriptorNotFound) ||
sqlerrors.IsUndefinedRelationError(err) {
handleTableDescriptorDeleted(ctx, parentID, progress)
return nil
}
if err != nil {
return errors.Wrapf(err, "removing index %d zone configs", index.IndexID)
}
markIndexGCed(
ctx, index.IndexID, progress, jobspb.SchemaChangeGCProgress_CLEARED,
)
}
return nil
}
type clearOrDeleteSpanDataFunc = func(
ctx context.Context,
db *kv.DB,
distSender *kvcoord.DistSender,
span roachpb.RSpan,
) error
// clearIndexes issues Clear Range requests over all specified indexes.
func clearIndex(
ctx context.Context,
execCfg *sql.ExecutorConfig,
tableDesc catalog.TableDescriptor,
indexID descpb.IndexID,
clearOrDeleteSpanData clearOrDeleteSpanDataFunc,
) error {
log.Infof(ctx, "clearing index %d from table %d", indexID, tableDesc.GetID())
sp := tableDesc.IndexSpan(execCfg.Codec, indexID)
start, err := keys.Addr(sp.Key)
if err != nil {
return errors.Wrap(err, "failed to addr index start")
}
end, err := keys.Addr(sp.EndKey)
if err != nil {
return errors.Wrap(err, "failed to addr index end")
}
rSpan := roachpb.RSpan{Key: start, EndKey: end}
return clearOrDeleteSpanData(ctx, execCfg.DB, execCfg.DistSender, rSpan)
}
func deleteIndexZoneConfigsAfterGC(
ctx context.Context,
execCfg *sql.ExecutorConfig,
parentID descpb.ID,
progress *jobspb.SchemaChangeGCProgress,
) error {
for _, index := range progress.Indexes {
if index.Status == jobspb.SchemaChangeGCProgress_CLEARED {
continue
}
if err := waitForEmptyPrefix(
ctx, execCfg.DB, execCfg.SV(),
execCfg.GCJobTestingKnobs.SkipWaitingForMVCCGC,
execCfg.Codec.IndexPrefix(uint32(parentID), uint32(index.IndexID)),
); err != nil {
return errors.Wrapf(err, "waiting for gc of index %d from table %d",
index.IndexID, parentID)
}
// All the data chunks have been removed. Now also removed the
// zone configs for the dropped indexes, if any.
removeIndexZoneConfigs := func(
ctx context.Context, txn *kv.Txn, descriptors *descs.Collection,
) error {
freshParentTableDesc, err := descriptors.GetMutableTableByID(
ctx, txn, parentID, tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{
AvoidLeased: true,
Required: true,
IncludeDropped: true,
IncludeOffline: true,
},
})
if err != nil {
return err
}
return sql.RemoveIndexZoneConfigs(
ctx, txn, execCfg, descriptors, freshParentTableDesc, []uint32{uint32(index.IndexID)},
)
}
err := sql.DescsTxn(ctx, execCfg, removeIndexZoneConfigs)
switch {
case errors.Is(err, catalog.ErrDescriptorNotFound),
sqlerrors.IsUndefinedRelationError(err):
log.Infof(ctx, "removing index %d zone config from table %d failed: %v",
index.IndexID, parentID, err)
case err != nil:
return errors.Wrapf(err, "removing index %d zone configs", index.IndexID)
}
markIndexGCed(
ctx, index.IndexID, progress, jobspb.SchemaChangeGCProgress_CLEARED,
)
}
return nil
}
// handleTableDescriptorDeleted should be called when logic detects that
// a table descriptor has been deleted while attempting to GC an index.
// The function marks in progress that all indexes have been cleared.
func handleTableDescriptorDeleted(
ctx context.Context, parentID descpb.ID, progress *jobspb.SchemaChangeGCProgress,
) {
droppedIndexes := progress.Indexes
// If the descriptor has been removed, then we need to assume that the relevant
// zone configs and data have been cleaned up by another process.
log.Infof(ctx, "descriptor %d dropped, assuming another process has handled GC", parentID)
for _, index := range droppedIndexes {
markIndexGCed(
ctx, index.IndexID, progress, jobspb.SchemaChangeGCProgress_CLEARED,
)
}
}