1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 // force a bad LSN during the forward scan.  recovery should fail.
40 
41 #include "test.h"
42 
43 
recover_callback_at_turnaround(void * UU (arg))44 static void recover_callback_at_turnaround(void *UU(arg)) {
45     // change the LSN in the first log entry of log 2.  this will cause an LSN error during the forward scan.
46     int r;
47     char logname[TOKU_PATH_MAX+1];
48     sprintf(logname, "%s/log000000000002.tokulog%d", TOKU_TEST_FILENAME, TOKU_LOG_VERSION);
49     FILE *f = fopen(logname, "r+b"); assert(f);
50     r = fseek(f, 025, SEEK_SET); assert(r == 0);
51     char c = 100;
52     size_t n = fwrite(&c, sizeof c, 1, f); assert(n == sizeof c);
53     r = fclose(f); assert(r == 0);
54 }
55 
56 static int
run_test(void)57 run_test(void) {
58     int r;
59 
60     // setup the test dir
61     toku_os_recursive_delete(TOKU_TEST_FILENAME);
62     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU); assert(r == 0);
63 
64     // log 1 has the checkpoint
65     TOKULOGGER logger;
66     r = toku_logger_create(&logger); assert(r == 0);
67     r = toku_logger_open(TOKU_TEST_FILENAME, logger); assert(r == 0);
68 
69     LSN beginlsn;
70     toku_log_begin_checkpoint(logger, &beginlsn, true, 0, 0);
71     toku_log_end_checkpoint(logger, NULL, true, beginlsn, 0, 0, 0);
72 
73     r = toku_logger_close(&logger); assert(r == 0);
74 
75     // log 2 has hello
76     r = toku_logger_create(&logger); assert(r == 0);
77     r = toku_logger_open(TOKU_TEST_FILENAME, logger); assert(r == 0);
78 
79     BYTESTRING hello  = { (uint32_t) strlen("hello"), (char *) "hello" };
80     toku_log_comment(logger, NULL, true, 0, hello);
81 
82     r = toku_logger_close(&logger); assert(r == 0);
83 
84     // log 3 has there
85     r = toku_logger_create(&logger); assert(r == 0);
86     r = toku_logger_open(TOKU_TEST_FILENAME, logger); assert(r == 0);
87 
88     BYTESTRING there  = { (uint32_t) strlen("there"), (char *) "there" };
89     toku_log_comment(logger, NULL, true, 0, there);
90 
91     r = toku_logger_close(&logger); assert(r == 0);
92 
93     // redirect stderr
94     int devnul = open(DEV_NULL_FILE, O_WRONLY);
95     assert(devnul>=0);
96     r = toku_dup2(devnul, fileno(stderr)); 	    assert(r==fileno(stderr));
97     r = close(devnul);                      assert(r==0);
98 
99     // delete log 2 at the turnaround to force
100     toku_recover_set_callback(recover_callback_at_turnaround, NULL);
101 
102     // run recovery
103     r = tokuft_recover(NULL,
104 		       NULL_prepared_txn_callback,
105 		       NULL_keep_cachetable_callback,
106 		       NULL_logger, TOKU_TEST_FILENAME, TOKU_TEST_FILENAME, 0, 0, 0, NULL, 0);
107     assert(r != 0);
108 
109     toku_os_recursive_delete(TOKU_TEST_FILENAME);
110 
111     return 0;
112 }
113 
114 int
test_main(int UU (argc),const char * UU (argv[]))115 test_main(int UU(argc), const char *UU(argv[])) {
116     int r;
117     r = run_test();
118     return r;
119 }
120