Skip to content

sql: fix dropping offline objects#84189

Merged
craig[bot] merged 3 commits intocockroachdb:masterfrom
postamar:fix-84137
Jul 12, 2022
Merged

sql: fix dropping offline objects#84189
craig[bot] merged 3 commits intocockroachdb:masterfrom
postamar:fix-84137

Conversation

@postamar
Copy link
Copy Markdown

@postamar postamar commented Jul 11, 2022

Fixes #84137.

I intend to base #83915 on this PR, backport the first two commits to 22.1, and backport only the first commit to 21.2.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@postamar postamar force-pushed the fix-84137 branch 2 times, most recently from 81b6983 to 3b19cd9 Compare July 11, 2022 20:25
@postamar postamar marked this pull request as ready for review July 11, 2022 21:08
@postamar postamar requested review from a team and dt July 11, 2022 21:08
Copy link
Copy Markdown
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Looks good, a lot of the comments are superficial/nitty.

Reviewed 4 of 4 files at r1, 4 of 4 files at r2, 4 of 4 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt and @postamar)


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go line 60 at r2 (raw file):

}

func undroppedElements(b BuildCtx, id catid.DescID) ElementResultSet {

Nit: Might be a bit cleaner to have a comment on this one its a shared helper function. Though we aren't good with that convention.


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go line 98 at r2 (raw file):

	})
	if name == "" {
		return fmt.Sprintf("%s #%d", typ, id)

It feels like there should be something stricter, but I don't think there is a nice solution here. i.e. For any new element types that are missing logic in here.


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go line 117 at r2 (raw file):

// CASCADE.
func dropCascadeDescriptor(b BuildCtx, id catid.DescID) {
	undropped := undroppedElements(b, id)

Nice, much cleaner than the old pattern!


pkg/ccl/backupccl/backup_test.go line 7851 at r1 (raw file):

		{
			const re = `type .* is offline|relation .* is offline|cannot drop a database or a schema with OFFLINE`
			sqlDB.ExpectErr(t, re, `DROP TYPE newdb.sc.typ`)

Nit: Might be just cleaner to embed the individual for the first two, there is no real commonality except for the last one.

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:

Reviewed 4 of 4 files at r1, 4 of 4 files at r2, 4 of 4 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @dt and @postamar)


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go line 74 at r2 (raw file):

}

func errMsgPrefix(b BuildCtx, id catid.DescID) string {

Can you comment the intention of this function, it's not obvious


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go line 92 at r2 (raw file):

			typ = "type"
		case *scpb.Namespace:
			if name == "" || target != scpb.ToPublic {

this != scpb.ToPublic deserves commentary


pkg/sql/importer/import_stmt_test.go line 1231 at r1 (raw file):

				defer func() {
					_, _ = db.Exec(fmt.Sprintf("DROP DATABASE %s", dbName))
				}()

This change doesn't leave me feeling great. I understand why you're doing it: sometimes the drop is going to fail due to offline descriptors, but I have to assume that it was here for a reason. Perhaps some of it was to validate the state of the database. Maybe augment this test with a scan of crdb_internal.invalid_objects?

Code quote:

				defer sqlDB.Exec(t, fmt.Sprintf(`DROP DATABASE %s`, dbName))

Copy link
Copy Markdown
Contributor

@chengxiong-ruan chengxiong-ruan left a comment

Choose a reason for hiding this comment

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

thanks for fixing this.

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


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go line 50 at r3 (raw file):

func dropRestrictDescriptor(b BuildCtx, id catid.DescID) (hasChanged bool) {
	undroppedElements(b, id).ForEachElementStatus(func(_ scpb.Status, target scpb.TargetStatus, e scpb.Element) {
		if target == scpb.ToAbsent || target == scpb.Transient {

nit, looks like we don't need this check since everything is from undroppedElements?


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/helpers.go line 62 at r3 (raw file):

func undroppedElements(b BuildCtx, id catid.DescID) ElementResultSet {
	return b.QueryByID(id).Filter(func(current scpb.Status, target scpb.TargetStatus, e scpb.Element) bool {
		if target == scpb.InvalidTarget {

could you add a comment that why target ==scpb.InvalidTarget indicate a non-public element?

Marius Posta added 3 commits July 11, 2022 19:13
Partially fixes cockroachdb#84137.

Release note (bug fix): DROP SCHEMA ... CASCADE in the legacy
schema changer now correctly fails when a backup restore or
an import of an underlying table or type is concurrently underway.
Partially fixes cockroachdb#84137.

Release note (bug fix): DROP ... CASCADE of a database or a schema
in the declarative schema changer now correctly fails when a
backup restore or an import is concurrently underway.
This commit does not change any functionality. These changes are
separate from the previous commit to facilitate backporting.

Release note: None
@postamar
Copy link
Copy Markdown
Author

Thanks for the reviews! I've addressed most comments. I added a bunch of commentary. Thank you all for suggesting it, it needed it, and I didn't notice from the tunnel vision of hammering this change through the tests all day.

I'll wait for a green CI light and bors.

@postamar
Copy link
Copy Markdown
Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 12, 2022

Build succeeded:

@craig craig bot merged commit 0aa3c44 into cockroachdb:master Jul 12, 2022
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.

schema: cascade interacting with offline tables

5 participants