-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathvalidate.go
145 lines (135 loc) · 4.82 KB
/
validate.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
// Copyright 2021 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 descs
import (
"context"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate"
"github.com/cockroachdb/errors"
)
// Validate returns any descriptor validation errors after validating using the
// descriptor collection for retrieving referenced descriptors and namespace
// entries, if applicable.
func (tc *Collection) Validate(
ctx context.Context,
txn *kv.Txn,
telemetry catalog.ValidationTelemetry,
targetLevel catalog.ValidationLevel,
descriptors ...catalog.Descriptor,
) (err error) {
vd := tc.newValidationDereferencer(txn)
version := tc.settings.Version.ActiveVersion(ctx)
return validate.Validate(
ctx,
version,
vd,
telemetry,
targetLevel,
descriptors...).CombinedError()
}
// ValidateUncommittedDescriptors validates all uncommitted descriptors.
// Validation includes cross-reference checks. Referenced descriptors are
// read from the store unless they happen to also be part of the uncommitted
// descriptor set. We purposefully avoid using leased descriptors as those may
// be one version behind, in which case it's possible (and legitimate) that
// those are missing back-references which would cause validation to fail.
// Optionally, the zone config will be validated if validateZoneConfigs is
// set to true.
func (tc *Collection) ValidateUncommittedDescriptors(
ctx context.Context,
txn *kv.Txn,
validateZoneConfigs bool,
zoneConfigValidator ZoneConfigValidator,
) (err error) {
if tc.skipValidationOnWrite || !ValidateOnWriteEnabled.Get(&tc.settings.SV) {
return nil
}
var descs []catalog.Descriptor
_ = tc.uncommitted.iterateUncommittedByID(func(desc catalog.Descriptor) error {
descs = append(descs, desc)
return nil
})
if len(descs) == 0 {
return nil
}
if err := tc.Validate(ctx, txn, catalog.ValidationWriteTelemetry, validate.Write, descs...); err != nil {
return err
}
// Next validate any zone configs that may have been modified
// in the descriptor set, only if this type of validation is required.
// We only do this type of validation if region configs are modified.
if validateZoneConfigs {
if zoneConfigValidator == nil {
return errors.AssertionFailedf("zone config validator is required to " +
"validate zone configs")
}
for _, desc := range descs {
switch t := desc.(type) {
case catalog.DatabaseDescriptor:
if err = zoneConfigValidator.ValidateDbZoneConfig(ctx, t); err != nil {
return err
}
}
}
}
return nil
}
func (tc *Collection) newValidationDereferencer(txn *kv.Txn) validate.ValidationDereferencer {
return &collectionBackedDereferencer{tc: tc, sd: tc.stored.NewValidationDereferencer(txn)}
}
// collectionBackedDereferencer wraps a Collection to implement the
// validate.ValidationDereferencer interface for validation.
type collectionBackedDereferencer struct {
tc *Collection
sd validate.ValidationDereferencer
}
var _ validate.ValidationDereferencer = &collectionBackedDereferencer{}
// DereferenceDescriptors implements the validate.ValidationDereferencer
// interface by leveraging the collection's uncommitted descriptors as well
// as its storage cache.
func (c collectionBackedDereferencer) DereferenceDescriptors(
ctx context.Context, version clusterversion.ClusterVersion, reqs []descpb.ID,
) (ret []catalog.Descriptor, _ error) {
ret = make([]catalog.Descriptor, len(reqs))
fallbackReqs := make([]descpb.ID, 0, len(reqs))
fallbackRetIndexes := make([]int, 0, len(reqs))
for i, id := range reqs {
if uc := c.tc.uncommitted.getUncommittedByID(id); uc == nil {
fallbackReqs = append(fallbackReqs, id)
fallbackRetIndexes = append(fallbackRetIndexes, i)
} else {
ret[i] = uc
}
}
if len(fallbackReqs) == 0 {
return ret, nil
}
fallbackRet, err := c.sd.DereferenceDescriptors(ctx, version, fallbackReqs)
if err != nil {
return nil, err
}
for j, desc := range fallbackRet {
if desc != nil {
ret[fallbackRetIndexes[j]] = desc
}
}
return ret, nil
}
// DereferenceDescriptorIDs implements the validate.ValidationDereferencer
// interface by delegating to the storage cache.
func (c collectionBackedDereferencer) DereferenceDescriptorIDs(
ctx context.Context, reqs []descpb.NameInfo,
) (ret []descpb.ID, _ error) {
// TODO(postamar): namespace operations in general should go through Collection
return c.sd.DereferenceDescriptorIDs(ctx, reqs)
}