Skip to content

Commit 26bee83

Browse files
committed
restore: delete comments after failed or cancelled cluster restore
This patch ensures that after a failed or canceled cluster restore, comments from dropped descriptors are removed. Informs #149453 Release note: none
1 parent 2f72cf4 commit 26bee83

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

pkg/backup/backup_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11183,3 +11183,60 @@ CREATE TABLE child_pk (k INT8 PRIMARY KEY REFERENCES parent);
1118311183
sqlDB.Exec(t, `DROP DATABASE test`)
1118411184
}
1118511185
}
11186+
11187+
func TestRestoreFailureDeletesComments(t *testing.T) {
11188+
defer leaktest.AfterTest(t)()
11189+
defer log.Scope(t).Close(t)
11190+
11191+
_, sqlDB, cleanupFn := backupRestoreTestSetupEmpty(t, singleNode, "", InitManualReplication, base.TestClusterArgs{})
11192+
defer cleanupFn()
11193+
11194+
// Set pause point for after the system tables have been published.
11195+
sqlDB.Exec(t, `SET CLUSTER SETTING jobs.debug.pausepoints = 'restore.after_cleanup_temp_system_tables'`)
11196+
11197+
commentCountQuery := `SELECT count(*) FROM system.comments`
11198+
11199+
var count int
11200+
sqlDB.QueryRow(t, commentCountQuery).Scan(&count)
11201+
require.Equal(t, 0, count)
11202+
11203+
// Create a database with tables, types, and schemas that have comments
11204+
sqlDB.Exec(t, `CREATE DATABASE test_db`)
11205+
sqlDB.Exec(t, `USE test_db`)
11206+
11207+
sqlDB.Exec(t, `CREATE TYPE custom_type AS ENUM ('val1', 'val2')`)
11208+
sqlDB.Exec(t, `COMMENT ON TYPE custom_type IS 'This is a custom type comment'`)
11209+
11210+
sqlDB.Exec(t, `CREATE SCHEMA test_schema`)
11211+
sqlDB.Exec(t, `COMMENT ON SCHEMA test_schema IS 'This is a schema comment'`)
11212+
11213+
sqlDB.Exec(t, `CREATE TABLE test_schema.test_table (id INT PRIMARY KEY, name STRING)`)
11214+
sqlDB.Exec(t, `COMMENT ON TABLE test_schema.test_table IS 'This is a table comment'`)
11215+
11216+
sqlDB.Exec(t, `COMMENT ON DATABASE test_db IS 'This is a database comment'`)
11217+
11218+
sqlDB.QueryRow(t, commentCountQuery).Scan(&count)
11219+
require.Equal(t, 4, count)
11220+
11221+
sqlDB.Exec(t, `BACKUP INTO 'nodelocal://1/test_backup'`)
11222+
11223+
sqlDB.Exec(t, `USE system`)
11224+
11225+
sqlDB.Exec(t, `DROP DATABASE test_db CASCADE`)
11226+
sqlDB.QueryRow(t, commentCountQuery).Scan(&count)
11227+
require.Equal(t, 0, count)
11228+
11229+
var jobID jobspb.JobID
11230+
sqlDB.QueryRow(t, `RESTORE FROM LATEST IN 'nodelocal://1/test_backup' WITH detached`).Scan(&jobID)
11231+
jobutils.WaitForJobToPause(t, sqlDB, jobID)
11232+
11233+
sqlDB.QueryRow(t, commentCountQuery).Scan(&count)
11234+
require.Equal(t, 4, count)
11235+
11236+
// Cancel the restore job
11237+
sqlDB.Exec(t, `CANCEL JOB $1`, jobID)
11238+
jobutils.WaitForJobToCancel(t, sqlDB, jobID)
11239+
11240+
sqlDB.QueryRow(t, commentCountQuery).Scan(&count)
11241+
require.Equal(t, 0, count)
11242+
}

pkg/backup/restore_job.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
3939
"github.com/cockroachdb/cockroach/pkg/sql"
4040
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
41+
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
4142
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
4243
"github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc"
4344
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen"
@@ -2809,6 +2810,10 @@ func (r *restoreResumer) dropDescriptors(
28092810
tablesToGC = append(tablesToGC, tableToDrop.ID)
28102811
tableToDrop.SetDropped()
28112812

2813+
if err := descsCol.DeleteTableComments(ctx, kvTrace, b, tableToDrop.ID); err != nil {
2814+
return err
2815+
}
2816+
28122817
// Drop any schedules we may have implicitly created.
28132818
if tableToDrop.HasRowLevelTTL() {
28142819
scheduleID := tableToDrop.RowLevelTTL.ScheduleID
@@ -2864,6 +2869,14 @@ func (r *restoreResumer) dropDescriptors(
28642869
}
28652870
mutType.SetDropped()
28662871

2872+
if err := descsCol.DeleteCommentInBatch(
2873+
ctx,
2874+
kvTrace,
2875+
b,
2876+
catalogkeys.MakeCommentKey(uint32(typDesc.ID), 0, catalogkeys.TypeCommentType)); err != nil {
2877+
return err
2878+
}
2879+
28672880
if err := descsCol.DeleteNamespaceEntryToBatch(ctx, kvTrace, typDesc, b); err != nil {
28682881
return err
28692882
}
@@ -2959,6 +2972,14 @@ func (r *restoreResumer) dropDescriptors(
29592972
entry.db = mutParent.(*dbdesc.Mutable)
29602973
}
29612974

2975+
if err := descsCol.DeleteCommentInBatch(
2976+
ctx,
2977+
kvTrace,
2978+
b,
2979+
catalogkeys.MakeCommentKey(uint32(schemaDesc.GetID()), 0, catalogkeys.SchemaCommentType)); err != nil {
2980+
return err
2981+
}
2982+
29622983
// Delete schema entries in descriptor and namespace system tables.
29632984
if err := descsCol.DeleteNamespaceEntryToBatch(ctx, kvTrace, mutSchema, b); err != nil {
29642985
return err
@@ -3042,6 +3063,14 @@ func (r *restoreResumer) dropDescriptors(
30423063
return err
30433064
}
30443065
}
3066+
if err := descsCol.DeleteCommentInBatch(
3067+
ctx,
3068+
kvTrace,
3069+
b,
3070+
catalogkeys.MakeCommentKey(uint32(dbDesc.GetID()), 0, catalogkeys.DatabaseCommentType)); err != nil {
3071+
return err
3072+
}
3073+
30453074
if err := descsCol.DeleteNamespaceEntryToBatch(ctx, kvTrace, db, b); err != nil {
30463075
return err
30473076
}

0 commit comments

Comments
 (0)