]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-die-during-transaction.c
tdb2: test: try (almost) all tests with TDB_VERSION1 flag.
[ccan] / ccan / tdb2 / test / run-tdb1-die-during-transaction.c
1 #include <ccan/tdb2/private.h>
2 #include <unistd.h>
3 #include "tdb1-lock-tracking.h"
4 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
5 static ssize_t write_check(int fd, const void *buf, size_t count);
6 static int ftruncate_check(int fd, off_t length);
7
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_with_lockcheck1
11 #define ftruncate ftruncate_check
12
13 #include "tdb2-source.h"
14 #include <ccan/tap/tap.h>
15 #include <stdlib.h>
16 #include <stdbool.h>
17 #include <stdarg.h>
18 #include <err.h>
19 #include <setjmp.h>
20 #include "tdb1-external-agent.h"
21 #include "logging.h"
22
23 #undef write
24 #undef pwrite
25 #undef fcntl
26 #undef ftruncate
27
28 static bool in_transaction;
29 static int target, current;
30 static jmp_buf jmpbuf;
31 #define TEST_DBNAME "run-die-during-transaction.tdb1"
32 #define KEY_STRING "helloworld"
33
34 static void maybe_die(int fd)
35 {
36         if (in_transaction && current++ == target) {
37                 longjmp(jmpbuf, 1);
38         }
39 }
40
41 static ssize_t pwrite_check(int fd,
42                             const void *buf, size_t count, off_t offset)
43 {
44         ssize_t ret;
45
46         maybe_die(fd);
47
48         ret = pwrite(fd, buf, count, offset);
49         if (ret != count)
50                 return ret;
51
52         maybe_die(fd);
53         return ret;
54 }
55
56 static ssize_t write_check(int fd, const void *buf, size_t count)
57 {
58         ssize_t ret;
59
60         maybe_die(fd);
61
62         ret = write(fd, buf, count);
63         if (ret != count)
64                 return ret;
65
66         maybe_die(fd);
67         return ret;
68 }
69
70 static int ftruncate_check(int fd, off_t length)
71 {
72         int ret;
73
74         maybe_die(fd);
75
76         ret = ftruncate(fd, length);
77
78         maybe_die(fd);
79         return ret;
80 }
81
82 static bool test_death(enum operation op, struct agent *agent)
83 {
84         struct tdb_context *tdb = NULL;
85         TDB_DATA key;
86         enum agent_return ret;
87         int needed_recovery = 0;
88         union tdb_attribute hsize;
89
90         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
91         hsize.base.next = &tap_log_attr;
92         hsize.tdb1_hashsize.hsize = 1024;
93
94         current = target = 0;
95 reset:
96         unlink(TEST_DBNAME);
97         tdb = tdb_open(TEST_DBNAME, TDB_VERSION1|TDB_NOMMAP,
98                        O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
99
100         if (setjmp(jmpbuf) != 0) {
101                 /* We're partway through.  Simulate our death. */
102                 close(tdb->file->fd);
103                 forget_locking1();
104                 in_transaction = false;
105
106                 ret = external_agent_operation1(agent, NEEDS_RECOVERY, "");
107                 if (ret == SUCCESS)
108                         needed_recovery++;
109                 else if (ret != FAILED) {
110                         diag("Step %u agent NEEDS_RECOVERY = %s", current,
111                              agent_return_name1(ret));
112                         return false;
113                 }
114
115                 ret = external_agent_operation1(agent, op, KEY_STRING);
116                 if (ret != SUCCESS) {
117                         diag("Step %u op %s failed = %s", current,
118                              operation_name1(op),
119                              agent_return_name1(ret));
120                         return false;
121                 }
122
123                 ret = external_agent_operation1(agent, NEEDS_RECOVERY, "");
124                 if (ret != FAILED) {
125                         diag("Still needs recovery after step %u = %s",
126                              current, agent_return_name1(ret));
127                         return false;
128                 }
129
130                 ret = external_agent_operation1(agent, CHECK, "");
131                 if (ret != SUCCESS) {
132                         diag("Step %u check failed = %s", current,
133                              agent_return_name1(ret));
134                         return false;
135                 }
136
137                 ret = external_agent_operation1(agent, CLOSE, "");
138                 if (ret != SUCCESS) {
139                         diag("Step %u close failed = %s", current,
140                              agent_return_name1(ret));
141                         return false;
142                 }
143
144                 /* Suppress logging as this tries to use closed fd. */
145                 suppress_logging = true;
146                 suppress_lockcheck1 = true;
147                 tdb_close(tdb);
148                 suppress_logging = false;
149                 suppress_lockcheck1 = false;
150                 target++;
151                 current = 0;
152                 goto reset;
153         }
154
155         /* Put key for agent to fetch. */
156         key.dsize = strlen(KEY_STRING);
157         key.dptr = (void *)KEY_STRING;
158         if (tdb_store(tdb, key, key, TDB_INSERT) != TDB_SUCCESS)
159                 return false;
160
161         /* This is the key we insert in transaction. */
162         key.dsize--;
163
164         ret = external_agent_operation1(agent, OPEN, TEST_DBNAME);
165         if (ret != SUCCESS)
166                 errx(1, "Agent failed to open: %s", agent_return_name1(ret));
167
168         ret = external_agent_operation1(agent, FETCH, KEY_STRING);
169         if (ret != SUCCESS)
170                 errx(1, "Agent failed find key: %s", agent_return_name1(ret));
171
172         in_transaction = true;
173         if (tdb_transaction_start(tdb) != TDB_SUCCESS)
174                 return false;
175
176         if (tdb_store(tdb, key, key, TDB_INSERT) != TDB_SUCCESS)
177                 return false;
178
179         if (tdb_transaction_commit(tdb) != TDB_SUCCESS)
180                 return false;
181
182         in_transaction = false;
183
184         /* We made it! */
185         diag("Completed %u runs", current);
186         tdb_close(tdb);
187         ret = external_agent_operation1(agent, CLOSE, "");
188         if (ret != SUCCESS) {
189                 diag("Step %u close failed = %s", current,
190                      agent_return_name1(ret));
191                 return false;
192         }
193
194         ok1(needed_recovery);
195         ok1(locking_errors1 == 0);
196         ok1(forget_locking1() == 0);
197         locking_errors1 = 0;
198         return true;
199 }
200
201 int main(int argc, char *argv[])
202 {
203         enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
204         struct agent *agent;
205         int i;
206
207         plan_tests(12);
208         unlock_callback1 = maybe_die;
209
210         agent = prepare_external_agent1();
211         if (!agent)
212                 err(1, "preparing agent");
213
214         for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
215                 diag("Testing %s after death", operation_name1(ops[i]));
216                 ok1(test_death(ops[i], agent));
217         }
218
219         return exit_status();
220 }