Skip to content

sql/catalog/dbdesc: repair old descriptors corrupted due to DROPPED SCHEMA name#68738

Merged
craig[bot] merged 2 commits intocockroachdb:masterfrom
sajjadrizvi:repair_desc_drop_schema_bug
Aug 16, 2021
Merged

sql/catalog/dbdesc: repair old descriptors corrupted due to DROPPED SCHEMA name#68738
craig[bot] merged 2 commits intocockroachdb:masterfrom
sajjadrizvi:repair_desc_drop_schema_bug

Conversation

@sajjadrizvi
Copy link
Copy Markdown

@sajjadrizvi sajjadrizvi commented Aug 11, 2021

#63119 fixes a bug that corrupts a database descriptor when a child schema was
dropped, adding an entry in schema-info structure erroneously with database name
instead of schema name . Although the bug was fixed , there can be database backups
with corrupted descriptors.

This commit adds a post-deserialization function to repair a corrupted descriptor. Moreover,
it adds a test function to ensure that descriptors with such corruption are fixed during
migration.

Release note: None

Fixes: #63148

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@sajjadrizvi sajjadrizvi force-pushed the repair_desc_drop_schema_bug branch 5 times, most recently from 8e67036 to 9d2a6f4 Compare August 12, 2021 21:18
@sajjadrizvi sajjadrizvi changed the title [WIP] sql/catalog/dbdesc: fix descriptors corrupted due to DROPPED SCHEMA name sql/catalog/dbdesc: repair old descriptors corrupted due to DROPPED SCHEMA name Aug 12, 2021
@sajjadrizvi sajjadrizvi marked this pull request as ready for review August 12, 2021 21:33
@sajjadrizvi sajjadrizvi requested a review from a team August 12, 2021 21:33
@sajjadrizvi sajjadrizvi requested a review from a team as a code owner August 12, 2021 21:33
@sajjadrizvi sajjadrizvi requested review from a team and nihalpednekar and removed request for a team August 12, 2021 21:33
@sajjadrizvi sajjadrizvi force-pushed the repair_desc_drop_schema_bug branch from 9d2a6f4 to dbc80a0 Compare August 12, 2021 21:40
Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: mod naming and commentary nits

Reviewed 2 of 11 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @nihalpednekar and @sajjadrizvi)


pkg/sql/catalog/dbdesc/database_desc.go, line 441 at r1 (raw file):

// maybeFixDroppedSchemaName fixes the name of a dropped child schema in a
// database descriptor.

This first sentence is too vague and might lead somebody to believe it's a very general function. How about:

// maybeRemoveDroppedSelfEntryFromSchemas removes an entry in the Schemas map corresponding to the
// database itself which was added due to a bug in prior versions when dropping any user-defined schema. 
// The bug inserted an entry for the database rather than the schema being dropped. This function fixes the
// problem by deleting the erroneous entry.
func maybeRemoveDroppedSelfEntryFromSchemas(dbDesc *descpb.DatabaseDescriptor) bool {

pkg/sql/catalog/dbdesc/database_desc.go, line 452 at r1 (raw file):

	}
	if sc, ok := dbDesc.Schemas[dbDesc.Name]; ok {
		if sc.Dropped && sc.ID == dbDesc.ID {

I don't think the Dropped check is necessary. The database should never think of itself as a schema inside of itself.


pkg/sql/catalog/dbdesc/database_desc_builder.go, line 63 at r1 (raw file):

	privsChanged := descpb.MaybeFixPrivileges(ddb.maybeModified.ID, ddb.maybeModified.ID,
		&ddb.maybeModified.Privileges, privilege.Database)
	nameChanged := maybeFixDroppedSchemaName(ddb.maybeModified)

nameChanged is a surprising name for this variable. The name of the descriptor did not change. Can you change it to something more descriptive like `re

Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @nihalpednekar and @sajjadrizvi)


pkg/sql/catalog/dbdesc/database_desc_builder.go, line 63 at r1 (raw file):

Previously, ajwerner wrote…

nameChanged is a surprising name for this variable. The name of the descriptor did not change. Can you change it to something more descriptive like `re

heh didn't finish that. removedSelfEntryInSchemas?

Copy link
Copy Markdown
Author

@sajjadrizvi sajjadrizvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner and @nihalpednekar)


pkg/sql/catalog/dbdesc/database_desc.go, line 441 at r1 (raw file):

Previously, ajwerner wrote…
// maybeFixDroppedSchemaName fixes the name of a dropped child schema in a
// database descriptor.

This first sentence is too vague and might lead somebody to believe it's a very general function. How about:

// maybeRemoveDroppedSelfEntryFromSchemas removes an entry in the Schemas map corresponding to the
// database itself which was added due to a bug in prior versions when dropping any user-defined schema. 
// The bug inserted an entry for the database rather than the schema being dropped. This function fixes the
// problem by deleting the erroneous entry.
func maybeRemoveDroppedSelfEntryFromSchemas(dbDesc *descpb.DatabaseDescriptor) bool {

Done.

@sajjadrizvi sajjadrizvi force-pushed the repair_desc_drop_schema_bug branch from dbc80a0 to 6e51cc4 Compare August 13, 2021 15:47
Sajjad Rizvi added 2 commits August 14, 2021 12:22
This commit adds a mechanism to delete wrong schema-info entries from
database descriptors as part of post-deserialization changes.

Release note: None
…info entries

This commit adds a test for fix_descriptor_migration to validate
that a corrupted database descriptor is fixed during migration.

Release note: None
@sajjadrizvi sajjadrizvi force-pushed the repair_desc_drop_schema_bug branch from 6e51cc4 to 88bf4ba Compare August 14, 2021 16:27
@sajjadrizvi
Copy link
Copy Markdown
Author

TFTR!

r=ajwerner

@sajjadrizvi
Copy link
Copy Markdown
Author

bors r=ajwerner

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 16, 2021

Build succeeded:

@craig craig bot merged commit f2aded5 into cockroachdb:master Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: repair corruption due to DROP SCHEMA bug

3 participants