From c0404a58da643a2997cb57ec6b97d6160d9dc744 Mon Sep 17 00:00:00 2001 From: Spas Bojanov Date: Thu, 1 Oct 2020 12:01:46 +0300 Subject: [PATCH] sql: doctor check for dropped tables If a table is dropped it should not have a namespace entry. Release note: none. --- pkg/sql/doctor/doctor.go | 6 ++++++ pkg/sql/doctor/doctor_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pkg/sql/doctor/doctor.go b/pkg/sql/doctor/doctor.go index 9d38e7082750..46c649030416 100644 --- a/pkg/sql/doctor/doctor.go +++ b/pkg/sql/doctor/doctor.go @@ -176,6 +176,12 @@ func ExamineDescriptors( } continue } + + if desc.Dropped() { + fmt.Fprint(stdout, reportMsg(desc, "dropped but namespace entry(s) found: %v", names)) + problemsFound = true + } + // We delete all pointed descriptors to leave what is missing in the // descriptor table. delete(nMap, row.ID) diff --git a/pkg/sql/doctor/doctor_test.go b/pkg/sql/doctor/doctor_test.go index 149224a072ea..800cdc584d89 100644 --- a/pkg/sql/doctor/doctor_test.go +++ b/pkg/sql/doctor/doctor_test.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/doctor" "github.com/cockroachdb/cockroach/pkg/sql/types" + "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/leaktest" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/cockroach/pkg/util/protoutil" @@ -69,6 +70,10 @@ func TestExamineDescriptors(t *testing.T) { defer leaktest.AfterTest(t)() defer log.Scope(t).Close(t) + droppedValidTableDesc := protoutil.Clone(validTableDesc).(*descpb.Descriptor) + descpb.TableFromDescriptor(droppedValidTableDesc, hlc.Timestamp{WallTime: 1}). + State = descpb.DescriptorState_DROP + tests := []struct { descTable doctor.DescriptorTable namespaceTable doctor.NamespaceTable @@ -269,6 +274,24 @@ Row(s) [{ParentID:0 ParentSchemaID:0 Name:null}]: NULL value found }, expected: `Examining 1 descriptors and 3 namespace entries... Database 1: ParentID 0, ParentSchemaID 0, Name 'db': extra draining names found [{ParentID:0 ParentSchemaID:0 Name:db3}] +`, + }, + { + descTable: doctor.DescriptorTable{ + {ID: 1, DescBytes: toBytes(t, droppedValidTableDesc)}, + { + ID: 2, + DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{ + Database: &descpb.DatabaseDescriptor{Name: "db", ID: 2}, + }}), + }, + }, + namespaceTable: doctor.NamespaceTable{ + {NameInfo: descpb.NameInfo{ParentID: 2, ParentSchemaID: 29, Name: "t"}, ID: 1}, + {NameInfo: descpb.NameInfo{Name: "db"}, ID: 2}, + }, + expected: `Examining 2 descriptors and 2 namespace entries... + Table 1: ParentID 2, ParentSchemaID 29, Name 't': dropped but namespace entry(s) found: [{2 29 t}] `, }, }