-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathshow_zone_config.go
409 lines (379 loc) · 12.6 KB
/
show_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
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
400
401
402
403
404
405
406
407
408
409
// Copyright 2017 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 (
"bytes"
"context"
"strings"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/lexbase"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
yaml "gopkg.in/yaml.v2"
)
// These must match crdb_internal.zones.
var showZoneConfigColumns = colinfo.ResultColumns{
{Name: "zone_id", Typ: types.Int, Hidden: true},
{Name: "subzone_id", Typ: types.Int, Hidden: true},
{Name: "target", Typ: types.String},
{Name: "range_name", Typ: types.String, Hidden: true},
{Name: "database_name", Typ: types.String, Hidden: true},
{Name: "schema_name", Typ: types.String, Hidden: true},
{Name: "table_name", Typ: types.String, Hidden: true},
{Name: "index_name", Typ: types.String, Hidden: true},
{Name: "partition_name", Typ: types.String, Hidden: true},
{Name: "raw_config_yaml", Typ: types.String, Hidden: true},
{Name: "raw_config_sql", Typ: types.String},
{Name: "raw_config_protobuf", Typ: types.Bytes, Hidden: true},
{Name: "full_config_yaml", Typ: types.String, Hidden: true},
{Name: "full_config_sql", Typ: types.String, Hidden: true},
}
// These must match showZoneConfigColumns.
const (
zoneIDCol int = iota
subZoneIDCol
targetCol
rangeNameCol
databaseNameCol
schemaNameCol
tableNameCol
indexNameCol
partitionNameCol
rawConfigYAMLCol
rawConfigSQLCol
rawConfigProtobufCol
fullConfigYamlCol
fullConfigSQLCol
)
func (p *planner) ShowZoneConfig(ctx context.Context, n *tree.ShowZoneConfig) (planNode, error) {
if !ZonesTableExists(ctx, p.ExecCfg().Codec, p.ExecCfg().Settings.Version) {
// Secondary tenants prior to the introduction of system.zones for them
// could not set/show zone configurations on individual objects.
return nil, errorutil.UnsupportedWithMultiTenancy(MultitenancyZoneCfgIssueNo)
}
return &delayedNode{
name: n.String(),
columns: showZoneConfigColumns,
constructor: func(ctx context.Context, p *planner) (planNode, error) {
v := p.newContainerValuesNode(showZoneConfigColumns, 0)
// This signifies SHOW ALL.
// However, SHOW ALL should be handled by the delegate.
if n.ZoneSpecifier == (tree.ZoneSpecifier{}) {
return nil, errors.AssertionFailedf("zone must be specified")
}
row, err := getShowZoneConfigRow(ctx, p, n.ZoneSpecifier)
if err != nil {
v.Close(ctx)
return nil, err
}
if _, err := v.rows.AddRow(ctx, row); err != nil {
v.Close(ctx)
return nil, err
}
return v, nil
},
}, nil
}
func getShowZoneConfigRow(
ctx context.Context, p *planner, zoneSpecifier tree.ZoneSpecifier,
) (tree.Datums, error) {
tblDesc, err := p.resolveTableForZone(ctx, &zoneSpecifier)
if err != nil {
return nil, err
}
if zoneSpecifier.TableOrIndex.Table.ObjectName != "" {
if err = p.CheckAnyPrivilege(ctx, tblDesc); err != nil {
return nil, err
}
} else if zoneSpecifier.Database != "" {
database, err := p.Descriptors().GetImmutableDatabaseByName(
ctx,
p.txn,
string(zoneSpecifier.Database),
tree.DatabaseLookupFlags{Required: true},
)
if err != nil {
return nil, err
}
if err = p.CheckAnyPrivilege(ctx, database); err != nil {
return nil, err
}
}
targetID, err := resolveZone(ctx, p.ExecCfg().Codec, p.txn, &zoneSpecifier)
if err != nil {
return nil, err
}
index, partition, err := resolveSubzone(&zoneSpecifier, tblDesc)
if err != nil {
return nil, err
}
subZoneIdx := uint32(0)
zoneID, zone, subzone, err := GetZoneConfigInTxn(
ctx, p.txn, p.ExecCfg().Codec, targetID, index, partition, false, /* getInheritedDefault */
)
if errors.Is(err, errNoZoneConfigApplies) {
// TODO(benesch): This shouldn't be the caller's responsibility;
// GetZoneConfigInTxn should just return the default zone config if no zone
// config applies.
zone = p.execCfg.DefaultZoneConfig
zoneID = keys.RootNamespaceID
} else if err != nil {
return nil, err
} else if subzone != nil {
for i := range zone.Subzones {
subZoneIdx++
if subzone == &zone.Subzones[i] {
break
}
}
zone = &subzone.Config
}
// Determine the zone specifier for the zone config that actually applies
// without performing another KV lookup.
zs := ascendZoneSpecifier(zoneSpecifier, targetID, zoneID, subzone)
// Ensure subzone configs don't infect the output of config_bytes.
zone.Subzones = nil
zone.SubzoneSpans = nil
vals := make(tree.Datums, len(showZoneConfigColumns))
if err := generateZoneConfigIntrospectionValues(
vals, tree.NewDInt(tree.DInt(zoneID)), tree.NewDInt(tree.DInt(subZoneIdx)), &zs, zone, nil,
); err != nil {
return nil, err
}
return vals, nil
}
// zoneConfigToSQL pretty prints a zone configuration as a SQL string.
func zoneConfigToSQL(zs *tree.ZoneSpecifier, zone *zonepb.ZoneConfig) (string, error) {
// Use FutureLineWrap to avoid wrapping long lines. This is required for
// cases where one of the zone config fields is longer than 80 characters.
// In that case, without FutureLineWrap, the output will have `\n`
// characters interspersed every 80 characters. FutureLineWrap ensures that
// the whole field shows up as a single line.
yaml.FutureLineWrap()
constraints, err := yamlMarshalFlow(zonepb.ConstraintsList{
Constraints: zone.Constraints,
Inherited: zone.InheritedConstraints})
if err != nil {
return "", err
}
constraints = strings.TrimSpace(constraints)
voterConstraints, err := yamlMarshalFlow(zonepb.ConstraintsList{
Constraints: zone.VoterConstraints,
Inherited: zone.InheritedVoterConstraints(),
})
if err != nil {
return "", err
}
voterConstraints = strings.TrimSpace(voterConstraints)
prefs, err := yamlMarshalFlow(zone.LeasePreferences)
if err != nil {
return "", err
}
prefs = strings.TrimSpace(prefs)
useComma := false
maybeWriteComma := func(f *tree.FmtCtx) {
if useComma {
f.Printf(",\n")
}
useComma = true
}
f := tree.NewFmtCtx(tree.FmtParsable)
f.WriteString("ALTER ")
f.FormatNode(zs)
f.WriteString(" CONFIGURE ZONE USING\n")
if zone.RangeMinBytes != nil {
maybeWriteComma(f)
f.Printf("\trange_min_bytes = %d", *zone.RangeMinBytes)
}
if zone.RangeMaxBytes != nil {
maybeWriteComma(f)
f.Printf("\trange_max_bytes = %d", *zone.RangeMaxBytes)
}
if zone.GC != nil {
maybeWriteComma(f)
f.Printf("\tgc.ttlseconds = %d", zone.GC.TTLSeconds)
}
if zone.GlobalReads != nil {
maybeWriteComma(f)
f.Printf("\tglobal_reads = %t", *zone.GlobalReads)
}
if zone.NumReplicas != nil {
maybeWriteComma(f)
f.Printf("\tnum_replicas = %d", *zone.NumReplicas)
}
if zone.NumVoters != nil {
maybeWriteComma(f)
f.Printf("\tnum_voters = %d", *zone.NumVoters)
}
if !zone.InheritedConstraints {
maybeWriteComma(f)
f.Printf("\tconstraints = %s", lexbase.EscapeSQLString(constraints))
}
if !zone.InheritedVoterConstraints() && zone.NumVoters != nil && *zone.NumVoters > 0 {
maybeWriteComma(f)
f.Printf("\tvoter_constraints = %s", lexbase.EscapeSQLString(voterConstraints))
}
if !zone.InheritedLeasePreferences {
maybeWriteComma(f)
f.Printf("\tlease_preferences = %s", lexbase.EscapeSQLString(prefs))
}
return f.String(), nil
}
// generateZoneConfigIntrospectionValues creates a result row
// suitable for populating crdb_internal.zones or SHOW ZONE CONFIG.
// The values are populated into the `values` first argument.
// The caller is responsible for creating the DInt for the ID and
// provide it as 2nd argument. The function will compute
// the remaining values based on the zone specifier and configuration.
// The fullZoneConfig argument is a zone config populated with all
// inherited zone configuration information. If this argument is nil,
// then the zone argument is used to populate the full_config_sql and
// full_config_yaml columns.
func generateZoneConfigIntrospectionValues(
values tree.Datums,
zoneID tree.Datum,
subZoneID tree.Datum,
zs *tree.ZoneSpecifier,
zone *zonepb.ZoneConfig,
fullZoneConfig *zonepb.ZoneConfig,
) error {
// Populate the ID column.
values[zoneIDCol] = zoneID
values[subZoneIDCol] = subZoneID
// Populate the zone specifier columns.
values[targetCol] = tree.DNull
values[rangeNameCol] = tree.DNull
values[databaseNameCol] = tree.DNull
values[schemaNameCol] = tree.DNull
values[tableNameCol] = tree.DNull
values[indexNameCol] = tree.DNull
values[partitionNameCol] = tree.DNull
if zs != nil {
values[targetCol] = tree.NewDString(zs.String())
if zs.NamedZone != "" {
values[rangeNameCol] = tree.NewDString(string(zs.NamedZone))
}
if zs.Database != "" {
values[databaseNameCol] = tree.NewDString(string(zs.Database))
}
if zs.TableOrIndex.Table.ObjectName != "" {
values[databaseNameCol] = tree.NewDString(string(zs.TableOrIndex.Table.CatalogName))
values[schemaNameCol] = tree.NewDString(string(zs.TableOrIndex.Table.SchemaName))
values[tableNameCol] = tree.NewDString(string(zs.TableOrIndex.Table.ObjectName))
}
if zs.TableOrIndex.Index != "" {
values[indexNameCol] = tree.NewDString(string(zs.TableOrIndex.Index))
}
if zs.Partition != "" {
values[partitionNameCol] = tree.NewDString(string(zs.Partition))
}
}
// Populate the YAML column.
yamlConfig, err := yaml.Marshal(zone)
if err != nil {
return err
}
values[rawConfigYAMLCol] = tree.NewDString(string(yamlConfig))
// Populate the SQL column.
if zs == nil {
values[rawConfigSQLCol] = tree.DNull
} else {
sqlStr, err := zoneConfigToSQL(zs, zone)
if err != nil {
return err
}
values[rawConfigSQLCol] = tree.NewDString(sqlStr)
}
// Populate the protobuf column.
protoConfig, err := protoutil.Marshal(zone)
if err != nil {
return err
}
values[rawConfigProtobufCol] = tree.NewDBytes(tree.DBytes(protoConfig))
// Populate the full_config_yaml and full_config_sql columns.
inheritedConfig := fullZoneConfig
if inheritedConfig == nil {
inheritedConfig = zone
}
yamlConfig, err = yaml.Marshal(inheritedConfig)
if err != nil {
return err
}
values[fullConfigYamlCol] = tree.NewDString(string(yamlConfig))
if zs == nil {
values[fullConfigSQLCol] = tree.DNull
} else {
sqlStr, err := zoneConfigToSQL(zs, inheritedConfig)
if err != nil {
return err
}
values[fullConfigSQLCol] = tree.NewDString(sqlStr)
}
return nil
}
func yamlMarshalFlow(v interface{}) (string, error) {
var buf bytes.Buffer
e := yaml.NewEncoder(&buf)
e.UseStyle(yaml.FlowStyle)
if err := e.Encode(v); err != nil {
return "", err
}
if err := e.Close(); err != nil {
return "", err
}
return buf.String(), nil
}
// ascendZoneSpecifier logically ascends the zone hierarchy for the zone
// specified by (zs, resolvedID) until the zone matching actualID is found, and
// returns that zone's specifier. Results are undefined if actualID is not in
// the hierarchy for (zs, resolvedID).
//
// Under the hood, this function encodes knowledge about the zone lookup
// hierarchy to avoid performing any KV lookups, and so must be kept in sync
// with GetZoneConfig.
//
// TODO(benesch): Teach GetZoneConfig to return the specifier of the zone it
// finds without impacting performance.
func ascendZoneSpecifier(
zs tree.ZoneSpecifier, resolvedID, actualID descpb.ID, actualSubzone *zonepb.Subzone,
) tree.ZoneSpecifier {
if actualID == keys.RootNamespaceID {
// We had to traverse to the top of the hierarchy, so we're showing the
// default zone config.
zs.NamedZone = zonepb.DefaultZoneName
zs.Database = ""
zs.TableOrIndex = tree.TableIndexName{}
// Since the default zone has no partition, we can erase the
// partition name field.
zs.Partition = ""
} else if resolvedID != actualID {
// We traversed at least one level up, and we're not at the top of the
// hierarchy, so we're showing the database zone config.
zs.Database = zs.TableOrIndex.Table.CatalogName
zs.TableOrIndex = tree.TableIndexName{}
// Since databases don't have partition, we can erase the
// partition name field.
zs.Partition = ""
} else if actualSubzone == nil {
// We didn't find a subzone, so no index or partition zone config exists.
zs.TableOrIndex.Index = ""
zs.Partition = ""
} else if actualSubzone.PartitionName == "" {
// The resolved subzone did not name a partition, just an index.
zs.Partition = ""
}
return zs
}