Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-21.1: backupccl: mark type descs as dropped if there is a failure in restore #62454

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6817,6 +6817,39 @@ func TestBackupDoesNotHangOnIntent(t *testing.T) {
require.Error(t, tx.Commit())
}

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

_, tc, sqlDB, _, cleanupFn := BackupRestoreTestSetup(t, singleNode, 0, InitManualReplication)
defer cleanupFn()

for _, server := range tc.Servers {
registry := server.JobRegistry().(*jobs.Registry)
registry.TestingResumerCreationKnobs = map[jobspb.Type]func(raw jobs.Resumer) jobs.Resumer{
jobspb.TypeRestore: func(raw jobs.Resumer) jobs.Resumer {
r := raw.(*restoreResumer)
r.testingKnobs.beforePublishingDescriptors = func() error {
return errors.New("boom")
}
return r
},
}
}

sqlDB.Exec(t, `
CREATE DATABASE db;
CREATE TYPE db.typ AS ENUM();
CREATE TABLE db.table (k INT PRIMARY KEY, v db.typ);
`)

// Back up the database, drop it, and restore into it.
sqlDB.Exec(t, `BACKUP DATABASE db TO 'nodelocal://0/test/'`)
sqlDB.Exec(t, `DROP DATABASE db`)
sqlDB.ExpectErr(t, "boom", `RESTORE DATABASE db FROM 'nodelocal://0/test/'`)
sqlDB.CheckQueryResults(t, `SELECT count(*) FROM system.namespace WHERE name = 'typ'`, [][]string{{"0"}})
}

// TestRestoreResetsDescriptorVersions tests that new descriptors created while
// restoring have their versions reset. Descriptors end up at version 2 after
// the job is finished, since they are updated once at the end of the job to
Expand Down
15 changes: 15 additions & 0 deletions pkg/ccl/backupccl/restore_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,16 @@ func (r *restoreResumer) dropDescriptors(
// TypeDescriptors don't have a GC job process, so we can just write them
// as dropped here.
typDesc := details.TypeDescs[i]
mutType, err := descsCol.GetMutableTypeByID(ctx, txn, typDesc.ID, tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{
AvoidCached: true,
IncludeOffline: true,
},
})
if err != nil {
return err
}

catalogkv.WriteObjectNamespaceEntryRemovalToBatch(
ctx,
b,
Expand All @@ -1905,6 +1915,11 @@ func (r *restoreResumer) dropDescriptors(
typDesc.Name,
false, /* kvTrace */
)
mutType.State = descpb.DescriptorState_DROP
if err := descsCol.WriteDescToBatch(ctx, false /* kvTrace */, mutType, b); err != nil {
return errors.Wrap(err, "writing dropping type to batch")
}
// Remove the system.descriptor entry.
b.Del(catalogkeys.MakeDescMetadataKey(codec, typDesc.ID))
}

Expand Down