-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
catalog.go
297 lines (269 loc) · 8.71 KB
/
catalog.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
290
291
292
293
294
295
296
297
// Copyright 2022 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 nstree
import (
"context"
"strings"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate"
"github.com/cockroachdb/errors"
)
// Catalog is used to store an in-memory copy of the whole catalog, or a portion
// thereof.
type Catalog struct {
underlying Map
byteSize int64
}
// ForEachDescriptorEntry iterates over all descriptor table entries in an
// ordered fashion.
func (c Catalog) ForEachDescriptorEntry(fn func(desc catalog.Descriptor) error) error {
if !c.IsInitialized() {
return nil
}
return c.underlying.byID.ascend(func(e catalog.NameEntry) error {
return fn(e.(catalog.Descriptor))
})
}
// ForEachNamespaceEntry iterates over all namespace table entries in an ordered
// fashion.
func (c Catalog) ForEachNamespaceEntry(fn func(e catalog.NameEntry) error) error {
if !c.IsInitialized() {
return nil
}
return c.underlying.byName.ascend(fn)
}
// LookupDescriptorEntry looks up a descriptor by ID.
func (c Catalog) LookupDescriptorEntry(id descpb.ID) catalog.Descriptor {
if !c.IsInitialized() || id == descpb.InvalidID {
return nil
}
e := c.underlying.byID.get(id)
if e == nil {
return nil
}
return e.(catalog.Descriptor)
}
// LookupNamespaceEntry looks up a descriptor ID by name.
func (c Catalog) LookupNamespaceEntry(key catalog.NameKey) catalog.NameEntry {
if !c.IsInitialized() || key == nil {
return nil
}
return c.underlying.byName.getByName(key.GetParentID(), key.GetParentSchemaID(), key.GetName())
}
// OrderedDescriptors returns the descriptors in an ordered fashion.
func (c Catalog) OrderedDescriptors() []catalog.Descriptor {
if !c.IsInitialized() {
return nil
}
ret := make([]catalog.Descriptor, 0, c.underlying.byID.t.Len())
_ = c.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
ret = append(ret, desc)
return nil
})
return ret
}
// OrderedDescriptorIDs returns the descriptor IDs in an ordered fashion.
func (c Catalog) OrderedDescriptorIDs() []descpb.ID {
if !c.IsInitialized() {
return nil
}
ret := make([]descpb.ID, 0, c.underlying.byName.t.Len())
_ = c.ForEachNamespaceEntry(func(e catalog.NameEntry) error {
ret = append(ret, e.GetID())
return nil
})
return ret
}
// IsInitialized returns false if the underlying map has not yet been
// initialized. Initialization is done lazily when
func (c Catalog) IsInitialized() bool {
return c.underlying.initialized()
}
var _ validate.ValidationDereferencer = Catalog{}
var _ validate.ValidationDereferencer = MutableCatalog{}
// DereferenceDescriptors implements the validate.ValidationDereferencer
// interface.
func (c Catalog) DereferenceDescriptors(
ctx context.Context, version clusterversion.ClusterVersion, reqs []descpb.ID,
) ([]catalog.Descriptor, error) {
ret := make([]catalog.Descriptor, len(reqs))
for i, id := range reqs {
ret[i] = c.LookupDescriptorEntry(id)
}
return ret, nil
}
// DereferenceDescriptorIDs implements the validate.ValidationDereferencer
// interface.
func (c Catalog) DereferenceDescriptorIDs(
_ context.Context, reqs []descpb.NameInfo,
) ([]descpb.ID, error) {
ret := make([]descpb.ID, len(reqs))
for i, req := range reqs {
ne := c.LookupNamespaceEntry(req)
if ne == nil {
continue
}
ret[i] = ne.GetID()
}
return ret, nil
}
// Validate delegates to validate.Validate.
func (c Catalog) Validate(
ctx context.Context,
version clusterversion.ClusterVersion,
telemetry catalog.ValidationTelemetry,
targetLevel catalog.ValidationLevel,
descriptors ...catalog.Descriptor,
) (ve catalog.ValidationErrors) {
return validate.Validate(ctx, version, c, telemetry, targetLevel, descriptors...)
}
// ValidateNamespaceEntry returns an error if the specified namespace entry
// is invalid.
func (c Catalog) ValidateNamespaceEntry(key catalog.NameKey) error {
ne := c.LookupNamespaceEntry(key)
if ne == nil {
return errors.New("invalid descriptor ID")
}
// Handle special cases.
switch ne.GetID() {
case descpb.InvalidID:
return errors.New("invalid descriptor ID")
case keys.PublicSchemaID:
// The public schema doesn't have a descriptor.
return nil
default:
isSchema := ne.GetParentID() != keys.RootNamespaceID && ne.GetParentSchemaID() == keys.RootNamespaceID
if isSchema && strings.HasPrefix(ne.GetName(), "pg_temp_") {
// Temporary schemas have namespace entries but not descriptors.
return nil
}
}
// Compare the namespace entry with the referenced descriptor.
desc := c.LookupDescriptorEntry(ne.GetID())
if desc == nil {
return catalog.ErrDescriptorNotFound
}
// TODO(postamar): remove draining name checks in 22.2
for _, dn := range desc.GetDrainingNames() {
if ne.GetParentID() == dn.GetParentID() &&
ne.GetParentSchemaID() == dn.GetParentSchemaID() &&
ne.GetName() == dn.GetName() {
return nil
}
}
if desc.Dropped() {
return errors.Newf("no matching name info in draining names of dropped %s",
desc.DescriptorType())
}
if ne.GetParentID() == desc.GetParentID() &&
ne.GetParentSchemaID() == desc.GetParentSchemaID() &&
ne.GetName() == desc.GetName() {
return nil
}
return errors.Newf("no matching name info found in non-dropped %s %q",
desc.DescriptorType(), desc.GetName())
}
// ValidateWithRecover is like Validate but which recovers from panics.
// This is useful when we're validating many descriptors separately and we don't
// want a corrupt descriptor to prevent validating the others.
func (c Catalog) ValidateWithRecover(
ctx context.Context, version clusterversion.ClusterVersion, desc catalog.Descriptor,
) (ve catalog.ValidationErrors) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = errors.Newf("%v", r)
}
err = errors.WithAssertionFailure(errors.Wrap(err, "validation"))
ve = append(ve, err)
}
}()
return c.Validate(ctx, version, catalog.NoValidationTelemetry, catalog.ValidationLevelAllPreTxnCommit, desc)
}
// ByteSize returns memory usage of the underlying map in bytes.
func (c Catalog) ByteSize() int64 {
return c.byteSize
}
// MutableCatalog is like Catalog but mutable.
type MutableCatalog struct {
Catalog
}
// UpsertDescriptorEntry adds a descriptor to the MutableCatalog.
func (mc *MutableCatalog) UpsertDescriptorEntry(desc catalog.Descriptor) {
if desc == nil || desc.GetID() == descpb.InvalidID {
return
}
mc.underlying.maybeInitialize()
if replaced := mc.underlying.byID.upsert(desc); replaced != nil {
mc.byteSize -= replaced.(catalog.Descriptor).ByteSize()
}
mc.byteSize += desc.ByteSize()
}
// DeleteDescriptorEntry removes a descriptor from the MutableCatalog.
func (mc *MutableCatalog) DeleteDescriptorEntry(id descpb.ID) {
if id == descpb.InvalidID || !mc.IsInitialized() {
return
}
mc.underlying.maybeInitialize()
if removed := mc.underlying.byID.delete(id); removed != nil {
mc.byteSize -= removed.(catalog.Descriptor).ByteSize()
}
}
// UpsertNamespaceEntry adds a name -> id mapping to the MutableCatalog.
func (mc *MutableCatalog) UpsertNamespaceEntry(key catalog.NameKey, id descpb.ID) {
if key == nil || id == descpb.InvalidID {
return
}
mc.underlying.maybeInitialize()
nsEntry := &namespaceEntry{
NameInfo: descpb.NameInfo{
ParentID: key.GetParentID(),
ParentSchemaID: key.GetParentSchemaID(),
Name: key.GetName(),
},
ID: id,
}
if replaced := mc.underlying.byName.upsert(nsEntry); replaced != nil {
mc.byteSize -= replaced.(*namespaceEntry).ByteSize()
}
mc.byteSize += nsEntry.ByteSize()
}
// DeleteNamespaceEntry removes a name -> id mapping from the MutableCatalog.
func (mc *MutableCatalog) DeleteNamespaceEntry(key catalog.NameKey) {
if key == nil || !mc.IsInitialized() {
return
}
mc.underlying.maybeInitialize()
if removed := mc.underlying.byName.delete(key); removed != nil {
mc.byteSize -= removed.(*namespaceEntry).ByteSize()
}
}
// Clear empties the MutableCatalog.
func (mc *MutableCatalog) Clear() {
mc.underlying.Clear()
}
type namespaceEntry struct {
descpb.NameInfo
descpb.ID
}
var _ catalog.NameEntry = namespaceEntry{}
// GetID implements the catalog.NameEntry interface.
func (e namespaceEntry) GetID() descpb.ID {
return e.ID
}
// ByteSize returns the number of bytes a namespaceEntry object takes.
func (e namespaceEntry) ByteSize() int64 {
return int64(e.NameInfo.Size()) + int64(unsafe.Sizeof(e.ID))
}