1 : /* Copyright (C) 2006-2008 MySQL AB
2 :
3 : This program is free software; you can redistribute it and/or modify
4 : it under the terms of the GNU General Public License as published by
5 : the Free Software Foundation; version 2 of the License.
6 :
7 : This program is distributed in the hope that it will be useful,
8 : but WITHOUT ANY WARRANTY; without even the implied warranty of
9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 : GNU General Public License for more details.
11 :
12 : You should have received a copy of the GNU General Public License
13 : along with this program; if not, write to the Free Software
14 : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
15 :
16 : #include "../maria_def.h"
17 : #include <stdio.h>
18 : #include <errno.h>
19 : #include <tap.h>
20 : #include "../trnman.h"
21 :
22 : extern my_bool maria_log_remove();
23 : extern void translog_example_table_init();
24 :
25 : #ifndef DBUG_OFF
26 : static const char *default_dbug_option;
27 : #endif
28 :
29 : #define PCACHE_SIZE (1024*1024*10)
30 : #define PCACHE_PAGE TRANSLOG_PAGE_SIZE
31 : #define LOG_FILE_SIZE (1024L*1024L*1024L + 1024L*1024L*512)
32 : #define LOG_FLAGS 0
33 :
34 : static char *first_translog_file= (char*)"maria_log.00000001";
35 : static char *file1_name= (char*)"page_cache_test_file_1";
36 : static PAGECACHE_FILE file1;
37 :
38 :
39 : /**
40 : @brief Dummy pagecache callback.
41 : */
42 :
43 : static my_bool
44 : dummy_callback(uchar *page __attribute__((unused)),
45 : pgcache_page_no_t page_no __attribute__((unused)),
46 : uchar* data_ptr __attribute__((unused)))
47 1 : {
48 1 : return 0;
49 : }
50 :
51 :
52 : /**
53 : @brief Dummy pagecache callback.
54 : */
55 :
56 : static void
57 : dummy_fail_callback(uchar* data_ptr __attribute__((unused)))
58 0 : {
59 : return;
60 : }
61 :
62 :
63 : int main(int argc __attribute__((unused)), char *argv[])
64 1 : {
65 : uint pagen;
66 : uchar long_tr_id[6];
67 : PAGECACHE pagecache;
68 : LSN lsn;
69 : my_off_t file_size;
70 : LEX_CUSTRING parts[TRANSLOG_INTERNAL_PARTS + 1];
71 :
72 1 : MY_INIT(argv[0]);
73 :
74 1 : plan(1);
75 :
76 1 : bzero(&pagecache, sizeof(pagecache));
77 1 : maria_data_root= (char *)".";
78 1 : if (maria_log_remove())
79 0 : exit(1);
80 : /* be sure that we have no logs in the directory*/
81 1 : my_delete(CONTROL_FILE_BASE_NAME, MYF(0));
82 1 : my_delete(first_translog_file, MYF(0));
83 :
84 1 : bzero(long_tr_id, 6);
85 : #ifndef DBUG_OFF
86 : #if defined(__WIN__)
87 : default_dbug_option= "d:t:i:O,\\ma_test_loghandler_pagecache.trace";
88 : #else
89 1 : default_dbug_option= "d:t:i:o,/tmp/ma_test_loghandler_pagecache.trace";
90 : #endif
91 1 : if (argc > 1)
92 : {
93 0 : DBUG_SET(default_dbug_option);
94 0 : DBUG_SET_INITIAL(default_dbug_option);
95 : }
96 : #endif
97 :
98 1 : if (ma_control_file_open(TRUE, TRUE))
99 : {
100 0 : fprintf(stderr, "Can't init control file (%d)\n", errno);
101 0 : exit(1);
102 : }
103 1 : if ((pagen= init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
104 : PCACHE_PAGE, 0)) == 0)
105 : {
106 0 : fprintf(stderr, "Got error: init_pagecache() (errno: %d)\n", errno);
107 0 : exit(1);
108 : }
109 1 : if (translog_init_with_table(".", LOG_FILE_SIZE, 50112, 0, &pagecache,
110 : LOG_FLAGS, 0, &translog_example_table_init,
111 : 0))
112 : {
113 0 : fprintf(stderr, "Can't init loghandler (%d)\n", errno);
114 0 : exit(1);
115 : }
116 : /* Suppressing of automatic record writing */
117 1 : dummy_transaction_object.first_undo_lsn|= TRANSACTION_LOGGED_LONG_ID;
118 :
119 1 : if ((file1.file= my_open(first_translog_file, O_RDONLY, MYF(MY_WME))) < 0)
120 : {
121 0 : fprintf(stderr, "There is no %s (%d)\n", first_translog_file, errno);
122 0 : exit(1);
123 : }
124 1 : file_size= my_seek(file1.file, 0, SEEK_END, MYF(MY_WME));
125 1 : if (file_size != TRANSLOG_PAGE_SIZE)
126 : {
127 0 : fprintf(stderr,
128 : "incorrect initial size of %s: %ld instead of %ld\n",
129 : first_translog_file, (long)file_size, (long)TRANSLOG_PAGE_SIZE);
130 0 : exit(1);
131 : }
132 1 : my_close(file1.file, MYF(MY_WME));
133 1 : int4store(long_tr_id, 0);
134 1 : parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_tr_id;
135 1 : parts[TRANSLOG_INTERNAL_PARTS + 0].length= 6;
136 1 : dummy_transaction_object.first_undo_lsn= TRANSACTION_LOGGED_LONG_ID;
137 1 : if (translog_write_record(&lsn,
138 : LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
139 : &dummy_transaction_object, NULL, 6,
140 : TRANSLOG_INTERNAL_PARTS + 1,
141 : parts, NULL, NULL))
142 : {
143 0 : fprintf(stderr, "Can't write record #%lu\n", (ulong) 0);
144 0 : translog_destroy();
145 0 : exit(1);
146 : }
147 :
148 1 : if ((file1.file= my_open(file1_name,
149 : O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
150 : {
151 0 : fprintf(stderr, "Got error during file1 creation from open() (errno: %d)\n",
152 : errno);
153 0 : exit(1);
154 : }
155 1 : pagecache_file_init(file1, &dummy_callback, &dummy_callback,
156 : &dummy_fail_callback, maria_flush_log_for_page, NULL);
157 1 : if (my_chmod(file1_name, S_IRWXU | S_IRWXG | S_IRWXO, MYF(MY_WME)))
158 0 : exit(1);
159 :
160 : {
161 : uchar page[PCACHE_PAGE];
162 :
163 1 : bzero(page, PCACHE_PAGE);
164 1 : lsn_store(page, lsn);
165 1 : pagecache_write(&pagecache, &file1, 0, 3, page,
166 : PAGECACHE_LSN_PAGE,
167 : PAGECACHE_LOCK_LEFT_UNLOCKED,
168 : PAGECACHE_PIN_LEFT_UNPINNED,
169 : PAGECACHE_WRITE_DELAY,
170 : 0, LSN_IMPOSSIBLE);
171 1 : flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
172 : }
173 1 : my_close(file1.file, MYF(MY_WME));
174 1 : if ((file1.file= my_open(first_translog_file, O_RDONLY, MYF(MY_WME))) < 0)
175 : {
176 0 : fprintf(stderr, "can't open %s (%d)\n", first_translog_file, errno);
177 0 : exit(1);
178 : }
179 1 : file_size= my_seek(file1.file, 0, SEEK_END, MYF(MY_WME));
180 1 : if (file_size != TRANSLOG_PAGE_SIZE * 2)
181 : {
182 0 : fprintf(stderr,
183 : "incorrect initial size of %s: %ld instead of %ld\n",
184 : first_translog_file,
185 : (long)file_size, (long)(TRANSLOG_PAGE_SIZE * 2));
186 0 : ok(0, "log triggered");
187 0 : exit(1);
188 : }
189 1 : my_close(file1.file, MYF(MY_WME));
190 1 : ok(1, "log triggered");
191 :
192 1 : translog_destroy();
193 1 : end_pagecache(&pagecache, 1);
194 1 : ma_control_file_end();
195 1 : my_delete(CONTROL_FILE_BASE_NAME, MYF(0));
196 1 : my_delete(first_translog_file, MYF(0));
197 1 : my_delete(file1_name, MYF(0));
198 :
199 1 : exit(0);
200 : }
|