@@ -17,24 +17,29 @@ import (
1717
1818 "github.com/cockroachdb/cockroach/pkg/base"
1919 "github.com/cockroachdb/cockroach/pkg/clusterversion"
20+ "github.com/cockroachdb/cockroach/pkg/config"
2021 "github.com/cockroachdb/cockroach/pkg/jobs"
22+ "github.com/cockroachdb/cockroach/pkg/keys"
2123 "github.com/cockroachdb/cockroach/pkg/migration"
2224 "github.com/cockroachdb/cockroach/pkg/migration/migrations"
2325 "github.com/cockroachdb/cockroach/pkg/roachpb"
2426 "github.com/cockroachdb/cockroach/pkg/security"
2527 "github.com/cockroachdb/cockroach/pkg/server"
2628 "github.com/cockroachdb/cockroach/pkg/settings/cluster"
2729 "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slinstance"
30+ "github.com/cockroachdb/cockroach/pkg/testutils"
31+ "github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
2832 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
2933 "github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
3034 "github.com/cockroachdb/cockroach/pkg/util/leaktest"
3135 "github.com/cockroachdb/cockroach/pkg/util/stop"
36+ "github.com/cockroachdb/errors"
3237 "github.com/stretchr/testify/require"
3338)
3439
3540// TestTenantUpgrade exercises the case where a system tenant is in a
3641// non-finalized version state and creates a tenant. The test ensures
37- // that that newly created tenant begins in that same version.
42+ // that the newly created tenant begins in that same version.
3843//
3944// The first subtest creates the tenant in the mixed version state,
4045// then upgrades the system tenant, then upgrades the secondary tenant,
@@ -358,3 +363,111 @@ func TestTenantUpgradeFailure(t *testing.T) {
358363 tenantInfo .v2onMigrationStopper .Stop (ctx )
359364 })
360365}
366+
367+ // TestTenantSystemConfigUpgrade ensures that the tenant GC job uses the
368+ // appropriate view of the GC TTL.
369+ func TestTenantSystemConfigUpgrade (t * testing.T ) {
370+ defer leaktest .AfterTest (t )()
371+ ctx := context .Background ()
372+ settings := cluster .MakeTestingClusterSettingsWithVersions (
373+ clusterversion .TestingBinaryVersion ,
374+ clusterversion .TestingBinaryMinSupportedVersion ,
375+ false , // initializeVersion
376+ )
377+ // Initialize the version to the BinaryMinSupportedVersion.
378+ require .NoError (t , clusterversion .Initialize (ctx ,
379+ clusterversion .TestingBinaryMinSupportedVersion , & settings .SV ))
380+ tc := testcluster .StartTestCluster (t , 1 , base.TestClusterArgs {
381+ ServerArgs : base.TestServerArgs {
382+ Settings : settings ,
383+ Knobs : base.TestingKnobs {
384+ Server : & server.TestingKnobs {
385+ DisableAutomaticVersionUpgrade : make (chan struct {}),
386+ BinaryVersionOverride : clusterversion .TestingBinaryMinSupportedVersion ,
387+ },
388+ },
389+ },
390+ })
391+ hostDB := sqlutils .MakeSQLRunner (tc .ServerConn (0 ))
392+ hostDB .Exec (t , `SET CLUSTER SETTING kv.closed_timestamp.target_duration = '20ms'` )
393+ hostDB .Exec (t , `SET CLUSTER SETTING kv.closed_timestamp.side_transport_interval = '20ms'` )
394+ defer tc .Stopper ().Stop (ctx )
395+ connectToTenant := func (t * testing.T , addr string ) (_ * gosql.DB , cleanup func ()) {
396+ pgURL , cleanupPGUrl := sqlutils .PGUrl (t , addr , "Tenant" , url .User (security .RootUser ))
397+ tenantDB , err := gosql .Open ("postgres" , pgURL .String ())
398+ require .NoError (t , err )
399+ return tenantDB , func () {
400+ tenantDB .Close ()
401+ cleanupPGUrl ()
402+ }
403+ }
404+ mkTenant := func (t * testing.T , id uint64 ) (
405+ tenant serverutils.TestTenantInterface ,
406+ ) {
407+ settings := cluster .MakeTestingClusterSettingsWithVersions (
408+ clusterversion .TestingBinaryVersion ,
409+ clusterversion .TestingBinaryMinSupportedVersion ,
410+ false , // initializeVersion
411+ )
412+ // Initialize the version to the minimum it could be.
413+ require .NoError (t , clusterversion .Initialize (ctx ,
414+ clusterversion .TestingBinaryMinSupportedVersion , & settings .SV ))
415+ tenantArgs := base.TestTenantArgs {
416+ TenantID : roachpb .MakeTenantID (id ),
417+ TestingKnobs : base.TestingKnobs {},
418+ Settings : settings ,
419+ }
420+ tenant , err := tc .Server (0 ).StartTenant (ctx , tenantArgs )
421+ require .NoError (t , err )
422+ return tenant
423+ }
424+ const tenantID = 10
425+ codec := keys .MakeSQLCodec (roachpb .MakeTenantID (tenantID ))
426+ tenant := mkTenant (t , tenantID )
427+ tenantSQL , cleanup := connectToTenant (t , tenant .SQLAddr ())
428+ defer cleanup ()
429+ tenantDB := sqlutils .MakeSQLRunner (tenantSQL )
430+ tenantDB .CheckQueryResults (t , "SHOW CLUSTER SETTING version" , [][]string {{"21.2" }})
431+ tenantDB .Exec (t , "CREATE TABLE foo ()" )
432+ fooID := sqlutils .QueryTableID (t , tenantSQL , "defaultdb" , "public" , "foo" )
433+ tenantP := tenant .SystemConfigProvider ()
434+ ch , _ := tenantP .RegisterSystemConfigChannel ()
435+
436+ hostDB .Exec (t , "SET CLUSTER SETTING version = crdb_internal.node_executable_version()" )
437+ hostDB .Exec (t , "ALTER RANGE tenants CONFIGURE ZONE USING gc.ttlseconds = 111" )
438+ hostDB .Exec (t ,
439+ "ALTER TENANT $1 SET CLUSTER SETTING sql.zone_configs.allow_for_secondary_tenant.enabled = true;" ,
440+ tenantID )
441+ tenantDB .CheckQueryResultsRetry (
442+ t , "SHOW CLUSTER SETTING sql.zone_configs.allow_for_secondary_tenant.enabled" ,
443+ [][]string {{"true" }},
444+ )
445+ tenantVersion := func () clusterversion.ClusterVersion {
446+ return tenant .ClusterSettings ().Version .ActiveVersionOrEmpty (ctx )
447+ }
448+ checkConfigEqual := func (t * testing.T , exp int32 ) {
449+ testutils .SucceedsSoon (t , func () error {
450+ cfg := tenantP .GetSystemConfig ()
451+ if cfg == nil {
452+ return errors .New ("no config" )
453+ }
454+ conf , err := tenantP .GetSystemConfig ().GetZoneConfigForObject (codec , tenantVersion (), config .ObjectID (fooID ))
455+ if err != nil {
456+ return err
457+ }
458+ if conf .GC .TTLSeconds != exp {
459+ return errors .Errorf ("got %d, expected %d" , conf .GC .TTLSeconds , exp )
460+ }
461+ return nil
462+ })
463+ }
464+ checkConfigEqual (t , 111 )
465+ <- ch
466+ hostDB .Exec (t , "ALTER RANGE tenants CONFIGURE ZONE USING gc.ttlseconds = 112" )
467+ <- ch
468+ checkConfigEqual (t , 112 )
469+ tenantDB .Exec (t , "SET CLUSTER SETTING version = crdb_internal.node_executable_version()" )
470+ tenantDB .Exec (t , "ALTER TABLE foo CONFIGURE ZONE USING gc.ttlseconds = 113" )
471+ <- ch
472+ checkConfigEqual (t , 113 )
473+ }
0 commit comments