-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptor.go
294 lines (266 loc) · 9.09 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
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package sql
import (
"fmt"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
var (
errEmptyDatabaseName = errors.New("empty database name")
errNoDatabase = errors.New("no database specified")
errNoTable = errors.New("no table specified")
)
// DescriptorAccessor provides helper methods for using descriptors
// to SQL objects.
type DescriptorAccessor interface {
// createDescriptor takes a Table or 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).
createDescriptor(
ctx context.Context,
plainKey sqlbase.DescriptorKey,
descriptor sqlbase.DescriptorProto,
ifNotExists bool,
) (bool, error)
}
var _ DescriptorAccessor = &planner{}
type descriptorAlreadyExistsErr struct {
desc sqlbase.DescriptorProto
name string
}
func (d descriptorAlreadyExistsErr) Error() string {
return fmt.Sprintf("%s %q already exists", d.desc.TypeName(), d.name)
}
// 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 0, err
}
return sqlbase.ID(newVal - 1), nil
}
// createDescriptor implements the DescriptorAccessor interface.
func (p *planner) createDescriptor(
ctx context.Context,
plainKey sqlbase.DescriptorKey,
descriptor sqlbase.DescriptorProto,
ifNotExists bool,
) (bool, error) {
idKey := plainKey.Key()
if exists, err := descExists(ctx, p.txn, idKey); err == nil && exists {
if ifNotExists {
// Noop.
return false, nil
}
// Key exists, but we don't want it to: error out.
switch descriptor.TypeName() {
case "database":
return false, sqlbase.NewDatabaseAlreadyExistsError(plainKey.Name())
case "table", "view":
return false, sqlbase.NewRelationAlreadyExistsError(plainKey.Name())
default:
return false, descriptorAlreadyExistsErr{descriptor, plainKey.Name()}
}
} else if err != nil {
return false, err
}
id, err := GenerateUniqueDescID(ctx, p.session.execCfg.DB)
if err != nil {
return false, err
}
return true, p.createDescriptorWithID(ctx, idKey, id, descriptor)
}
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,
) 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.
descKey := sqlbase.MakeDescMetadataKey(descriptor.GetID())
b := &client.Batch{}
descID := descriptor.GetID()
descDesc := sqlbase.WrapDescriptor(descriptor)
if p.session.Tracing.KVTracingEnabled() {
log.VEventf(ctx, 2, "CPut %s -> %d", idKey, descID)
log.VEventf(ctx, 2, "CPut %s -> %s", descKey, descDesc)
}
b.CPut(idKey, descID, nil)
b.CPut(descKey, descDesc, nil)
p.session.setTestingVerifyMetadata(func(systemConfig config.SystemConfig) error {
if err := expectDescriptorID(systemConfig, idKey, descID); err != nil {
return err
}
return expectDescriptor(systemConfig, descKey, descDesc)
})
if desc, ok := descriptor.(*sqlbase.TableDescriptor); ok {
p.session.tables.addUncommittedTable(*desc)
}
return p.txn.Run(ctx, b)
}
// getDescriptor looks up the descriptor for `plainKey`, validates it,
// and unmarshals it into `descriptor`.
//
// If `plainKey` doesn't exist, returns false and nil error.
// In most cases you'll want to use wrappers: `getDatabaseDesc` or
// `getTableDesc`.
func getDescriptor(
ctx context.Context,
txn *client.Txn,
plainKey sqlbase.DescriptorKey,
descriptor sqlbase.DescriptorProto,
) (bool, error) {
gr, err := txn.Get(ctx, plainKey.Key())
if err != nil {
return false, err
}
if !gr.Exists() {
return false, nil
}
return getDescriptorByID(ctx, txn, sqlbase.ID(gr.ValueInt()), descriptor)
}
// 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,
) (bool, error) {
descKey := sqlbase.MakeDescMetadataKey(id)
desc := &sqlbase.Descriptor{}
if err := txn.GetProto(ctx, descKey, desc); err != nil {
return false, err
}
switch t := descriptor.(type) {
case *sqlbase.TableDescriptor:
table := desc.GetTable()
if table == nil {
return false, errors.Errorf("%q is not a table", desc.String())
}
table.MaybeUpgradeFormatVersion()
// TODO(dan): Write the upgraded TableDescriptor back to kv. This will break
// the ability to use a previous version of cockroach with the on-disk data,
// but it's worth it to avoid having to do the upgrade every time the
// descriptor is fetched. Our current test for this enforces compatibility
// backward and forward, so that'll have to be extended before this is done.
if err := table.Validate(ctx, txn); err != nil {
return false, err
}
*t = *table
case *sqlbase.DatabaseDescriptor:
database := desc.GetDatabase()
if database == nil {
return false, errors.Errorf("%q is not a database", desc.String())
}
if err := database.Validate(); err != nil {
return false, err
}
*t = *database
}
return true, nil
}
// getAllDescriptors looks up and returns all available descriptors.
func getAllDescriptors(ctx context.Context, txn *client.Txn) ([]sqlbase.DescriptorProto, error) {
descsKey := sqlbase.MakeAllDescsMetadataKey()
kvs, err := txn.Scan(ctx, descsKey, descsKey.PrefixEnd(), 0)
if err != nil {
return nil, err
}
descs := make([]sqlbase.DescriptorProto, len(kvs))
for i, 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:
descs[i] = desc.GetTable()
case *sqlbase.Descriptor_Database:
descs[i] = desc.GetDatabase()
default:
return nil, errors.Errorf("Descriptor.Union has unexpected type %T", t)
}
}
return descs, nil
}
// getDescriptorsFromTargetList fetches the descriptors for the targets.
func getDescriptorsFromTargetList(
ctx context.Context, txn *client.Txn, vt VirtualTabler, db string, targets parser.TargetList,
) ([]sqlbase.DescriptorProto, error) {
if targets.Databases != nil {
if len(targets.Databases) == 0 {
return nil, errNoDatabase
}
descs := make([]sqlbase.DescriptorProto, 0, len(targets.Databases))
for _, database := range targets.Databases {
descriptor, err := MustGetDatabaseDesc(ctx, txn, vt, string(database))
if err != nil {
return nil, err
}
descs = append(descs, descriptor)
}
return descs, nil
}
if len(targets.Tables) == 0 {
return nil, errNoTable
}
descs := make([]sqlbase.DescriptorProto, 0, len(targets.Tables))
for _, tableTarget := range targets.Tables {
tableGlob, err := tableTarget.NormalizeTablePattern()
if err != nil {
return nil, err
}
tables, err := expandTableGlob(ctx, txn, vt, db, tableGlob)
if err != nil {
return nil, err
}
for i := range tables {
descriptor, err := MustGetTableOrViewDesc(
ctx, txn, vt, &tables[i], true /*allowAdding*/)
if err != nil {
return nil, err
}
descs = append(descs, descriptor)
}
}
return descs, nil
}