-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
descriptor_utils.go
95 lines (84 loc) · 2.84 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
91
92
93
94
95
// 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/kv"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
func updateDescriptorGCMutations(
ctx context.Context,
execCfg *sql.ExecutorConfig,
table *sqlbase.TableDescriptor,
garbageCollectedIndexID sqlbase.IndexID,
) error {
log.Infof(ctx, "updating GCMutations for table %d after removing index %d", table.ID, garbageCollectedIndexID)
// Remove the mutation from the table descriptor.
updateTableMutations := func(tbl *sqlbase.MutableTableDescriptor) error {
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:]...)
break
}
}
return nil
}
_, err := execCfg.LeaseManager.Publish(
ctx,
table.ID,
updateTableMutations,
nil, /* logEvent */
)
if err != nil {
return err
}
return nil
}
// dropTableDesc removes a descriptor from the KV database.
func dropTableDesc(ctx context.Context, db *kv.DB, tableDesc *sqlbase.TableDescriptor) error {
log.Infof(ctx, "removing table descriptor for table %d", tableDesc.ID)
descKey := sqlbase.MakeDescMetadataKey(tableDesc.ID)
zoneKeyPrefix := config.MakeZoneKeyPrefix(uint32(tableDesc.ID))
return db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
b := &kv.Batch{}
// Delete the descriptor.
b.Del(descKey)
// Delete the zone config entry for this table.
b.DelRange(zoneKeyPrefix, zoneKeyPrefix.PrefixEnd(), false /* returnKeys */)
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
return txn.Run(ctx, b)
})
}
// deleteDatabaseZoneConfig removes the zone config for a given database ID.
func deleteDatabaseZoneConfig(ctx context.Context, db *kv.DB, databaseID sqlbase.ID) error {
return db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
b := &kv.Batch{}
// Delete the zone config entry for the dropped database associated with the
// job, if it exists.
if databaseID == sqlbase.InvalidID {
return nil
}
dbZoneKeyPrefix := config.MakeZoneKeyPrefix(uint32(databaseID))
b.DelRange(dbZoneKeyPrefix, dbZoneKeyPrefix.PrefixEnd(), false /* returnKeys */)
return txn.Run(ctx, b)
})
}