-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
doctor_test.go
633 lines (620 loc) · 21.3 KB
/
doctor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package doctor_test
import (
"bytes"
"context"
"fmt"
"testing"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catenumpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/doctor"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"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"
"github.com/stretchr/testify/require"
)
var validTableDesc = &descpb.Descriptor{
Union: &descpb.Descriptor_Table{
Table: &descpb.TableDescriptor{
Name: "t", ID: 51, ParentID: 52,
Columns: []descpb.ColumnDescriptor{
{Name: "col", ID: 1, Type: types.Int},
},
NextColumnID: 2,
NextConstraintID: 2,
Families: []descpb.ColumnFamilyDescriptor{
{ID: 0, Name: "f", ColumnNames: []string{"col"}, ColumnIDs: []descpb.ColumnID{1}, DefaultColumnID: 1},
},
NextFamilyID: 1,
PrimaryIndex: descpb.IndexDescriptor{
Name: tabledesc.PrimaryKeyIndexName("t"),
ID: 1,
Unique: true,
KeyColumnNames: []string{"col"},
KeyColumnDirections: []catenumpb.IndexColumn_Direction{catenumpb.IndexColumn_ASC},
KeyColumnIDs: []descpb.ColumnID{1},
Version: descpb.LatestIndexDescriptorVersion,
EncodingType: catenumpb.PrimaryIndexEncoding,
ConstraintID: 1,
},
NextIndexID: 2,
Privileges: catpb.NewCustomSuperuserPrivilegeDescriptor(
privilege.ReadWriteData, username.NodeUserName()),
FormatVersion: descpb.InterleavedFormatVersion,
NextMutationID: 1,
},
},
}
func toBytes(t *testing.T, desc *descpb.Descriptor) []byte {
table, database, typ, schema, function := descpb.GetDescriptors(desc)
if table != nil {
parentSchemaID := table.GetUnexposedParentSchemaID()
if parentSchemaID == descpb.InvalidID {
parentSchemaID = keys.PublicSchemaID
}
if _, err := catprivilege.MaybeFixPrivileges(
&table.Privileges,
table.GetParentID(),
parentSchemaID,
privilege.Table,
table.GetName(),
); err != nil {
panic(err)
}
if table.FormatVersion == 0 {
table.FormatVersion = descpb.InterleavedFormatVersion
}
} else if database != nil {
if _, err := catprivilege.MaybeFixPrivileges(
&database.Privileges,
descpb.InvalidID,
descpb.InvalidID,
privilege.Database,
database.GetName(),
); err != nil {
panic(err)
}
} else if typ != nil {
if _, err := catprivilege.MaybeFixPrivileges(
&typ.Privileges,
typ.GetParentID(),
typ.GetParentSchemaID(),
privilege.Type,
typ.GetName(),
); err != nil {
panic(err)
}
} else if schema != nil {
if _, err := catprivilege.MaybeFixPrivileges(
&schema.Privileges,
schema.GetParentID(),
descpb.InvalidID,
privilege.Schema,
schema.GetName(),
); err != nil {
panic(err)
}
} else if function != nil {
if _, err := catprivilege.MaybeFixPrivileges(
&function.Privileges,
function.GetParentID(),
descpb.InvalidID,
privilege.Routine,
function.GetName(),
); err != nil {
panic(err)
}
}
res, err := protoutil.Marshal(desc)
require.NoError(t, err)
return res
}
func TestExamineDescriptors(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
droppedValidTableDesc := protoutil.Clone(validTableDesc).(*descpb.Descriptor)
{
tbl, _, _, _, _ := descpb.GetDescriptors(droppedValidTableDesc)
tbl.State = descpb.DescriptorState_DROP
}
// Use 51 as the Schema ID, we do not want to use a reserved system ID (1-49)
// for the Schema because there should be no schemas with 1-49. A schema with
// an ID from 1-49 would fail privilege checks due to incompatible privileges
// the privileges returned from the SystemAllowedPrivileges map in privilege.go.
validTableDescWithParentSchema := protoutil.Clone(validTableDesc).(*descpb.Descriptor)
{
tbl, _, _, _, _ := descpb.GetDescriptors(validTableDescWithParentSchema)
tbl.UnexposedParentSchemaID = 53
}
tests := []struct {
descTable doctor.DescriptorTable
namespaceTable doctor.NamespaceTable
jobsTable doctor.JobsTable
valid bool
errStr string
expected string
}{
{ // 1
valid: true,
expected: "Examining 0 descriptors and 0 namespace entries...\n",
},
{ // 2
descTable: doctor.DescriptorTable{{ID: 1, DescBytes: []byte("#$@#@#$#@#")}},
errStr: "failed to unmarshal descriptor",
expected: "Examining 1 descriptors and 0 namespace entries...\n",
},
{ // 3
descTable: doctor.DescriptorTable{
{
ID: 1,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Table{
Table: &descpb.TableDescriptor{ID: 2},
}}),
},
},
expected: `Examining 1 descriptors and 0 namespace entries...
ParentID 0, ParentSchemaID 29: relation "" (2): different id in descriptor table: 1
ParentID 0, ParentSchemaID 29: relation "" (2): empty relation name
ParentID 0, ParentSchemaID 29: relation "" (2): invalid parent ID 0
ParentID 0, ParentSchemaID 29: relation "" (2): table must contain at least 1 column
`,
},
{ // 4
descTable: doctor.DescriptorTable{
{
ID: 1,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Table{
Table: &descpb.TableDescriptor{Name: "foo", ID: 1, State: descpb.DescriptorState_DROP},
}}),
},
},
valid: true,
expected: "Examining 1 descriptors and 0 namespace entries...\n",
},
{ // 5
descTable: doctor.DescriptorTable{
{
ID: 1,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Table{
Table: &descpb.TableDescriptor{Name: "foo", ID: 1},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{ParentSchemaID: 29, Name: "foo"}, ID: 1},
},
expected: `Examining 1 descriptors and 1 namespace entries...
ParentID 0, ParentSchemaID 29: relation "foo" (1): invalid parent ID 0
ParentID 0, ParentSchemaID 29: relation "foo" (1): table must contain at least 1 column
`,
},
{ // 6
descTable: doctor.DescriptorTable{
{
ID: 1,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 1},
}}),
},
},
expected: `Examining 1 descriptors and 0 namespace entries...
ParentID 0, ParentSchemaID 0: database "db" (1): expected matching namespace entry, found none
`,
},
{ // 7
descTable: doctor.DescriptorTable{
{ID: 51, DescBytes: toBytes(t, validTableDesc)},
{
ID: 52,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 52},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{ParentSchemaID: 29, Name: "t"}, ID: 51},
{NameInfo: descpb.NameInfo{Name: "db"}, ID: 52},
},
expected: `Examining 2 descriptors and 2 namespace entries...
ParentID 52, ParentSchemaID 29: relation "t" (51): expected matching namespace entry, found none
ParentID 0, ParentSchemaID 29: namespace entry "t" (51): mismatched name "t" in relation descriptor
`,
},
{ // 8
descTable: doctor.DescriptorTable{
{
ID: 51,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Schema{
Schema: &descpb.SchemaDescriptor{Name: "schema", ID: 51, ParentID: 2},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{ParentID: 2, Name: "schema"}, ID: 51},
},
expected: `Examining 1 descriptors and 1 namespace entries...
ParentID 2, ParentSchemaID 0: schema "schema" (51): referenced database ID 2: referenced descriptor not found
`,
},
{ // 9
descTable: doctor.DescriptorTable{
{
ID: 51,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Type{
Type: &descpb.TypeDescriptor{Name: "type", ID: 51},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{Name: "type"}, ID: 51},
},
expected: `Examining 1 descriptors and 1 namespace entries...
ParentID 0, ParentSchemaID 0: type "type" (51): invalid parentID 0
ParentID 0, ParentSchemaID 0: type "type" (51): invalid parent schema ID 0
`,
},
{ // 10
descTable: doctor.DescriptorTable{
{
ID: 51,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Type{
Type: &descpb.TypeDescriptor{Name: "type", ID: 51, ParentID: 3, ParentSchemaID: 2},
}}),
},
{
ID: 3,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 3},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{ParentID: 3, ParentSchemaID: 2, Name: "type"}, ID: 51},
{NameInfo: descpb.NameInfo{Name: "db"}, ID: 3},
},
expected: `Examining 2 descriptors and 2 namespace entries...
ParentID 3, ParentSchemaID 2: type "type" (51): referenced schema ID 2: referenced descriptor not found
ParentID 3, ParentSchemaID 2: type "type" (51): arrayTypeID 0 does not exist for "ENUM": referenced type ID 0: referenced descriptor not found
`,
},
{ // 11
descTable: doctor.DescriptorTable{
{
ID: 51,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 51},
}}),
},
{
ID: 52,
DescBytes: func() []byte {
desc := &descpb.Descriptor{Union: &descpb.Descriptor_Type{
Type: &descpb.TypeDescriptor{Name: "type", ID: 52, ParentID: 51, ParentSchemaID: keys.PublicSchemaID},
}}
res, err := protoutil.Marshal(desc)
require.NoError(t, err)
return res
}(),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{Name: "db"}, ID: 51},
{NameInfo: descpb.NameInfo{ParentID: 51, ParentSchemaID: keys.PublicSchemaID, Name: "type"}, ID: 52},
},
expected: `Examining 2 descriptors and 2 namespace entries...
ParentID 51, ParentSchemaID 29: type "type" (52): arrayTypeID 0 does not exist for "ENUM": referenced type ID 0: referenced descriptor not found
`,
},
{ // 12
descTable: doctor.DescriptorTable{
{ID: 51, DescBytes: toBytes(t, validTableDescWithParentSchema)},
{
ID: 52,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 52},
}}),
},
{
ID: 53,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Schema{
Schema: &descpb.SchemaDescriptor{Name: "schema", ID: 53, ParentID: 54},
}}),
},
{
ID: 54,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db2", ID: 54},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{ParentID: 52, ParentSchemaID: 53, Name: "t"}, ID: 51},
{NameInfo: descpb.NameInfo{Name: "db"}, ID: 52},
{NameInfo: descpb.NameInfo{ParentID: 54, Name: "schema"}, ID: 53},
{NameInfo: descpb.NameInfo{Name: "db2"}, ID: 54},
},
expected: `Examining 4 descriptors and 4 namespace entries...
ParentID 52, ParentSchemaID 53: relation "t" (51): parent schema 53 is in different database 54
ParentID 54, ParentSchemaID 0: schema "schema" (53): not present in parent database [54] schemas mapping
`,
},
{ // 13
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{Name: "foo"}, ID: keys.PublicSchemaID},
{NameInfo: descpb.NameInfo{Name: "bar"}, ID: keys.PublicSchemaID},
{NameInfo: descpb.NameInfo{Name: "pg_temp_foo", ParentID: 123}, ID: 1},
{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 schema ID 2: referenced descriptor not found
`,
},
{ // 14
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{Name: "null"}, ID: int64(descpb.InvalidID)},
},
expected: `Examining 0 descriptors and 1 namespace entries...
ParentID 0, ParentSchemaID 0: namespace entry "null" (0): invalid namespace entry
`,
},
{ // 15
valid: true,
descTable: doctor.DescriptorTable{
{ID: 51, DescBytes: toBytes(t, validTableDesc)},
{
ID: 52,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 52},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{ParentID: 52, ParentSchemaID: 29, Name: "t"}, ID: 51},
{NameInfo: descpb.NameInfo{Name: "db"}, ID: 52},
},
expected: "Examining 2 descriptors and 2 namespace entries...\n",
},
{ // 16
descTable: doctor.DescriptorTable{
{ID: 51, DescBytes: toBytes(t, droppedValidTableDesc)},
{
ID: 52,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 52},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{Name: "db"}, ID: 52},
},
valid: true,
expected: "Examining 2 descriptors and 1 namespace entries...\n",
},
{ // 17
descTable: doctor.DescriptorTable{
{ID: 51, DescBytes: toBytes(t, func() *descpb.Descriptor {
desc := protoutil.Clone(validTableDesc).(*descpb.Descriptor)
tbl, _, _, _, _ := descpb.GetDescriptors(desc)
tbl.PrimaryIndex.Disabled = true
return desc
}())},
{
ID: 52,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 52},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{ParentID: 52, ParentSchemaID: 29, Name: "t"}, ID: 51},
{NameInfo: descpb.NameInfo{Name: "db"}, ID: 52},
},
expected: `Examining 2 descriptors and 2 namespace entries...
ParentID 52, ParentSchemaID 29: relation "t" (51): unimplemented: primary key dropped without subsequent addition of new primary key in same transaction
`,
},
{ // 18
descTable: doctor.DescriptorTable{
{ID: 51, DescBytes: toBytes(t, func() *descpb.Descriptor {
desc := protoutil.Clone(validTableDesc).(*descpb.Descriptor)
tbl, _, _, _, _ := descpb.GetDescriptors(desc)
tbl.MutationJobs = []descpb.TableDescriptor_MutationJob{{MutationID: 1, JobID: 123}}
tbl.Mutations = []descpb.DescriptorMutation{{MutationID: 1}}
return desc
}())},
{
ID: 52,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Database{
Database: &descpb.DatabaseDescriptor{Name: "db", ID: 52},
}}),
},
},
namespaceTable: doctor.NamespaceTable{
{NameInfo: descpb.NameInfo{ParentID: 52, ParentSchemaID: 29, Name: "t"}, ID: 51},
{NameInfo: descpb.NameInfo{Name: "db"}, ID: 52},
},
jobsTable: doctor.JobsTable{
{
ID: 123,
Payload: &jobspb.Payload{Details: jobspb.WrapPayloadDetails(jobspb.SchemaChangeDetails{})},
Status: jobs.StatusCanceled,
},
},
expected: `Examining 2 descriptors and 2 namespace entries...
ParentID 52, ParentSchemaID 29: relation "t" (51): mutation in state UNKNOWN, direction NONE, and no column/index descriptor
ParentID 52, ParentSchemaID 29: relation "t" (51): mutation job 123 has terminal status (canceled)
`,
},
}
for i, test := range tests {
var buf bytes.Buffer
for j := range test.descTable {
test.descTable[j].ModTime = hlc.MaxTimestamp
}
valid, err := doctor.ExamineDescriptors(
context.Background(),
clusterversion.TestingClusterVersion,
test.descTable,
test.namespaceTable,
test.jobsTable,
true,
false,
&buf)
msg := fmt.Sprintf("Test %d failed!", i+1)
if test.errStr != "" {
require.Containsf(t, err.Error(), test.errStr, msg)
} else {
require.NoErrorf(t, err, msg)
}
require.Equalf(t, test.expected, buf.String(), msg)
require.Equalf(t, test.valid, valid, msg)
}
}
func TestExamineJobs(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
tests := []struct {
descTable doctor.DescriptorTable
jobsTable doctor.JobsTable
valid bool
errStr string
expected string
}{
{
jobsTable: doctor.JobsTable{
{
ID: 1,
Payload: &jobspb.Payload{Details: jobspb.WrapPayloadDetails(jobspb.BackupDetails{})},
Status: jobs.StatusRunning,
},
},
valid: true,
expected: "Examining 1 jobs...\n",
},
{
descTable: doctor.DescriptorTable{
{
ID: 2,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Table{
Table: &descpb.TableDescriptor{ID: 2},
}}),
},
},
jobsTable: doctor.JobsTable{
{
ID: 100,
Payload: &jobspb.Payload{Details: jobspb.WrapPayloadDetails(jobspb.SchemaChangeGCDetails{})},
Progress: &jobspb.Progress{Details: jobspb.WrapProgressDetails(
jobspb.SchemaChangeGCProgress{
Tables: []jobspb.SchemaChangeGCProgress_TableProgress{
{ID: 1, Status: jobspb.SchemaChangeGCProgress_CLEARED},
{ID: 2, Status: jobspb.SchemaChangeGCProgress_CLEARING},
{ID: 3, Status: jobspb.SchemaChangeGCProgress_WAITING_FOR_CLEAR},
},
})},
Status: jobs.StatusRunning,
},
{
ID: 200,
Payload: &jobspb.Payload{Details: jobspb.WrapPayloadDetails(jobspb.SchemaChangeGCDetails{})},
Progress: &jobspb.Progress{Details: jobspb.WrapProgressDetails(
jobspb.SchemaChangeGCProgress{
Tables: []jobspb.SchemaChangeGCProgress_TableProgress{
{ID: 1, Status: jobspb.SchemaChangeGCProgress_CLEARED},
{ID: 3, Status: jobspb.SchemaChangeGCProgress_WAITING_FOR_CLEAR},
},
})},
Status: jobs.StatusPauseRequested,
},
{
ID: 300,
Payload: &jobspb.Payload{Details: jobspb.WrapPayloadDetails(jobspb.SchemaChangeGCDetails{})},
Progress: &jobspb.Progress{Details: jobspb.WrapProgressDetails(
jobspb.SchemaChangeGCProgress{
Tables: []jobspb.SchemaChangeGCProgress_TableProgress{
{ID: 1, Status: jobspb.SchemaChangeGCProgress_CLEARED},
{ID: 3, Status: jobspb.SchemaChangeGCProgress_WAITING_FOR_CLEAR},
},
Indexes: []jobspb.SchemaChangeGCProgress_IndexProgress{
{IndexID: 10, Status: jobspb.SchemaChangeGCProgress_WAITING_FOR_CLEAR},
},
})},
Status: jobs.StatusPaused,
},
},
expected: `Examining 3 jobs...
job 100: running schema change GC refers to missing table descriptor(s) [3]; existing descriptors that still need to be dropped [2]; job safe to delete: false.
job 200: pause-requested schema change GC refers to missing table descriptor(s) [3]; existing descriptors that still need to be dropped []; job safe to delete: true.
job 300: paused schema change GC refers to missing table descriptor(s) [3]; existing descriptors that still need to be dropped []; job safe to delete: false.
`,
},
{
jobsTable: doctor.JobsTable{
{
ID: 100,
Payload: &jobspb.Payload{Details: jobspb.WrapPayloadDetails(jobspb.TypeSchemaChangeDetails{TypeID: 500})},
Progress: &jobspb.Progress{Details: jobspb.WrapProgressDetails(jobspb.TypeSchemaChangeProgress{})},
Status: jobs.StatusRunning,
},
},
expected: `Examining 1 jobs...
job 100: running type schema change refers to missing type descriptor [500].
`,
},
{
descTable: doctor.DescriptorTable{
{
ID: 2,
DescBytes: toBytes(t, &descpb.Descriptor{Union: &descpb.Descriptor_Table{
Table: &descpb.TableDescriptor{ID: 2},
}}),
},
},
jobsTable: doctor.JobsTable{
{
ID: 100,
Payload: &jobspb.Payload{Details: jobspb.WrapPayloadDetails(jobspb.SchemaChangeDetails{DescID: 2, DroppedDatabaseID: 500})},
Progress: &jobspb.Progress{Details: jobspb.WrapProgressDetails(jobspb.SchemaChangeProgress{})},
Status: jobs.StatusRunning,
},
},
expected: `Examining 1 jobs...
job 100: running schema change refers to missing descriptor(s) [500].
`,
},
}
for i, test := range tests {
var buf bytes.Buffer
valid, err := doctor.ExamineJobs(
context.Background(), test.descTable, test.jobsTable, false, &buf)
msg := fmt.Sprintf("Test %d failed!", i+1)
if test.errStr != "" {
require.Containsf(t, err.Error(), test.errStr, msg)
} else {
require.NoErrorf(t, err, msg)
}
if test.valid != valid {
t.Errorf("%s valid\n\texpected: %v\n\tactual: %v", msg, test.valid, valid)
}
require.Equalf(t, test.expected, buf.String(), msg)
}
}