From 38ca035d0ada45e015158e45a8ea52d763ea26cf Mon Sep 17 00:00:00 2001 From: Rafi Shamim Date: Fri, 8 Sep 2023 02:22:46 -0400 Subject: [PATCH] testserver: populate startSingleNode param of RunInitialSQL correctly RunInitialSQL accepts a flag that disables all replication in the cluster if it is true. This should be set for testservers that are a single node cluster. Before this change, only `testserver.Cfg.DefaultZoneConfig.NumReplicas` would be set to 1, but not any of the other builtin ranges, like the meta or system ranges. Release note: None --- pkg/config/zonepb/zone.go | 6 ++ pkg/kv/kvserver/client_split_test.go | 10 +++ pkg/server/testserver.go | 2 +- pkg/sql/catalog/lease/lease_test.go | 105 ++++++++++++++------------- 4 files changed, 71 insertions(+), 52 deletions(-) diff --git a/pkg/config/zonepb/zone.go b/pkg/config/zonepb/zone.go index 91c2f71d378f..979772a62232 100644 --- a/pkg/config/zonepb/zone.go +++ b/pkg/config/zonepb/zone.go @@ -346,6 +346,12 @@ func (z *ZoneConfig) ValidateTandemFields() error { var minRangeMaxBytes = envutil.EnvOrDefaultInt64("COCKROACH_MIN_RANGE_MAX_BYTES", 64<<20 /* 64 MiB */) +func TestingSetMinRangeMaxBytes(v int64) func() { + old := minRangeMaxBytes + minRangeMaxBytes = v + return func() { minRangeMaxBytes = old } +} + // Validate returns an error if the ZoneConfig specifies a known-dangerous or // disallowed configuration. func (z *ZoneConfig) Validate() error { diff --git a/pkg/kv/kvserver/client_split_test.go b/pkg/kv/kvserver/client_split_test.go index ff5762092136..cd8f9c77bfd9 100644 --- a/pkg/kv/kvserver/client_split_test.go +++ b/pkg/kv/kvserver/client_split_test.go @@ -1235,9 +1235,13 @@ func TestStoreRangeSplitBackpressureWrites(t *testing.T) { // Set maxBytes to something small so we can exceed the maximum split // size without adding 2x64MB of data. + defer zonepb.TestingSetMinRangeMaxBytes(1 << 16)() + const minBytes = 1 << 12 const maxBytes = 1 << 17 zoneConfig := zonepb.DefaultZoneConfig() + zoneConfig.RangeMinBytes = proto.Int64(minBytes) zoneConfig.RangeMaxBytes = proto.Int64(maxBytes) + zoneConfig.NumReplicas = proto.Int32(1) testingRequestFilter := func(_ context.Context, ba *kvpb.BatchRequest) *kvpb.Error { @@ -2577,18 +2581,24 @@ func TestUnsplittableRange(t *testing.T) { ctx := context.Background() ttl := 1 * time.Hour + defer zonepb.TestingSetMinRangeMaxBytes(1 << 16)() + const minBytes = 1 << 12 const maxBytes = 1 << 17 manualClock := hlc.NewHybridManualClock() zoneConfig := zonepb.DefaultZoneConfig() + zoneConfig.RangeMinBytes = proto.Int64(minBytes) zoneConfig.RangeMaxBytes = proto.Int64(maxBytes) zoneConfig.GC = &zonepb.GCPolicy{ TTLSeconds: int32(ttl.Seconds()), } + zoneConfig.NumReplicas = proto.Int32(1) zoneSystemConfig := zonepb.DefaultSystemZoneConfig() + zoneSystemConfig.RangeMinBytes = proto.Int64(minBytes) zoneSystemConfig.RangeMaxBytes = proto.Int64(maxBytes) zoneSystemConfig.GC = &zonepb.GCPolicy{ TTLSeconds: int32(ttl.Seconds()), } + zoneSystemConfig.NumReplicas = proto.Int32(1) splitQueuePurgatoryChan := make(chan time.Time, 1) s := serverutils.StartServerOnly(t, base.TestServerArgs{ diff --git a/pkg/server/testserver.go b/pkg/server/testserver.go index 177d2eb3e07f..281986a9f50f 100644 --- a/pkg/server/testserver.go +++ b/pkg/server/testserver.go @@ -677,7 +677,7 @@ func (ts *testServer) Activate(ctx context.Context) error { } // In tests we need some, but not all of RunInitialSQL functionality. if err := ts.topLevelServer.RunInitialSQL( - ctx, false /* startSingleNode */, "" /* adminUser */, "", /* adminPassword */ + ctx, !ts.params.PartOfCluster, "" /* adminUser */, "", /* adminPassword */ ); err != nil { return err } diff --git a/pkg/sql/catalog/lease/lease_test.go b/pkg/sql/catalog/lease/lease_test.go index 9f47cbf1db09..0aa3a2d40ded 100644 --- a/pkg/sql/catalog/lease/lease_test.go +++ b/pkg/sql/catalog/lease/lease_test.go @@ -71,7 +71,7 @@ import ( type leaseTest struct { testing.TB - topServer serverutils.TestServerInterface + cluster serverutils.TestClusterInterface server serverutils.ApplicationLayerInterface db *gosql.DB kvDB *kv.DB @@ -96,25 +96,26 @@ func init() { } -func newLeaseTest(tb testing.TB, params base.TestServerArgs) *leaseTest { - s, db, kvDB := serverutils.StartServer(tb, params) - leaseTest := &leaseTest{ - TB: tb, - topServer: s, - server: s.ApplicationLayer(), - db: db, - kvDB: kvDB, - nodes: map[uint32]*lease.Manager{}, - } - if params.Knobs.SQLLeaseManager != nil { - leaseTest.leaseManagerTestingKnobs = - *params.Knobs.SQLLeaseManager.(*lease.ManagerTestingKnobs) - } - return leaseTest +func newLeaseTest(tb testing.TB, params base.TestClusterArgs) *leaseTest { + c := serverutils.StartCluster(tb, 3, params) + lt := &leaseTest{ + TB: tb, + cluster: c, + server: c.Server(0).ApplicationLayer(), + db: c.ServerConn(0), + kvDB: c.Server(0).DB(), + nodes: map[uint32]*lease.Manager{}, + } + + if params.ServerArgs.Knobs.SQLLeaseManager != nil { + lt.leaseManagerTestingKnobs = + *params.ServerArgs.Knobs.SQLLeaseManager.(*lease.ManagerTestingKnobs) + } + return lt } func (t *leaseTest) cleanup() { - t.topServer.Stopper().Stop(context.Background()) + t.cluster.Stopper().Stop(context.Background()) } func (t *leaseTest) getLeases(descID descpb.ID) string { @@ -269,8 +270,8 @@ func TestLeaseManager(testingT *testing.T) { defer log.Scope(testingT).Close(testingT) removalTracker := lease.NewLeaseRemovalTracker() - var params base.TestServerArgs - params.Knobs = base.TestingKnobs{ + var params base.TestClusterArgs + params.ServerArgs.Knobs = base.TestingKnobs{ SQLLeaseManager: &lease.ManagerTestingKnobs{ LeaseStoreTestingKnobs: lease.StorageTestingKnobs{ LeaseReleasedEvent: removalTracker.LeaseRemovedNotification, @@ -380,20 +381,21 @@ func TestLeaseManagerReacquire(testingT *testing.T) { defer log.Scope(testingT).Close(testingT) ctx := context.Background() - var params base.TestServerArgs - params.Settings = cluster.MakeTestingClusterSettings() + var params base.TestClusterArgs + params.ServerArgs.Settings = cluster.MakeTestingClusterSettings() // Set the lease duration such that the next lease acquisition will // require the lease to be reacquired. - lease.LeaseDuration.Override(ctx, ¶ms.SV, 0) + lease.LeaseDuration.Override(ctx, ¶ms.ServerArgs.SV, 0) removalTracker := lease.NewLeaseRemovalTracker() - params.Knobs = base.TestingKnobs{ + params.ServerArgs.Knobs = base.TestingKnobs{ SQLLeaseManager: &lease.ManagerTestingKnobs{ LeaseStoreTestingKnobs: lease.StorageTestingKnobs{ LeaseReleasedEvent: removalTracker.LeaseRemovedNotification, }, }, } + t := newLeaseTest(testingT, params) defer t.cleanup() @@ -429,7 +431,7 @@ func TestLeaseManagerPublishVersionChanged(testingT *testing.T) { defer leaktest.AfterTest(testingT)() defer log.Scope(testingT).Close(testingT) - t := newLeaseTest(testingT, base.TestServerArgs{}) + t := newLeaseTest(testingT, base.TestClusterArgs{}) defer t.cleanup() descID := t.makeTableForTest() @@ -493,7 +495,7 @@ func TestLeaseManagerPublishIllegalVersionChange(testingT *testing.T) { defer leaktest.AfterTest(testingT)() defer log.Scope(testingT).Close(testingT) - t := newLeaseTest(testingT, base.TestServerArgs{}) + t := newLeaseTest(testingT, base.TestClusterArgs{}) defer t.cleanup() if _, err := t.node(1).Publish( @@ -518,9 +520,9 @@ func TestLeaseManagerDrain(testingT *testing.T) { defer leaktest.AfterTest(testingT)() defer log.Scope(testingT).Close(testingT) - var params base.TestServerArgs + var params base.TestClusterArgs leaseRemovalTracker := lease.NewLeaseRemovalTracker() - params.Knobs = base.TestingKnobs{ + params.ServerArgs.Knobs = base.TestingKnobs{ SQLLeaseManager: &lease.ManagerTestingKnobs{ LeaseStoreTestingKnobs: lease.StorageTestingKnobs{ LeaseReleasedEvent: leaseRemovalTracker.LeaseRemovedNotification, @@ -582,8 +584,8 @@ func TestCantLeaseDeletedTable(testingT *testing.T) { var mu syncutil.Mutex clearSchemaChangers := false - var params base.TestServerArgs - params.Knobs = base.TestingKnobs{ + var params base.TestClusterArgs + params.ServerArgs.Knobs = base.TestingKnobs{ SQLSchemaChanger: &sql.SchemaChangerTestingKnobs{ SchemaChangeJobNoOp: func() bool { mu.Lock() @@ -1178,7 +1180,7 @@ func BenchmarkLeaseAcquireByNameCached(b *testing.B) { defer leaktest.AfterTest(b)() defer log.Scope(b).Close(b) - t := newLeaseTest(b, base.TestServerArgs{}) + t := newLeaseTest(b, base.TestClusterArgs{}) defer t.cleanup() if _, err := t.db.Exec(` @@ -1236,8 +1238,8 @@ func TestLeaseRenewedAutomatically(testingT *testing.T) { var testAcquiredCount int32 var testAcquisitionBlockCount int32 - var params base.TestServerArgs - params.Knobs = base.TestingKnobs{ + var params base.TestClusterArgs + params.ServerArgs.Knobs = base.TestingKnobs{ SQLLeaseManager: &lease.ManagerTestingKnobs{ LeaseStoreTestingKnobs: lease.StorageTestingKnobs{ // We want to track when leases get acquired and when they are renewed. @@ -1259,14 +1261,14 @@ func TestLeaseRenewedAutomatically(testingT *testing.T) { }, }, } - params.Settings = cluster.MakeTestingClusterSettings() + params.ServerArgs.Settings = cluster.MakeTestingClusterSettings() // The lease jitter is set to ensure newer leases have higher // expiration timestamps. - lease.LeaseJitterFraction.Override(ctx, ¶ms.SV, 0) + lease.LeaseJitterFraction.Override(ctx, ¶ms.ServerArgs.SV, 0) // The renewal timeout is set to be the duration, so background // renewal should begin immediately after accessing a lease. - lease.LeaseRenewalDuration.Override(ctx, ¶ms.SV, - lease.LeaseDuration.Get(¶ms.SV)) + lease.LeaseRenewalDuration.Override(ctx, ¶ms.ServerArgs.SV, + lease.LeaseDuration.Get(¶ms.ServerArgs.SV)) t := newLeaseTest(testingT, params) defer t.cleanup() @@ -1727,7 +1729,7 @@ func TestModificationTimeTxnOrdering(testingT *testing.T) { // Which table to exercise the test against. const descID = keys.LeaseTableID - t := newLeaseTest(testingT, base.TestServerArgs{}) + t := newLeaseTest(testingT, base.TestClusterArgs{}) defer t.cleanup() if _, err := t.db.Exec(` @@ -1820,8 +1822,8 @@ func TestLeaseRenewedPeriodically(testingT *testing.T) { var testAcquisitionBlockCount int32 var expected catalog.DescriptorIDSet - var params base.TestServerArgs - params.Knobs = base.TestingKnobs{ + var params base.TestClusterArgs + params.ServerArgs.Knobs = base.TestingKnobs{ SQLLeaseManager: &lease.ManagerTestingKnobs{ LeaseStoreTestingKnobs: lease.StorageTestingKnobs{ // We want to track when leases get acquired and when they are renewed. @@ -1853,16 +1855,16 @@ func TestLeaseRenewedPeriodically(testingT *testing.T) { }, }, } - params.Settings = cluster.MakeTestingClusterSettings() + params.ServerArgs.Settings = cluster.MakeTestingClusterSettings() // The lease jitter is set to ensure newer leases have higher // expiration timestamps. - lease.LeaseJitterFraction.Override(ctx, ¶ms.SV, 0) + lease.LeaseJitterFraction.Override(ctx, ¶ms.ServerArgs.SV, 0) // Lease duration to something small. - lease.LeaseDuration.Override(ctx, ¶ms.SV, 50*time.Millisecond) + lease.LeaseDuration.Override(ctx, ¶ms.ServerArgs.SV, 50*time.Millisecond) // Renewal timeout to 0 saying that the lease will get renewed only // after the lease expires when a request requests the descriptor. - lease.LeaseRenewalDuration.Override(ctx, ¶ms.SV, 0) + lease.LeaseRenewalDuration.Override(ctx, ¶ms.ServerArgs.SV, 0) t := newLeaseTest(testingT, params) defer t.cleanup() @@ -2066,8 +2068,8 @@ func TestDeleteOrphanedLeases(testingT *testing.T) { defer leaktest.AfterTest(testingT)() defer log.Scope(testingT).Close(testingT) - var params base.TestServerArgs - params.Knobs = base.TestingKnobs{ + var params base.TestClusterArgs + params.ServerArgs.Knobs = base.TestingKnobs{ SQLLeaseManager: &lease.ManagerTestingKnobs{}, } @@ -3132,14 +3134,14 @@ func TestLeaseBulkInsertWithImplicitTxn(t *testing.T) { ctx := context.Background() - var params base.TestServerArgs - params.Settings = cluster.MakeTestingClusterSettings() + var params base.TestClusterArgs + params.ServerArgs.Settings = cluster.MakeTestingClusterSettings() // Set the lease duration such that the next lease acquisition will // require the lease to be reacquired. - lease.LeaseDuration.Override(ctx, ¶ms.SV, 0) + lease.LeaseDuration.Override(ctx, ¶ms.ServerArgs.SV, 0) var leaseManager *lease.Manager leaseTableID := uint64(0) - params.Knobs.SQLExecutor = &sql.ExecutorTestingKnobs{ + params.ServerArgs.Knobs.SQLExecutor = &sql.ExecutorTestingKnobs{ // The before execute hook will be to set up to pause // the beforeExecuteStmt, which will then be resumed // when the beforeExecuteResumeStmt statement is observed. @@ -3183,9 +3185,10 @@ func TestLeaseBulkInsertWithImplicitTxn(t *testing.T) { }, } - srv, conn, _ := serverutils.StartServer(t, params) + srv := serverutils.StartCluster(t, 3, params) defer srv.Stopper().Stop(ctx) - s := srv.ApplicationLayer() + s := srv.Server(0).ApplicationLayer() + conn := srv.ServerConn(0) sql.SecondaryTenantSplitAtEnabled.Override(ctx, &s.ClusterSettings().SV, true) // Setup tables for the test. _, err := conn.Exec(`