Skip to content

Commit

Permalink
Merge pull request cockroachdb#100137 from chengxiong-ruan/backport22…
Browse files Browse the repository at this point in the history
….2-99888

release-22.2: sql: mark index as GCed if table has been GCed in legacy gc path
  • Loading branch information
chengxiong-ruan committed Mar 30, 2023
2 parents 9aa5d3f + 29ac031 commit e7b37a7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/sql/gcjob/refresh_statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ func updateStatusForGCElements(
if errors.Is(err, catalog.ErrDescriptorNotFound) {
log.Warningf(ctx, "table %d not found, marking as GC'd", tableID)
markTableGCed(ctx, tableID, progress, jobspb.SchemaChangeGCProgress_CLEARED)
for indexID := range indexDropTimes {
markIndexGCed(ctx, indexID, progress, jobspb.SchemaChangeGCProgress_CLEARED)
}
return false, true, maxDeadline
}
log.Warningf(ctx, "error while calculating GC time for table %d, err: %+v", tableID, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/gcjob_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ go_test(
"//pkg/sql/gcjob/gcjobnotifier",
"//pkg/sql/sem/catid",
"//pkg/sql/sqlutil",
"//pkg/sql/tests",
"//pkg/storage",
"//pkg/testutils",
"//pkg/testutils/jobutils",
Expand Down
68 changes: 68 additions & 0 deletions pkg/sql/gcjob_test/gc_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/gcjob/gcjobnotifier"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/jobutils"
Expand Down Expand Up @@ -771,3 +772,70 @@ func (f *fakeSystemConfigProvider) numCalls() int {
}

var _ config.SystemConfigProvider = (*fakeSystemConfigProvider)(nil)

func TestLegacyIndexGCSucceedsWithMissingDescriptor(t *testing.T) {
defer leaktest.AfterTest(t)()
params, _ := tests.CreateTestServerParams()
params.Knobs.JobsTestingKnobs = jobs.NewTestingKnobsWithShortIntervals()

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

tDB.Exec(t, `CREATE TABLE t(a INT)`)
tDB.Exec(t, `INSERT INTO t VALUES (1), (2)`)
tDB.Exec(t, `TRUNCATE TABLE t`)

var truncateJobID string
testutils.SucceedsSoon(t, func() error {
rslt := tDB.QueryStr(t, `SELECT job_id, status, running_status FROM [SHOW JOBS] WHERE description = 'GC for TRUNCATE TABLE defaultdb.public.t'`)
if len(rslt) != 1 {
t.Fatalf("expect only 1 truncate job, found %d", len(rslt))
}
if rslt[0][1] != "running" {
return errors.New("job not running yet")
}
if rslt[0][2] != "waiting for GC TTL" {
return errors.New("not waiting for gc yet")
}
truncateJobID = rslt[0][0]
return nil
})

tDB.Exec(t, `PAUSE JOB `+truncateJobID)
testutils.SucceedsSoon(t, func() error {
rslt := tDB.QueryStr(t, `SELECT status FROM [SHOW JOBS] WHERE job_id = `+truncateJobID)
if len(rslt) != 1 {
t.Fatalf("expect only 1 truncate job, found %d", len(rslt))
}
if rslt[0][0] != "paused" {
return errors.New("job not paused yet")
}
return nil
})

tDB.Exec(t, `ALTER TABLE t CONFIGURE ZONE USING gc.ttlseconds = 1;`)
tDB.Exec(t, `DROP TABLE t`)
testutils.SucceedsSoon(t, func() error {
rslt := tDB.QueryStr(t, `SELECT status FROM [SHOW JOBS] WHERE description = 'GC for DROP TABLE defaultdb.public.t'`)
if len(rslt) != 1 {
t.Fatalf("expect only 1 truncate job, found %d", len(rslt))
}
if rslt[0][0] != "succeeded" {
return errors.New("job not running yet")
}
return nil
})

tDB.Exec(t, `RESUME JOB `+truncateJobID)
testutils.SucceedsSoon(t, func() error {
rslt := tDB.QueryStr(t, `SELECT status FROM [SHOW JOBS] WHERE job_id = `+truncateJobID)
if len(rslt) != 1 {
t.Fatalf("expect only 1 truncate job, found %d", len(rslt))
}
if rslt[0][0] != "succeeded" {
return errors.New("job not running")
}
return nil
})
}

0 comments on commit e7b37a7

Please sign in to comment.