summaryrefslogtreecommitdiff
path: root/lib/ntdb/test/run-57-die-during-transaction.c
blob: 32f781e4a89585f63765f65e51adf8e4b0af0fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "private.h"
#include <unistd.h>
#include "lock-tracking.h"
#include "tap-interface.h"
#include <stdlib.h>
#include <assert.h>
static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
static ssize_t write_check(int fd, const void *buf, size_t count);
static int ftruncate_check(int fd, off_t length);

#define pwrite pwrite_check
#define write write_check
#define fcntl fcntl_with_lockcheck
#define ftruncate ftruncate_check

/* There's a malloc inside transaction_setup_recovery, and valgrind complains
 * when we longjmp and leak it. */
#define MAX_ALLOCATIONS 10
static void *allocated[MAX_ALLOCATIONS];
static unsigned max_alloc = 0;

static void *malloc_noleak(size_t len)
{
	unsigned int i;

	for (i = 0; i < MAX_ALLOCATIONS; i++)
		if (!allocated[i]) {
			allocated[i] = malloc(len);
			if (i > max_alloc) {
				max_alloc = i;
				diag("max_alloc: %i", max_alloc);
			}
			return allocated[i];
		}
	diag("Too many allocations!");
	abort();
}

static void *realloc_noleak(void *p, size_t size)
{
	unsigned int i;

	for (i = 0; i < MAX_ALLOCATIONS; i++) {
		if (allocated[i] == p) {
			if (i > max_alloc) {
				max_alloc = i;
				diag("max_alloc: %i", max_alloc);
			}
			return allocated[i] = realloc(p, size);
		}
	}
	diag("Untracked realloc!");
	abort();
}

static void free_noleak(void *p)
{
	unsigned int i;

	/* We don't catch asprintf, so don't complain if we miss one. */
	for (i = 0; i < MAX_ALLOCATIONS; i++) {
		if (allocated[i] == p) {
			allocated[i] = NULL;
			break;
		}
	}
	free(p);
}

static void free_all(void)
{
	unsigned int i;

	for (i = 0; i < MAX_ALLOCATIONS; i++) {
		free(allocated[i]);
		allocated[i] = NULL;
	}
}

#define malloc malloc_noleak
#define free(x) free_noleak(x)
#define realloc realloc_noleak

#include "ntdb-source.h"

#undef malloc
#undef free
#undef realloc
#undef write
#undef pwrite
#undef fcntl
#undef ftruncate

#include <stdbool.h>
#include <stdarg.h>
#include <ccan/err/err.h>
#include <setjmp.h>
#include "external-agent.h"
#include "logging.h"

static bool in_transaction;
static int target, current;
static jmp_buf jmpbuf;
#define TEST_DBNAME "run-57-die-during-transaction.ntdb"
#define KEY_STRING "helloworld"
#define DATA_STRING "Helloworld"

static void maybe_die(int fd)
{
	if (in_transaction && current++ == target) {
		longjmp(jmpbuf, 1);
	}
}

static ssize_t pwrite_check(int fd,
			    const void *buf, size_t count, off_t offset)
{
	ssize_t ret;

	maybe_die(fd);

	ret = pwrite(fd, buf, count, offset);
	if (ret != count)
		return ret;

	maybe_die(fd);
	return ret;
}

static ssize_t write_check(int fd, const void *buf, size_t count)
{
	ssize_t ret;

	maybe_die(fd);

	ret = write(fd, buf, count);
	if (ret != count)
		return ret;

	maybe_die(fd);
	return ret;
}

static int ftruncate_check(int fd, off_t length)
{
	int ret;

	maybe_die(fd);

	ret = ftruncate(fd, length);

	maybe_die(fd);
	return ret;
}

