-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathrowfetcher_cache.go
385 lines (347 loc) · 12.3 KB
/
rowfetcher_cache.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
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package cdcevent
import (
"context"
"time"
"github.com/cockroachdb/cockroach/pkg/ccl/changefeedccl/changefeedbase"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"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/catalog/fetchpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/lease"
"github.com/cockroachdb/cockroach/pkg/sql/row"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/cache"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// RowFetcherTraceKVLogFrequency controls how frequently KVs are logged when
// KV tracing is enabled.
var traceKVLogFrequency = settings.RegisterDurationSetting(
settings.ApplicationLevel,
"changefeed.cdcevent.trace_kv.log_frequency",
"controls how frequently KVs are logged when KV tracing is enabled",
500*time.Millisecond,
settings.NonNegativeDuration,
)
// rowFetcherCache maintains a cache of single table row.Fetchers. Given a key
// with an MVCC timestamp, it retrieves the correct TableDescriptor for that key
// and returns a row.Fetcher initialized with that table. This Fetcher's
// ConsumeKVProvider() can be used to turn that key (or all the keys making up
// the column families of one row) into a row.
type rowFetcherCache struct {
codec keys.SQLCodec
descFetcher tableDescFetcher
fetchers *cache.UnorderedCache
watchedFamilies map[watchedFamily]struct{}
rfArgs rowFetcherArgs
a tree.DatumAlloc
}
type tableDescFetcher interface {
// FetchTableDesc returns the TableDescriptor for the gven descriptor ID at the given timestamp.
FetchTableDesc(context.Context, descpb.ID, hlc.Timestamp) (catalog.TableDescriptor, error)
}
// dbTableDescFetcher is a tableDescFetcher that fetches table
// descriptors from the underlying descriptor collection and DB.
type dbTableDescFetcher struct {
leaseMgr *lease.Manager
collection *descs.Collection
db *kv.DB
}
func (f *dbTableDescFetcher) FetchTableDesc(
ctx context.Context, tableID descpb.ID, ts hlc.Timestamp,
) (catalog.TableDescriptor, error) {
// Retrieve the target TableDescriptor from the lease manager. No caching
// is attempted because the lease manager does its own caching.
desc, err := f.leaseMgr.Acquire(ctx, ts, tableID)
if err != nil {
// Manager can return all kinds of errors during chaos, but based on
// its usage, none of them should ever be terminal.
return nil, changefeedbase.MarkRetryableError(err)
}
tableDesc := desc.Underlying().(catalog.TableDescriptor)
// Immediately release the lease, since we only need it for the exact
// timestamp requested.
desc.Release(ctx)
if tableDesc.MaybeRequiresTypeHydration() {
tableDesc, err = refreshUDT(ctx, tableID, f.db, f.collection, ts)
if err != nil {
return nil, err
}
}
return tableDesc, nil
}
func refreshUDT(
ctx context.Context, tableID descpb.ID, db *kv.DB, collection *descs.Collection, ts hlc.Timestamp,
) (tableDesc catalog.TableDescriptor, err error) {
// If the table contains user defined types, then use the
// descs.Collection to retrieve a TableDescriptor with type metadata
// hydrated. We open a transaction here only because the
// descs.Collection needs one to get a read timestamp. We do this lookup
// again behind a conditional to avoid allocating any transaction
// metadata if the table has user defined types. This can be bypassed
// once (#53751) is fixed. Once the descs.Collection can take in a read
// timestamp rather than a whole transaction, we can use the
// descs.Collection directly here.
// TODO (SQL Schema): #53751.
if err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
err := txn.SetFixedTimestamp(ctx, ts)
if err != nil {
return err
}
tableDesc, err = collection.ByIDWithLeased(txn).WithoutNonPublic().Get().Table(ctx, tableID)
return err
}); err != nil {
if errors.Is(err, catalog.ErrDescriptorDropped) {
// Dropped descriptors are a bad news.
return nil, changefeedbase.WithTerminalError(err)
}
// Manager can return all kinds of errors during chaos, but based on
// its usage, none of them should ever be terminal.
return nil, changefeedbase.MarkRetryableError(err)
}
// Immediately release the lease, since we only need it for the exact
// timestamp requested.
collection.ReleaseAll(ctx)
return tableDesc, nil
}
// rowFetcherArgs contains arguments to pass to all row fetchers
// created by this cache.
type rowFetcherArgs struct {
traceKV bool
traceKVLogFrequency time.Duration
}
type cachedFetcher struct {
tableDesc catalog.TableDescriptor
fetcher row.Fetcher
familyDesc descpb.ColumnFamilyDescriptor
skip bool
}
type watchedFamily struct {
tableID descpb.ID
familyName string
}
// newRowFetcherCache constructs row fetcher cache.
func newRowFetcherCache(
ctx context.Context,
codec keys.SQLCodec,
leaseMgr *lease.Manager,
cf *descs.CollectionFactory,
db *kv.DB,
s *cluster.Settings,
targets changefeedbase.Targets,
) (*rowFetcherCache, error) {
watchedFamilies, err := watchedFamilesFromTarget(targets)
if err != nil {
return nil, err
}
return &rowFetcherCache{
codec: codec,
descFetcher: &dbTableDescFetcher{
leaseMgr: leaseMgr,
db: db,
collection: cf.NewCollection(ctx),
},
fetchers: cache.NewUnorderedCache(DefaultCacheConfig),
watchedFamilies: watchedFamilies,
rfArgs: rowFetcherArgs{
traceKV: log.V(row.TraceKVVerbosity),
traceKVLogFrequency: traceKVLogFrequency.Get(&s.SV),
},
}, nil
}
func watchedFamilesFromTarget(targets changefeedbase.Targets) (map[watchedFamily]struct{}, error) {
if targets.Size == 0 {
return nil, errors.AssertionFailedf("Expected at least one target, found 0")
}
watchedFamilies := make(map[watchedFamily]struct{}, targets.Size)
err := targets.EachTarget(func(t changefeedbase.Target) error {
watchedFamilies[watchedFamily{tableID: t.TableID, familyName: t.FamilyName}] = struct{}{}
return nil
})
if err != nil {
return nil, err
}
if len(watchedFamilies) == 0 {
return nil, errors.AssertionFailedf("No watched families resulted from %+v", targets)
}
return watchedFamilies, nil
}
func (c *rowFetcherCache) tableDescForKey(
ctx context.Context, key roachpb.Key, ts hlc.Timestamp,
) (catalog.TableDescriptor, descpb.FamilyID, error) {
key, err := c.codec.StripTenantPrefix(key)
if err != nil {
return nil, descpb.FamilyID(0), err
}
remaining, tableID, _, err := rowenc.DecodePartialTableIDIndexID(key)
if err != nil {
return nil, descpb.FamilyID(0), err
}
familyID, err := keys.DecodeFamilyKey(key)
if err != nil {
return nil, descpb.FamilyID(0), err
}
family := descpb.FamilyID(familyID)
tableDesc, err := c.descFetcher.FetchTableDesc(ctx, tableID, ts)
if err != nil {
return nil, family, err
}
// Skip over the column data.
for skippedCols := 0; skippedCols < tableDesc.GetPrimaryIndex().NumKeyColumns(); skippedCols++ {
l, err := encoding.PeekLength(remaining)
if err != nil {
return nil, family, err
}
remaining = remaining[l:]
}
return tableDesc, family, nil
}
// ErrUnwatchedFamily is a sentinel error that indicates this part of the row
// is not being watched and does not need to be decoded.
var ErrUnwatchedFamily = errors.New("watched table but unwatched family")
// RowFetcherForColumnFamily returns row.Fetcher for the specified column family.
// Returns ErrUnwatchedFamily error if family is not watched.
func (c *rowFetcherCache) RowFetcherForColumnFamily(
tableDesc catalog.TableDescriptor,
family descpb.FamilyID,
sysCols []descpb.ColumnDescriptor,
keyOnly bool,
) (*row.Fetcher, *descpb.ColumnFamilyDescriptor, error) {
idVer := CacheKey{ID: tableDesc.GetID(), Version: tableDesc.GetVersion(), FamilyID: family}
if v, ok := c.fetchers.Get(idVer); ok {
f := v.(*cachedFetcher)
if f.skip {
return nil, nil, ErrUnwatchedFamily
}
// Ensure that all user defined types are up to date with the cached
// version and the desired version to use the cache. It is safe to use
// UserDefinedTypeColsHaveSameVersion if we have a hit because we are
// guaranteed that the tables have the same version. Additionally, these
// fetchers are always initialized with a single tabledesc.Get.
if safe, err := catalog.UserDefinedTypeColsInFamilyHaveSameVersion(tableDesc, f.tableDesc, family); err != nil {
return nil, nil, err
} else if safe {
return &f.fetcher, &f.familyDesc, nil
}
}
familyDesc, err := catalog.MustFindFamilyByID(tableDesc, family)
if err != nil {
return nil, nil, err
}
f := &cachedFetcher{
tableDesc: tableDesc,
familyDesc: *familyDesc,
}
rf := &f.fetcher
_, wholeTableWatched := c.watchedFamilies[watchedFamily{tableID: tableDesc.GetID()}]
if !wholeTableWatched {
_, familyWatched := c.watchedFamilies[watchedFamily{tableID: tableDesc.GetID(), familyName: familyDesc.Name}]
if !familyWatched {
f.skip = true
return nil, nil, ErrUnwatchedFamily
}
}
var spec fetchpb.IndexFetchSpec
var relevantColumns descpb.ColumnIDs
if keyOnly {
relevantColumns = tableDesc.GetPrimaryIndex().CollectKeyColumnIDs().Ordered()
} else {
relevantColumns, err = getRelevantColumnsForFamily(tableDesc, familyDesc)
}
if err != nil {
return nil, nil, err
}
if err := rowenc.InitIndexFetchSpec(
&spec, c.codec, tableDesc, tableDesc.GetPrimaryIndex(), relevantColumns,
); err != nil {
return nil, nil, err
}
// Add system columns.
for _, sc := range sysCols {
spec.FetchedColumns = append(spec.FetchedColumns, fetchpb.IndexFetchSpec_Column{
ColumnID: sc.ID,
Name: sc.Name,
Type: sc.Type,
IsNonNullable: !sc.Nullable,
})
}
if err := rf.Init(
context.TODO(),
row.FetcherInitArgs{
WillUseKVProvider: true,
Alloc: &c.a,
Spec: &spec,
TraceKV: c.rfArgs.traceKV,
TraceKVEvery: &util.EveryN{N: c.rfArgs.traceKVLogFrequency},
},
); err != nil {
return nil, nil, err
}
c.fetchers.Add(idVer, f)
return rf, familyDesc, nil
}
// fixedDescFetcher is a tableDescFetcher that returns descriptors from a given
// fixed set of descriptors.
type fixedDescFetcher struct {
descCol map[descpb.ID]catalog.TableDescriptor
}
// FetchTableDesc implements tableDescFetcher. Note that the timestamp is
// currently ignored.
func (f *fixedDescFetcher) FetchTableDesc(
_ context.Context, id descpb.ID, _ hlc.Timestamp,
) (catalog.TableDescriptor, error) {
if tableDesc, ok := f.descCol[id]; ok {
return tableDesc, nil
} else {
return nil, errors.Newf("could not find descriptor for %d in fixed descriptor set", id)
}
}
// newFixedRowFetcherCache constructs row fetcher cache that uses only the fixed
// set of descriptors provided.
//
// TODO(ssd): This may not be necessary into the future if we push this logic
// down into the descriptor collection exactly.
//
// TODO(ssd): If we do keep this, what we likely want here is to inject a
// descFetcher that can be served off of a feed of schema updates from a
// rnagefeed and which respects timestamps.
//
// TODO(ssd): Right now we take a collection of TableDescriptors. I don't think
// this correctly supports UDTs at the moment.
func NewFixedRowFetcherCache(
ctx context.Context,
codec keys.SQLCodec,
s *cluster.Settings,
targets changefeedbase.Targets,
descs map[descpb.ID]catalog.TableDescriptor,
) (*rowFetcherCache, error) {
watchedFamilies, err := watchedFamilesFromTarget(targets)
if err != nil {
return nil, err
}
return &rowFetcherCache{
codec: codec,
descFetcher: &fixedDescFetcher{
descCol: descs,
},
fetchers: cache.NewUnorderedCache(DefaultCacheConfig),
watchedFamilies: watchedFamilies,
rfArgs: rowFetcherArgs{
traceKV: log.V(row.TraceKVVerbosity),
traceKVLogFrequency: traceKVLogFrequency.Get(&s.SV),
},
}, nil
}