Skip to content

backupccl: fix crash in SHOW BACKUP when types have cross-db references#55774

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
pbardea:show-backup-x-db
Oct 21, 2020
Merged

backupccl: fix crash in SHOW BACKUP when types have cross-db references#55774
craig[bot] merged 1 commit intocockroachdb:masterfrom
pbardea:show-backup-x-db

Conversation

@pbardea
Copy link
Copy Markdown
Contributor

@pbardea pbardea commented Oct 20, 2020

Previously, SHOW BACKUP would expect that the database and schema it
references is in the backup. In the case that we had a cross-db
reference, the schema or database that it references would not be
included in the backup, and would crash. It now reports objects that it
cannot find as NULL.

Fixes #55772.

Release note (bug fix): Fix crash when performing a SHOW BACKUP against
a backup which contains a table that references a type in another
database. This state was only reachable in 20.2 release candidates and
earlier.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@pbardea
Copy link
Copy Markdown
Contributor Author

pbardea commented Oct 20, 2020

cc @ajwerner @lucy-zhang

This is a first attempt at it. It's getting close, but I'm running into issues with hydrating the descriptors in this cross-db reference state...

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x5362921]

goroutine 3542 [running]:
github.com/cockroachdb/cockroach/pkg/sql/catalog/descs.HydrateGivenDescriptors.func1(0x93e8900, 0xc0024f8bd0, 0x36, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/catalog/descs/collection.go:1601 +0xd1
github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc.TypeLookupFunc.GetTypeDescriptor(0xc0016135e0, 0x93e8900, 0xc0024f8bd0, 0x36, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc/type_desc.go:521 +0x87
github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc.HydrateTypesInTableDescriptor.func1(0xc0005dcc00, 0xc001b2b638, 0x4011048)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc/type_desc.go:555 +0x123
github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc.HydrateTypesInTableDescriptor(0x93e8900, 0xc0024f8bd0, 0xc001938000, 0x935ec40, 0xc0016135e0, 0x3, 0xc00223b748)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc/type_desc.go:567 +0xd1
github.com/cockroachdb/cockroach/pkg/sql/catalog/descs.HydrateGivenDescriptors(0x93e8900, 0xc0024f8bd0, 0xc001a1ebc0, 0x4, 0x4, 0x0, 0x0)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/catalog/descs/collection.go:1612 +0x1ee
github.com/cockroachdb/cockroach/pkg/sql.newInternalLookupCtxFromDescriptors(0x93e8900, 0xc0024f8bd0, 0xc001a1eb40, 0x4, 0x4, 0x0, 0x0, 0x0, 0xbb3bd10)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/resolver.go:709 +0x9bf
github.com/cockroachdb/cockroach/pkg/sql.(*planner).ShowCreate(0xc001856418, 0x93e8900, 0xc0024f8bd0, 0xc00223b748, 0x7, 0xc001a1eb40, 0x4, 0x4, 0xc002eab900, 0x3, ...)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/show_create.go:245 +0x163
github.com/cockroachdb/cockroach/pkg/ccl/backupccl.backupShowerDefault.func1(0xc001d40dc0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/ccl/backupccl/show.go:328 +0x1542
github.com/cockroachdb/cockroach/pkg/ccl/backupccl.showBackupPlanHook.func1(0x93e8840, 0xc0024f0400, 0x0, 0x0, 0x0, 0xc001f7d320, 0x0, 0x0)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/ccl/backupccl/show.go:186 +0x80e
github.com/cockroachdb/cockroach/pkg/sql.(*hookFnNode).startExec.func1(0xc0017cd580, 0x93e8840, 0xc0024f0400, 0xc00176bb80, 0xc001856418)
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/planhook.go:146 +0x7d
created by github.com/cockroachdb/cockroach/pkg/sql.(*hookFnNode).startExec
        /Users/pbardea/Developer/go/src/github.com/cockroachdb/cockroach/pkg/sql/planhook.go:145 +0xcc
FAIL    github.com/cockroachdb/cockroach/pkg/ccl/backupccl      3.194s

@ajwerner
Copy link
Copy Markdown
Contributor

Try:

diff --git a/pkg/sql/catalog/descs/collection.go b/pkg/sql/catalog/descs/collection.go
index 1985efab32..ac8f123602 100644
--- a/pkg/sql/catalog/descs/collection.go
+++ b/pkg/sql/catalog/descs/collection.go
@@ -1585,8 +1585,17 @@ func HydrateGivenDescriptors(ctx context.Context, descs []catalog.Descriptor) er
                // we need to hydrate our types. Set up an accessor for the type hydration
                // method to look into the scanned set of descriptors.
                typeLookup := func(ctx context.Context, id descpb.ID) (tree.TypeName, catalog.TypeDescriptor, error) {
-                       typDesc := typDescs[id]
-                       dbDesc := dbDescs[typDesc.ParentID]
+                       typDesc, ok := typDescs[id]
+                       if !ok {
+                               n := tree.MakeUnresolvedName(fmt.Sprintf("[%d]", id))
+                               return tree.TypeName{}, nil, sqlerrors.NewUndefinedObjectError(&n,
+                                       tree.TypeObject)
+                       }
+                       dbDesc, ok := dbDescs[typDesc.ParentID]
+                       if !ok {
+                               n := fmt.Sprintf("[%d]", typDesc.ParentID)
+                               return tree.TypeName{}, nil, sqlerrors.NewUndefinedDatabaseError(n)
+                       }

@ajwerner
Copy link
Copy Markdown
Contributor

Maybe that is bad because it now just omits the table altogether.

@ajwerner
Copy link
Copy Markdown
Contributor

What's the right UX here?

@pbardea pbardea force-pushed the show-backup-x-db branch 2 times, most recently from 2e39da5 to 84aa9f6 Compare October 20, 2020 20:54
@pbardea
Copy link
Copy Markdown
Contributor Author

pbardea commented Oct 20, 2020

Latest change logs and silently ignores any error we get showing the create statement. I also played around with displaying an error message in that cell rather than "NULL"... but I'm not sure that's the right way to go here.

@ajwerner
Copy link
Copy Markdown
Contributor

Silently ignoring the error is sort of good, right, because it leads to omitting the table, right? That seems sane because you can't restore that table.

@pbardea
Copy link
Copy Markdown
Contributor Author

pbardea commented Oct 20, 2020

We can silently ignore it in 2 ways:

  1. Show the table in the SHOW BACKUP command (what it does now, but show the parent db and schema as NULL). This leads to a bit of confusion since we list a table that we can't restore. However, we also show that the database doesn't resovle (NULL) which is the reason we can't restore it. I also like that it keeps the simplicity of SHOW BACKUP to show me all the descriptors in the backup.

  2. We could omit the table from the output. I don't think we should do this since SHOW BACKUP is no longer "show me all the descriptors in the backup" but rather "show me everything I can restore from the backup", which is a lot trickier. I'm not sure if just skipping over rows where the references can't be resolved is enough to be able to claim that we can restore the tables.

@ajwerner
Copy link
Copy Markdown
Contributor

SGTM

@pbardea
Copy link
Copy Markdown
Contributor Author

pbardea commented Oct 20, 2020

Reworking the tests since this will fail once we disallow the rename.

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 2 of 7 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner, @dt, @lucy-zhang, and @pbardea)


pkg/ccl/backupccl/restore_old_versions_test.go, line 89 at r1 (raw file):

					CREATE TYPE t AS ENUM ('foo');
				  CREATE TABLE tbl (a t);

(I don't care enough for you to fix it tbh) nit: spacing

@pbardea
Copy link
Copy Markdown
Contributor Author

pbardea commented Oct 20, 2020

Removed the map changes so that we have the minimal set of changes go in the backport.

Copy link
Copy Markdown

@thoszhang thoszhang left a comment

Choose a reason for hiding this comment

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

Reviewed 7 of 7 files at r1, 2 of 2 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @dt and @pbardea)


pkg/ccl/backupccl/restore_old_versions_test.go, line 94 at r1 (raw file):

				  BACKUP DATABASE otherdb TO 'nodelocal://1/xDbRef';

					This was permitted on some release candidates of v20.2.

Add an issue number?


pkg/ccl/backupccl/restore_old_versions_test.go, line 123 at r1 (raw file):

			sqlDB.ExpectErr(t, `type "t" has unknown ParentID 50`, `RESTORE DATABASE otherdb FROM $1`, LocalFoo)

			sqlDB.CheckQueryResults(t, `

Maybe briefly comment on what we're looking for here? (i.e., no crash, and we get some NULLs)

Copy link
Copy Markdown

@thoszhang thoszhang 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! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @dt and @pbardea)


pkg/ccl/backupccl/restore_old_versions_test.go, line 94 at r1 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

Add an issue number?

(#55709)

Previously, SHOW BACKUP would expect that the database and schema it
references is in the backup. In the case that we had a cross-db
reference, the schema or database that it references would not be
included in the backup, and would crash. It now reports objects that it
cannot find as NULL.

Release note (bug fix): Fix crash when performing a SHOW BACKUP against
a backup which contains a table that references a type in another
database. This state was only reachable in 20.2 release candidates and
earlier.
Copy link
Copy Markdown
Contributor Author

@pbardea pbardea 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 the reviews!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @ajwerner, @dt, and @lucy-zhang)


pkg/ccl/backupccl/restore_old_versions_test.go, line 89 at r1 (raw file):

Previously, ajwerner wrote…

(I don't care enough for you to fix it tbh) nit: spacing

Done.


pkg/ccl/backupccl/restore_old_versions_test.go, line 94 at r1 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

(#55709)

Done.


pkg/ccl/backupccl/restore_old_versions_test.go, line 123 at r1 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

Maybe briefly comment on what we're looking for here? (i.e., no crash, and we get some NULLs)

Done.

@pbardea
Copy link
Copy Markdown
Contributor Author

pbardea commented Oct 21, 2020

bors r=lucy-zhang,ajwerner

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Oct 21, 2020

Build succeeded:

@craig craig bot merged commit 738848e into cockroachdb:master Oct 21, 2020
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.

backupccl: crash on SHOW BACKUP for database with table referencing type in other database

4 participants