journal.c 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
22
#include <sys/stat.h>
23
#include <tap/basic.h>
24

25
#include "libknot/libknot.h"
26
#include "knot/server/journal.h"
27
#include "knot/zone/zone.h"
28 29 30 31

#define RAND_RR_LABEL 16
#define RAND_RR_PAYLOAD 64
#define MIN_SOA_SIZE 22
32 33 34 35 36 37 38 39 40 41 42 43

/*! \brief Generate random string with given length. */
static int randstr(char* dst, size_t len)
{
	for (int i = 0; i < len - 1; ++i) {
		dst[i] = '0' + (int) (('Z'-'0') * (rand() / (RAND_MAX + 1.0)));
	}
	dst[len - 1] = '\0';

	return 0;
}

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
/*! \brief Init RRSet with type SOA and given serial. */
static void init_soa(knot_rrset_t *rr, const uint32_t serial, const knot_dname_t *apex)
{
	knot_rrset_init(rr, knot_dname_copy(apex, NULL), KNOT_RRTYPE_SOA, KNOT_CLASS_IN);

	assert(serial < 256);
	uint8_t soa_data[MIN_SOA_SIZE] = { 0, 0, 0, 0, 0, serial };
	int ret = knot_rrset_add_rdata(rr, soa_data, sizeof(soa_data), 3600, NULL);
	assert(ret == KNOT_EOK);
}

/*! \brief Init RRSet with type TXT, random owner and random payload. */
static void init_random_rr(knot_rrset_t *rr , const knot_dname_t *apex)
{
	/* Create random label. */
	char owner[RAND_RR_LABEL + knot_dname_size(apex)];
	owner[0] = RAND_RR_LABEL - 1;
	randstr(owner + 1, RAND_RR_LABEL);

	/* Append zone apex. */
	memcpy(owner + RAND_RR_LABEL, apex, knot_dname_size(apex));
	knot_rrset_init(rr, knot_dname_copy((knot_dname_t *)owner, NULL), KNOT_RRTYPE_TXT, KNOT_CLASS_IN);

	/* Create random RDATA. */
	uint8_t txt[RAND_RR_PAYLOAD + 1];
	txt[0] = RAND_RR_PAYLOAD - 1;
	randstr((char *)(txt + 1), RAND_RR_PAYLOAD);

	int ret = knot_rrset_add_rdata(rr, txt, RAND_RR_PAYLOAD, 3600, NULL);
	assert(ret == KNOT_EOK);
}

/*! \brief Init changeset with random changes. */
static void init_random_changeset(changeset_t *ch, const uint32_t from, const uint32_t to, const size_t size, const knot_dname_t *apex)
{
	int ret = changeset_init(ch, apex);
	assert(ret == KNOT_EOK);
81

82 83 84
	// Add SOAs
	knot_rrset_t soa;
	init_soa(&soa, from, apex);
85

86 87 88
	ch->soa_from = knot_rrset_copy(&soa, NULL);
	assert(ch->soa_from);
	knot_rrset_clear(&soa, NULL);
89

90 91 92 93
	init_soa(&soa, to, apex);
	ch->soa_to = knot_rrset_copy(&soa, NULL);
	assert(ch->soa_to);
	knot_rrset_clear(&soa, NULL);
94

95 96 97 98
	// Add RRs to add section
	for (size_t i = 0; i < size / 2; ++i) {
		knot_rrset_t rr;
		init_random_rr(&rr, apex);
99
		int ret = changeset_add_rrset(ch, &rr, false);
100 101 102 103 104 105 106 107
		assert(ret == KNOT_EOK);
		knot_rrset_clear(&rr, NULL);
	}

	// Add RRs to remove section
	for (size_t i = 0; i < size / 2; ++i) {
		knot_rrset_t rr;
		init_random_rr(&rr, apex);
108
		int ret = changeset_rem_rrset(ch, &rr, false);
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
		assert(ret == KNOT_EOK);
		knot_rrset_clear(&rr, NULL);
	}
}

