Skip to content

Commit 344c262

Browse files
committed
sql: enable tenant testing for zone_config_test
Since `GetSpanConfigForKey` is only available in system tenant, it is not used when running under secondary tenants.
1 parent 45fb12f commit 344c262

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

pkg/sql/zone_config_test.go

+44-34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"testing"
1111

12+
"github.com/cockroachdb/cockroach/pkg/base"
1213
"github.com/cockroachdb/cockroach/pkg/config"
1314
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
1415
"github.com/cockroachdb/cockroach/pkg/keys"
@@ -36,12 +37,14 @@ import (
3637
)
3738

3839
var configID = descpb.ID(1)
39-
var configDescKey = catalogkeys.MakeDescMetadataKey(keys.SystemSQLCodec, descpb.ID(bootstrap.TestingUserDescID(0)))
4040

4141
// forceNewConfig forces a system config update by writing a bogus descriptor with an
4242
// incremented value inside. It then repeatedly fetches the gossip config until the
4343
// just-written descriptor is found.
4444
func forceNewConfig(t testing.TB, s serverutils.TestServerInterface) *config.SystemConfig {
45+
var configDescKey = catalogkeys.MakeDescMetadataKey(
46+
s.Codec(), descpb.ID(bootstrap.TestingUserDescID(0)))
47+
4548
configID++
4649
configDesc := &descpb.Descriptor{
4750
Union: &descpb.Descriptor_Database{
@@ -59,15 +62,12 @@ func forceNewConfig(t testing.TB, s serverutils.TestServerInterface) *config.Sys
5962
}); err != nil {
6063
t.Fatal(err)
6164
}
62-
return waitForConfigChange(t, s)
63-
}
6465

