Skip to content

Commit

Permalink
backupccl: add test showing restored tenant cancel import
Browse files Browse the repository at this point in the history
Release note: none.
  • Loading branch information
dt committed Feb 12, 2022
1 parent faa731a commit e9fa45f
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/ccl/backupccl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ go_test(
"backup_destination_test.go",
"backup_intents_test.go",
"backup_rand_test.go",
"backup_tenant_test.go",
"backup_test.go",
"bench_covering_test.go",
"bench_test.go",
Expand All @@ -167,6 +168,7 @@ go_test(
deps = [
"//pkg/base",
"//pkg/blobs",
"//pkg/ccl/importccl",
"//pkg/ccl/kvccl",
"//pkg/ccl/multiregionccl",
"//pkg/ccl/multitenantccl",
Expand Down
121 changes: 121 additions & 0 deletions pkg/ccl/backupccl/backup_tenant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2022 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt

package backupccl_test

import (
"context"
"testing"

"github.com/cockroachdb/cockroach/pkg/base"
_ "github.com/cockroachdb/cockroach/pkg/ccl/importccl"
_ "github.com/cockroachdb/cockroach/pkg/cloud/impl" // register cloud storage providers
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)

func TestBackupTenantImportingTable(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{})
defer tc.Stopper().Stop(ctx)
sqlDB := sqlutils.MakeSQLRunner(tc.Conns[0])

tSrv, tSQL := serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{
TenantID: roachpb.MakeTenantID(10),
TestingKnobs: base.TestingKnobs{JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals()},
})
defer tSQL.Close()

if _, err := tSQL.Exec("SET CLUSTER SETTING jobs.debug.pausepoints = 'import.after_ingest';"); err != nil {
t.Fatal(err)
}
if _, err := tSQL.Exec("CREATE TABLE x (id INT PRIMARY KEY, n INT, s STRING)"); err != nil {
t.Fatal(err)
}
if _, err := tSQL.Exec("INSERT INTO x VALUES (1000, 1, 'existing')"); err != nil {
t.Fatal(err)
}
if _, err := tSQL.Exec("IMPORT INTO x CSV DATA ('workload:///csv/bank/bank?rows=100&version=1.0.0')"); !testutils.IsError(err, "pause") {
t.Fatal(err)
}
var jobID int
if err := tSQL.QueryRow(`SELECT job_id FROM [show jobs] WHERE job_type = 'IMPORT'`).Scan(&jobID); err != nil {
t.Fatal(err)
}
tc.Servers[0].JobRegistry().(*jobs.Registry).TestingNudgeAdoptionQueue()
// wait for it to pause

testutils.SucceedsSoon(t, func() error {
var status string
if err := tSQL.QueryRow(`SELECT status FROM [show jobs] WHERE job_id = $1`, jobID).Scan(&status); err != nil {
t.Fatal(err)
}
if status == string(jobs.StatusPaused) {
return nil
}
return errors.Newf("%s", status)
})

// tenant now has a fully ingested, paused import, so back them up.
const dst = "userfile:///t"
if _, err := sqlDB.DB.ExecContext(ctx, `BACKUP TENANT 10 TO $1`, dst); err != nil {
t.Fatal(err)
}
// Destroy the tenant, then restore it.
tSrv.Stopper().Stop(ctx)
if _, err := sqlDB.DB.ExecContext(ctx, "SELECT crdb_internal.destroy_tenant(10, true)"); err != nil {
t.Fatal(err)
}
if _, err := sqlDB.DB.ExecContext(ctx, "RESTORE TENANT 10 FROM $1", dst); err != nil {
t.Fatal(err)
}
tSrv, tSQL = serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{
TenantID: roachpb.MakeTenantID(10),
Existing: true,
TestingKnobs: base.TestingKnobs{JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals()},
})
defer tSQL.Close()

if _, err := tSQL.Exec(`UPDATE system.jobs SET claim_session_id = NULL, claim_instance_id = NULL WHERE id = $1`, jobID); err != nil {
t.Fatal(err)
}
if _, err := tSQL.Exec(`DELETE FROM system.lease`); err != nil {
t.Fatal(err)
}
if _, err := tSQL.Exec(`CANCEL JOB $1`, jobID); err != nil {
t.Fatal(err)
}
tSrv.JobRegistry().(*jobs.Registry).TestingNudgeAdoptionQueue()
testutils.SucceedsSoon(t, func() error {
var status string
if err := tSQL.QueryRow(`SELECT status FROM [show jobs] WHERE job_id = $1`, jobID).Scan(&status); err != nil {
t.Fatal(err)
}
if status == string(jobs.StatusCanceled) {
return nil
}
return errors.Newf("%s", status)
})

var rowCount int
if err := tSQL.QueryRow(`SELECT count(*) FROM x`).Scan(&rowCount); err != nil {
t.Fatal(err)
}
require.Equal(t, 1, rowCount)
}

0 comments on commit e9fa45f

Please sign in to comment.