-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
descriptor.go
327 lines (302 loc) · 9.92 KB
/
descriptor.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
// Copyright 2016 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 (
"context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
//
// This file contains routines for low-level access to stored
// descriptors.
//
// For higher levels in the SQL layer, these interface are likely not
// suitable; consider instead schema_accessors.go and resolver.go.
//
var (
errEmptyDatabaseName = pgerror.New(pgcode.Syntax, "empty database name")
errNoDatabase = pgerror.New(pgcode.InvalidName, "no database specified")
errNoTable = pgerror.New(pgcode.InvalidName, "no table specified")
errNoMatch = pgerror.New(pgcode.UndefinedObject, "no object matched")
)
// GenerateUniqueDescID returns the next available Descriptor ID and increments
// the counter. The incrementing is non-transactional, and the counter could be
// incremented multiple times because of retries.
func GenerateUniqueDescID(ctx context.Context, db *client.DB) (sqlbase.ID, error) {
// Increment unique descriptor counter.
newVal, err := client.IncrementValRetryable(ctx, db, keys.DescIDGenerator, 1)
if err != nil {
return sqlbase.InvalidID, err
}
return sqlbase.ID(newVal - 1), nil
}
// createdatabase takes Database descriptor and creates it if needed,
// incrementing the descriptor counter. Returns true if the descriptor
// is actually created, false if it already existed, or an error if one was
// encountered. The ifNotExists flag is used to declare if the "already existed"
// state should be an error (false) or a no-op (true).
// createDatabase implements the DatabaseDescEditor interface.
func (p *planner) createDatabase(
ctx context.Context, desc *sqlbase.DatabaseDescriptor, ifNotExists bool,
) (bool, error) {
plainKey := sqlbase.NewDatabaseKey(desc.Name)
idKey := plainKey.Key()
if exists, err := descExists(ctx, p.txn, idKey); err == nil && exists {
if ifNotExists {
// Noop.
return false, nil
}
return false, sqlbase.NewDatabaseAlreadyExistsError(plainKey.Name())
} else if err != nil {
return false, err
}
id, err := GenerateUniqueDescID(ctx, p.ExecCfg().DB)
if err != nil {
return false, err
}
return true, p.createDescriptorWithID(ctx, idKey, id, desc, nil)
}
func descExists(ctx context.Context, txn *client.Txn, idKey roachpb.Key) (bool, error) {
// Check whether idKey exists.
gr, err := txn.Get(ctx, idKey)
if err != nil {
return false, err
}
return gr.Exists(), nil
}
func (p *planner) createDescriptorWithID(
ctx context.Context,
idKey roachpb.Key,
id sqlbase.ID,
descriptor sqlbase.DescriptorProto,
st *cluster.Settings,
) error {
descriptor.SetID(id)
// TODO(pmattis): The error currently returned below is likely going to be
// difficult to interpret.
//
// TODO(pmattis): Need to handle if-not-exists here as well.
//
// TODO(pmattis): This is writing the namespace and descriptor table entries,
// but not going through the normal INSERT logic and not performing a precise
// mimicry. In particular, we're only writing a single key per table, while
// perfect mimicry would involve writing a sentinel key for each row as well.
b := &client.Batch{}
descID := descriptor.GetID()
if p.ExtendedEvalContext().Tracing.KVTracingEnabled() {
log.VEventf(ctx, 2, "CPut %s -> %d", idKey, descID)
}
b.CPut(idKey, descID, nil)
if err := WriteNewDescToBatch(ctx, p.ExtendedEvalContext().Tracing.KVTracingEnabled(), st, b, descID, descriptor); err != nil {
return err
}
mutDesc, isTable := descriptor.(*sqlbase.MutableTableDescriptor)
if isTable {
if err := mutDesc.ValidateTable(); err != nil {
return err
}
if err := p.Tables().addUncommittedTable(*mutDesc); err != nil {
return err
}
}
if err := p.txn.Run(ctx, b); err != nil {
return err
}
if isTable && mutDesc.Adding() {
p.queueSchemaChange(mutDesc.TableDesc(), sqlbase.InvalidMutationID)
}
return nil
}
// getDescriptorID looks up the ID for plainKey.
// InvalidID is returned if the name cannot be resolved.
func getDescriptorID(
ctx context.Context, txn *client.Txn, plainKey sqlbase.DescriptorKey,
) (sqlbase.ID, error) {
key := plainKey.Key()
log.Eventf(ctx, "looking up descriptor ID for name key %q", key)
gr, err := txn.Get(ctx, key)
if err != nil {
return sqlbase.InvalidID, err
}
if !gr.Exists() {
return sqlbase.InvalidID, nil
}
return sqlbase.ID(gr.ValueInt()), nil
}
// getDescriptorByID looks up the descriptor for `id`, validates it,
// and unmarshals it into `descriptor`.
//
// In most cases you'll want to use wrappers: `getDatabaseDescByID` or
// `getTableDescByID`.
func getDescriptorByID(
ctx context.Context, txn *client.Txn, id sqlbase.ID, descriptor sqlbase.DescriptorProto,
) error {
log.Eventf(ctx, "fetching descriptor with ID %d", id)
descKey := sqlbase.MakeDescMetadataKey(id)
desc := &sqlbase.Descriptor{}
ts, err := txn.GetProtoTs(ctx, descKey, desc)
if err != nil {
return err
}
switch t := descriptor.(type) {
case *sqlbase.TableDescriptor:
table := desc.Table(ts)
if table == nil {
return pgerror.Newf(pgcode.WrongObjectType,
"%q is not a table", desc.String())
}
if err := table.MaybeFillInDescriptor(ctx, txn); err != nil {
return nil
}
if err := table.Validate(ctx, txn); err != nil {
return err
}
*t = *table
case *sqlbase.DatabaseDescriptor:
database := desc.GetDatabase()
if database == nil {
return pgerror.Newf(pgcode.WrongObjectType,
"%q is not a database", desc.String())
}
if err := database.Validate(); err != nil {
return err
}
*t = *database
}
return nil
}
// GetAllDescriptors looks up and returns all available descriptors.
func GetAllDescriptors(ctx context.Context, txn *client.Txn) ([]sqlbase.DescriptorProto, error) {
log.Eventf(ctx, "fetching all descriptors")
descsKey := sqlbase.MakeAllDescsMetadataKey()
kvs, err := txn.Scan(ctx, descsKey, descsKey.PrefixEnd(), 0)
if err != nil {
return nil, err
}
descs := make([]sqlbase.DescriptorProto, 0, len(kvs))
for _, kv := range kvs {
desc := &sqlbase.Descriptor{}
if err := kv.ValueProto(desc); err != nil {
return nil, err
}
switch t := desc.Union.(type) {
case *sqlbase.Descriptor_Table:
table := desc.Table(kv.Value.Timestamp)
if err := table.MaybeFillInDescriptor(ctx, txn); err != nil {
return nil, err
}
descs = append(descs, table)
case *sqlbase.Descriptor_Database:
descs = append(descs, desc.GetDatabase())
default:
return nil, errors.AssertionFailedf("Descriptor.Union has unexpected type %T", t)
}
}
return descs, nil
}
// GetAllDatabaseDescriptorIDs looks up and returns all available database
// descriptor IDs.
func GetAllDatabaseDescriptorIDs(ctx context.Context, txn *client.Txn) ([]sqlbase.ID, error) {
log.Eventf(ctx, "fetching all database descriptor IDs")
nameKey := sqlbase.MakeNameMetadataKey(keys.RootNamespaceID, "" /* name */)
kvs, err := txn.Scan(ctx, nameKey, nameKey.PrefixEnd(), 0 /*maxRows */)
if err != nil {
return nil, err
}
descIDs := make([]sqlbase.ID, 0, len(kvs))
for _, kv := range kvs {
descIDs = append(descIDs, sqlbase.ID(kv.ValueInt()))
}
return descIDs, nil
}
// writeDescToBatch adds a Put command writing a descriptor proto to the
// descriptors table. It writes the descriptor desc at the id descID. If kvTrace
// is enabled, it will log an event explaining the put that was performed.
func writeDescToBatch(
ctx context.Context,
kvTrace bool,
s *cluster.Settings,
b *client.Batch,
descID sqlbase.ID,
desc sqlbase.DescriptorProto,
) (err error) {
var tableToDowngrade *TableDescriptor
switch d := desc.(type) {
case *TableDescriptor:
tableToDowngrade = d
case *MutableTableDescriptor:
tableToDowngrade = d.TableDesc()
case *DatabaseDescriptor:
default:
return errors.AssertionFailedf("unexpected proto type %T", desc)
}
if tableToDowngrade != nil {
didDowngrade, downgraded, err := tableToDowngrade.MaybeDowngradeForeignKeyRepresentation(ctx, s)
if err != nil {
return err
}
if didDowngrade {
desc = downgraded
}
}
descKey := sqlbase.MakeDescMetadataKey(descID)
descDesc := sqlbase.WrapDescriptor(desc)
if kvTrace {
log.VEventf(ctx, 2, "Put %s -> %s", descKey, descDesc)
}
b.Put(descKey, descDesc)
return nil
}
// WriteNewDescToBatch adds a CPut command writing a descriptor proto to the
// descriptors table. It writes the descriptor desc at the id descID, asserting
// that there was no previous descriptor at that id present already. If kvTrace
// is enabled, it will log an event explaining the CPut that was performed.
func WriteNewDescToBatch(
ctx context.Context,
kvTrace bool,
s *cluster.Settings,
b *client.Batch,
tableID sqlbase.ID,
desc sqlbase.DescriptorProto,
) (err error) {
var tableToDowngrade *TableDescriptor
switch d := desc.(type) {
case *TableDescriptor:
tableToDowngrade = d
case *MutableTableDescriptor:
tableToDowngrade = d.TableDesc()
case *DatabaseDescriptor:
default:
return errors.AssertionFailedf("unexpected proto type %T", desc)
}
if tableToDowngrade != nil {
didDowngrade, downgraded, err := tableToDowngrade.MaybeDowngradeForeignKeyRepresentation(ctx, s)
if err != nil {
return err
}
if didDowngrade {
desc = downgraded
}
}
descKey := sqlbase.MakeDescMetadataKey(tableID)
descDesc := sqlbase.WrapDescriptor(desc)
if kvTrace {
log.VEventf(ctx, 2, "CPut %s -> %s", descKey, descDesc)
}
b.CPut(descKey, descDesc, nil)
return nil
}