Skip to content

Commit bdca2a4

Browse files
sql: unskip TestCancelSchemaChange
Informs cockroachdb#51796 This commit unskip TestCancelSchemaChange by fixing several wrong assumption of schema changer jobs. Also removed some variables which made no effects on the test. Release note: None Release justification: test only change.
1 parent 2d508b2 commit bdca2a4

File tree

1 file changed

+29
-46
lines changed

1 file changed

+29
-46
lines changed

pkg/sql/schema_changer_test.go

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4699,18 +4699,14 @@ ALTER TABLE t.test ADD COLUMN c INT AS (v + 4) STORED, ADD COLUMN d INT DEFAULT
46994699
func TestCancelSchemaChange(t *testing.T) {
47004700
defer leaktest.AfterTest(t)()
47014701
defer log.Scope(t).Close(t)
4702-
skip.WithIssue(t, 51796)
47034702

47044703
const (
4705-
numNodes = 3
47064704
maxValue = 100
47074705
)
47084706

47094707
var sqlDB *sqlutils.SQLRunner
4710-
var db *gosql.DB
47114708
params, _ := createTestServerParams()
47124709
doCancel := false
4713-
var enableAsyncSchemaChanges uint32
47144710
params.Knobs = base.TestingKnobs{
47154711
SQLSchemaChanger: &sql.SchemaChangerTestingKnobs{
47164712
WriteCheckpointInterval: time.Nanosecond, // checkpoint after every chunk.
@@ -4721,52 +4717,41 @@ func TestCancelSchemaChange(t *testing.T) {
47214717
if !doCancel {
47224718
return nil
47234719
}
4724-
if _, err := db.Exec(`CANCEL JOB (
4720+
sqlDB.Exec(t, `CANCEL JOB (
47254721
SELECT job_id FROM [SHOW JOBS]
47264722
WHERE
47274723
job_type = 'SCHEMA CHANGE' AND
47284724
status = $1 AND
47294725
description NOT LIKE 'ROLL BACK%'
4730-
)`, jobs.StatusRunning); err != nil {
4731-
panic(err)
4732-
}
4726+
)`, jobs.StatusRunning)
47334727
return nil
47344728
},
47354729
},
47364730
}
47374731

4738-
tc := serverutils.StartNewTestCluster(t, numNodes, base.TestClusterArgs{
4739-
ReplicationMode: base.ReplicationManual,
4740-
ServerArgs: params,
4741-
})
4742-
defer tc.Stopper().Stop(context.Background())
4743-
db = tc.ServerConn(0)
4744-
kvDB := tc.Server(0).DB()
4745-
codec := tc.Server(0).ApplicationLayer().Codec()
4732+
server, db, kvDB := serverutils.StartServer(t, params)
47464733
sqlDB = sqlutils.MakeSQLRunner(db)
4734+
defer server.Stopper().Stop(context.Background())
4735+
codec := server.ApplicationLayer().Codec()
47474736

47484737
// Disable strict GC TTL enforcement because we're going to shove a zero-value
47494738
// TTL into the system with AddImmediateGCZoneConfig.
47504739
defer sqltestutils.DisableGCTTLStrictEnforcement(t, db)()
47514740

47524741
sqlDB.Exec(t, `
4742+
SET use_declarative_schema_changer = 'off';
47534743
CREATE DATABASE t;
47544744
CREATE TABLE t.test (k INT PRIMARY KEY, v INT, pi DECIMAL DEFAULT (DECIMAL '3.14'));
47554745
`)
47564746

4747+
sqlDB.Exec(t, `SET CLUSTER SETTING jobs.registry.interval.adopt = '1s';`)
4748+
47574749
// Bulk insert.
47584750
if err := sqltestutils.BulkInsertIntoTable(db, maxValue); err != nil {
47594751
t.Fatal(err)
47604752
}
47614753

47624754
tableDesc := desctestutils.TestingGetPublicTableDescriptor(kvDB, codec, "t", "test")
4763-
// Split the table into multiple ranges.
4764-
var sps []serverutils.SplitPoint
4765-
const numSplits = numNodes * 2
4766-
for i := 1; i <= numSplits-1; i++ {
4767-
sps = append(sps, serverutils.SplitPoint{TargetNodeIdx: i % numNodes, Vals: []interface{}{maxValue / numSplits * i}})
4768-
}
4769-
tc.SplitTable(t, tableDesc, sps)
47704755

47714756
ctx := context.Background()
47724757
if err := sqltestutils.CheckTableKeyCount(ctx, kvDB, codec, 1, maxValue); err != nil {
@@ -4780,49 +4765,48 @@ func TestCancelSchemaChange(t *testing.T) {
47804765
// Set to true if the rollback returns in a running, waiting status.
47814766
isGC bool
47824767
}{
4783-
{`ALTER TABLE t.public.test ADD COLUMN x DECIMAL DEFAULT 1.4::DECIMAL CREATE FAMILY f2, ADD CHECK (x >= 0)`,
4768+
{`ALTER TABLE t.public.test ADD COLUMN x DECIMAL DEFAULT 1.4 CREATE FAMILY f2, ADD CHECK (x >= 0)`,
47844769
true, false},
47854770
{`CREATE INDEX foo ON t.public.test (v)`,
47864771
true, true},
4787-
{`ALTER TABLE t.public.test ADD COLUMN x DECIMAL DEFAULT 1.2::DECIMAL CREATE FAMILY f3, ADD CHECK (x >= 0)`,
4772+
{`ALTER TABLE t.public.test ADD COLUMN x DECIMAL DEFAULT 1.2 CREATE FAMILY f3, ADD CHECK (x >= 0)`,
47884773
false, false},
47894774
{`CREATE INDEX foo ON t.public.test (v)`,
47904775
false, true},
47914776
}
47924777

47934778
idx := 0
4779+
gcIdx := 0
47944780
for _, tc := range testCases {
47954781
doCancel = tc.cancel
47964782
if doCancel {
47974783
if _, err := db.Exec(tc.sql); !testutils.IsError(err, "job canceled") {
47984784
t.Fatalf("unexpected %v", err)
47994785
}
4800-
if err := jobutils.VerifySystemJob(t, sqlDB, idx, jobspb.TypeSchemaChange, jobs.StatusCanceled, jobs.Record{
4801-
Username: username.RootUserName(),
4802-
Description: tc.sql,
4803-
DescriptorIDs: descpb.IDs{
4804-
tableDesc.GetID(),
4805-
},
4806-
}); err != nil {
4807-
t.Fatal(err)
4808-
}
4809-
jobID := jobutils.GetJobID(t, sqlDB, idx)
4810-
idx++
4786+
testutils.SucceedsSoon(t, func() error {
4787+
return jobutils.VerifySystemJob(
4788+
t, sqlDB, idx, jobspb.TypeSchemaChange, jobs.StatusCanceled,
4789+
jobs.Record{
4790+
Username: username.RootUserName(),
4791+
Description: tc.sql,
4792+
DescriptorIDs: descpb.IDs{
4793+
tableDesc.GetID(),
4794+
},
4795+
},
4796+
)
4797+
})
48114798
jobRecord := jobs.Record{
48124799
Username: username.RootUserName(),
4813-
Description: fmt.Sprintf("ROLL BACK JOB %d: %s", jobID, tc.sql),
4800+
Description: fmt.Sprintf("GC for ROLLBACK of %s", tc.sql),
48144801
DescriptorIDs: descpb.IDs{
48154802
tableDesc.GetID(),
48164803
},
48174804
}
4818-
var err error
48194805
if tc.isGC {
4820-
err = jobutils.VerifyRunningSystemJob(t, sqlDB, idx, jobspb.TypeSchemaChange, sql.RunningStatusWaitingGC, jobRecord)
4821-
} else {
4822-
err = jobutils.VerifySystemJob(t, sqlDB, idx, jobspb.TypeSchemaChange, jobs.StatusSucceeded, jobRecord)
4823-
}
4824-
if err != nil {
4825-
t.Fatal(err)
4806+
testutils.SucceedsSoon(t, func() error {
4807+
return jobutils.VerifyRunningSystemJob(t, sqlDB, gcIdx*2, jobspb.TypeSchemaChangeGC, sql.RunningStatusWaitingForMVCCGC, jobRecord)
4808+
})
4809+
gcIdx++
48264810
}
48274811
} else {
48284812
sqlDB.Exec(t, tc.sql)
@@ -4841,7 +4825,7 @@ func TestCancelSchemaChange(t *testing.T) {
48414825

48424826
// Verify that the index foo over v is consistent, and that column x has
48434827
// been backfilled properly.
4844-
rows, err := db.Query(`SELECT v, x from t.test@foo`)
4828+
rows, err := db.Query(`SELECT v, x from t.test@foo ORDER BY v`)
48454829
if err != nil {
48464830
t.Fatal(err)
48474831
}
@@ -4871,7 +4855,6 @@ func TestCancelSchemaChange(t *testing.T) {
48714855
}
48724856

48734857
// Verify that the data from the canceled CREATE INDEX is cleaned up.
4874-
atomic.StoreUint32(&enableAsyncSchemaChanges, 1)
48754858
// TODO (lucy): when this test is no longer canceled, have it correctly handle doing GC immediately
48764859
if _, err := sqltestutils.AddImmediateGCZoneConfig(db, tableDesc.GetID()); err != nil {
48774860
t.Fatal(err)

0 commit comments

Comments
 (0)