-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
database_zone_config.go
214 lines (187 loc) · 6.79 KB
/
database_zone_config.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
// Copyright 2024 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 scbuildstmt
import (
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
)
// databaseZoneConfigObj is used to represent a table-specific zone configuration
// object.
type databaseZoneConfigObj struct {
databaseID catid.DescID
zoneConfig *zonepb.ZoneConfig
seqNum uint32
}
var _ zoneConfigObject = &databaseZoneConfigObj{}
func (dzo *databaseZoneConfigObj) addZoneConfigToBuildCtx(b BuildCtx) scpb.Element {
dzo.seqNum += 1
elem := &scpb.DatabaseZoneConfig{
DatabaseID: dzo.databaseID,
ZoneConfig: dzo.zoneConfig,
SeqNum: dzo.seqNum,
}
b.Add(elem)
return elem
}
func (dzo *databaseZoneConfigObj) checkPrivilegeForSetZoneConfig(
b BuildCtx, zs tree.ZoneSpecifier,
) error {
// Can configure zone of a database if user has either CREATE or ZONECONFIG
// privilege on the database.
dbElem := b.ResolveDatabase(zs.Database, ResolveParams{}).FilterDatabase().MustGetOneElement()
dbCreatePrivilegeErr := b.CheckPrivilege(dbElem, privilege.CREATE)
dbZoneConfigPrivilegeErr := b.CheckPrivilege(dbElem, privilege.ZONECONFIG)
if dbZoneConfigPrivilegeErr == nil || dbCreatePrivilegeErr == nil {
return nil
}
// For the system database, the user must be an admin. Otherwise, we
// require CREATE or ZONECONFIG privilege on the database in question.
reqNonAdminPrivs := []privilege.Kind{privilege.ZONECONFIG, privilege.CREATE}
if zs.Database == "system" {
return b.CheckGlobalPrivilege(privilege.REPAIRCLUSTER)
}
return sqlerrors.NewInsufficientPrivilegeOnDescriptorError(b.CurrentUser(),
reqNonAdminPrivs, string(catalog.Database),
mustRetrieveNamespaceElem(b, dbElem.DatabaseID).Name)
}
func (dzo *databaseZoneConfigObj) checkZoneConfigChangePermittedForMultiRegion(
b BuildCtx, zs tree.ZoneSpecifier, options tree.KVOptions,
) error {
// If the user has specified that they're overriding, then the world is
// their oyster.
if b.SessionData().OverrideMultiRegionZoneConfigEnabled {
// Note that we increment the telemetry counter unconditionally here.
// It's possible that this will lead to over-counting as the user may
// have left the override on and is now updating a zone configuration
// that is not protected by the multi-region abstractions. To get finer
// grained counting, however, would be more difficult to code, and may
// not even prove to be that valuable, so we have decided to live with
// the potential for over-counting.
telemetry.Inc(sqltelemetry.OverrideMultiRegionZoneConfigurationUser)
return nil
}
// Check if what we're altering is a multi-region entity.
dbRegionConfigElem := b.ResolveDatabase(zs.Database,
ResolveParams{}).FilterDatabaseRegionConfig().MustGetZeroOrOneElement()
if dbRegionConfigElem == nil {
// Not a multi-region database, we're done here.
return nil
}
return maybeMultiregionErrorWithHint(options)
}
func (dzo *databaseZoneConfigObj) getTargetID() catid.DescID {
return dzo.databaseID
}
func (dzo *databaseZoneConfigObj) getSeqNum() uint32 {
return dzo.seqNum
}
func (dzo *databaseZoneConfigObj) retrievePartialZoneConfig(b BuildCtx) *zonepb.ZoneConfig {
sameDB := func(e *scpb.DatabaseZoneConfig) bool {
return e.DatabaseID == dzo.getTargetID()
}
mostRecentElem := findMostRecentZoneConfig(dzo, func(id catid.DescID) *scpb.ElementCollection[*scpb.DatabaseZoneConfig] {
return b.QueryByID(id).FilterDatabaseZoneConfig()
}, sameDB)
if mostRecentElem != nil {
dzo.zoneConfig = mostRecentElem.ZoneConfig
dzo.seqNum = mostRecentElem.SeqNum
}
return dzo.zoneConfig
}
func (dzo *databaseZoneConfigObj) retrieveCompleteZoneConfig(
b BuildCtx, getInheritedDefault bool,
) (*zonepb.ZoneConfig, *zonepb.Subzone, error) {
var err error
zc := &zonepb.ZoneConfig{}
if getInheritedDefault {
zc, err = dzo.getInheritedDefaultZoneConfig(b)
} else {
zc, _, err = dzo.getZoneConfig(b, false /* inheritDefaultRange */)
}
if err != nil {
return nil, nil, err
}
completeZc := *zc
if err = dzo.completeZoneConfig(b, &completeZc); err != nil {
return nil, nil, err
}
return zc, nil, nil
}
func (dzo *databaseZoneConfigObj) completeZoneConfig(b BuildCtx, zone *zonepb.ZoneConfig) error {
if zone.IsComplete() {
return nil
}
// Check if zone is complete. If not, inherit from the default zone config
if zone.IsComplete() {
return nil
}
defaultZone, _, err := dzo.getZoneConfig(b, true /* inheritDefaultRange */)
if err != nil {
return err
}
zone.InheritFromParent(defaultZone)
return nil
}
func (dzo *databaseZoneConfigObj) setZoneConfigToWrite(zone *zonepb.ZoneConfig) {
dzo.zoneConfig = zone
}
func (dzo *databaseZoneConfigObj) getInheritedDefaultZoneConfig(
b BuildCtx,
) (*zonepb.ZoneConfig, error) {
// Get the zone config of the DEFAULT RANGE.
zc, _, err := dzo.getZoneConfig(b, true /* inheritDefaultRange */)
return zc, err
}
func (dzo *databaseZoneConfigObj) getZoneConfig(
b BuildCtx, inheritDefaultRange bool,
) (*zonepb.ZoneConfig, *zonepb.ZoneConfig, error) {
var subzones []zonepb.Subzone
zc, subzones, err := lookUpSystemZonesTable(b, dzo, inheritDefaultRange)
if err != nil {
return nil, nil, err
}
// If the zone config exists, we know that it is not a subzone placeholder.
if zc != nil {
return zc, nil, err
}
zc = zonepb.NewZoneConfig()
zc.Subzones = subzones
subzone := zc
// No zone config for this ID. Retrieve the default zone config, but only as
// long as that wasn't the ID we were trying to retrieve
// (to avoid infinite recursion).
if !inheritDefaultRange {
zc, _, err := dzo.getZoneConfig(b, true /* inheritDefaultRange */)
if err != nil {
return nil, nil, err
}
return zc, subzone, nil
}
// `targetID == keys.RootNamespaceID` but that zc config is not found
// in `system.zones` table. Return a special, recognizable error!
return nil, nil, sqlerrors.ErrNoZoneConfigApplies
}
func (dzo *databaseZoneConfigObj) applyZoneConfig(
b BuildCtx,
n *tree.SetZoneConfig,
copyFromParentList []tree.Name,
setters []func(c *zonepb.ZoneConfig),
) error {
partialZone, err := prepareZoneConfig(b, n, copyFromParentList, setters, dzo)
dzo.setZoneConfigToWrite(partialZone)
return err
}