Skip to content

Commit a3a389e

Browse files
craig[bot]rafiss
andcommitted
108347: catalog: add more detail to DescriptorNotFoundErrors r=rafiss a=rafiss This should make it easier to debug some tests where this error is happening rarely informs cockroachdb#104164 --- ### catalog: use NewReferencedDescriptorNotFoundError This is easier to remember than having to wrap the error at each call site. --- ### catalog: add more detail to ErrDescriptorNotFound errors Now the ID will always be shown when this error happens. --- Release note: None Co-authored-by: Rafi Shamim <[email protected]>
2 parents 93546c2 + b291808 commit a3a389e

File tree

13 files changed

+38
-26
lines changed

13 files changed

+38
-26
lines changed

pkg/ccl/kvccl/kvfollowerreadsccl/testdata/boundedstaleness/single_row

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ ALTER TABLE t2 ADD COLUMN new_col INT
244244
query idx=2
245245
SELECT * FROM t2 AS OF SYSTEM TIME with_min_timestamp(now() - '10s', true) WHERE pk = 2
246246
----
247-
pq: referenced descriptor ID 105: descriptor not found
247+
pq: referenced descriptor ID 105: looking up ID 105: descriptor not found
248248
events (7 found):
249249
* event 1: colbatchscan trace on node_idx 2: local read
250250
* event 2: transaction retry on node_idx: 2
@@ -257,7 +257,7 @@ events (7 found):
257257
query idx=2
258258
SELECT * FROM t2 AS OF SYSTEM TIME with_min_timestamp(now() - '10s', true) WHERE pk = 2
259259
----
260-
pq: referenced descriptor ID 105: descriptor not found
260+
pq: referenced descriptor ID 105: looking up ID 105: descriptor not found
261261
events (7 found):
262262
* event 1: colbatchscan trace on node_idx 2: local read
263263
* event 2: transaction retry on node_idx: 2

pkg/cli/testdata/doctor/test_examine_zipdir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Examining 37 descriptors and 42 namespace entries...
99
ParentID 52, ParentSchemaID 29: relation "vehicle_location_histories" (56): referenced database ID 52: referenced descriptor not found
1010
ParentID 52, ParentSchemaID 29: relation "promo_codes" (57): referenced database ID 52: referenced descriptor not found
1111
ParentID 52, ParentSchemaID 29: relation "user_promo_codes" (58): referenced database ID 52: referenced descriptor not found
12-
ParentID 0, ParentSchemaID 0: namespace entry "movr" (52): referenced descriptor not found
12+
ParentID 0, ParentSchemaID 0: namespace entry "movr" (52): referenced schema ID 52: referenced descriptor not found
1313
Examining 2 jobs...
1414
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.
1515
ERROR: validation failed

pkg/cli/testdata/doctor/test_examine_zipdir_verbose

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Examining 37 descriptors and 42 namespace entries...
5050
ParentID 52, ParentSchemaID 29: relation "user_promo_codes" (58): referenced database ID 52: referenced descriptor not found
5151
ParentID 52, ParentSchemaID 29: relation "user_promo_codes" (58): processed
5252
ParentID 0, ParentSchemaID 0: namespace entry "defaultdb" (50): processed
53-
ParentID 0, ParentSchemaID 0: namespace entry "movr" (52): referenced descriptor not found
53+
ParentID 0, ParentSchemaID 0: namespace entry "movr" (52): referenced schema ID 52: referenced descriptor not found
5454
ParentID 0, ParentSchemaID 0: namespace entry "postgres" (51): processed
5555
ParentID 0, ParentSchemaID 0: namespace entry "system" (1): processed
5656
ParentID 1, ParentSchemaID 0: namespace entry "public" (29): processed