/*! \brief Compare two changesets for equality. */
static bool changesets_eq(const changeset_t *ch1, changeset_t *ch2)
{
	if (changeset_size(ch1) != changeset_size(ch2)) {
		return false;
	}

	changeset_iter_t it1;
	changeset_iter_all(&it1, ch1, true);
	changeset_iter_t it2;
	changeset_iter_all(&it2, ch2, true);

	knot_rrset_t rr1 = changeset_iter_next(&it1);
	knot_rrset_t rr2 = changeset_iter_next(&it2);
	bool ret = true;
	while (!knot_rrset_empty(&rr1)) {
		if (!knot_rrset_equal(&rr1, &rr2, KNOT_RRSET_COMPARE_WHOLE)) {
			ret = false;
			break;
		}
		rr1 = changeset_iter_next(&it1);
		rr2 = changeset_iter_next(&it2);
	}

	changeset_iter_clear(&it1);
	changeset_iter_clear(&it2);

	return ret;
}

144
/*! \brief Journal fillup test with size check. */
145
static void test_fillup(journal_t *journal, size_t fsize, unsigned iter, size_t chunk_size)
146 147 148
{
	int ret = KNOT_EOK;
	char *mptr = NULL;
149
	char *large_entry = malloc(chunk_size);
150
	randstr(large_entry, chunk_size);
151
	assert(large_entry);
152

153
	unsigned i = 0;
154 155 156
	bool read_passed = true;
	for (; i < 2 * JOURNAL_NCOUNT; ++i) {
		uint64_t chk_key = 0xBEBE + i;
157
		size_t entry_len = chunk_size/2 + rand() % (chunk_size/2);
158 159

		/* Write */
160
		ret = journal_map(journal, chk_key, &mptr, entry_len, false);
161 162 163
		if (ret != KNOT_EOK) {
			break;
		}
164
		memcpy(mptr, large_entry, entry_len);
165 166 167 168 169 170 171 172
		ret = journal_unmap(journal, chk_key, mptr, 1);
		if (ret != KNOT_EOK) {
			diag("journal_unmap = %s", knot_strerror(ret));
			read_passed = true;
			break;
		}

		/* Read */
173
		ret = journal_map(journal, chk_key, &mptr, entry_len, true);
174
		if (ret == KNOT_EOK) {
175
			ret = memcmp(large_entry, mptr, entry_len);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
			if (ret != 0) {
				diag("integrity check failed");
				read_passed = false;
			} else {
				ret = journal_unmap(journal, chk_key, mptr, 0);
				if (ret != KNOT_EOK) {
					diag("journal_unmap(rdonly) = %s", knot_strerror(ret));
					read_passed = false;
				}
			}
		} else {
			diag("journal_map(rdonly) = %s", knot_strerror(ret));
			read_passed = false;
		}

		if (!read_passed) {
			break;
		}
194
	}
195 196
	ok(read_passed, "journal: fillup #%u, reading written entries", iter);
	ok(ret != KNOT_EOK, "journal: fillup #%u (%d entries)", iter, i);
197 198 199 200 201
	free(large_entry);

	/* Check file size. */
	struct stat st;
	fstat(journal->fd, &st);
202 203
	ok(st.st_size < fsize + chunk_size, "journal: fillup / size check #%u", iter);
	if (st.st_size > fsize + chunk_size) {
204
		diag("journal: fillup / size check #%u fsize(%zu) > max(%zu)",
205
		     iter, (size_t)st.st_size, fsize + chunk_size);
206
	}
207 208
}

