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 // test recovery of "hello" comments
40 
41 #include "test.h"
42 
43 
44 static int
run_test(void)45 run_test(void) {
46     // leave this many bytes in file
47     const int magic_begin_end_checkpoint_sz = 8 // "tokulogg" magic 8 byte header
48                                              +4 // version
49                                              +toku_log_begin_checkpoint_overhead
50                                              +toku_log_end_checkpoint_overhead;
51 
52     int r;
53     int trim = 1;
54     toku_struct_stat st;
55 
56     while ( 1 ) {
57         // setup the test dir
58         toku_os_recursive_delete(TOKU_TEST_FILENAME);
59         r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU); assert(r == 0);
60 
61         // create the log
62         TOKULOGGER logger;
63         BYTESTRING hello  = { (uint32_t) strlen("hello"), (char *) "hello" };
64         BYTESTRING world  = { (uint32_t) strlen("world"), (char *) "world" };
65         BYTESTRING there  = { (uint32_t) strlen("there"), (char *) "there" };
66         r = toku_logger_create(&logger); assert(r == 0);
67         r = toku_logger_open(TOKU_TEST_FILENAME, logger); assert(r == 0);
68         LSN beginlsn;
69         // all logs must contain a valid checkpoint
70         toku_log_begin_checkpoint(logger, &beginlsn, true, 0, 0);
71         toku_log_end_checkpoint(logger, NULL, true, beginlsn, 0, 0, 0);
72         toku_log_comment(logger, NULL, true, 0, hello);
73         toku_log_comment(logger, NULL, true, 0, world);
74         toku_log_begin_checkpoint(logger, &beginlsn, true, 0, 0);
75         toku_log_end_checkpoint(logger, NULL, true, beginlsn, 0, 0, 0);
76         toku_log_comment(logger, NULL, true, 0, hello);
77         toku_log_comment(logger, NULL, true, 0, there);
78         toku_logger_close(&logger);
79 
80         // redirect stderr
81         int devnul = open(DEV_NULL_FILE, O_WRONLY);
82         assert(devnul>=0);
83         r = toku_dup2(devnul, fileno(stderr)); 	    assert(r==fileno(stderr));
84         r = close(devnul);                      assert(r==0);
85 
86         char fname[TOKU_PATH_MAX + 1];
87         sprintf(fname,
88                 "%s/%s%d",
89                 TOKU_TEST_FILENAME,
90                 "log000000000000.tokulog",
91                 TOKU_LOG_VERSION);
92 
93         r = toku_stat(fname, &st, toku_uninstrumented);
94         assert(r == 0);
95         if (st.st_size - trim > magic_begin_end_checkpoint_sz) {
96             r = truncate(fname, st.st_size - trim);
97             CKERR(r);
98         }
99         else
100             break;
101         // run recovery
102         r = tokuft_recover(NULL,
103 			   NULL_prepared_txn_callback,
104 			   NULL_keep_cachetable_callback,
105 			   NULL_logger,
106 			   TOKU_TEST_FILENAME, TOKU_TEST_FILENAME, 0, 0, 0, NULL, 0);
107         assert(r == 0);
108 
109         trim += 1;
110     }
111     toku_os_recursive_delete(TOKU_TEST_FILENAME);
112     return 0;
113 }
114 
115 int
test_main(int UU (argc),const char * UU (argv[]))116 test_main(int UU(argc), const char *UU(argv[])) {
117     int r;
118     r = run_test();
119     return r;
120 }
121