Skip to content

Commit

Permalink
catalog: add more detail to ErrDescriptorNotFound errors
Browse files Browse the repository at this point in the history
Now the ID will always be shown when this error happens.

Release note: None
  • Loading branch information
rafiss committed Aug 9, 2023
1 parent d5193f7 commit b291808
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ ALTER TABLE t2 ADD COLUMN new_col INT
query idx=2
SELECT * FROM t2 AS OF SYSTEM TIME with_min_timestamp(now() - '10s', true) WHERE pk = 2
----
pq: referenced descriptor ID 105: descriptor not found
pq: referenced descriptor ID 105: looking up ID 105: descriptor not found
events (7 found):
* event 1: colbatchscan trace on node_idx 2: local read
* event 2: transaction retry on node_idx: 2
Expand All @@ -257,7 +257,7 @@ events (7 found):
query idx=2
SELECT * FROM t2 AS OF SYSTEM TIME with_min_timestamp(now() - '10s', true) WHERE pk = 2
----
pq: referenced descriptor ID 105: descriptor not found
pq: referenced descriptor ID 105: looking up ID 105: descriptor not found
events (7 found):
* event 1: colbatchscan trace on node_idx 2: local read
* event 2: transaction retry on node_idx: 2
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/testdata/doctor/test_examine_zipdir
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Examining 37 descriptors and 42 namespace entries...
ParentID 52, ParentSchemaID 29: relation "vehicle_location_histories" (56): referenced database ID 52: referenced descriptor not found
ParentID 52, ParentSchemaID 29: relation "promo_codes" (57): referenced database ID 52: referenced descriptor not found
ParentID 52, ParentSchemaID 29: relation "user_promo_codes" (58): referenced database ID 52: referenced descriptor not found
ParentID 0, ParentSchemaID 0: namespace entry "movr" (52): referenced descriptor not found
ParentID 0, ParentSchemaID 0: namespace entry "movr" (52): referenced schema ID 52: referenced descriptor not found
Examining 2 jobs...
job 587337426984566785: running schema change GC refers to missing table descriptor(s) [59]; existing descriptors that still need to be dropped []; job safe to delete: true.
ERROR: validation failed
2 changes: 1 addition & 1 deletion pkg/cli/testdata/doctor/test_examine_zipdir_verbose
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Examining 37 descriptors and 42 namespace entries...
ParentID 52, ParentSchemaID 29: relation "user_promo_codes" (58): referenced database ID 52: referenced descriptor not found
ParentID 52, ParentSchemaID 29: relation "user_promo_codes" (58): processed
ParentID 0, ParentSchemaID 0: namespace entry "defaultdb" (50): processed
ParentID 0, ParentSchemaID 0: namespace entry "movr" (52): referenced descriptor not found
ParentID 0, ParentSchemaID 0: namespace entry "movr" (52): referenced schema ID 52: referenced descriptor not found
ParentID 0, ParentSchemaID 0: namespace entry "postgres" (51): processed
ParentID 0, ParentSchemaID 0: namespace entry "system" (1): processed
ParentID 1, ParentSchemaID 0: namespace entry "public" (29): processed
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/catalog/descs/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func getDescriptorsByID(
if flags.layerFilters.withoutStorage {
// Some descriptors are still missing and there's nowhere left to get
// them from.
return catalog.ErrDescriptorNotFound
return errors.Wrapf(catalog.ErrDescriptorNotFound, "looking up descriptor(s) %v", readIDs)
}
const isDescriptorRequired = true
read, err := tc.cr.GetByIDs(ctx, txn, readIDs.Ordered(), isDescriptorRequired, catalog.Any)
Expand Down Expand Up @@ -235,7 +235,7 @@ func getDescriptorsByID(
func filterDescriptor(desc catalog.Descriptor, flags getterFlags) error {
if expected := flags.descFilters.maybeParentID; expected != descpb.InvalidID {
if actual := desc.GetParentID(); actual != descpb.InvalidID && actual != expected {
return catalog.ErrDescriptorNotFound
return errors.Wrapf(catalog.ErrDescriptorNotFound, "expected %d, got %d", expected, actual)
}
}
if flags.descFilters.withoutDropped {
Expand Down Expand Up @@ -371,7 +371,7 @@ func (q *byIDLookupContext) lookupLeased(
// If we have already read all of the descriptors, use it as a negative
// cache to short-circuit a lookup we know will be doomed to fail.
if q.tc.cr.IsDescIDKnownToNotExist(id, q.flags.descFilters.maybeParentID) {
return nil, catalog.NoValidation, catalog.ErrDescriptorNotFound
return nil, catalog.NoValidation, catalog.NewDescriptorNotFoundError(id)
}
desc, shouldReadFromStore, err := q.tc.leased.getByID(q.ctx, q.tc.deadlineHolder(q.txn), id)
if err != nil || shouldReadFromStore {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/descs/hydrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func HydrateCatalog(ctx context.Context, c nstree.MutableCatalog) error {
defer sp.Finish()

fakeLookupFunc := func(_ context.Context, id descpb.ID, skipHydration bool) (catalog.Descriptor, error) {
return nil, catalog.WrapDescRefErr(id, catalog.ErrDescriptorNotFound)
return nil, catalog.NewDescriptorNotFoundError(id)
}
typeLookupFunc := makeTypeLookupFuncForHydration(c, fakeLookupFunc)
var hydratable []catalog.Descriptor
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/catalog/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func HasInactiveDescriptorError(err error) bool {
// found with the given id.
var ErrDescriptorNotFound = errors.New("descriptor not found")

func NewDescriptorNotFoundError(id descpb.ID) error {
return errors.Wrapf(ErrDescriptorNotFound, "looking up ID %d", errors.Safe(id))
}

// ErrReferencedDescriptorNotFound is like ErrDescriptorNotFound but for
// descriptors referenced within another descriptor.
var ErrReferencedDescriptorNotFound = errors.New("referenced descriptor not found")
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/internal/catkv/catalog_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,5 @@ func requiredError(expectedType catalog.DescriptorType, id descpb.ID) (err error
default:
err = errors.Errorf("failed to find descriptor [%d]", id)
}
return errors.CombineErrors(catalog.ErrDescriptorNotFound, err)
return errors.CombineErrors(catalog.NewDescriptorNotFoundError(id), err)
}
2 changes: 1 addition & 1 deletion pkg/sql/catalog/internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ var _ catalog.ValidationDescGetter = (*validationDescGetterImpl)(nil)
func (vdg *validationDescGetterImpl) GetDescriptor(id descpb.ID) (catalog.Descriptor, error) {
desc, found := vdg.descriptors[id]
if !found || desc == nil {
return nil, catalog.NewReferencedDescriptorNotFoundError("object", id)
return nil, catalog.NewReferencedDescriptorNotFoundError("descriptor", id)
}
return desc, nil
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/sql/catalog/lease/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ func (m *Manager) AcquireByName(
// If the name we had doesn't match the newest descriptor in the DB, then
// we're trying to use an old name.
desc.Release(ctx)
return nil, catalog.ErrDescriptorNotFound
return nil, catalog.NewDescriptorNotFoundError(id)
}
}
return validateDescriptorForReturn(desc)
Expand Down Expand Up @@ -974,7 +974,10 @@ func (m *Manager) resolveName(
return id, err
}
if id == descpb.InvalidID {
return id, catalog.ErrDescriptorNotFound
return id, errors.Wrapf(catalog.ErrDescriptorNotFound,
"resolving name %s with parentID %d and parentSchemaID %d",
name, parentID, parentSchemaID,
)
}
return id, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/crdb_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,9 @@ UPDATE system.namespace SET id = %d WHERE id = %d;
{fmt.Sprintf("%d", schemaID), fmt.Sprintf("[%d]", databaseID), "public", "",
fmt.Sprintf(`schema "public" (%d): referenced database ID %d: referenced descriptor not found`, schemaID, databaseID),
},
{fmt.Sprintf("%d", databaseID), "t", "", "", `referenced descriptor not found`},
{fmt.Sprintf("%d", tableFkTblID), "defaultdb", "public", "fktbl", `referenced descriptor not found`},
{fmt.Sprintf("%d", fakeID), fmt.Sprintf("[%d]", databaseID), "public", "test", `referenced descriptor not found`},
{fmt.Sprintf("%d", databaseID), "t", "", "", `referenced schema ID 104: referenced descriptor not found`},
{fmt.Sprintf("%d", tableFkTblID), "defaultdb", "public", "fktbl", `referenced schema ID 107: referenced descriptor not found`},
{fmt.Sprintf("%d", fakeID), fmt.Sprintf("[%d]", databaseID), "public", "test", `referenced schema ID 12345: referenced descriptor not found`},
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/doctor/doctor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func TestExamineDescriptors(t *testing.T) {
{NameInfo: descpb.NameInfo{Name: "causes_error"}, ID: 2},
},
expected: `Examining 0 descriptors and 4 namespace entries...
ParentID 0, ParentSchemaID 0: namespace entry "causes_error" (2): referenced descriptor not found
ParentID 0, ParentSchemaID 0: namespace entry "causes_error" (2): referenced schema ID 2: referenced descriptor not found
`,
},
{ // 14
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
Expand Up @@ -1113,10 +1113,10 @@ query ITTTT colnames
SELECT * FROM "".crdb_internal.invalid_objects ORDER BY id
----
id database_name schema_name obj_name error
500 baddb · · referenced descriptor not found
501 system badschema · referenced descriptor not found
502 system public badobj referenced descriptor not found
503 system [404] badobj referenced descriptor not found
500 baddb · · referenced schema ID 500: referenced descriptor not found
501 system badschema · referenced schema ID 501: referenced descriptor not found
502 system public badobj referenced schema ID 502: referenced descriptor not found
503 system [404] badobj referenced schema ID 503: referenced descriptor not found

statement ok
SELECT crdb_internal.unsafe_delete_namespace_entry(0, 0, 'baddb', 500, true);
Expand Down

0 comments on commit b291808

Please sign in to comment.