-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
zone_config
452 lines (370 loc) · 13.5 KB
/
zone_config
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# Check that we can alter the default zone config.
statement ok
ALTER RANGE default CONFIGURE ZONE USING num_replicas = 1
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR RANGE default]
----
0 ALTER RANGE default CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 14400,
num_replicas = 1,
constraints = '[]',
lease_preferences = '[]'
# Check that we can reset the default zone config to defaults.
statement ok
ALTER RANGE default CONFIGURE ZONE USING DEFAULT
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR RANGE default]
----
0 ALTER RANGE default CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 14400,
num_replicas = 3,
constraints = '[]',
lease_preferences = '[]'
# Make an override for the tests below
statement ok
ALTER RANGE default CONFIGURE ZONE USING range_min_bytes = 1234567
statement ok
CREATE TABLE a (id INT PRIMARY KEY)
# Ensure that SHOW ZONE CONFIGURATION retrieves the default zone (ID 0) if
# no zone was set.
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
0 ALTER RANGE default CONFIGURE ZONE USING
range_min_bytes = 1234567,
range_max_bytes = 536870912,
gc.ttlseconds = 14400,
num_replicas = 3,
constraints = '[]',
lease_preferences = '[]'
# Once USING DEFAULT has been used, we get the default config
# but with our own zone config ID.
statement ok
ALTER TABLE a CONFIGURE ZONE USING DEFAULT
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
106 ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 1234567,
range_max_bytes = 536870912,
gc.ttlseconds = 14400,
num_replicas = 3,
constraints = '[]',
lease_preferences = '[]'
# Check that configurations can be adjusted with USING.
statement ok
ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 200000 + 1,
range_max_bytes = 100000000 + 1,
gc.ttlseconds = 3000 + 600,
num_replicas = floor(1.2)::int,
constraints = '[+region=test]',
lease_preferences = '[[+region=test]]'
# This should reflect in the metrics.
query T
SELECT feature_name FROM crdb_internal.feature_usage
WHERE feature_name IN (
'sql.schema.zone_config.table.range_min_bytes',
'sql.schema.zone_config.table.range_max_bytes',
'sql.schema.zone_config.table.gc.ttlseconds',
'sql.schema.zone_config.table.num_replicas',
'sql.schema.zone_config.table.constraints'
) AND usage_count > 0 ORDER BY feature_name
----
sql.schema.zone_config.table.constraints
sql.schema.zone_config.table.gc.ttlseconds
sql.schema.zone_config.table.num_replicas
sql.schema.zone_config.table.range_max_bytes
sql.schema.zone_config.table.range_min_bytes
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
106 ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 200001,
range_max_bytes = 100000001,
gc.ttlseconds = 3600,
num_replicas = 1,
constraints = '[+region=test]',
lease_preferences = '[[+region=test]]'
# Check that we can set just one value without altering the others.
statement ok
ALTER TABLE a CONFIGURE ZONE USING range_max_bytes = 400000000
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
106 ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 200001,
range_max_bytes = 400000000,
gc.ttlseconds = 3600,
num_replicas = 1,
constraints = '[+region=test]',
lease_preferences = '[[+region=test]]'
# Check that we can configure zones in tables in non-public schemas, and that
# they don't conflict with tables of the same name in different schemas.
statement ok
CREATE SCHEMA test
statement ok
CREATE TABLE test.a (a INT PRIMARY KEY)
statement ok
ALTER TABLE test.a CONFIGURE ZONE USING gc.ttlseconds=1234
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE test.a]
----
108 ALTER TABLE test.a CONFIGURE ZONE USING
range_min_bytes = 1234567,
range_max_bytes = 536870912,
gc.ttlseconds = 1234,
num_replicas = 3,
constraints = '[]',
lease_preferences = '[]'
# Check that the original table's zone config is unmodified.
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
106 ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 200001,
range_max_bytes = 400000000,
gc.ttlseconds = 3600,
num_replicas = 1,
constraints = '[+region=test]',
lease_preferences = '[[+region=test]]'
# Test SHOW CREATE TABLE correctly shows the CREATE TABLE.
query TT
SHOW CREATE TABLE a
----
a CREATE TABLE public.a (
id INT8 NOT NULL,
CONSTRAINT a_pkey PRIMARY KEY (id ASC)
);
ALTER TABLE test.public.a CONFIGURE ZONE USING
range_min_bytes = 200001,
range_max_bytes = 400000000,
gc.ttlseconds = 3600,
num_replicas = 1,
constraints = '[+region=test]',
lease_preferences = '[[+region=test]]'
# Check that we can reset the configuration to defaults.
statement ok
ALTER TABLE a CONFIGURE ZONE USING DEFAULT
# Note: the range_min_bytes here should reflect the non-standard
# default that was set initially.
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
106 ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 1234567,
range_max_bytes = 536870912,
gc.ttlseconds = 14400,
num_replicas = 3,
constraints = '[]',
lease_preferences = '[]'
# Check that we can drop a configuration to get back to inherinting
# the defaults.
statement ok
ALTER TABLE a CONFIGURE ZONE DISCARD
query I
SELECT zone_id FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
0
subtest alter_table_telemetry
query T
SELECT feature_name FROM crdb_internal.feature_usage
WHERE feature_name IN ('sql.schema.alter_range.configure_zone', 'sql.schema.alter_table.configure_zone')
ORDER BY feature_name
----
sql.schema.alter_range.configure_zone
sql.schema.alter_table.configure_zone
# Check that configuring num_voters separately from num_replicas behaves as
# expected, across setting them directly and through inheritance.
#
# 1. Check that voter_constraints cannot be set without setting num_voters as
# well.
statement error pq: could not validate zone config: when voter_constraints are set, num_voters must be set as well
ALTER TABLE a CONFIGURE ZONE USING voter_constraints = '{"+region=test": 3}'
# 2. Check that num_voters and voter_constraints show up in tandem once
# num_voters is explicitly set.
statement ok
ALTER TABLE a CONFIGURE ZONE USING num_replicas = 3;
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
106 ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 1234567,
range_max_bytes = 536870912,
gc.ttlseconds = 14400,
num_replicas = 3,
constraints = '[]',
lease_preferences = '[]'
statement ok
ALTER TABLE a CONFIGURE ZONE USING num_voters = 1;
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
106 ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 1234567,
range_max_bytes = 536870912,
gc.ttlseconds = 14400,
num_replicas = 3,
num_voters = 1,
constraints = '[]',
voter_constraints = '[]',
lease_preferences = '[]'
# 3. Sanity check that `voter_constraints` can be reset.
statement ok
ALTER TABLE a CONFIGURE ZONE USING voter_constraints = '{"+region=test": 1}'
statement error pq: could not validate zone config: when voter_constraints are set, num_voters must be set as well
ALTER TABLE a CONFIGURE ZONE USING num_voters = COPY FROM PARENT
query IT
SELECT zone_id, raw_config_sql FROM [SHOW ZONE CONFIGURATION FOR TABLE a]
----
106 ALTER TABLE a CONFIGURE ZONE USING
range_min_bytes = 1234567,
range_max_bytes = 536870912,
gc.ttlseconds = 14400,
num_replicas = 3,
num_voters = 1,
constraints = '[]',
voter_constraints = '{+region=test: 1}',
lease_preferences = '[]'
# Different error based on if the test is being run in the system tenant or
# a secondary tenant.
statement error pq: (.* matches no existing nodes within the cluster)|(region "shouldFail" not found)
ALTER TABLE a CONFIGURE ZONE USING voter_constraints = '{"+region=shouldFail": 1}'
# Check entities for which we can set zone configs.
subtest test_entity_validity
statement ok
CREATE TABLE zc (
a INT PRIMARY KEY,
b INT
)
statement ok
ALTER TABLE zc CONFIGURE ZONE USING gc.ttlseconds = 100000
statement ok
CREATE MATERIALIZED VIEW vm (x, y) AS SELECT a,b FROM zc; ALTER TABLE vm CONFIGURE ZONE USING gc.ttlseconds = 100000
statement error pgcode 42809 cannot set a zone configuration on non-physical object v
CREATE VIEW v (x, y) AS SELECT a, b FROM zc; ALTER TABLE v CONFIGURE ZONE USING gc.ttlseconds = 100000
user root
onlyif config local-legacy-schema-changer local-mixed-24.2 local-mixed-24.3
statement error pq: user root does not have CREATE or ZONECONFIG privilege on relation pg_type
ALTER TABLE pg_catalog.pg_type CONFIGURE ZONE USING gc.ttlseconds = 100000
onlyif config local-legacy-schema-changer local-mixed-24.2 local-mixed-24.3
statement error pq: user root does not have CREATE or ZONECONFIG privilege on relation columns
ALTER TABLE information_schema.columns CONFIGURE ZONE USING gc.ttlseconds = 100000
skipif config local-legacy-schema-changer local-mixed-24.2 local-mixed-24.3
statement error pq: pg_type is a system catalog
ALTER TABLE pg_catalog.pg_type CONFIGURE ZONE USING gc.ttlseconds = 100000
skipif config local-legacy-schema-changer local-mixed-24.2 local-mixed-24.3
statement error pq: columns is a virtual object and cannot be modified
ALTER TABLE information_schema.columns CONFIGURE ZONE USING gc.ttlseconds = 100000
statement ok
CREATE TABLE roachie(i int)
user testuser
statement error pq: user testuser does not have CREATE or ZONECONFIG privilege on relation roachie
ALTER TABLE roachie CONFIGURE ZONE USING gc.ttlseconds = 1
user root
# Test tables in different schemas do not show the zone configs
# of the other.
statement ok
CREATE TABLE same_table_name();
ALTER TABLE same_table_name CONFIGURE ZONE USING gc.ttlseconds = 500;
CREATE SCHEMA alternative_schema;
CREATE TABLE alternative_schema.same_table_name();
ALTER TABLE alternative_schema.same_table_name CONFIGURE ZONE USING gc.ttlseconds = 600
query TT
SHOW CREATE TABLE same_table_name
----
same_table_name CREATE TABLE public.same_table_name (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT same_table_name_pkey PRIMARY KEY (rowid ASC)
);
ALTER TABLE test.public.same_table_name CONFIGURE ZONE USING
gc.ttlseconds = 500
query TT
SHOW CREATE TABLE alternative_schema.same_table_name
----
alternative_schema.same_table_name CREATE TABLE alternative_schema.same_table_name (
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT same_table_name_pkey PRIMARY KEY (rowid ASC)
);
ALTER TABLE test.alternative_schema.same_table_name CONFIGURE ZONE USING
gc.ttlseconds = 600
statement ok
DROP TABLE zc CASCADE
statement ok
CREATE DATABASE foo
statement ok
ALTER DATABASE foo CONFIGURE ZONE USING gc.ttlseconds = 12345
statement ok
CREATE TABLE foo.public.bar (x INT PRIMARY KEY)
statement ok
DROP DATABASE foo CASCADE
statement ok
CREATE TABLE baz (x INT PRIMARY KEY)
statement ok
DROP TABLE baz
query TT
SELECT name, ttl FROM crdb_internal.kv_dropped_relations ORDER BY name;
----
bar 03:25:45
baz 04:00:00
vm 27:46:40
zc 27:46:40
statement error pgcode 23514 pq: cannot remove default zone
ALTER RANGE default CONFIGURE ZONE DISCARD;
# Regression test for #108253. Ensure that transactional configure zone changes
# do not produce an error.
subtest transactional_schemachanges
statement ok
CREATE DATABASE foo
statement ok
ALTER RANGE default CONFIGURE ZONE USING gc.ttlseconds = 3; ALTER RANGE default CONFIGURE ZONE USING gc.ttlseconds = 4;
statement ok
ALTER DATABASE foo CONFIGURE ZONE USING gc.ttlseconds = 3; ALTER DATABASE foo CONFIGURE ZONE USING gc.ttlseconds = 4;
statement ok
ALTER DATABASE foo CONFIGURE ZONE DISCARD; ALTER DATABASE foo CONFIGURE ZONE DISCARD;
subtest end
subtest seq_zone_config
statement ok
CREATE SEQUENCE seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
statement ok
ALTER TABLE seq CONFIGURE ZONE USING gc.ttlseconds = 100000;
subtest end
subtest txn_zone_config
skipif config local-mixed-24.2 local-mixed-24.3
statement ok
SET use_declarative_schema_changer = unsafe_always;
skipif config 3node-tenant-default-configs
statement ok
BEGIN;
ALTER RANGE meta CONFIGURE ZONE USING num_replicas = 8;
ALTER RANGE meta CONFIGURE ZONE DISCARD;
ALTER RANGE meta CONFIGURE ZONE USING num_replicas = 7;
ALTER RANGE meta CONFIGURE ZONE USING gc.ttlseconds = 10000;
COMMIT;
skipif config 3node-tenant-default-configs
query TI colnames
WITH settings AS (
SELECT
(crdb_internal.pb_to_json('cockroach.config.zonepb.ZoneConfig', config) -> 'numReplicas') AS replicas,
((crdb_internal.pb_to_json('cockroach.config.zonepb.ZoneConfig', config) -> 'gc') ->> 'ttlSeconds')::INT AS ttl
FROM system.zones
-- 16 is the ID for the meta range
WHERE id = 16
)
SELECT *
FROM settings;
----
replicas ttl
7 10000
statement ok
RESET use_declarative_schema_changer;
subtest end