-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
physical_schema_accessors.go
330 lines (299 loc) · 11.1 KB
/
physical_schema_accessors.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
// Copyright 2018 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 sql
import (
"bytes"
"context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// This file provides reference implementations of the schema accessor
// interface defined in schema_accessors.go.
//
// They are meant to be used to access stored descriptors only.
// For a higher-level implementation that also knows about
// virtual schemas, check out logical_schema_accessors.go.
//
// The following implementations are provided:
//
// - UncachedPhysicalAccessor, for uncached db accessors
//
// - CachedPhysicalAccessor, which adds an object cache
// - plugged on top another SchemaAccessor.
// - uses a `*TableCollection` (table.go) as cache.
//
// UncachedPhysicalAccessor implements direct access to DB descriptors,
// without any kind of caching.
type UncachedPhysicalAccessor struct{}
var _ SchemaAccessor = UncachedPhysicalAccessor{}
// GetDatabaseDesc implements the SchemaAccessor interface.
func (a UncachedPhysicalAccessor) GetDatabaseDesc(
ctx context.Context, txn *client.Txn, name string, flags tree.DatabaseLookupFlags,
) (desc *DatabaseDescriptor, err error) {
if name == sqlbase.SystemDB.Name {
// We can't return a direct reference to SystemDB, because the
// caller expects a private object that can be modified in-place.
sysDB := sqlbase.MakeSystemDatabaseDesc()
return &sysDB, nil
}
found, descID, err := sqlbase.LookupDatabaseID(ctx, txn, name)
if err != nil {
return nil, err
} else if !found {
if flags.Required {
return nil, sqlbase.NewUndefinedDatabaseError(name)
}
return nil, nil
}
desc = &sqlbase.DatabaseDescriptor{}
if err := getDescriptorByID(ctx, txn, descID, desc); err != nil {
return nil, err
}
return desc, nil
}
// IsValidSchema implements the SchemaAccessor interface.
func (a UncachedPhysicalAccessor) IsValidSchema(
ctx context.Context, txn *client.Txn, dbID sqlbase.ID, scName string,
) (bool, sqlbase.ID, error) {
// Try to use the system name resolution bypass. Avoids a hotspot by explicitly
// checking for public schema.
if scName == tree.PublicSchema {
return true, keys.PublicSchemaID, nil
}
sKey := sqlbase.NewSchemaKey(dbID, scName)
schemaID, err := getDescriptorID(ctx, txn, sKey)
if err != nil || schemaID == sqlbase.InvalidID {
return false, sqlbase.InvalidID, err
}
return true, schemaID, nil
}
// GetObjectNames implements the SchemaAccessor interface.
func (a UncachedPhysicalAccessor) GetObjectNames(
ctx context.Context,
txn *client.Txn,
dbDesc *DatabaseDescriptor,
scName string,
flags tree.DatabaseListFlags,
) (TableNames, error) {
ok, schemaID, err := a.IsValidSchema(ctx, txn, dbDesc.ID, scName)
if !ok || err != nil {
if flags.Required {
tn := tree.MakeTableNameWithSchema(tree.Name(dbDesc.Name), tree.Name(scName), "")
return nil, sqlbase.NewUnsupportedSchemaUsageError(tree.ErrString(&tn.TableNamePrefix))
}
return nil, nil
}
log.Eventf(ctx, "fetching list of objects for %q", dbDesc.Name)
prefix := sqlbase.NewTableKey(dbDesc.ID, schemaID, "").Key()
sr, err := txn.Scan(ctx, prefix, prefix.PrefixEnd(), 0)
if err != nil {
return nil, err
}
// We scan both the deprecated and new system.namespace table to get the
// complete list of tables. Duplicate entries may be present in both the tables,
// so we filter those out. If a duplicate entry is present, it doesn't matter
// which table it is read from -- system.namespace entries are never modified,
// they are only added/deleted. Entries are written to only one table, so
// duplicate entries must have been copied over during migration. Thus, it
// doesn't matter which table (newer/deprecated) the value is read from.
//
// It may seem counter-intuitive to read both tables if we have found data in
// the newer version. The migration copied all entries from the deprecated
// system.namespace and all new entries after the cluster version bump are added
// to the new system.namespace. Why do we do this then?
// This is to account the scenario where a table was created before
// the cluster version was bumped, but after the older system.namespace was
// copied into the newer system.namespace. Objects created in this window
// will only be present in the older system.namespace. To account for this
// scenario, we must do this filtering logic.
// TODO(whomever): This complexity can be removed in 20.2.
dprefix := sqlbase.NewDeprecatedTableKey(dbDesc.ID, "").Key()
dsr, err := txn.Scan(ctx, dprefix, dprefix.PrefixEnd(), 0)
if err != nil {
return nil, err
}
alreadySeen := make(map[string]bool)
var tableNames tree.TableNames
for _, row := range sr {
_, tableName, err := encoding.DecodeUnsafeStringAscending(bytes.TrimPrefix(
row.Key, prefix), nil)
if err != nil {
return nil, err
}
alreadySeen[tableName] = true
tn := tree.MakeTableName(tree.Name(dbDesc.Name), tree.Name(tableName))
tn.ExplicitCatalog = flags.ExplicitPrefix
tn.ExplicitSchema = flags.ExplicitPrefix
tableNames = append(tableNames, tn)
}
for _, row := range dsr {
// Decode using the deprecated key prefix.
_, tableName, err := encoding.DecodeUnsafeStringAscending(
bytes.TrimPrefix(row.Key, dprefix), nil)
if err != nil {
return nil, err
}
if alreadySeen[tableName] {
continue
}
tn := tree.MakeTableName(tree.Name(dbDesc.Name), tree.Name(tableName))
tn.ExplicitCatalog = flags.ExplicitPrefix
tn.ExplicitSchema = flags.ExplicitPrefix
tableNames = append(tableNames, tn)
}
return tableNames, nil
}
// GetObjectDesc implements the SchemaAccessor interface.
func (a UncachedPhysicalAccessor) GetObjectDesc(
ctx context.Context,
txn *client.Txn,
settings *cluster.Settings,
name *ObjectName,
flags tree.ObjectLookupFlags,
) (ObjectDescriptor, error) {
// Look up the database ID.
dbID, err := getDatabaseID(ctx, txn, name.Catalog(), flags.Required)
if err != nil || dbID == sqlbase.InvalidID {
// dbID can still be invalid if required is false and the database is not found.
return nil, err
}
ok, schemaID, err := a.IsValidSchema(ctx, txn, dbID, name.Schema())
if !ok || err != nil {
if flags.Required {
return nil, sqlbase.NewUnsupportedSchemaUsageError(tree.ErrString(name))
}
return nil, nil
}
// Try to use the system name resolution bypass. This avoids a hotspot.
// Note: we can only bypass name to ID resolution. The desc
// lookup below must still go through KV because system descriptors
// can be modified on a running cluster.
descID := sqlbase.LookupSystemTableDescriptorID(ctx, settings, dbID, name.Table())
if descID == sqlbase.InvalidID {
var found bool
found, descID, err = sqlbase.LookupObjectID(ctx, txn, dbID, schemaID, name.Table())
if err != nil {
return nil, err
}
if !found {
// KV name resolution failed.
if flags.Required {
return nil, sqlbase.NewUndefinedRelationError(name)
}
return nil, nil
}
}
// Look up the table using the discovered database descriptor.
desc := &sqlbase.TableDescriptor{}
err = getDescriptorByID(ctx, txn, descID, desc)
if err != nil {
return nil, err
}
// We have a descriptor, allow it to be in the PUBLIC or ADD state. Possibly
// OFFLINE if the relevant flag is set.
acceptableStates := map[sqlbase.TableDescriptor_State]bool{
sqlbase.TableDescriptor_ADD: true,
sqlbase.TableDescriptor_PUBLIC: true,
sqlbase.TableDescriptor_OFFLINE: flags.IncludeOffline,
}
if acceptableStates[desc.State] {
// Immediately after a RENAME an old name still points to the
// descriptor during the drain phase for the name. Do not
// return a descriptor during draining.
//
// The second or condition ensures that clusters < 20.1 access the
// system.namespace_deprecated table when selecting from system.namespace.
// As this table can not be renamed by users, it is okay that the first
// check fails.
if desc.Name == name.Table() ||
name.Table() == sqlbase.NamespaceTable.Name && name.Catalog() == sqlbase.SystemDB.Name {
if flags.RequireMutable {
return sqlbase.NewMutableExistingTableDescriptor(*desc), nil
}
return sqlbase.NewImmutableTableDescriptor(*desc), nil
}
}
return nil, nil
}
// CachedPhysicalAccessor adds a cache on top of any SchemaAccessor.
type CachedPhysicalAccessor struct {
SchemaAccessor
tc *TableCollection
}
var _ SchemaAccessor = &CachedPhysicalAccessor{}
// GetDatabaseDesc implements the SchemaAccessor interface.
func (a *CachedPhysicalAccessor) GetDatabaseDesc(
ctx context.Context, txn *client.Txn, name string, flags tree.DatabaseLookupFlags,
) (desc *DatabaseDescriptor, err error) {
isSystemDB := name == sqlbase.SystemDB.Name
if !(flags.AvoidCached || isSystemDB || testDisableTableLeases) {
refuseFurtherLookup, dbID, err := a.tc.getUncommittedDatabaseID(name, flags.Required)
if refuseFurtherLookup || err != nil {
return nil, err
}
if dbID != sqlbase.InvalidID {
// Some database ID was found in the list of uncommitted DB changes.
// Use that to get the descriptor.
desc, err := a.tc.databaseCache.getDatabaseDescByID(ctx, txn, dbID)
if desc == nil && flags.Required {
return nil, sqlbase.NewUndefinedDatabaseError(name)
}
return desc, err
}
// The database was not known in the uncommitted list. Have the db
// cache look it up by name for us.
return a.tc.databaseCache.getDatabaseDesc(ctx, a.tc.leaseMgr.db.Txn, name, flags.Required)
}
// We avoided the cache. Go lower.
return a.SchemaAccessor.GetDatabaseDesc(ctx, txn, name, flags)
}
// GetObjectDesc implements the SchemaAccessor interface.
func (a *CachedPhysicalAccessor) GetObjectDesc(
ctx context.Context,
txn *client.Txn,
settings *cluster.Settings,
name *ObjectName,
flags tree.ObjectLookupFlags,
) (ObjectDescriptor, error) {
// TODO(arul): Actually fix this to return the cached descriptor, by adding a
// schema cache to table collection. Until this is fixed, public tables with
// the same name as temporary tables might return the wrong data, as the wrong descriptor
// might be cached.
if name.Schema() != tree.PublicSchema {
phyAccessor := UncachedPhysicalAccessor{}
obj, err := phyAccessor.GetObjectDesc(ctx, txn, settings, name, flags)
if obj == nil {
return nil, err
}
if flags.RequireMutable {
return obj.(*sqlbase.MutableTableDescriptor), err
}
return obj.(*sqlbase.ImmutableTableDescriptor), err
}
if flags.RequireMutable {
table, err := a.tc.getMutableTableDescriptor(ctx, txn, name, flags)
if table == nil {
// return nil interface.
return nil, err
}
return table, err
}
table, err := a.tc.getTableVersion(ctx, txn, name, flags)
if table == nil {
// return nil interface.
return nil, err
}
return table, err
}