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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
/*-
* Public Domain 2014-2015 MongoDB, Inc.
* Public Domain 2008-2014 WiredTiger, Inc.
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "test_util.i"
#include <assert.h>
#define DUMP "__slvg.dump" /* Dump file */
#define LOAD "__slvg.load" /* Build file */
#define RSLT "__slvg.result" /* Result file */
#define SLVG "__slvg.slvg" /* Salvage file */
#define PSIZE (2 * 1024)
#define OSIZE (PSIZE / 20)
#define CHECK(x) do { \
if (!(x)) { \
fprintf(stderr, "%s: failed\n", #x); \
assert(0); \
} \
} while (0)
void build(int, int, int);
void copy(u_int, u_int);
void empty(int);
void print_res(int, int, int);
void process(void);
void run(int);
void t(int, u_int, int);
int usage(void);
static const char *progname; /* Program name */
static FILE *res_fp; /* Results file */
static u_int page_type; /* File types */
static int value_unique; /* Values are unique */
static int verbose; /* -v flag */
extern int __wt_optind;
extern char *__wt_optarg;
int
main(int argc, char *argv[])
{
u_int ptype;
int ch, r;
if ((progname = strrchr(argv[0], DIR_DELIM)) == NULL)
progname = argv[0];
else
++progname;
r = 0;
ptype = 0;
while ((ch = __wt_getopt(progname, argc, argv, "r:t:v")) != EOF)
switch (ch) {
case 'r':
r = atoi(__wt_optarg);
if (r == 0)
return (usage());
break;
case 't':
if (strcmp(__wt_optarg, "fix") == 0)
ptype = WT_PAGE_COL_FIX;
else if (strcmp(__wt_optarg, "var") == 0)
ptype = WT_PAGE_COL_VAR;
else if (strcmp(__wt_optarg, "row") == 0)
ptype = WT_PAGE_ROW_LEAF;
else
return (usage());
break;
case 'v':
verbose = 1;
break;
case '?':
default:
return (usage());
}
argc -= __wt_optind;
argv += __wt_optind;
if (argc != 0)
return (usage());
printf("salvage test run started\n");
t(r, ptype, 1);
t(r, ptype, 0);
printf("salvage test run completed\n");
return (EXIT_SUCCESS);
}
void
t(int r, u_int ptype, int unique)
{
printf("%sunique values\n", unique ? "" : "non-");
value_unique = unique;
#define NTESTS 24
if (r == 0) {
if (ptype == 0) {
page_type = WT_PAGE_COL_FIX;
for (r = 1; r <= NTESTS; ++r)
run(r);
page_type = WT_PAGE_COL_VAR;
for (r = 1; r <= NTESTS; ++r)
run(r);
page_type = WT_PAGE_ROW_LEAF;
for (r = 1; r <= NTESTS; ++r)
run(r);
} else {
page_type = ptype;
for (r = 1; r <= NTESTS; ++r)
run(r);
}
} else if (ptype == 0) {
page_type = WT_PAGE_COL_FIX;
run(r);
page_type = WT_PAGE_COL_VAR;
run(r);
page_type = WT_PAGE_ROW_LEAF;
run(r);
} else {
page_type = ptype;
run(r);
}
}
int
usage(void)
{
(void)fprintf(stderr,
"usage: %s [-v] [-r run] [-t fix|rle|var|row]\n", progname);
return (EXIT_FAILURE);
}
void
run(int r)
{
char buf[128];
printf("\t%s: run %d\n", __wt_page_type_string(page_type), r);
CHECK(system("rm -f WiredTiger* __slvg.* __schema.*") == 0);
CHECK((res_fp = fopen(RSLT, "w")) != NULL);
/*
* Each run builds the LOAD file, and then appends the first page of
* the LOAD file into the SLVG file. The SLVG file is then salvaged,
* verified, and dumped into the DUMP file, which is compared to the
* results file, which are the expected results.
*/
switch (r) {
case 1:
/*
* Smoke test: empty files.
*/
build(0, 0, 0); copy(0, 0);
break;
case 2:
/*
* Smoke test:
* Sequential pages, all pages should be kept.
*/
build(100, 100, 20); copy(6, 1);
build(200, 200, 20); copy(7, 21);
build(300, 300, 20); copy(8, 41);
print_res(100, 100, 20);
print_res(200, 200, 20);
print_res(300, 300, 20);
break;
case 3:
/*
* Smoke test:
* Sequential pages, all pages should be kept.
*/
build(100, 100, 20); copy(8, 1);
build(200, 200, 20); copy(7, 21);
build(300, 300, 20); copy(6, 41);
print_res(100, 100, 20);
print_res(200, 200, 20);
print_res(300, 300, 20);
break;
case 4:
/*
* Case #1:
* 3 pages, each with 20 records starting with the same record
* and sequential LSNs; salvage should leave the page with the
* largest LSN.
*/
build(100, 100, 20); copy(6, 1);
build(100, 200, 20); copy(7, 1);
build(100, 300, 20); copy(8, 1);
print_res(100, 300, 20);
break;
case 5:
/*
* Case #1:
* 3 pages, each with 20 records starting with the same record
* and sequential LSNs; salvage should leave the page with the
* largest LSN.
*/
build(100, 100, 20); copy(6, 1);
build(100, 200, 20); copy(8, 1);
build(100, 300, 20); copy(7, 1);
print_res(100, 200, 20);
break;
case 6:
/*
* Case #1:
* 3 pages, each with 20 records starting with the same record
* and sequential LSNs; salvage should leave the page with the
* largest LSN.
*/
build(100, 100, 20); copy(8, 1);
build(100, 200, 20); copy(7, 1);
build(100, 300, 20); copy(6, 1);
print_res(100, 100, 20);
break;
case 7:
/*
* Case #2:
* The second page overlaps the beginning of the first page, and
* the first page has a higher LSN.
*/
build(110, 100, 20); copy(7, 11);
build(100, 200, 20); copy(6, 1);
print_res(100, 200, 10);
print_res(110, 100, 20);
break;
case 8:
/*
* Case #2:
* The second page overlaps the beginning of the first page, and
* the second page has a higher LSN.
*/
build(110, 100, 20); copy(6, 11);
build(100, 200, 20); copy(7, 1);
print_res(100, 200, 20);
print_res(120, 110, 10);
break;
case 9:
/*
* Case #3:
* The second page overlaps with the end of the first page, and
* the first page has a higher LSN.
*/
build(100, 100, 20); copy(7, 1);
build(110, 200, 20); copy(6, 11);
print_res(100, 100, 20);
print_res(120, 210, 10);
break;
case 10:
/*
* Case #3:
* The second page overlaps with the end of the first page, and
* the second page has a higher LSN.
*/
build(100, 100, 20); copy(6, 1);
build(110, 200, 20); copy(7, 11);
print_res(100, 100, 10);
print_res(110, 200, 20);
break;
case 11:
/*
* Case #4:
* The second page is a prefix of the first page, and the first
* page has a higher LSN.
*/
build(100, 100, 20); copy(7, 1);
build(100, 200, 5); copy(6, 1);
print_res(100, 100, 20);
break;
case 12:
/*
* Case #4:
* The second page is a prefix of the first page, and the second
* page has a higher LSN.
*/
build(100, 100, 20); copy(6, 1);
build(100, 200, 5); copy(7, 1);
print_res(100, 200, 5);
print_res(105, 105, 15);
break;
case 13:
/*
* Case #5:
* The second page is in the middle of the first page, and the
* first page has a higher LSN.
*/
build(100, 100, 40); copy(7, 1);
build(110, 200, 10); copy(6, 11);
print_res(100, 100, 40);
break;
case 14:
/*
* Case #5:
* The second page is in the middle of the first page, and the
* second page has a higher LSN.
*/
build(100, 100, 40); copy(6, 1);
build(110, 200, 10); copy(7, 11);
print_res(100, 100, 10);
print_res(110, 200, 10);
print_res(120, 120, 20);
break;
case 15:
/*
* Case #6:
* The second page is a suffix of the first page, and the first
* page has a higher LSN.
*/
build(100, 100, 40); copy(7, 1);
build(130, 200, 10); copy(6, 31);
print_res(100, 100, 40);
break;
case 16:
/*
* Case #6:
* The second page is a suffix of the first page, and the second
* page has a higher LSN.
*/
build(100, 100, 40); copy(6, 1);
build(130, 200, 10); copy(7, 31);
print_res(100, 100, 30);
print_res(130, 200, 10);
break;
case 17:
/*
* Case #9:
* The first page is a prefix of the second page, and the first
* page has a higher LSN.
*/
build(100, 100, 20); copy(7, 1);
build(100, 200, 40); copy(6, 1);
print_res(100, 100, 20);
print_res(120, 220, 20);
break;
case 18:
/*
* Case #9:
* The first page is a prefix of the second page, and the second
* page has a higher LSN.
*/
build(100, 100, 20); copy(6, 1);
build(100, 200, 40); copy(7, 1);
print_res(100, 200, 40);
break;
case 19:
/*
* Case #10:
* The first page is a suffix of the second page, and the first
* page has a higher LSN.
*/
build(130, 100, 10); copy(7, 31);
build(100, 200, 40); copy(6, 1);
print_res(100, 200, 30);
print_res(130, 100, 10);
break;
case 20:
/*
* Case #10:
* The first page is a suffix of the second page, and the second
* page has a higher LSN.
*/
build(130, 100, 10); copy(6, 31);
build(100, 200, 40); copy(7, 1);
print_res(100, 200, 40);
break;
case 21:
/*
* Case #11:
* The first page is in the middle of the second page, and the
* first page has a higher LSN.
*/
build(110, 100, 10); copy(7, 11);
build(100, 200, 40); copy(6, 1);
print_res(100, 200, 10);
print_res(110, 100, 10);
print_res(120, 220, 20);
break;
case 22:
/*
* Case #11:
* The first page is in the middle of the second page, and the
* second page has a higher LSN.
*/
build(110, 100, 10); copy(6, 11);
build(100, 200, 40); copy(7, 1);
print_res(100, 200, 40);
break;
case 23:
/*
* Column-store only: missing an initial key range of 99
* records.
*/
build(100, 100, 10); copy(1, 100);
empty(99);
print_res(100, 100, 10);
break;
case 24:
/*
* Column-store only: missing a middle key range of 37
* records.
*/
build(100, 100, 10); copy(1, 1);
build(138, 138, 10); copy(1, 48);
print_res(100, 100, 10);
empty(37);
print_res(138, 138, 10);
break;
default:
fprintf(stderr, "salvage: %d: no such test\n", r);
exit(EXIT_FAILURE);
}
CHECK(fclose(res_fp) == 0);
process();
snprintf(buf, sizeof(buf), "cmp %s %s > /dev/null", DUMP, RSLT);
if (system(buf)) {
fprintf(stderr,
"check failed, salvage results were incorrect\n");
exit(EXIT_FAILURE);
}
}
/*
* file_exists --
* Return if the file exists.
*/
static int
file_exists(const char *path)
{
struct stat sb;
return (stat(path, &sb) == 0);
}
/*
* build --
* Build a row- or column-store page in a file.
*/
void
build(int ikey, int ivalue, int cnt)
{
WT_CONNECTION *conn;
WT_CURSOR *cursor;
WT_ITEM key, value;
WT_SESSION *session;
char config[256], kbuf[64], vbuf[64];
int new_slvg;
/*
* Disable logging: we're modifying files directly, we don't want to
* run recovery.
*/
CHECK(wiredtiger_open(
NULL, NULL, "create,log=(enabled=false)", &conn) == 0);
CHECK(conn->open_session(conn, NULL, NULL, &session) == 0);
CHECK(session->drop(session, "file:" LOAD, "force") == 0);
switch (page_type) {
case WT_PAGE_COL_FIX:
(void)snprintf(config, sizeof(config),
"key_format=r,value_format=7t,"
"allocation_size=%d,"
"internal_page_max=%d,internal_item_max=%d,"
"leaf_page_max=%d,leaf_item_max=%d",
PSIZE, PSIZE, OSIZE, PSIZE, OSIZE);
break;
case WT_PAGE_COL_VAR:
(void)snprintf(config, sizeof(config),
"key_format=r,"
"allocation_size=%d,"
"internal_page_max=%d,internal_item_max=%d,"
"leaf_page_max=%d,leaf_item_max=%d",
PSIZE, PSIZE, OSIZE, PSIZE, OSIZE);
break;
case WT_PAGE_ROW_LEAF:
(void)snprintf(config, sizeof(config),
"key_format=u,"
"allocation_size=%d,"
"internal_page_max=%d,internal_item_max=%d,"
"leaf_page_max=%d,leaf_item_max=%d",
PSIZE, PSIZE, OSIZE, PSIZE, OSIZE);
break;
default:
assert(0);
}
CHECK(session->create(session, "file:" LOAD, config) == 0);
CHECK(session->open_cursor(
session, "file:" LOAD, NULL, "bulk,append", &cursor) == 0);
for (; cnt > 0; --cnt, ++ikey, ++ivalue) {
switch (page_type) { /* Build the key. */
case WT_PAGE_COL_FIX:
case WT_PAGE_COL_VAR:
break;
case WT_PAGE_ROW_LEAF:
snprintf(kbuf, sizeof(kbuf), "%010d KEY------", ikey);
key.data = kbuf;
key.size = 20;
cursor->set_key(cursor, &key);
break;
}
switch (page_type) { /* Build the value. */
case WT_PAGE_COL_FIX:
cursor->set_value(cursor, ivalue & 0x7f);
break;
case WT_PAGE_COL_VAR:
case WT_PAGE_ROW_LEAF:
snprintf(vbuf, sizeof(vbuf),
"%010d VALUE----", value_unique ? ivalue : 37);
value.data = vbuf;
value.size = 20;
cursor->set_value(cursor, &value);
}
CHECK(cursor->insert(cursor) == 0);
}
/*
* The first time through this routine we create the salvage file and
* then remove it (all we want is the appropriate schema entry, we're
* creating the salvage file itself by hand).
*/
new_slvg = !file_exists(SLVG);
if (new_slvg) {
CHECK(session->drop(session, "file:" SLVG, "force") == 0);
CHECK(session->create(session, "file:" SLVG, config) == 0);
}
CHECK(conn->close(conn, 0) == 0);
if (new_slvg)
(void)remove(SLVG);
}
/*
* copy --
* Copy the created page to the end of the salvage file.
*/
void
copy(u_int gen, u_int recno)
{
FILE *ifp, *ofp;
WT_PAGE_HEADER *dsk;
WT_BLOCK_HEADER *blk;
char buf[PSIZE];
CHECK((ifp = fopen(LOAD, "r")) != NULL);
/*
* If the salvage file doesn't exist, then we're creating it:
* copy the first sector (the file description).
* Otherwise, we are appending to an existing file.
*/
if (file_exists(SLVG))
CHECK((ofp = fopen(SLVG, "a")) != NULL);
else {
CHECK((ofp = fopen(SLVG, "w")) != NULL);
CHECK(fread(buf, 1, PSIZE, ifp) == PSIZE);
CHECK(fwrite(buf, 1, PSIZE, ofp) == PSIZE);
}
/*
* If there's data, copy/update the first formatted page.
*/
if (gen != 0) {
CHECK(fseek(ifp, (long)PSIZE, SEEK_SET) == 0);
CHECK(fread(buf, 1, PSIZE, ifp) == PSIZE);
dsk = (void *)buf;
if (page_type != WT_PAGE_ROW_LEAF)
dsk->recno = recno;
dsk->write_gen = gen;
blk = WT_BLOCK_HEADER_REF(buf);
blk->cksum = 0;
blk->cksum = __wt_cksum(dsk, PSIZE);
CHECK(fwrite(buf, 1, PSIZE, ofp) == PSIZE);
}
CHECK(fclose(ifp) == 0);
CHECK(fclose(ofp) == 0);
}
/*
* process --
* Salvage, verify and dump the created file.
*/
void
process(void)
{
FILE *fp;
WT_CONNECTION *conn;
WT_CURSOR *cursor;
const char *key, *value;
WT_SESSION *session;
char config[100];
/* Salvage. */
config[0] = '\0';
if (verbose)
snprintf(config, sizeof(config),
"error_prefix=\"%s\",verbose=[salvage,verify],",
progname);
strcat(config, "log=(enabled=false),");
CHECK(wiredtiger_open(NULL, NULL, config, &conn) == 0);
CHECK(conn->open_session(conn, NULL, NULL, &session) == 0);
CHECK(session->salvage(session, "file:" SLVG, 0) == 0);
CHECK(conn->close(conn, 0) == 0);
/* Verify. */
CHECK(wiredtiger_open(NULL, NULL, config, &conn) == 0);
CHECK(conn->open_session(conn, NULL, NULL, &session) == 0);
CHECK(session->verify(session, "file:" SLVG, 0) == 0);
CHECK(conn->close(conn, 0) == 0);
/* Dump. */
CHECK((fp = fopen(DUMP, "w")) != NULL);
CHECK(wiredtiger_open(NULL, NULL, config, &conn) == 0);
CHECK(conn->open_session(conn, NULL, NULL, &session) == 0);
CHECK(session->open_cursor(
session, "file:" SLVG, NULL, "dump=print", &cursor) == 0);
while (cursor->next(cursor) == 0) {
if (page_type == WT_PAGE_ROW_LEAF) {
CHECK(cursor->get_key(cursor, &key) == 0);
CHECK(fputs(key, fp) >= 0);
CHECK(fputc('\n', fp) >= 0);
}
CHECK(cursor->get_value(cursor, &value) == 0);
CHECK(fputs(value, fp) >= 0);
CHECK(fputc('\n', fp) >= 0);
}
CHECK(conn->close(conn, 0) == 0);
CHECK(fclose(fp) == 0);
}
/*
* empty --
* Print empty print_res, for fixed-length column-store files.
*/
void
empty(int cnt)
{
int i;
if (page_type == WT_PAGE_COL_FIX)
for (i = 0; i < cnt; ++i)
fputs("\\00\n", res_fp);
}
/*
* print_res --
* Write results file.
*/
void
print_res(int key, int value, int cnt)
{
static const char hex[] = "0123456789abcdef";
int ch;
for (; cnt > 0; ++key, ++value, --cnt) {
switch (page_type) { /* Print key */
case WT_PAGE_COL_FIX:
case WT_PAGE_COL_VAR:
break;
case WT_PAGE_ROW_LEAF:
fprintf(res_fp, "%010d KEY------\n", key);
break;
}
switch (page_type) { /* Print value */
case WT_PAGE_COL_FIX:
ch = value & 0x7f;
if (isprint(ch)) {
if (ch == '\\')
fputc('\\', res_fp);
fputc(ch, res_fp);
} else {
fputc('\\', res_fp);
fputc(hex[(ch & 0xf0) >> 4], res_fp);
fputc(hex[ch & 0x0f], res_fp);
}
fputc('\n', res_fp);
break;
case WT_PAGE_COL_VAR:
case WT_PAGE_ROW_LEAF:
fprintf(res_fp,
"%010d VALUE----\n", value_unique ? value : 37);
break;
}
}
}
|