-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
descriptor_utils.go
90 lines (84 loc) · 2.73 KB
/
descriptor_utils.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
// 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/config"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// updateDescriptorGCMutations removes the GCMutation for tie given
// index ID. We no longer populate thif field, but we still search it
// to remove existing entries.
func updateDescriptorGCMutations(
ctx context.Context,
execCfg *sql.ExecutorConfig,
tableID descpb.ID,
garbageCollectedIndexID descpb.IndexID,
) error {
return sql.DescsTxn(ctx, execCfg, func(
ctx context.Context, txn *kv.Txn, descsCol *descs.Collection,
) error {
tbl, err := descsCol.GetMutableTableVersionByID(ctx, tableID, txn)
if err != nil {
return err
}
found := false
for i := 0; i < len(tbl.GCMutations); i++ {
other := tbl.GCMutations[i]
if other.IndexID == garbageCollectedIndexID {
tbl.GCMutations = append(tbl.GCMutations[:i], tbl.GCMutations[i+1:]...)
found = true
break
}
}
if found {
log.Infof(ctx, "updating GCMutations for table %d after removing index %d",
tableID, garbageCollectedIndexID)
// Remove the mutation from the table descriptor.
b := txn.NewBatch()
if err := descsCol.WriteDescToBatch(ctx, false /* kvTrace */, tbl, b); err != nil {
return err
}
return txn.Run(ctx, b)
}
return nil
})
}
// deleteDatabaseZoneConfig removes the zone config for a given database ID.
func deleteDatabaseZoneConfig(
ctx context.Context,
db *kv.DB,
codec keys.SQLCodec,
settings *cluster.Settings,
databaseID descpb.ID,
) error {
if databaseID == descpb.InvalidID {
return nil
}
return db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
if !descs.UnsafeSkipSystemConfigTrigger.Get(&settings.SV) {
if err := txn.SetSystemConfigTrigger(codec.ForSystemTenant()); err != nil {
return err
}
}
b := &kv.Batch{}
// Delete the zone config entry for the dropped database associated with the
// job, if it exists.
dbZoneKeyPrefix := config.MakeZoneKeyPrefix(codec, databaseID)
b.DelRange(dbZoneKeyPrefix, dbZoneKeyPrefix.PrefixEnd(), false /* returnKeys */)
return txn.Run(ctx, b)
})
}