]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/run-57-die-during-transaction.c
ntdb: fix up tests.
[ccan] / ccan / ntdb / test / run-57-die-during-transaction.c
1 #include "../private.h"
2 #include <unistd.h>
3 #include "lock-tracking.h"
4 #include "tap-interface.h"
5 #include <stdlib.h>
6 #include <assert.h>
7 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
8 static ssize_t write_check(int fd, const void *buf, size_t count);
9 static int ftruncate_check(int fd, off_t length);
10
11 #define pwrite pwrite_check
12 #define write write_check
13 #define fcntl fcntl_with_lockcheck
14 #define ftruncate ftruncate_check
15
16 /* There's a malloc inside transaction_setup_recovery, and valgrind complains
17  * when we longjmp and leak it. */
18 #define MAX_ALLOCATIONS 10
19 static void *allocated[MAX_ALLOCATIONS];
20 static unsigned max_alloc = 0;
21
22 static void *malloc_noleak(size_t len)
23 {
24         unsigned int i;
25
26         for (i = 0; i < MAX_ALLOCATIONS; i++)
27                 if (!allocated[i]) {
28                         allocated[i] = malloc(len);
29                         if (i > max_alloc) {
30                                 max_alloc = i;
31                                 diag("max_alloc: %i", max_alloc);
32                         }
33                         return allocated[i];
34                 }
35         diag("Too many allocations!");
36         abort();
37 }
38
39 static void *realloc_noleak(void *p, size_t size)
40 {
41         unsigned int i;
42
43         for (i = 0; i < MAX_ALLOCATIONS; i++) {
44                 if (allocated[i] == p) {
45                         if (i > max_alloc) {
46                                 max_alloc = i;
47                                 diag("max_alloc: %i", max_alloc);
48                         }
49                         return allocated[i] = realloc(p, size);
50                 }
51         }
52         diag("Untracked realloc!");
53         abort();
54 }
55
56 static void free_noleak(void *p)
57 {
58         unsigned int i;
59
60         /* We don't catch asprintf, so don't complain if we miss one. */
61         for (i = 0; i < MAX_ALLOCATIONS; i++) {
62                 if (allocated[i] == p) {
63                         allocated[i] = NULL;
64                         break;
65                 }
66         }
67         free(p);
68 }
69
70 static void free_all(void)
71 {
72         unsigned int i;
73
74         for (i = 0; i < MAX_ALLOCATIONS; i++) {
75                 free(allocated[i]);
76                 allocated[i] = NULL;
77         }
78 }
79
80 #define malloc malloc_noleak
81 #define free(x) free_noleak(x)
82 #define realloc realloc_noleak
83
84 #include "ntdb-source.h"
85
86 #undef malloc
87 #undef free
88 #undef realloc
89 #undef write
90 #undef pwrite
91 #undef fcntl
92 #undef ftruncate
93
94 #include <stdbool.h>
95 #include <stdarg.h>
96 #include <ccan/err/err.h>
97 #include <setjmp.h>
98 #include "external-agent.h"
99 #include "logging.h"
100 #include "helprun-external-agent.h"
101
102 static bool in_transaction;
103 static int target, current;
104 static jmp_buf jmpbuf;
105 #define TEST_DBNAME "run-57-die-during-transaction.ntdb"
106 #define KEY_STRING "helloworld"
107 #define DATA_STRING "Helloworld"
108
109 static void maybe_die(int fd)
110 {
111         if (in_transaction && current++ == target) {
112                 longjmp(jmpbuf, 1);
113         }
114 }
115
116 static ssize_t pwrite_check(int fd,
117                             const void *buf, size_t count, off_t offset)
118 {
119         ssize_t ret;
120
121         maybe_die(fd);
122
123         ret = pwrite(fd, buf, count, offset);
124         if (ret != count)
125                 return ret;
126
127         maybe_die(fd);
128         return ret;
129 }
130
131 static ssize_t write_check(int fd, const void *buf, size_t count)
132 {
133         ssize_t ret;
134
135         maybe_die(fd);
136
137         ret = write(fd, buf, count);
138         if (ret != count)
139                 return ret;
140
141         maybe_die(fd);
142         return ret;
143 }
144
145 static int ftruncate_check(int fd, off_t length)
146 {
147         int ret;
148
149         maybe_die(fd);
150
151         ret = ftruncate(fd, length);
152
153         maybe_die(fd);
154         return ret;
155 }
156
157 static bool test_death(enum operation op, struct agent *agent,
158                        bool pre_create_recovery)
159 {
160         struct ntdb_context *ntdb = NULL;
161         NTDB_DATA key, data;
162         enum agent_return ret;
163         int needed_recovery = 0;
164
165         current = target = 0;
166         /* Big long data to force a change. */
167         data = ntdb_mkdata(DATA_STRING, strlen(DATA_STRING));
168
169 reset:
170         unlink(TEST_DBNAME);
171         ntdb = ntdb_open(TEST_DBNAME, NTDB_NOMMAP|MAYBE_NOSYNC,
172                          O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
173         if (!ntdb) {
174                 diag("Failed opening NTDB: %s", strerror(errno));
175                 return false;
176         }
177
178         if (setjmp(jmpbuf) != 0) {
179                 /* We're partway through.  Simulate our death. */
180                 close(ntdb->file->fd);
181                 forget_locking();
182                 in_transaction = false;
183
184                 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
185                 if (ret == SUCCESS)
186                         needed_recovery++;
187                 else if (ret != FAILED) {
188                         diag("Step %u agent NEEDS_RECOVERY = %s", current,
189                              agent_return_name(ret));
190                         return false;
191                 }
192
193                 /* Could be key, or data. */
194                 ret = external_agent_operation(agent, op,
195                                                KEY_STRING "=" KEY_STRING);
196                 if (ret != SUCCESS) {
197                         ret = external_agent_operation(agent, op,
198                                                        KEY_STRING
199                                                        "=" DATA_STRING);
200                 }
201                 if (ret != SUCCESS) {
202                         diag("Step %u op %s failed = %s", current,
203                              operation_name(op),
204                              agent_return_name(ret));
205                         return false;
206                 }
207
208                 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
209                 if (ret != FAILED) {
210                         diag("Still needs recovery after step %u = %s",
211                              current, agent_return_name(ret));
212                         return false;
213                 }
214
215                 ret = external_agent_operation(agent, CHECK, "");
216                 if (ret != SUCCESS) {
217                         diag("Step %u check failed = %s", current,
218                              agent_return_name(ret));
219                         return false;
220                 }
221
222                 ret = external_agent_operation(agent, CLOSE, "");
223                 if (ret != SUCCESS) {
224                         diag("Step %u close failed = %s", current,
225                              agent_return_name(ret));
226                         return false;
227                 }
228
229                 /* Suppress logging as this tries to use closed fd. */
230                 suppress_logging = true;
231                 suppress_lockcheck = true;
232                 ntdb_close(ntdb);
233                 suppress_logging = false;
234                 suppress_lockcheck = false;
235                 target++;
236                 current = 0;
237                 free_all();
238                 goto reset;
239         }
240
241         /* Put key for agent to fetch. */
242         key = ntdb_mkdata(KEY_STRING, strlen(KEY_STRING));
243
244         if (pre_create_recovery) {
245                 /* Using a transaction now means we allocate the recovery
246                  * area immediately.  That makes the later transaction smaller
247                  * and thus tickles a bug we had. */
248                 if (ntdb_transaction_start(ntdb) != 0)
249                         return false;
250         }
251         if (ntdb_store(ntdb, key, key, NTDB_INSERT) != 0)
252                 return false;
253         if (pre_create_recovery) {
254                 if (ntdb_transaction_commit(ntdb) != 0)
255                         return false;
256         }
257
258         /* This is the key we insert in transaction. */
259         key.dsize--;
260
261         ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
262         if (ret != SUCCESS)
263                 errx(1, "Agent failed to open: %s", agent_return_name(ret));
264
265         ret = external_agent_operation(agent, FETCH, KEY_STRING "=" KEY_STRING);
266         if (ret != SUCCESS)
267                 errx(1, "Agent failed find key: %s", agent_return_name(ret));
268
269         in_transaction = true;
270         if (ntdb_transaction_start(ntdb) != 0)
271                 return false;
272
273         if (ntdb_store(ntdb, key, data, NTDB_INSERT) != 0)
274                 return false;
275
276         if (ntdb_transaction_commit(ntdb) != 0)
277                 return false;
278
279         in_transaction = false;
280
281         /* We made it! */
282         diag("Completed %u runs", current);
283         ntdb_close(ntdb);
284         ret = external_agent_operation(agent, CLOSE, "");
285         if (ret != SUCCESS) {
286                 diag("Step %u close failed = %s", current,
287                      agent_return_name(ret));
288                 return false;
289         }
290
291         ok1(needed_recovery);
292         ok1(locking_errors == 0);
293         ok1(forget_locking() == 0);
294         locking_errors = 0;
295         return true;
296 }
297
298 int main(int argc, char *argv[])
299 {
300         enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
301         struct agent *agent;
302         int i, j;
303
304         plan_tests(24);
305         unlock_callback = maybe_die;
306
307         external_agent_free = free_noleak;
308         agent = prepare_external_agent();
309         if (!agent)
310                 err(1, "preparing agent");
311
312         for (j = 0; j < 2; j++) {
313                 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
314                         diag("Testing %s after death (%s recovery area)",
315                              operation_name(ops[i]), j ? "with" : "without");
316                         ok1(test_death(ops[i], agent, j));
317                 }
318         }
319
320         free_external_agent(agent);
321         return exit_status();
322 }