-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
catalog_reader_cached.go
457 lines (424 loc) · 13.9 KB
/
catalog_reader_cached.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// 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 catkv
import (
"context"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree"
"github.com/cockroachdb/cockroach/pkg/util/iterutil"
"github.com/cockroachdb/cockroach/pkg/util/mon"
)
// cachedCatalogReader is a CatalogReader implementation which aggressively
// caches all catalog query results.
type cachedCatalogReader struct {
// cr is the CatalogReader which is delegated to for cache misses.
cr CatalogReader
// cache contains the cached catalog.
cache nstree.MutableCatalog
// by*State contain further caches and other state.
byIDState map[descpb.ID]byIDStateValue
byNameState map[descpb.NameInfo]byNameStateValue
// has* indicates previously completed lookups. When set, we
// know the corresponding catalog data is in the cache.
hasScanAll bool
hasScanAllComments bool
hasScanNamespaceForDatabases bool
// memAcc is the actual account of an injected, upstream monitor
// to track memory usage of cachedCatalogReader.
memAcc *mon.BoundAccount
// systemDatabaseCache is a per-node cache of system database catalog
// information. Its presence is entirely optional and only serves to
// eliminate superfluous round trips to KV.
systemDatabaseCache *SystemDatabaseCache
// version only needs to be set when systemDatabaseCache is set.
version clusterversion.ClusterVersion
}
type byIDStateValue struct {
// These flags indicate previously completed lookups scoped to
// this descriptor ID.
hasScanNamespaceForDatabaseEntries bool
hasScanNamespaceForDatabaseSchemas bool
hasGetDescriptorEntries bool
}
type byNameStateValue struct {
// These flags indicate previously completed lookups scoped to
// this name key.
hasGetNamespaceEntries bool
}
// NewCatalogReader is the constructor for the default CatalogReader
// implementation, which is cached.
func NewCatalogReader(
codec keys.SQLCodec,
version clusterversion.ClusterVersion,
systemDatabaseCache *SystemDatabaseCache,
maybeMonitor *mon.BytesMonitor,
) CatalogReader {
ccr := &cachedCatalogReader{
cr: &catalogReader{codec: codec},
version: version,
systemDatabaseCache: systemDatabaseCache,
}
if maybeMonitor != nil {
memAcc := maybeMonitor.MakeBoundAccount()
ccr.memAcc = &memAcc
}
return ccr
}
var _ CatalogReader = &cachedCatalogReader{}
// Codec is part of the CatalogReader interface.
func (c *cachedCatalogReader) Codec() keys.SQLCodec {
return c.cr.Codec()
}
// Cache is part of the CatalogReader interface.
func (c *cachedCatalogReader) Cache() nstree.Catalog {
return c.cache.Catalog
}
// IsIDInCache is part of the CatalogReader interface.
func (c *cachedCatalogReader) IsIDInCache(id descpb.ID) bool {
return c.byIDState[id].hasGetDescriptorEntries
}
// IsNameInCache is part of the CatalogReader interface.
func (c *cachedCatalogReader) IsNameInCache(key catalog.NameKey) bool {
return c.cache.LookupNamespaceEntry(key) != nil
}
// IsDescIDKnownToNotExist is part of the CatalogReader interface.
func (c *cachedCatalogReader) IsDescIDKnownToNotExist(id, maybeParentID descpb.ID) bool {
if c.cache.LookupDescriptor(id) != nil {
return false
}
if c.hasScanAll {
return true
}
if c.byIDState[maybeParentID].hasScanNamespaceForDatabaseEntries {
var found bool
_ = c.cache.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
if e.GetParentID() == maybeParentID && e.GetID() == id {
found = true
return iterutil.StopIteration()
}
return nil
})
return !found
}
return false
}
// Reset is part of the CatalogReader interface.
func (c *cachedCatalogReader) Reset(ctx context.Context) {
c.cr.Reset(ctx)
c.cache.Clear()
if c.memAcc != nil {
c.memAcc.Clear(ctx)
}
old := *c
*c = cachedCatalogReader{
cr: old.cr,
memAcc: old.memAcc,
systemDatabaseCache: old.systemDatabaseCache,
version: old.version,
}
}
// ScanAllComments is part of the CatalogReader interface.
func (c *cachedCatalogReader) ScanAllComments(
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor,
) (nstree.Catalog, error) {
if c.hasScanAllComments {
return c.cache.Catalog, nil
}
// Scan all catalog comments.
read, err := c.cr.ScanAllComments(ctx, txn, db)
if err != nil {
return nstree.Catalog{}, err
}
// We don't wipe out anything we already read when
// updating the cache. So add the comments in and then
// add back any descriptors + comments we read earlier.
mergedCatalog := nstree.MutableCatalog{}
mergedCatalog.AddAll(read)
mergedCatalog.AddAll(c.cache.Catalog)
if err := c.ensure(ctx, mergedCatalog.Catalog); err != nil {
return nstree.Catalog{}, err
}
c.hasScanAllComments = true
return read, nil
}
// ScanAll is part of the CatalogReader interface.
func (c *cachedCatalogReader) ScanAll(ctx context.Context, txn *kv.Txn) (nstree.Catalog, error) {
if c.hasScanAll {
return c.cache.Catalog, nil
}
// These ids don't have corresponding descriptors but some of them may have
// zone configs.
{
s := byIDStateValue{hasGetDescriptorEntries: true}
c.setByIDState(keys.RootNamespaceID, s)
for _, id := range keys.PseudoTableIDs {
c.setByIDState(descpb.ID(id), s)
}
}
// Scan all catalog tables.
read, err := c.cr.ScanAll(ctx, txn)
if err != nil {
return nstree.Catalog{}, err
}
if err := c.ensure(ctx, read); err != nil {
return nstree.Catalog{}, err
}
c.hasScanAll = true
c.hasScanNamespaceForDatabases = true
c.hasScanAllComments = true
if err := read.ForEachDescriptor(func(desc catalog.Descriptor) error {
// We must update the byID and byName states for each descriptor that
// was read.
var idState byIDStateValue
var nameState byNameStateValue
idState.hasScanNamespaceForDatabaseEntries = true
idState.hasScanNamespaceForDatabaseSchemas = true
idState.hasGetDescriptorEntries = true
nameState.hasGetNamespaceEntries = true
c.setByIDState(desc.GetID(), idState)
ni := descpb.NameInfo{
ParentID: desc.GetParentID(),
Name: desc.GetName(),
}
if typ := desc.DescriptorType(); typ != catalog.Database && typ != catalog.Schema {
ni.ParentSchemaID = desc.GetParentSchemaID()
}
c.setByNameState(ni, nameState)
return nil
}); err != nil {
return nstree.Catalog{}, err
}
return read, nil
}
// ScanNamespaceForDatabases is part of the CatalogReader interface.
func (c *cachedCatalogReader) ScanNamespaceForDatabases(
ctx context.Context, txn *kv.Txn,
) (nstree.Catalog, error) {
if c.hasScanNamespaceForDatabases {
var mc nstree.MutableCatalog
_ = c.cache.ForEachDatabaseNamespaceEntry(func(e nstree.NamespaceEntry) error {
mc.UpsertNamespaceEntry(e, e.GetID(), e.GetMVCCTimestamp())
return nil
})
return mc.Catalog, nil
}
read, err := c.cr.ScanNamespaceForDatabases(ctx, txn)
if err != nil {
return nstree.Catalog{}, err
}
if err := c.ensure(ctx, read); err != nil {
return nstree.Catalog{}, err
}
c.hasScanNamespaceForDatabases = true
return read, nil
}
// ScanNamespaceForDatabaseSchemas is part of the CatalogReader interface.
func (c *cachedCatalogReader) ScanNamespaceForDatabaseSchemas(
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor,
) (nstree.Catalog, error) {
s := c.byIDState[db.GetID()]
if s.hasScanNamespaceForDatabaseSchemas {
var mc nstree.MutableCatalog
_ = c.cache.ForEachSchemaNamespaceEntryInDatabase(db.GetID(), func(e nstree.NamespaceEntry) error {
mc.UpsertNamespaceEntry(e, e.GetID(), e.GetMVCCTimestamp())
return nil
})
return mc.Catalog, nil
}
read, err := c.cr.ScanNamespaceForDatabaseSchemas(ctx, txn, db)
if err != nil {
return nstree.Catalog{}, err
}
if err := c.ensure(ctx, read); err != nil {
return nstree.Catalog{}, err
}
s.hasScanNamespaceForDatabaseSchemas = true
c.setByIDState(db.GetID(), s)
return read, nil
}
// ScanNamespaceForDatabaseSchemasAndObjects is part of the CatalogReader
// interface.
func (c *cachedCatalogReader) ScanNamespaceForDatabaseSchemasAndObjects(
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor,
) (nstree.Catalog, error) {
s := c.byIDState[db.GetID()]
if s.hasScanNamespaceForDatabaseEntries {
var mc nstree.MutableCatalog
_ = c.cache.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
if e.GetParentID() == db.GetID() {
mc.UpsertNamespaceEntry(e, e.GetID(), e.GetMVCCTimestamp())
}
return nil
})
return mc.Catalog, nil
}
read, err := c.cr.ScanNamespaceForDatabaseSchemasAndObjects(ctx, txn, db)
if err != nil {
return nstree.Catalog{}, err
}
if err := c.ensure(ctx, read); err != nil {
return nstree.Catalog{}, err
}
s.hasScanNamespaceForDatabaseEntries = true
s.hasScanNamespaceForDatabaseSchemas = true
c.setByIDState(db.GetID(), s)
return read, nil
}
// ScanNamespaceForSchemaObjects is part of the CatalogReader interface.
func (c *cachedCatalogReader) ScanNamespaceForSchemaObjects(
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, sc catalog.SchemaDescriptor,
) (nstree.Catalog, error) {
s := c.byIDState[db.GetID()]
if s.hasScanNamespaceForDatabaseEntries {
var mc nstree.MutableCatalog
_ = c.cache.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
if e.GetParentID() == db.GetID() && e.GetParentSchemaID() == sc.GetID() {
mc.UpsertNamespaceEntry(e, e.GetID(), e.GetMVCCTimestamp())
}
return nil
})
return mc.Catalog, nil
}
read, err := c.cr.ScanNamespaceForSchemaObjects(ctx, txn, db, sc)
if err != nil {
return nstree.Catalog{}, err
}
if err := c.ensure(ctx, read); err != nil {
return nstree.Catalog{}, err
}
return read, nil
}
// GetByIDs is part of the CatalogReader interface.
func (c *cachedCatalogReader) GetByIDs(
ctx context.Context,
txn *kv.Txn,
ids []descpb.ID,
isDescriptorRequired bool,
expectedType catalog.DescriptorType,
) (nstree.Catalog, error) {
numUncached := 0
// Move any uncached IDs to the front of the slice.
for i, id := range ids {
if c.byIDState[id].hasGetDescriptorEntries || c.hasScanAll {
continue
}
if desc := c.systemDatabaseCache.lookupDescriptor(c.version, id); desc != nil {
c.cache.UpsertDescriptor(desc)
}
ids[i], ids[numUncached] = ids[numUncached], id
numUncached++
}
if numUncached > 0 && !(c.hasScanAll && !isDescriptorRequired) {
uncachedIDs := ids[:numUncached]
read, err := c.cr.GetByIDs(ctx, txn, uncachedIDs, isDescriptorRequired, expectedType)
if err != nil {
return nstree.Catalog{}, err
}
if err := c.ensure(ctx, read); err != nil {
return nstree.Catalog{}, err
}
for _, id := range uncachedIDs {
s := c.byIDState[id]
s.hasGetDescriptorEntries = true
c.setByIDState(id, s)
}
}
ret := c.cache.FilterByIDsExclusive(ids)
if isDescriptorRequired {
for _, id := range ids[numUncached:] {
if ret.LookupDescriptor(id) == nil {
return nstree.Catalog{}, wrapError(expectedType, id, requiredError(expectedType, id))
}
}
}
return ret, nil
}
// GetByNames is part of the CatalogReader interface.
func (c *cachedCatalogReader) GetByNames(
ctx context.Context, txn *kv.Txn, nameInfos []descpb.NameInfo,
) (nstree.Catalog, error) {
numUncached := 0
// Move any uncached name keys to the front of the slice.
for i, ni := range nameInfos {
if c.byNameState[ni].hasGetNamespaceEntries || c.hasScanAll {
continue
}
if id, ts := c.systemDatabaseCache.lookupDescriptorID(c.version, &ni); id != descpb.InvalidID {
c.cache.UpsertNamespaceEntry(&ni, id, ts)
s := c.byNameState[ni]
s.hasGetNamespaceEntries = true
c.setByNameState(ni, s)
continue
}
nameInfos[i], nameInfos[numUncached] = nameInfos[numUncached], ni
numUncached++
}
if numUncached > 0 && !c.hasScanAll {
uncachedNameInfos := nameInfos[:numUncached]
read, err := c.cr.GetByNames(ctx, txn, uncachedNameInfos)
if err != nil {
return nstree.Catalog{}, err
}
if err := c.ensure(ctx, read); err != nil {
return nstree.Catalog{}, err
}
for _, ni := range uncachedNameInfos {
s := c.byNameState[ni]
s.hasGetNamespaceEntries = true
c.setByNameState(ni, s)
}
}
return c.cache.FilterByNames(nameInfos), nil
}
// ensure adds descriptors, namespace, comment, zone config entries to the
// cache. This will not cause any information loss.
func (c *cachedCatalogReader) ensure(ctx context.Context, read nstree.Catalog) error {
oldSize := c.cache.ByteSize()
c.cache.AddAll(read)
c.systemDatabaseCache.update(c.version, read)
_ = read.ForEachDescriptor(func(desc catalog.Descriptor) error {
if desc.Dropped() {
return nil
}
if !desc.SkipNamespace() {
// The descriptor is expected to have a matching namespace entry.
c.cache.UpsertNamespaceEntry(desc, desc.GetID(), desc.GetModificationTime())
}
if db, ok := desc.(catalog.DatabaseDescriptor); ok {
// Database descriptors know the name -> ID mappings of their schemas.
_ = db.ForEachSchema(func(id descpb.ID, name string) error {
key := descpb.NameInfo{ParentID: db.GetID(), Name: name}
c.cache.UpsertNamespaceEntry(&key, id, desc.GetModificationTime())
return nil
})
}
return nil
})
if c.memAcc == nil {
return nil
}
return c.memAcc.Grow(ctx, c.cache.ByteSize()-oldSize)
}
func (c *cachedCatalogReader) setByIDState(id descpb.ID, s byIDStateValue) {
if c.byIDState == nil {
c.byIDState = make(map[descpb.ID]byIDStateValue)
}
c.byIDState[id] = s
}
func (c *cachedCatalogReader) setByNameState(ni descpb.NameInfo, s byNameStateValue) {
if c.byNameState == nil {
c.byNameState = make(map[descpb.NameInfo]byNameStateValue)
}
c.byNameState[ni] = s
}