Skip to content

Commit

Permalink
sql: re-add GC job on schema element deletion
Browse files Browse the repository at this point in the history
This commit creates GC jobs upon the deletion of an index, table or
database. Similarly to the previous implementation, it considers the
walltime at which the schema change completed to be the drop time of the
schema element.

Release note (sql change): Previously, after deleting an index, table,
or database the relevant schema change job would change its running
status to waiting for GC TTL. The schema change and the GC process are
now decoupled into 2 jobs.

Release justification: This is a follow up to the migration of turning
schema changes into actual jobs. This commit re-adds the ability to
properly GC indexes and tables.
  • Loading branch information
pbardea committed Mar 15, 2020
1 parent 793a920 commit 9c2b292
Show file tree
Hide file tree
Showing 23 changed files with 374 additions and 515 deletions.
1 change: 1 addition & 0 deletions pkg/base/testing_knobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type TestingKnobs struct {
SQLExecutor ModuleTestingKnobs
SQLLeaseManager ModuleTestingKnobs
SQLSchemaChanger ModuleTestingKnobs
GCJob ModuleTestingKnobs
PGWireTestingKnobs ModuleTestingKnobs
SQLMigrationManager ModuleTestingKnobs
DistSQL ModuleTestingKnobs
Expand Down
16 changes: 7 additions & 9 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1347,12 +1347,10 @@ func TestRestoreFailCleanup(t *testing.T) {
defer leaktest.AfterTest(t)()

params := base.TestServerArgs{}
// Disable external processing of mutations so that the final check of
// crdb_internal.tables is guaranteed to not be cleaned up. Although this
// was never observed by a stress test, it is here for safety.
params.Knobs.SQLSchemaChanger = &sql.SchemaChangerTestingKnobs{
// TODO (lucy): Turn on knob to disable GC once the GC job is implemented.
}
// Disable GC job so that the final check of crdb_internal.tables is
// guaranteed to not be cleaned up. Although this was never observed by a
// stress test, it is here for safety.
params.Knobs.GCJob = &sql.GCJobTestingKnobs{RunBeforeResume: func() { select {} /* blocks forever */ }}

const numAccounts = 1000
_, _, sqlDB, dir, cleanup := backupRestoreTestSetupWithParams(t, singleNode, numAccounts,
Expand Down Expand Up @@ -1395,9 +1393,8 @@ func TestRestoreFailDatabaseCleanup(t *testing.T) {
// Disable external processing of mutations so that the final check of
// crdb_internal.tables is guaranteed to not be cleaned up. Although this
// was never observed by a stress test, it is here for safety.
params.Knobs.SQLSchemaChanger = &sql.SchemaChangerTestingKnobs{
// TODO (lucy): Turn on knob to disable GC once the GC job is implemented.
}
blockGC := make(chan struct{})
params.Knobs.GCJob = &sql.GCJobTestingKnobs{RunBeforeResume: func() { <-blockGC }}

const numAccounts = 1000
_, _, sqlDB, dir, cleanup := backupRestoreTestSetupWithParams(t, singleNode, numAccounts,
Expand Down Expand Up @@ -1428,6 +1425,7 @@ func TestRestoreFailDatabaseCleanup(t *testing.T) {
t, `database "data" does not exist`,
`DROP DATABASE data`,
)
close(blockGC)
}

func TestBackupRestoreInterleaved(t *testing.T) {
Expand Down
13 changes: 7 additions & 6 deletions pkg/ccl/partitionccl/drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/gcjob"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/testutils"
Expand All @@ -40,15 +41,16 @@ func TestDropIndexWithZoneConfigCCL(t *testing.T) {

const numRows = 100

defer gcjob.SetSmallMaxGCIntervalForTest()()

asyncNotification := make(chan struct{})

params, _ := tests.CreateTestServerParams()
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &sql.SchemaChangerTestingKnobs{
// TODO (lucy): Currently there's no index GC job implemented. Eventually
// the GC job needs to block until the asyncNotification channel is
// closed, which will probably need to be controlled in a schema change
// knob.
GCJob: &sql.GCJobTestingKnobs{
RunBeforeResume: func() {
<-asyncNotification
},
},
}
s, sqlDBRaw, kvDB := serverutils.StartServer(t, params)
Expand Down Expand Up @@ -116,7 +118,6 @@ func TestDropIndexWithZoneConfigCCL(t *testing.T) {
}
close(asyncNotification)

t.Skip("skipping last portion of test until schema change GC job is implemented")
// Wait for index drop to complete so zone configs are updated.
testutils.SucceedsSoon(t, func() error {
if kvs, err := kvDB.Scan(context.TODO(), indexSpan.Key, indexSpan.EndKey, 0); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/distsql"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
_ "github.com/cockroachdb/cockroach/pkg/sql/gcjob" // register jobs declared outside of pkg/sql
"github.com/cockroachdb/cockroach/pkg/sql/pgwire"
"github.com/cockroachdb/cockroach/pkg/sql/querycache"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
Expand Down Expand Up @@ -821,6 +822,11 @@ func NewServer(cfg Config, stopper *stop.Stopper) (*Server, error) {
} else {
execCfg.SchemaChangerTestingKnobs = new(sql.SchemaChangerTestingKnobs)
}
if gcJobTestingKnobs := s.cfg.TestingKnobs.GCJob; gcJobTestingKnobs != nil {
execCfg.GCJobTestingKnobs = gcJobTestingKnobs.(*sql.GCJobTestingKnobs)
} else {
execCfg.GCJobTestingKnobs = new(sql.GCJobTestingKnobs)
}
if distSQLRunTestingKnobs := s.cfg.TestingKnobs.DistSQL; distSQLRunTestingKnobs != nil {
execCfg.DistSQLRunTestingKnobs = distSQLRunTestingKnobs.(*execinfra.TestingKnobs)
} else {
Expand Down
4 changes: 1 addition & 3 deletions pkg/sql/as_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ func TestAsOfTime(t *testing.T) {
defer leaktest.AfterTest(t)()

params, _ := tests.CreateTestServerParams()
params.Knobs.SQLSchemaChanger = &sql.SchemaChangerTestingKnobs{
// TODO (lucy): Turn on knob to disable GC once the GC job is implemented.
}
params.Knobs.GCJob = &sql.GCJobTestingKnobs{RunBeforeResume: func() { select {} }}
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.TODO())

Expand Down
Loading

0 comments on commit 9c2b292

Please sign in to comment.