pkg/sql/catalog/descs/descriptor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func getDescriptorsByID(
201201
if flags.layerFilters.withoutStorage {
202202
// Some descriptors are still missing and there's nowhere left to get
203203
// them from.
204-
return catalog.ErrDescriptorNotFound
204+
return errors.Wrapf(catalog.ErrDescriptorNotFound, "looking up descriptor(s) %v", readIDs)
205205
}
206206
const isDescriptorRequired = true
207207
read, err := tc.cr.GetByIDs(ctx, txn, readIDs.Ordered(), isDescriptorRequired, catalog.Any)
@@ -235,7 +235,7 @@ func getDescriptorsByID(
235235
func filterDescriptor(desc catalog.Descriptor, flags getterFlags) error {
236236
if expected := flags.descFilters.maybeParentID; expected != descpb.InvalidID {
237237
if actual := desc.GetParentID(); actual != descpb.InvalidID && actual != expected {
238-
return catalog.ErrDescriptorNotFound
238+
return errors.Wrapf(catalog.ErrDescriptorNotFound, "expected %d, got %d", expected, actual)
239239
}
240240
}
241241
if flags.descFilters.withoutDropped {
@@ -371,7 +371,7 @@ func (q *byIDLookupContext) lookupLeased(
371371
// If we have already read all of the descriptors, use it as a negative
372372
// cache to short-circuit a lookup we know will be doomed to fail.
373373
if q.tc.cr.IsDescIDKnownToNotExist(id, q.flags.descFilters.maybeParentID) {
374-
return nil, catalog.NoValidation, catalog.ErrDescriptorNotFound
374+
return nil, catalog.NoValidation, catalog.NewDescriptorNotFoundError(id)
375375
}
376376
desc, shouldReadFromStore, err := q.tc.leased.getByID(q.ctx, q.tc.deadlineHolder(q.txn), id)
377377
if err != nil || shouldReadFromStore {

pkg/sql/catalog/descs/hydrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func HydrateCatalog(ctx context.Context, c nstree.MutableCatalog) error {
190190
defer sp.Finish()
191191

192192
fakeLookupFunc := func(_ context.Context, id descpb.ID, skipHydration bool) (catalog.Descriptor, error) {
193-
return nil, catalog.WrapDescRefErr(id, catalog.ErrDescriptorNotFound)
193+
return nil, catalog.NewDescriptorNotFoundError(id)
194194
}
195195
typeLookupFunc := makeTypeLookupFuncForHydration(c, fakeLookupFunc)
196196
var hydratable []catalog.Descriptor

pkg/sql/catalog/errors.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
1717
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
1818
"github.com/cockroachdb/errors"
19+
"github.com/cockroachdb/redact"
1920
)
2021

2122
// ValidateName validates a name.
@@ -89,10 +90,18 @@ func HasInactiveDescriptorError(err error) bool {
8990
// found with the given id.
9091
var ErrDescriptorNotFound = errors.New("descriptor not found")
9192

93+
func NewDescriptorNotFoundError(id descpb.ID) error {
94+
return errors.Wrapf(ErrDescriptorNotFound, "looking up ID %d", errors.Safe(id))
95+
}
96+
9297
// ErrReferencedDescriptorNotFound is like ErrDescriptorNotFound but for
9398
// descriptors referenced within another descriptor.
9499
var ErrReferencedDescriptorNotFound = errors.New("referenced descriptor not found")
95100

101+
func NewReferencedDescriptorNotFoundError(descType string, id descpb.ID) error {
102+
return errors.Wrapf(ErrReferencedDescriptorNotFound, "referenced %s ID %d", redact.SafeString(descType), errors.Safe(id))
103+
}
104+
96105
// ErrDescriptorWrongType is returned to signal that a descriptor was found but
97106
// that it wasn't of the expected type.
98107
var ErrDescriptorWrongType = errors.New("unexpected descriptor type")

pkg/sql/catalog/internal/catkv/catalog_query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,5 @@ func requiredError(expectedType catalog.DescriptorType, id descpb.ID) (err error
214214
default:
215215
err = errors.Errorf("failed to find descriptor [%d]", id)
216216
}
217-
return errors.CombineErrors(catalog.ErrDescriptorNotFound, err)
217+
return errors.CombineErrors(catalog.NewDescriptorNotFoundError(id), err)
218218
}

pkg/sql/catalog/internal/validate/validate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ var _ catalog.ValidationDescGetter = (*validationDescGetterImpl)(nil)
294294
func (vdg *validationDescGetterImpl) GetDescriptor(id descpb.ID) (catalog.Descriptor, error) {
295295
desc, found := vdg.descriptors[id]
296296
if !found || desc == nil {
297-
return nil, catalog.WrapDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
297+
return nil, catalog.NewReferencedDescriptorNotFoundError("descriptor", id)
298298
}
299299
return desc, nil
300300
}
@@ -305,7 +305,7 @@ func (vdg *validationDescGetterImpl) GetDatabaseDescriptor(
305305
) (catalog.DatabaseDescriptor, error) {
306306
desc, found := vdg.descriptors[id]
307307
if !found || desc == nil {
308-
return nil, catalog.WrapDatabaseDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
308+
return nil, catalog.NewReferencedDescriptorNotFoundError("database", id)
309309
}
310310
return catalog.AsDatabaseDescriptor(desc)
311311
}
@@ -316,7 +316,7 @@ func (vdg *validationDescGetterImpl) GetSchemaDescriptor(
316316
) (catalog.SchemaDescriptor, error) {
317317
desc, found := vdg.descriptors[id]
318318
if !found || desc == nil {
319-
return nil, catalog.WrapSchemaDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
319+
return nil, catalog.NewReferencedDescriptorNotFoundError("schema", id)
320320
}
321321
return catalog.AsSchemaDescriptor(desc)
322322
}
@@ -327,7 +327,7 @@ func (vdg *validationDescGetterImpl) GetTableDescriptor(
327327
) (catalog.TableDescriptor, error) {
328328
desc, found := vdg.descriptors[id]
329329
if !found || desc == nil {
330-
return nil, catalog.WrapTableDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
330+
return nil, catalog.NewReferencedDescriptorNotFoundError("table", id)
331331
}
332332
return catalog.AsTableDescriptor(desc)
333333
}
@@ -338,7 +338,7 @@ func (vdg *validationDescGetterImpl) GetTypeDescriptor(
338338
) (catalog.TypeDescriptor, error) {
339339
desc, found := vdg.descriptors[id]
340340
if !found || desc == nil {
341-
return nil, catalog.WrapTypeDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
341+
return nil, catalog.NewReferencedDescriptorNotFoundError("type", id)
342342
}
343343
descriptor, err := catalog.AsTypeDescriptor(desc)
344344
if err != nil {
@@ -352,7 +352,7 @@ func (vdg *validationDescGetterImpl) GetFunctionDescriptor(
352352
) (catalog.FunctionDescriptor, error) {
353353
desc, found := vdg.descriptors[id]
354354
if !found || desc == nil {
355-
return nil, catalog.WrapFunctionDescRefErr(id, catalog.ErrReferencedDescriptorNotFound)
355+
return nil, catalog.NewReferencedDescriptorNotFoundError("function", id)
356356
}
357357
return catalog.AsFunctionDescriptor(desc)
358358
}

pkg/sql/catalog/lease/lease.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ func (m *Manager) AcquireByName(
933933
// If the name we had doesn't match the newest descriptor in the DB, then
934934
// we're trying to use an old name.
935935
desc.Release(ctx)
936-
return nil, catalog.ErrDescriptorNotFound
936+
return nil, catalog.NewDescriptorNotFoundError(id)
937937
}
938938
}
939939
return validateDescriptorForReturn(desc)
@@ -974,7 +974,10 @@ func (m *Manager) resolveName(
974974
return id, err
975975
}
976976
if id == descpb.InvalidID {
977-
return id, catalog.ErrDescriptorNotFound
977+
return id, errors.Wrapf(catalog.ErrDescriptorNotFound,
978+
"resolving name %s with parentID %d and parentSchemaID %d",
979+
name, parentID, parentSchemaID,
980+
)
978981
}
979982
return id, nil
980983
}

pkg/sql/catalog/nstree/catalog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (c Catalog) ValidateNamespaceEntry(key catalog.NameKey) error {
283283
// Compare the namespace entry with the referenced descriptor.
284284
desc := c.LookupDescriptor(ne.GetID())
285285
if desc == nil {
286-
return catalog.ErrReferencedDescriptorNotFound
286+
return catalog.NewReferencedDescriptorNotFoundError("schema", ne.GetID())
287287
}
288288
if desc.Dropped() {
289289
return catalog.ErrDescriptorDropped

0 commit comments

Comments
 (0)