209 210 211 212 213 214 215
/*! \brief Test behavior with real changesets. */
static void test_store_load(const char *jfilename)
{
	const size_t filesize = 100 * 1024;
	uint8_t *apex = (uint8_t *)"\4test";

	/* Create fake zone. */
216
	zone_t z = { .name = apex };
217 218 219 220 221 222 223 224

	/* Save and load changeset. */
	changeset_t ch;
	init_random_changeset(&ch, 0, 1, 128, apex);
	int ret = journal_store_changeset(&ch, jfilename, filesize);
	ok(ret == KNOT_EOK, "journal: store changeset");
	list_t l;
	init_list(&l);
225
	ret = journal_load_changesets(jfilename, &z, &l, 0, 1);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	ok(ret == KNOT_EOK && changesets_eq(TAIL(l), &ch), "journal: load changeset");
	changeset_clear(&ch);
	changesets_free(&l);
	init_list(&l);

	/* Fill the journal. */
	ret = KNOT_EOK;
	uint32_t serial = 1;
	for (; ret == KNOT_EOK; ++serial) {
		init_random_changeset(&ch, serial, serial + 1, 128, apex);
		ret = journal_store_changeset(&ch, jfilename, filesize);
		changeset_clear(&ch);
	}
	ok(ret == KNOT_EBUSY, "journal: overfill with changesets");

	/* Load all changesets stored until now. */
	serial--;
243
	ret = journal_load_changesets(jfilename, &z, &l, 0, serial);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	changesets_free(&l);
	ok(ret == KNOT_EOK, "journal: load changesets");

	/* Flush the journal. */
	ret = journal_mark_synced(jfilename);
	ok(ret == KNOT_EOK, "journal: flush");

	/* Store next changeset. */
	init_random_changeset(&ch, serial, serial + 1, 128, apex);
	ret = journal_store_changeset(&ch, jfilename, filesize);
		changeset_clear(&ch);
	ok(ret == KNOT_EOK, "journal: store after flush");

	/* Load all changesets, except the first one that got evicted. */
	init_list(&l);
259
	ret = journal_load_changesets(jfilename, &z, &l, 1, serial + 1);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	changesets_free(&l);
	ok(ret == KNOT_EOK, "journal: load changesets after flush");
}

/*! \brief Test behavior when writing to jurnal and flushing it. */
static void test_stress(const char *jfilename)
{
	uint8_t *apex = (uint8_t *)"\4test";
	const size_t filesize = 100 * 1024;
	int ret = KNOT_EOK;
	uint32_t serial = 0;
	size_t update_size = 3;
	for (; ret == KNOT_EOK && serial < 32; ++serial) {
		changeset_t ch;
		init_random_changeset(&ch, serial, serial + 1, update_size, apex);
		update_size *= 1.5;
		ret = journal_store_changeset(&ch, jfilename, filesize);
		changeset_clear(&ch);
		journal_mark_synced(jfilename);
	}
	ok(ret == KNOT_ESPACE, "journal: does not overfill under load");
}

