Skip to content

Commit

Permalink
gcjob: fix race in gc job test
Browse files Browse the repository at this point in the history
In the GC job test, the table descriptors were updated after creating
the job. Most of the time, the table descriptors were by the time the GC
job actually started running, but sometimes they would not be. This lead
to the GC job not noticing the dropped tables and therefore take too
long to complete (as it would wait another 5 minutes to check again).

This change ensures that the table descriptors are updated before the GC
job is created.

Release justification: only touches testing code, and fixes a flake.
Release note: None
  • Loading branch information
pbardea committed Mar 31, 2020
1 parent 8a50e9f commit 5a49e4d
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions pkg/sql/gcjob_test/gc_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func TestSchemaChangeGCJob(t *testing.T) {
},
ParentID: myTableID,
}
myTableDesc.Indexes = myTableDesc.Indexes[:0]
myTableDesc.GCMutations = append(myTableDesc.GCMutations, sqlbase.TableDescriptor_GCDescriptorMutation{
IndexID: sqlbase.IndexID(2),
})
case TABLE:
details = jobspb.SchemaChangeGCDetails{
Tables: []jobspb.SchemaChangeGCDetails_DroppedID{
Expand All @@ -117,6 +121,8 @@ func TestSchemaChangeGCJob(t *testing.T) {
},
},
}
myTableDesc.State = sqlbase.TableDescriptor_DROP
myTableDesc.DropTime = dropTime
case DATABASE:
details = jobspb.SchemaChangeGCDetails{
Tables: []jobspb.SchemaChangeGCDetails_DroppedID{
Expand All @@ -131,6 +137,23 @@ func TestSchemaChangeGCJob(t *testing.T) {
},
ParentID: myDBID,
}
myTableDesc.State = sqlbase.TableDescriptor_DROP
myTableDesc.DropTime = dropTime
myOtherTableDesc.State = sqlbase.TableDescriptor_DROP
myOtherTableDesc.DropTime = dropTime
}

if err := kvDB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
b := txn.NewBatch()
descKey := sqlbase.MakeDescMetadataKey(myTableID)
descDesc := sqlbase.WrapDescriptor(myTableDesc)
b.Put(descKey, descDesc)
descKey2 := sqlbase.MakeDescMetadataKey(myOtherTableID)
descDesc2 := sqlbase.WrapDescriptor(myOtherTableDesc)
b.Put(descKey2, descDesc2)
return txn.Run(ctx, b)
}); err != nil {
t.Fatal(err)
}

jobRecord := jobs.Record{
Expand All @@ -156,34 +179,6 @@ func TestSchemaChangeGCJob(t *testing.T) {
t.Fatal(err)
}

switch dropItem {
case INDEX:
myTableDesc.Indexes = myTableDesc.Indexes[:0]
myTableDesc.GCMutations = append(myTableDesc.GCMutations, sqlbase.TableDescriptor_GCDescriptorMutation{
IndexID: sqlbase.IndexID(2),
})
case DATABASE:
myOtherTableDesc.State = sqlbase.TableDescriptor_DROP
myOtherTableDesc.DropTime = dropTime
fallthrough
case TABLE:
myTableDesc.State = sqlbase.TableDescriptor_DROP
myTableDesc.DropTime = dropTime
}

if err := kvDB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
b := txn.NewBatch()
descKey := sqlbase.MakeDescMetadataKey(myTableID)
descDesc := sqlbase.WrapDescriptor(myTableDesc)
b.Put(descKey, descDesc)
descKey2 := sqlbase.MakeDescMetadataKey(myOtherTableID)
descDesc2 := sqlbase.WrapDescriptor(myOtherTableDesc)
b.Put(descKey2, descDesc2)
return txn.Run(ctx, b)
}); err != nil {
t.Fatal(err)
}

// Check that the job started.
jobIDStr := strconv.Itoa(int(*job.ID()))
if err := jobutils.VerifySystemJob(t, sqlDB, 0, jobspb.TypeSchemaChangeGC, jobs.StatusRunning, lookupJR); err != nil {
Expand Down

0 comments on commit 5a49e4d

Please sign in to comment.