Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: test namespace entry for multi-region enum is drained properly #60466

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions pkg/sql/multiregion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import (
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/sqltestutils"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)

func TestSettingPrimaryRegionAmidstDrop(t *testing.T) {
Expand Down Expand Up @@ -127,3 +130,77 @@ func TestSettingPrimaryRegionAmidstDrop(t *testing.T) {
return nil
})
}

// TestDroppingPrimaryRegionAsyncJobFailure drops the primary region of the
// database, which results in dropping the multi-region type descriptor. Then,
// it errors out the async job associated with the type descriptor cleanup and
// ensures the namespace entry is reclaimed back despite the injected error.
// We rely on this behavior to be able to add multi-region capability in the
// future.
func TestDroppingPrimaryRegionAsyncJobFailure(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

// Decrease the adopt loop interval so that retries happen quickly.
defer sqltestutils.SetTestJobsAdoptInterval()()

params, _ := tests.CreateTestServerParams()
params.Locality.Tiers = []roachpb.Tier{
{Key: "region", Value: "us-east-1"},
}

// Protects expectedCleanupRuns
var mu syncutil.Mutex
// We need to cleanup 2 times, once for the multi-region type descriptor and
// once for the array alias of the multi-region type descriptor.
haveWePerformedFirstRoundOfCleanup := false
cleanupFinished := make(chan struct{})
params.Knobs.SQLTypeSchemaChanger = &sql.TypeSchemaChangerTestingKnobs{
RunBeforeExec: func() error {
return errors.New("yikes")
},
RunAfterOnFailOrCancel: func() error {
mu.Lock()
defer mu.Unlock()
if haveWePerformedFirstRoundOfCleanup {
close(cleanupFinished)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if OnFailOrCancel fails, does it retry, or does the entry get stuck?

}
haveWePerformedFirstRoundOfCleanup = true
return nil
},
}

s, sqlDB, _ := serverutils.StartServer(t, params)
ctx := context.Background()
defer s.Stopper().Stop(ctx)

// Setup the test.
_, err := sqlDB.Exec(`
CREATE DATABASE db WITH PRIMARY REGION "us-east-1";
CREATE TABLE db.t(k INT) LOCALITY REGIONAL BY TABLE IN PRIMARY REGION;
`)
require.NoError(t, err)

_, err = sqlDB.Exec(`ALTER DATABASE db DROP REGION "us-east-1"`)
testutils.IsError(err, "yikes")

<-cleanupFinished

rows := sqlDB.QueryRow(`SELECT count(*) FROM system.namespace WHERE name = 'crdb_internal_region'`)
var count int
err = rows.Scan(&count)
require.NoError(t, err)
if count != 0 {
t.Fatal("expected crdb_internal_region not to be present in system.namespace")
}

_, err = sqlDB.Exec(`ALTER DATABASE db PRIMARY REGION "us-east-1"`)
require.NoError(t, err)

rows = sqlDB.QueryRow(`SELECT count(*) FROM system.namespace WHERE name = 'crdb_internal_region'`)
err = rows.Scan(&count)
require.NoError(t, err)
if count != 1 {
t.Fatal("expected crdb_internal_region to be present in system.namespace")
}
}