static bool test_death(enum operation op, struct agent *agent,
		       bool pre_create_recovery)
{
	struct ntdb_context *ntdb = NULL;
	NTDB_DATA key, data;
	enum agent_return ret;
	int needed_recovery = 0;

	current = target = 0;
	/* Big long data to force a change. */
	data = ntdb_mkdata(DATA_STRING, strlen(DATA_STRING));

reset:
	unlink(TEST_DBNAME);
	ntdb = ntdb_open(TEST_DBNAME, NTDB_NOMMAP|MAYBE_NOSYNC,
			 O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
	if (!ntdb) {
		diag("Failed opening NTDB: %s", strerror(errno));
		return false;
	}

	if (setjmp(jmpbuf) != 0) {
		/* We're partway through.  Simulate our death. */
		close(ntdb->file->fd);
		forget_locking();
		in_transaction = false;

		ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
		if (ret == SUCCESS)
			needed_recovery++;
		else if (ret != FAILED) {
			diag("Step %u agent NEEDS_RECOVERY = %s", current,
			     agent_return_name(ret));
			return false;
		}

		/* Could be key, or data. */
		ret = external_agent_operation(agent, op,
					       KEY_STRING "=" KEY_STRING);
		if (ret != SUCCESS) {
			ret = external_agent_operation(agent, op,
						       KEY_STRING
						       "=" DATA_STRING);
		}
		if (ret != SUCCESS) {
			diag("Step %u op %s failed = %s", current,
			     operation_name(op),
			     agent_return_name(ret));
			return false;
		}

		ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
		if (ret != FAILED) {
			diag("Still needs recovery after step %u = %s",
			     current, agent_return_name(ret));
			return false;
		}

		ret = external_agent_operation(agent, CHECK, "");
		if (ret != SUCCESS) {
			diag("Step %u check failed = %s", current,
			     agent_return_name(ret));
			return false;
		}

		ret = external_agent_operation(agent, CLOSE, "");
		if (ret != SUCCESS) {
			diag("Step %u close failed = %s", current,
			     agent_return_name(ret));
			return false;
		}

		/* Suppress logging as this tries to use closed fd. */
		suppress_logging = true;
		suppress_lockcheck = true;
		ntdb_close(ntdb);
		suppress_logging = false;
		suppress_lockcheck = false;
		target++;
		current = 0;
		free_all();
		goto reset;
	}

	/* Put key for agent to fetch. */
	key = ntdb_mkdata(KEY_STRING, strlen(KEY_STRING));

	if (pre_create_recovery) {
		/* Using a transaction now means we allocate the recovery
		 * area immediately.  That makes the later transaction smaller
		 * and thus tickles a bug we had. */
		if (ntdb_transaction_start(ntdb) != 0)
			return false;
	}
	if (ntdb_store(ntdb, key, key, NTDB_INSERT) != 0)
		return false;
	if (pre_create_recovery) {
		if (ntdb_transaction_commit(ntdb) != 0)
			return false;
	}

	/* This is the key we insert in transaction. */
	key.dsize--;

	ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
	if (ret != SUCCESS)
		errx(1, "Agent failed to open: %s", agent_return_name(ret));

	ret = external_agent_operation(agent, FETCH, KEY_STRING "=" KEY_STRING);
	if (ret != SUCCESS)
		errx(1, "Agent failed find key: %s", agent_return_name(ret));

	in_transaction = true;
	if (ntdb_transaction_start(ntdb) != 0)
		return false;

	if (ntdb_store(ntdb, key, data, NTDB_INSERT) != 0)
		return false;

	if (ntdb_transaction_commit(ntdb) != 0)
		return false;

	in_transaction = false;

	/* We made it! */
	diag("Completed %u runs", current);
	ntdb_close(ntdb);
	ret = external_agent_operation(agent, CLOSE, "");
	if (ret != SUCCESS) {
		diag("Step %u close failed = %s", current,
		     agent_return_name(ret));
		return false;
	}

	ok1(needed_recovery);
	ok1(locking_errors == 0);
	ok1(forget_locking() == 0);
	locking_errors = 0;
	return true;
}

int main(int argc, char *argv[])
{
	enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
	struct agent *agent;
	int i, j;

	plan_tests(24);
	unlock_callback = maybe_die;

	external_agent_free = free_noleak;
	agent = prepare_external_agent();
	if (!agent)
		err(1, "preparing agent");

	for (j = 0; j < 2; j++) {
		for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
			diag("Testing %s after death (%s recovery area)",
			     operation_name(ops[i]), j ? "with" : "without");
			ok1(test_death(ops[i], agent, j));
		}
	}

	free_external_agent(agent);
	return exit_status();
}