forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_desc.go
399 lines (348 loc) · 12.4 KB
/
database_desc.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
// Copyright 2020 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 dbdesc contains the concrete implementations of
// catalog.DatabaseDescriptor.
package dbdesc
import (
"fmt"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/multiregion"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/iterutil"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)
var _ catalog.DatabaseDescriptor = (*immutable)(nil)
var _ catalog.DatabaseDescriptor = (*Mutable)(nil)
var _ catalog.MutableDescriptor = (*Mutable)(nil)
// immutable wraps a database descriptor and provides methods
// on it.
type immutable struct {
descpb.DatabaseDescriptor
// isUncommittedVersion is set to true if this descriptor was created from
// a copy of a Mutable with an uncommitted version.
isUncommittedVersion bool
}
// Mutable wraps a database descriptor and provides methods
// on it. It can be mutated and generally has not been committed.
type Mutable struct {
immutable
ClusterVersion *immutable
}
// SafeMessage makes immutable a SafeMessager.
func (desc *immutable) SafeMessage() string {
return formatSafeMessage("dbdesc.immutable", desc)
}
// SafeMessage makes Mutable a SafeMessager.
func (desc *Mutable) SafeMessage() string {
return formatSafeMessage("dbdesc.Mutable", desc)
}
func formatSafeMessage(typeName string, desc catalog.DatabaseDescriptor) string {
var buf redact.StringBuilder
buf.Print(typeName + ": {")
catalog.FormatSafeDescriptorProperties(&buf, desc)
buf.Print("}")
return buf.String()
}
// DescriptorType returns the plain type of this descriptor.
func (desc *immutable) DescriptorType() catalog.DescriptorType {
return catalog.Database
}
// DatabaseDesc implements the Descriptor interface.
func (desc *immutable) DatabaseDesc() *descpb.DatabaseDescriptor {
return &desc.DatabaseDescriptor
}
// SetDrainingNames implements the MutableDescriptor interface.
func (desc *Mutable) SetDrainingNames(names []descpb.NameInfo) {
desc.DrainingNames = names
}
// GetParentID implements the Descriptor interface.
func (desc *immutable) GetParentID() descpb.ID {
return keys.RootNamespaceID
}
// IsUncommittedVersion implements the Descriptor interface.
func (desc *immutable) IsUncommittedVersion() bool {
return desc.isUncommittedVersion
}
// GetParentSchemaID implements the Descriptor interface.
func (desc *immutable) GetParentSchemaID() descpb.ID {
return keys.RootNamespaceID
}
// NameResolutionResult implements the Descriptor interface.
func (desc *immutable) NameResolutionResult() {}
// GetAuditMode is part of the DescriptorProto interface.
// This is a stub until per-database auditing is enabled.
func (desc *immutable) GetAuditMode() descpb.TableDescriptor_AuditMode {
return descpb.TableDescriptor_DISABLED
}
// Public implements the Descriptor interface.
func (desc *immutable) Public() bool {
return desc.State == descpb.DescriptorState_PUBLIC
}
// Adding implements the Descriptor interface.
func (desc *immutable) Adding() bool {
return false
}
// Offline implements the Descriptor interface.
func (desc *immutable) Offline() bool {
return desc.State == descpb.DescriptorState_OFFLINE
}
// Dropped implements the Descriptor interface.
func (desc *immutable) Dropped() bool {
return desc.State == descpb.DescriptorState_DROP
}
// DescriptorProto wraps a DatabaseDescriptor in a Descriptor.
func (desc *immutable) DescriptorProto() *descpb.Descriptor {
return &descpb.Descriptor{
Union: &descpb.Descriptor_Database{
Database: &desc.DatabaseDescriptor,
},
}
}
// IsMultiRegion returns whether the database has multi-region properties
// configured. If so, desc.RegionConfig can be used.
func (desc *immutable) IsMultiRegion() bool {
return desc.RegionConfig != nil
}
// MultiRegionEnumID returns the ID of the multi-region enum if the database
// is a multi-region database, and an error otherwise.
func (desc *immutable) MultiRegionEnumID() (descpb.ID, error) {
if !desc.IsMultiRegion() {
return descpb.InvalidID, errors.AssertionFailedf(
"can not get multi-region enum ID of a non multi-region database")
}
return desc.RegionConfig.RegionEnumID, nil
}
// PrimaryRegionName returns the primary region for a multi-region database.
func (desc *immutable) PrimaryRegionName() (descpb.RegionName, error) {
if !desc.IsMultiRegion() {
return "", errors.AssertionFailedf(
"can not get the primary region of a non multi-region database")
}
return desc.RegionConfig.PrimaryRegion, nil
}
// SetName sets the name on the descriptor.
func (desc *Mutable) SetName(name string) {
desc.Name = name
}
// ForEachSchemaInfo iterates f over each schema info mapping in the descriptor.
// iterutil.StopIteration is supported.
func (desc *immutable) ForEachSchemaInfo(
f func(id descpb.ID, name string, isDropped bool) error,
) error {
for name, info := range desc.Schemas {
if err := f(info.ID, name, info.Dropped); err != nil {
if iterutil.Done(err) {
return nil
}
return err
}
}
return nil
}
// GetSchemaID returns the ID in the schema mapping entry for the
// given name, 0 otherwise.
func (desc *immutable) GetSchemaID(name string) descpb.ID {
info := desc.Schemas[name]
return info.ID
}
// GetNonDroppedSchemaName returns the name in the schema mapping entry for the
// given ID, if it's not marked as dropped, empty string otherwise.
func (desc *immutable) GetNonDroppedSchemaName(schemaID descpb.ID) string {
for name, info := range desc.Schemas {
if !info.Dropped && info.ID == schemaID {
return name
}
}
return ""
}
// ValidateSelf validates that the database descriptor is well formed.
// Checks include validate the database name, and verifying that there
// is at least one read and write user.
func (desc *immutable) ValidateSelf(vea catalog.ValidationErrorAccumulator) {
// Validate local properties of the descriptor.
vea.Report(catalog.ValidateName(desc.GetName(), "descriptor"))
if desc.GetID() == descpb.InvalidID {
vea.Report(fmt.Errorf("invalid database ID %d", desc.GetID()))
}
// Validate the privilege descriptor.
vea.Report(desc.Privileges.Validate(desc.GetID(), privilege.Database))
if desc.IsMultiRegion() {
desc.validateMultiRegion(vea)
}
}
// validateMultiRegion performs checks specific to multi-region DBs.
func (desc *immutable) validateMultiRegion(vea catalog.ValidationErrorAccumulator) {
if desc.RegionConfig.PrimaryRegion == "" {
vea.Report(errors.AssertionFailedf(
"primary region unset on a multi-region db %d", desc.GetID()))
}
}
// GetReferencedDescIDs returns the IDs of all descriptors referenced by
// this descriptor, including itself.
func (desc *immutable) GetReferencedDescIDs() (catalog.DescriptorIDSet, error) {
ids := catalog.MakeDescriptorIDSet(desc.GetID())
if desc.IsMultiRegion() {
id, err := desc.MultiRegionEnumID()
if err != nil {
return catalog.DescriptorIDSet{}, err
}
ids.Add(id)
}
for _, schema := range desc.Schemas {
ids.Add(schema.ID)
}
return ids, nil
}
// ValidateCrossReferences implements the catalog.Descriptor interface.
func (desc *immutable) ValidateCrossReferences(
vea catalog.ValidationErrorAccumulator, vdg catalog.ValidationDescGetter,
) {
// Check multi-region enum type.
if enumID, err := desc.MultiRegionEnumID(); err == nil {
report := func(err error) {
vea.Report(errors.Wrap(err, "multi-region enum"))
}
typ, err := vdg.GetTypeDescriptor(enumID)
if err != nil {
report(err)
return
}
if typ.GetParentID() != desc.GetID() {
report(errors.Errorf("parentID is actually %d", typ.GetParentID()))
}
// Further validation should be handled by the type descriptor itself.
}
}
// ValidateTxnCommit implements the catalog.Descriptor interface.
func (desc *immutable) ValidateTxnCommit(
vea catalog.ValidationErrorAccumulator, vdg catalog.ValidationDescGetter,
) {
// Check schema references.
// This could be done in ValidateCrossReferences but it can be quite expensive
// so we do it here instead.
for schemaName, schemaInfo := range desc.Schemas {
if schemaInfo.Dropped {
continue
}
report := func(err error) {
vea.Report(errors.Wrapf(err, "schema mapping entry %q (%d)",
errors.Safe(schemaName), schemaInfo.ID))
}
schemaDesc, err := vdg.GetSchemaDescriptor(schemaInfo.ID)
if err != nil {
report(err)
continue
}
if schemaDesc.GetName() != schemaName {
report(errors.Errorf("schema name is actually %q", errors.Safe(schemaDesc.GetName())))
}
if schemaDesc.GetParentID() != desc.GetID() {
report(errors.Errorf("schema parentID is actually %d", schemaDesc.GetParentID()))
}
}
}
// SchemaMeta implements the tree.SchemaMeta interface.
// TODO (rohany): I don't want to keep this here, but it seems to be used
// by backup only for the fake resolution that occurs in backup. Is it possible
// to have this implementation only visible there? Maybe by creating a type
// alias for database descriptor in the backupccl package, and then defining
// SchemaMeta on it?
func (desc *immutable) SchemaMeta() {}
// MaybeIncrementVersion implements the MutableDescriptor interface.
func (desc *Mutable) MaybeIncrementVersion() {
// Already incremented, no-op.
if desc.ClusterVersion == nil || desc.Version == desc.ClusterVersion.Version+1 {
return
}
desc.Version++
desc.ModificationTime = hlc.Timestamp{}
}
// OriginalName implements the MutableDescriptor interface.
func (desc *Mutable) OriginalName() string {
if desc.ClusterVersion == nil {
return ""
}
return desc.ClusterVersion.Name
}
// OriginalID implements the MutableDescriptor interface.
func (desc *Mutable) OriginalID() descpb.ID {
if desc.ClusterVersion == nil {
return descpb.InvalidID
}
return desc.ClusterVersion.ID
}
// OriginalVersion implements the MutableDescriptor interface.
func (desc *Mutable) OriginalVersion() descpb.DescriptorVersion {
if desc.ClusterVersion == nil {
return 0
}
return desc.ClusterVersion.Version
}
// ImmutableCopy implements the MutableDescriptor interface.
func (desc *Mutable) ImmutableCopy() catalog.Descriptor {
imm := NewBuilder(desc.DatabaseDesc()).BuildImmutableDatabase()
imm.(*immutable).isUncommittedVersion = desc.IsUncommittedVersion()
return imm
}
// IsNew implements the MutableDescriptor interface.
func (desc *Mutable) IsNew() bool {
return desc.ClusterVersion == nil
}
// IsUncommittedVersion implements the Descriptor interface.
func (desc *Mutable) IsUncommittedVersion() bool {
return desc.IsNew() || desc.GetVersion() != desc.ClusterVersion.GetVersion()
}
// SetPublic implements the MutableDescriptor interface.
func (desc *Mutable) SetPublic() {
desc.State = descpb.DescriptorState_PUBLIC
desc.OfflineReason = ""
}
// SetDropped implements the MutableDescriptor interface.
func (desc *Mutable) SetDropped() {
desc.State = descpb.DescriptorState_DROP
desc.OfflineReason = ""
}
// SetOffline implements the MutableDescriptor interface.
func (desc *Mutable) SetOffline(reason string) {
desc.State = descpb.DescriptorState_OFFLINE
desc.OfflineReason = reason
}
// AddDrainingName adds a draining name to the DatabaseDescriptor's slice of
// draining names.
func (desc *Mutable) AddDrainingName(name descpb.NameInfo) {
desc.DrainingNames = append(desc.DrainingNames, name)
}
// UnsetMultiRegionConfig removes the stored multi-region config from the
// database descriptor.
func (desc *Mutable) UnsetMultiRegionConfig() {
desc.RegionConfig = nil
}
// SetInitialMultiRegionConfig initializes and sets a RegionConfig on a database
// descriptor. It returns an error if a RegionConfig already exists.
func (desc *Mutable) SetInitialMultiRegionConfig(config *multiregion.RegionConfig) error {
// We only should be doing this for the initial multi-region configuration.
if desc.RegionConfig != nil {
return errors.AssertionFailedf(
"expected no region config on database %q with ID %d",
desc.GetName(),
desc.GetID(),
)
}
desc.RegionConfig = &descpb.DatabaseDescriptor_RegionConfig{
SurvivalGoal: config.SurvivalGoal(),
PrimaryRegion: config.PrimaryRegion(),
RegionEnumID: config.RegionEnumID(),
}
return nil
}