283 284
int main(int argc, char *argv[])
{
285
	plan_lazy();
286 287

	/* Create tmpdir */
288
	size_t fsize = 10 * 1024 * 1024;
289
	char *tmpdir = test_tmpdir();
290 291
	char jfilename[256];
	snprintf(jfilename, sizeof(jfilename), "%s/%s", tmpdir, "journal.XXXXXX");
292

293
	/* Create tmpfile. */
294
	int tmp_fd = mkstemp(jfilename);
295 296
	ok(tmp_fd >= 0, "journal: create temporary file");
	if (tmp_fd < 0) {
297
		goto skip_all;
298 299
	}
	close(tmp_fd);
300
	remove(jfilename);
301

302 303 304 305
	/* Try to open journal with too small fsize. */
	journal_t *journal = journal_open(jfilename, 1024);
	ok(journal == NULL, "journal: open too small");

306
	/* Open/create new journal. */
307
	journal = journal_open(jfilename, fsize);
308
	ok(journal != NULL, "journal: open journal '%s'", jfilename);
309 310
	if (journal == NULL) {
		goto skip_all;
311
	}
312

313 314 315
	/* Write entry and check integrity. */
	char *mptr = NULL;
	uint64_t chk_key = 0;
316
	char chk_buf[64] = {'\0'};
317 318
	randstr(chk_buf, sizeof(chk_buf));
	int ret = journal_map(journal, chk_key, &mptr, sizeof(chk_buf), false);
319
	is_int(KNOT_EOK, ret, "journal: write data (map)");
320 321
	if (ret == KNOT_EOK) {
		memcpy(mptr, chk_buf, sizeof(chk_buf));
322 323
		ret = journal_unmap(journal, chk_key, mptr, 1);
		is_int(KNOT_EOK, ret, "journal: write data (unmap)");
324
	}
325

326
	ret = journal_map(journal, chk_key, &mptr, sizeof(chk_buf), true);
327
	is_int(KNOT_EOK, ret, "journal: data integrity check (map)");
328 329
	if (ret == KNOT_EOK) {
		ret = memcmp(chk_buf, mptr, sizeof(chk_buf));
330 331 332
		is_int(0, ret, "journal: data integrity check (cmp)");
		ret = journal_unmap(journal, chk_key, mptr, 0);
		is_int(KNOT_EOK, ret, "journal: data integrity check (unmap)");
333
	}
334

335
	/* Reopen log and re-read value. */
336
	journal_close(journal);
337
	journal = journal_open(jfilename, fsize);
338
	ok(journal != NULL, "journal: open journal '%s'", jfilename);
339

340 341 342
	ret = journal_map(journal, chk_key, &mptr, sizeof(chk_buf), true);
	if (ret == KNOT_EOK) {
		ret = memcmp(chk_buf, mptr, sizeof(chk_buf));
343
		journal_unmap(journal, chk_key, mptr, 0);
344 345
	}
	is_int(KNOT_EOK, ret, "journal: data integrity check after close/open");
346

347
	/*  Write random data. */
348
	ret = KNOT_EOK;
349
	for (int i = 0; i < 512; ++i) {
350
		chk_key = 0xDEAD0000 + i;
351
		ret = journal_map(journal, chk_key, &mptr, sizeof(chk_buf), false);
352 353 354 355
		if (ret != KNOT_EOK) {
			diag("journal_map failed: %s", knot_strerror(ret));
			break;
		}
356 357
		randstr(mptr, sizeof(chk_buf));
		if ((ret = journal_unmap(journal, chk_key, mptr, 1)) != KNOT_EOK) {
358 359 360 361
			diag("journal_unmap failed: %s", knot_strerror(ret));
			break;
		}
	}
362
	is_int(KNOT_EOK, ret, "journal: sustained mmap r/w");
363

364
	/* Overfill (yields ESPACE/EBUSY) */
365
	ret = journal_map(journal, chk_key, &mptr, fsize, false);
366
	ok(ret != KNOT_EOK, "journal: overfill");
367 368

	/* Fillup */
369 370 371
	size_t sizes[] = {16, 64, 1024, 4096, 512 * 1024, 1024 * 1024 };
	const int num_sizes = sizeof(sizes)/sizeof(size_t);
	for (unsigned i = 0; i < 2 * num_sizes; ++i) {
372 373 374 375 376 377 378
		/* Journal flush. */
		journal_close(journal);
		ret = journal_mark_synced(jfilename);
		is_int(KNOT_EOK, ret, "journal: flush after fillup #%u", i);
		journal = journal_open(jfilename, fsize);
		ok(journal != NULL, "journal: reopen after flush #%u", i);
		/* Journal fillup. */
379
		test_fillup(journal, fsize, i, sizes[i % num_sizes]);
380
	}
381

382
	/* Close journal. */
383
	journal_close(journal);
384 385 386

	/* Delete journal. */
	remove(jfilename);
387 388 389 390 391 392 393

	test_store_load(jfilename);
	remove(jfilename);

	test_stress(jfilename);
	remove(jfilename);

Marek Vavrusa's avatar
Marek Vavrusa committed
394
	free(tmpdir);
395

396
skip_all:
397 398
	return 0;
}