Skip to content

Commit

Permalink
server: use channel for DisableAutomaticVersionUpgrade
Browse files Browse the repository at this point in the history
DisableAutomaticVersionUpgrade is an atomic integer which is rechecked
in a retry loop. This is not a very clean mechanism, and can lead to
issues where you're unknowingly dealing with a copy of the knobs and
setting the wrong atomic. The retry loop can also add unnecessary
delays in tests.

This commit changes DisableAutomaticVersionUpgrade from an atomic
integer to a channel. If the channel is set, auto-upgrade waits until
the channel is closed.

Release note: None
  • Loading branch information
RaduBerinde committed Feb 15, 2022
1 parent dd4fa26 commit b2e112b
Show file tree
Hide file tree
Showing 27 changed files with 61 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestInsertMissingPublicSchemaNamespaceEntry(t *testing.T) {
ExternalIODir: dir,
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.InsertPublicSchemaNamespaceEntryOnRestore - 1),
},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/ccl/backupccl/restore_old_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ func restorePublicSchemaMixedVersion(exportDir string) func(t *testing.T) {
Knobs: base.TestingKnobs{
JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals(),
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.PublicSchemasWithDescriptors - 1),
},
},
Expand Down Expand Up @@ -920,7 +920,7 @@ func restoreSyntheticPublicSchemaNamespaceEntry(exportDir string) func(t *testin
Knobs: base.TestingKnobs{
JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals(),
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.PublicSchemasWithDescriptors - 1),
},
},
Expand Down Expand Up @@ -951,7 +951,7 @@ func restoreSyntheticPublicSchemaNamespaceEntryCleanupOnFail(exportDir string) f
Knobs: base.TestingKnobs{
JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals(),
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.PublicSchemasWithDescriptors - 1),
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/importccl/import_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7381,7 +7381,7 @@ func TestImportMixedVersion(t *testing.T) {
Knobs: base.TestingKnobs{
JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals(),
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.PublicSchemasWithDescriptors - 1),
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/kvccl/kvtenantccl/tenant_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestTenantUpgrade(t *testing.T) {
Settings: settings,
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.TestingBinaryMinSupportedVersion,
},
},
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestTenantUpgradeFailure(t *testing.T) {
Settings: settings,
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.TestingBinaryMinSupportedVersion,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestPreSeedSpanConfigsWrittenWhenActive(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.PreSeedTenantSpanConfigs,
),
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestSeedTenantSpanConfigs(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.PreSeedTenantSpanConfigs - 1,
),
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestSeedTenantSpanConfigsWithExistingEntry(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.PreSeedTenantSpanConfigs,
),
Expand Down
2 changes: 1 addition & 1 deletion pkg/jobs/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,7 @@ func TestStartableJobMixedVersion(t *testing.T) {
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
BinaryVersionOverride: clusterversion.TestingBinaryMinSupportedVersion,
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/client_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestMigrateWaitsForApplication(t *testing.T) {
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
BinaryVersionOverride: startV,
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
},
Store: &kvserver.StoreTestingKnobs{
TestingApplyFilter: func(args kvserverbase.ApplyFilterArgs) (int, *roachpb.Error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/client_replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4118,7 +4118,7 @@ func TestRangeMigration(t *testing.T) {
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
BinaryVersionOverride: startV,
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/migration/migrationmanager/manager_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestAlreadyRunningJobsAreHandledProperly(t *testing.T) {
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
BinaryVersionOverride: startCV.Version,
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
},
MigrationManager: &migration.TestingKnobs{
ListBetweenOverride: func(from, to clusterversion.ClusterVersion) []clusterversion.ClusterVersion {
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestMigrateUpdatesReplicaVersion(t *testing.T) {
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
BinaryVersionOverride: startCV.Version,
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
},
MigrationManager: &migration.TestingKnobs{
ListBetweenOverride: func(from, to clusterversion.ClusterVersion) []clusterversion.ClusterVersion {
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestConcurrentMigrationAttempts(t *testing.T) {
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
BinaryVersionOverride: versions[0].Version,
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
},
MigrationManager: &migration.TestingKnobs{
ListBetweenOverride: func(from, to clusterversion.ClusterVersion) []clusterversion.ClusterVersion {
Expand Down Expand Up @@ -401,7 +401,7 @@ func TestPauseMigration(t *testing.T) {
JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals(),
Server: &server.TestingKnobs{
BinaryVersionOverride: startCV.Version,
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
},
MigrationManager: &migration.TestingKnobs{
ListBetweenOverride: func(from, to clusterversion.ClusterVersion) []clusterversion.ClusterVersion {
Expand Down Expand Up @@ -521,7 +521,7 @@ func TestPrecondition(t *testing.T) {
}
knobs := base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: v0.Version,
},
// Inject a migration which would run to upgrade the cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAlterSystemStmtDiagReqs(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.AlterSystemStmtDiagReqs - 1),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestAlterSystemProtectedTimestampRecordsTable(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.AlterSystemProtectedTimestampAddColumn - 1),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestAlterSystemTableStatisticsTable(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.AlterSystemTableStatisticsAddAvgSizeCol - 1),
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/migration/migrations/builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestIsAtLeastVersionBuiltin(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.V21_2),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestEnsureIndexesExistForComments(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.DeleteCommentsWithDroppedIndexes - 1),
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/migration/migrations/ensure_constraint_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestEnsureConstraintIDs(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
tabledesc.ConstraintIDsAddedToTableDescsVersion - 1),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestEnsureNoDrainingNames(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.AvoidDrainingNames - 1),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestGrantOptionMigration(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.ValidateGrantOption - 1), // changed cluster version
},
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/migration/migrations/migrate_span_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestEnsureSpanConfigReconciliation(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.EnsureSpanConfigReconciliation - 1,
),
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestEnsureSpanConfigReconciliationMultiNode(t *testing.T) {
serverArgs[i] = base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.EnsureSpanConfigReconciliation - 1,
),
Expand All @@ -133,7 +133,7 @@ func TestEnsureSpanConfigReconciliationMultiNode(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.EnsureSpanConfigReconciliation - 1,
),
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestEnsureSpanConfigSubscription(t *testing.T) {
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.EnsureSpanConfigSubscription - 1,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func publicSchemaMigrationTest(t *testing.T, ctx context.Context, numTables int)
Settings: settings,
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.PublicSchemasWithDescriptors - 1),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestRaftAppliedIndexTermMigration(t *testing.T) {
// Start at the version immediately preceding the migration.
BinaryVersionOverride: bootstrapVersion,
// We want to exercise manual control over the upgrade process.
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
}
return args
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestLatestClusterDoesNotNeedRaftAppliedIndexTermMigration(t *testing.T) {
args.Knobs.Server = &server.TestingKnobs{
BinaryVersionOverride: binaryVersion,
// We want to exercise manual control over the upgrade process.
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
}
return args
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestConvertIncompatibleDatabasePrivilegesToDefaultPrivileges(t *testing.T)
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(
clusterversion.RemoveIncompatibleDatabasePrivileges - 1),
},
Expand Down
20 changes: 12 additions & 8 deletions pkg/server/auto_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package server

import (
"context"
"sync/atomic"
"time"

"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb"
Expand All @@ -36,16 +35,21 @@ func (s *Server) startAttemptUpgrade(ctx context.Context) {
Closer: s.stopper.ShouldQuiesce(),
}

for r := retry.StartWithCtx(ctx, retryOpts); r.Next(); {
// Check if auto upgrade is disabled for test purposes.
if k := s.cfg.TestingKnobs.Server; k != nil {
upgradeTestingKnobs := k.(*TestingKnobs)
if disable := atomic.LoadInt32(&upgradeTestingKnobs.DisableAutomaticVersionUpgrade); disable == 1 {
log.Infof(ctx, "auto upgrade disabled by testing")
continue
// Check if auto upgrade is disabled for test purposes.
if k := s.cfg.TestingKnobs.Server; k != nil {
upgradeTestingKnobs := k.(*TestingKnobs)
if disableCh := upgradeTestingKnobs.DisableAutomaticVersionUpgrade; disableCh != nil {
log.Infof(ctx, "auto upgrade disabled by testing")
select {
case <-disableCh:
log.Infof(ctx, "auto upgrade no longer disabled by testing")
case <-s.stopper.ShouldQuiesce():
return
}
}
}

for r := retry.StartWithCtx(ctx, retryOpts); r.Next(); {
// Check if we should upgrade cluster version, keep checking upgrade
// status, or stop attempting upgrade.
if quit, err := s.upgradeStatus(ctx); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestBumpClusterVersion(t *testing.T) {
BinaryVersionOverride: test.activeClusterVersion.Version,
// We're bumping cluster versions manually ourselves. We
// want avoid racing with the auto-upgrade process.
DisableAutomaticVersionUpgrade: 1,
DisableAutomaticVersionUpgrade: make(chan struct{}),
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/testing_knobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
// TestingKnobs groups testing knobs for the Server.
type TestingKnobs struct {
// DisableAutomaticVersionUpgrade, if set, temporarily disables the server's
// automatic version upgrade mechanism.
DisableAutomaticVersionUpgrade int32 // accessed atomically
// automatic version upgrade mechanism (until the channel is closed).
DisableAutomaticVersionUpgrade chan struct{}
// DefaultZoneConfigOverride, if set, overrides the default zone config
// defined in `pkg/config/zone.go`.
DefaultZoneConfigOverride *zonepb.ZoneConfig
Expand Down
Loading

0 comments on commit b2e112b

Please sign in to comment.