65-
func waitForConfigChange(t testing.TB, s serverutils.TestServerInterface) *config.SystemConfig {
66-
var foundDesc descpb.Descriptor
6766
var cfg *config.SystemConfig
6867
testutils.SucceedsSoon(t, func() error {
69-
if cfg = s.SystemConfigProvider().GetSystemConfig(); cfg != nil {
68+
if cfg = s.ApplicationLayer().SystemConfigProvider().GetSystemConfig(); cfg != nil {
7069
if val := cfg.GetValue(configDescKey); val != nil {
70+
var foundDesc descpb.Descriptor
7171
if err := val.GetProto(&foundDesc); err != nil {
7272
t.Fatal(err)
7373
}
@@ -86,7 +86,7 @@ func waitForConfigChange(t testing.TB, s serverutils.TestServerInterface) *confi
8686
func TestGetZoneConfig(t *testing.T) {
8787
defer leaktest.AfterTest(t)()
8888
defer log.Scope(t).Close(t)
89-
params, _ := createTestServerParams()
89+
params, _ := createTestServerParamsAllowTenants()
9090
defaultZoneConfig := zonepb.DefaultSystemZoneConfig()
9191
defaultZoneConfig.NumReplicas = proto.Int32(1)
9292
defaultZoneConfig.RangeMinBytes = proto.Int64(1 << 20)
@@ -101,10 +101,12 @@ func TestGetZoneConfig(t *testing.T) {
101101
s, sqlDB, _ := serverutils.StartServer(t, params)
102102
defer s.Stopper().Stop(context.Background())
103103
// Set the closed_timestamp interval to be short to shorten the test duration.
104-
tdb := sqlutils.MakeSQLRunner(sqlDB)
105-
tdb.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.target_duration = '20ms'`)
106-
tdb.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.side_transport_interval = '20ms'`)
107-
tdb.Exec(t, `SET CLUSTER SETTING kv.rangefeed.closed_timestamp_refresh_interval = '20ms'`)
104+
systemDB := sqlutils.MakeSQLRunner(s.SystemLayer().SQLConn(t))
105+
systemDB.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.target_duration = '20ms'`)
106+
systemDB.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.side_transport_interval = '20ms'`)
107+
systemDB.Exec(t, `SET CLUSTER SETTING kv.rangefeed.closed_timestamp_refresh_interval = '20ms'`)
108+
109+
codec := s.Codec()
108110

109111
type testCase struct {
110112
objectID uint32
@@ -119,9 +121,10 @@ func TestGetZoneConfig(t *testing.T) {
119121
cfg := forceNewConfig(t, s)
120122

121123
for tcNum, tc := range testCases {
122-
// Verify SystemConfig.GetZoneConfigForKey.
123-
{
124-
key := append(roachpb.RKey(keys.SystemSQLCodec.TablePrefix(tc.objectID)), tc.keySuffix...)
124+
// Verify SystemConfig.GetZoneConfigForKey. GetZoneConfigForKey is
125+
// exclusive to the system tenant.
126+
if !s.StartedDefaultTestTenant() {
127+
key := append(roachpb.RKey(codec.TablePrefix(tc.objectID)), tc.keySuffix...)
125128
_, zoneCfg, err := config.TestingGetSystemTenantZoneConfigForKey(cfg, key) // Complete ZoneConfig
126129
if err != nil {
127130
t.Fatalf("#%d: err=%s", tcNum, err)
@@ -203,10 +206,10 @@ func TestGetZoneConfig(t *testing.T) {
203206
tb21 := sqlutils.QueryTableID(t, sqlDB, "db2", "public", "tb1")
204207
tb22 := sqlutils.QueryTableID(t, sqlDB, "db2", "public", "tb2")
205208

206-
// We have no custom zone configs.
209+
t.Logf("Verifying with no custom zone configs")
207210
verifyZoneConfigs([]testCase{
208-
{0, nil, "", defaultZoneConfig},
209-
{1, nil, "", defaultZoneConfig},
211+
{keys.RootNamespaceID, nil, "", defaultZoneConfig},
212+
{keys.SystemDatabaseID, nil, "", defaultZoneConfig},
210213
{db1, nil, "", defaultZoneConfig},
211214
{db2, nil, "", defaultZoneConfig},
212215
{tb11, nil, "", defaultZoneConfig},
@@ -287,9 +290,10 @@ func TestGetZoneConfig(t *testing.T) {
287290
}
288291
}
289292

293+
t.Logf("Verifying with custom zone configs")
290294
verifyZoneConfigs([]testCase{
291-
{0, nil, "", defaultZoneConfig},
292-
{1, nil, "", defaultZoneConfig},
295+
{keys.RootNamespaceID, nil, "", defaultZoneConfig},
296+
{keys.SystemDatabaseID, nil, "", defaultZoneConfig},
293297
{db1, nil, "", db1Cfg},
294298
{db2, nil, "", defaultZoneConfig},
295299
{tb11, nil, "", tb11Cfg},
@@ -319,7 +323,7 @@ func TestGetZoneConfig(t *testing.T) {
319323
func TestCascadingZoneConfig(t *testing.T) {
320324
defer leaktest.AfterTest(t)()
321325
defer log.Scope(t).Close(t)
322-
params, _ := createTestServerParams()
326+
params, _ := createTestServerParamsAllowTenants()
323327

324328
defaultZoneConfig := zonepb.DefaultZoneConfig()
325329
defaultZoneConfig.NumReplicas = proto.Int32(1)
@@ -335,10 +339,12 @@ func TestCascadingZoneConfig(t *testing.T) {
335339
s, sqlDB, _ := serverutils.StartServer(t, params)
336340
defer s.Stopper().Stop(context.Background())
337341
// Set the closed_timestamp interval to be short to shorten the test duration.
338-
tdb := sqlutils.MakeSQLRunner(sqlDB)
339-
tdb.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.target_duration = '20ms'`)
340-
tdb.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.side_transport_interval = '20ms'`)
341-
tdb.Exec(t, `SET CLUSTER SETTING kv.rangefeed.closed_timestamp_refresh_interval = '20ms'`)
342+
systemDB := sqlutils.MakeSQLRunner(s.SystemLayer().SQLConn(t))
343+
systemDB.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.target_duration = '20ms'`)
344+
systemDB.Exec(t, `SET CLUSTER SETTING kv.closed_timestamp.side_transport_interval = '20ms'`)
345+
systemDB.Exec(t, `SET CLUSTER SETTING kv.rangefeed.closed_timestamp_refresh_interval = '20ms'`)
346+
347+
codec := s.Codec()
342348

343349
type testCase struct {
344350
objectID uint32
@@ -353,9 +359,10 @@ func TestCascadingZoneConfig(t *testing.T) {
353359
cfg := forceNewConfig(t, s)
354360

355361
for tcNum, tc := range testCases {
356-
// Verify SystemConfig.GetZoneConfigForKey.
357-
{
358-
key := append(roachpb.RKey(keys.SystemSQLCodec.TablePrefix(tc.objectID)), tc.keySuffix...)
362+
// Verify SystemConfig.GetZoneConfigForKey. GetZoneConfigForKey is
363+
// exclusive to the system tenant.
364+
if !s.StartedDefaultTestTenant() {
365+
key := append(roachpb.RKey(codec.TablePrefix(tc.objectID)), tc.keySuffix...)
359366
_, zoneCfg, err := config.TestingGetSystemTenantZoneConfigForKey(cfg, key) // Complete ZoneConfig
360367
if err != nil {
361368
t.Fatalf("#%d: err=%s", tcNum, err)
@@ -438,10 +445,10 @@ func TestCascadingZoneConfig(t *testing.T) {
438445
tb21 := sqlutils.QueryTableID(t, sqlDB, "db2", "public", "tb1")
439446
tb22 := sqlutils.QueryTableID(t, sqlDB, "db2", "public", "tb2")
440447

441-
// We have no custom zone configs.
448+
t.Logf("Verifying with no custom zone configs")
442449
verifyZoneConfigs([]testCase{
443-
{0, nil, "", defaultZoneConfig},
444-
{1, nil, "", defaultZoneConfig},
450+
{keys.RootNamespaceID, nil, "", defaultZoneConfig},
451+
{keys.SystemDatabaseID, nil, "", defaultZoneConfig},
445452
{db1, nil, "", defaultZoneConfig},
446453
{db2, nil, "", defaultZoneConfig},
447454
{tb11, nil, "", defaultZoneConfig},
@@ -567,9 +574,10 @@ func TestCascadingZoneConfig(t *testing.T) {
567574
}
568575
}
569576

577+
t.Logf("Verifying with custom zone configs")
570578
verifyZoneConfigs([]testCase{
571-
{0, nil, "", defaultZoneConfig},
572-
{1, nil, "", defaultZoneConfig},
579+
{keys.RootNamespaceID, nil, "", defaultZoneConfig},
580+
{keys.SystemDatabaseID, nil, "", defaultZoneConfig},
573581
{db1, nil, "", expectedDb1Cfg},
574582
{db2, nil, "", defaultZoneConfig},
575583
{tb11, nil, "", expectedTb11Cfg},
@@ -641,7 +649,9 @@ func BenchmarkGetZoneConfig(b *testing.B) {
641649
defer leaktest.AfterTest(b)()
642650
defer log.Scope(b).Close(b)
643651

644-
params, _ := createTestServerParams()
652+
params, _ := createTestServerParamsAllowTenants()
653+
// GetZoneConfigForKey is exclusive to the system tenant.
654+
params.DefaultTestTenant = base.TestIsSpecificToStorageLayerAndNeedsASystemTenant
645655
s, sqlDB, _ := serverutils.StartServer(b, params)
646656
defer s.Stopper().Stop(context.Background())
647657
// Set the closed_timestamp interval to be short to shorten the test duration.
@@ -651,7 +661,7 @@ func BenchmarkGetZoneConfig(b *testing.B) {
651661
tdb.Exec(b, `SET CLUSTER SETTING kv.rangefeed.closed_timestamp_refresh_interval = '20ms'`)
652662
cfg := forceNewConfig(b, s)
653663

654-
key := roachpb.RKey(keys.SystemSQLCodec.TablePrefix(bootstrap.TestingUserDescID(0)))
664+
key := roachpb.RKey(s.Codec().TablePrefix(bootstrap.TestingUserDescID(0)))
655665
b.ResetTimer()
656666
for i := 0; i < b.N; i++ {
657667
_, _, err := config.TestingGetSystemTenantZoneConfigForKey(cfg, key)

0 commit comments

Comments
 (0)