journalfile.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrdengine.h"
  3. static void flush_transaction_buffer_cb(uv_fs_t* req)
  4. {
  5. struct generic_io_descriptor *io_descr = req->data;
  6. struct rrdengine_worker_config* wc = req->loop->data;
  7. struct rrdengine_instance *ctx = wc->ctx;
  8. debug(D_RRDENGINE, "%s: Journal block was written to disk.", __func__);
  9. if (req->result < 0) {
  10. ++ctx->stats.io_errors;
  11. rrd_stat_atomic_add(&global_io_errors, 1);
  12. error("%s: uv_fs_write: %s", __func__, uv_strerror((int)req->result));
  13. } else {
  14. debug(D_RRDENGINE, "%s: Journal block was written to disk.", __func__);
  15. }
  16. uv_fs_req_cleanup(req);
  17. free(io_descr->buf);
  18. freez(io_descr);
  19. }
  20. /* Careful to always call this before creating a new journal file */
  21. void wal_flush_transaction_buffer(struct rrdengine_worker_config* wc)
  22. {
  23. struct rrdengine_instance *ctx = wc->ctx;
  24. int ret;
  25. struct generic_io_descriptor *io_descr;
  26. unsigned pos, size;
  27. struct rrdengine_journalfile *journalfile;
  28. if (unlikely(NULL == ctx->commit_log.buf || 0 == ctx->commit_log.buf_pos)) {
  29. return;
  30. }
  31. /* care with outstanding transactions when switching journal files */
  32. journalfile = ctx->datafiles.last->journalfile;
  33. io_descr = mallocz(sizeof(*io_descr));
  34. pos = ctx->commit_log.buf_pos;
  35. size = ctx->commit_log.buf_size;
  36. if (pos < size) {
  37. /* simulate an empty transaction to skip the rest of the block */
  38. *(uint8_t *) (ctx->commit_log.buf + pos) = STORE_PADDING;
  39. }
  40. io_descr->buf = ctx->commit_log.buf;
  41. io_descr->bytes = size;
  42. io_descr->pos = journalfile->pos;
  43. io_descr->req.data = io_descr;
  44. io_descr->completion = NULL;
  45. io_descr->iov = uv_buf_init((void *)io_descr->buf, size);
  46. ret = uv_fs_write(wc->loop, &io_descr->req, journalfile->file, &io_descr->iov, 1,
  47. journalfile->pos, flush_transaction_buffer_cb);
  48. fatal_assert(-1 != ret);
  49. journalfile->pos += RRDENG_BLOCK_SIZE;
  50. ctx->disk_space += RRDENG_BLOCK_SIZE;
  51. ctx->commit_log.buf = NULL;
  52. ctx->stats.io_write_bytes += RRDENG_BLOCK_SIZE;
  53. ++ctx->stats.io_write_requests;
  54. }
  55. void * wal_get_transaction_buffer(struct rrdengine_worker_config* wc, unsigned size)
  56. {
  57. struct rrdengine_instance *ctx = wc->ctx;
  58. int ret;
  59. unsigned buf_pos = 0, buf_size;
  60. fatal_assert(size);
  61. if (ctx->commit_log.buf) {
  62. unsigned remaining;
  63. buf_pos = ctx->commit_log.buf_pos;
  64. buf_size = ctx->commit_log.buf_size;
  65. remaining = buf_size - buf_pos;
  66. if (size > remaining) {
  67. /* we need a new buffer */
  68. wal_flush_transaction_buffer(wc);
  69. }
  70. }
  71. if (NULL == ctx->commit_log.buf) {
  72. buf_size = ALIGN_BYTES_CEILING(size);
  73. ret = posix_memalign((void *)&ctx->commit_log.buf, RRDFILE_ALIGNMENT, buf_size);
  74. if (unlikely(ret)) {
  75. fatal("posix_memalign:%s", strerror(ret));
  76. }
  77. memset(ctx->commit_log.buf, 0, buf_size);
  78. buf_pos = ctx->commit_log.buf_pos = 0;
  79. ctx->commit_log.buf_size = buf_size;
  80. }
  81. ctx->commit_log.buf_pos += size;
  82. return ctx->commit_log.buf + buf_pos;
  83. }
  84. void generate_journalfilepath(struct rrdengine_datafile *datafile, char *str, size_t maxlen)
  85. {
  86. (void) snprintfz(str, maxlen, "%s/" WALFILE_PREFIX RRDENG_FILE_NUMBER_PRINT_TMPL WALFILE_EXTENSION,
  87. datafile->ctx->dbfiles_path, datafile->tier, datafile->fileno);
  88. }
  89. void journalfile_init(struct rrdengine_journalfile *journalfile, struct rrdengine_datafile *datafile)
  90. {
  91. journalfile->file = (uv_file)0;
  92. journalfile->pos = 0;
  93. journalfile->datafile = datafile;
  94. }
  95. int close_journal_file(struct rrdengine_journalfile *journalfile, struct rrdengine_datafile *datafile)
  96. {
  97. struct rrdengine_instance *ctx = datafile->ctx;
  98. uv_fs_t req;
  99. int ret;
  100. char path[RRDENG_PATH_MAX];
  101. generate_journalfilepath(datafile, path, sizeof(path));
  102. ret = uv_fs_close(NULL, &req, journalfile->file, NULL);
  103. if (ret < 0) {
  104. error("uv_fs_close(%s): %s", path, uv_strerror(ret));
  105. ++ctx->stats.fs_errors;
  106. rrd_stat_atomic_add(&global_fs_errors, 1);
  107. }
  108. uv_fs_req_cleanup(&req);
  109. return ret;
  110. }
  111. int unlink_journal_file(struct rrdengine_journalfile *journalfile)
  112. {
  113. struct rrdengine_datafile *datafile = journalfile->datafile;
  114. struct rrdengine_instance *ctx = datafile->ctx;
  115. uv_fs_t req;
  116. int ret;
  117. char path[RRDENG_PATH_MAX];
  118. generate_journalfilepath(datafile, path, sizeof(path));
  119. ret = uv_fs_unlink(NULL, &req, path, NULL);
  120. if (ret < 0) {
  121. error("uv_fs_fsunlink(%s): %s", path, uv_strerror(ret));
  122. ++ctx->stats.fs_errors;
  123. rrd_stat_atomic_add(&global_fs_errors, 1);
  124. }
  125. uv_fs_req_cleanup(&req);
  126. ++ctx->stats.journalfile_deletions;
  127. return ret;
  128. }
  129. int destroy_journal_file(struct rrdengine_journalfile *journalfile, struct rrdengine_datafile *datafile)
  130. {
  131. struct rrdengine_instance *ctx = datafile->ctx;
  132. uv_fs_t req;
  133. int ret;
  134. char path[RRDENG_PATH_MAX];
  135. generate_journalfilepath(datafile, path, sizeof(path));
  136. ret = uv_fs_ftruncate(NULL, &req, journalfile->file, 0, NULL);
  137. if (ret < 0) {
  138. error("uv_fs_ftruncate(%s): %s", path, uv_strerror(ret));
  139. ++ctx->stats.fs_errors;
  140. rrd_stat_atomic_add(&global_fs_errors, 1);
  141. }
  142. uv_fs_req_cleanup(&req);
  143. ret = uv_fs_close(NULL, &req, journalfile->file, NULL);
  144. if (ret < 0) {
  145. error("uv_fs_close(%s): %s", path, uv_strerror(ret));
  146. ++ctx->stats.fs_errors;
  147. rrd_stat_atomic_add(&global_fs_errors, 1);
  148. }
  149. uv_fs_req_cleanup(&req);
  150. ret = uv_fs_unlink(NULL, &req, path, NULL);
  151. if (ret < 0) {
  152. error("uv_fs_fsunlink(%s): %s", path, uv_strerror(ret));
  153. ++ctx->stats.fs_errors;
  154. rrd_stat_atomic_add(&global_fs_errors, 1);
  155. }
  156. uv_fs_req_cleanup(&req);
  157. ++ctx->stats.journalfile_deletions;
  158. return ret;
  159. }
  160. int create_journal_file(struct rrdengine_journalfile *journalfile, struct rrdengine_datafile *datafile)
  161. {
  162. struct rrdengine_instance *ctx = datafile->ctx;
  163. uv_fs_t req;
  164. uv_file file;
  165. int ret, fd;
  166. struct rrdeng_jf_sb *superblock;
  167. uv_buf_t iov;
  168. char path[RRDENG_PATH_MAX];
  169. generate_journalfilepath(datafile, path, sizeof(path));
  170. fd = open_file_direct_io(path, O_CREAT | O_RDWR | O_TRUNC, &file);
  171. if (fd < 0) {
  172. ++ctx->stats.fs_errors;
  173. rrd_stat_atomic_add(&global_fs_errors, 1);
  174. return fd;
  175. }
  176. journalfile->file = file;
  177. ++ctx->stats.journalfile_creations;
  178. ret = posix_memalign((void *)&superblock, RRDFILE_ALIGNMENT, sizeof(*superblock));
  179. if (unlikely(ret)) {
  180. fatal("posix_memalign:%s", strerror(ret));
  181. }
  182. memset(superblock, 0, sizeof(*superblock));
  183. (void) strncpy(superblock->magic_number, RRDENG_JF_MAGIC, RRDENG_MAGIC_SZ);
  184. (void) strncpy(superblock->version, RRDENG_JF_VER, RRDENG_VER_SZ);
  185. iov = uv_buf_init((void *)superblock, sizeof(*superblock));
  186. ret = uv_fs_write(NULL, &req, file, &iov, 1, 0, NULL);
  187. if (ret < 0) {
  188. fatal_assert(req.result < 0);
  189. error("uv_fs_write: %s", uv_strerror(ret));
  190. ++ctx->stats.io_errors;
  191. rrd_stat_atomic_add(&global_io_errors, 1);
  192. }
  193. uv_fs_req_cleanup(&req);
  194. free(superblock);
  195. if (ret < 0) {
  196. destroy_journal_file(journalfile, datafile);
  197. return ret;
  198. }
  199. journalfile->pos = sizeof(*superblock);
  200. ctx->stats.io_write_bytes += sizeof(*superblock);
  201. ++ctx->stats.io_write_requests;
  202. return 0;
  203. }
  204. static int check_journal_file_superblock(uv_file file)
  205. {
  206. int ret;
  207. struct rrdeng_jf_sb *superblock;
  208. uv_buf_t iov;
  209. uv_fs_t req;
  210. ret = posix_memalign((void *)&superblock, RRDFILE_ALIGNMENT, sizeof(*superblock));
  211. if (unlikely(ret)) {
  212. fatal("posix_memalign:%s", strerror(ret));
  213. }
  214. iov = uv_buf_init((void *)superblock, sizeof(*superblock));
  215. ret = uv_fs_read(NULL, &req, file, &iov, 1, 0, NULL);
  216. if (ret < 0) {
  217. error("uv_fs_read: %s", uv_strerror(ret));
  218. uv_fs_req_cleanup(&req);
  219. goto error;
  220. }
  221. fatal_assert(req.result >= 0);
  222. uv_fs_req_cleanup(&req);
  223. if (strncmp(superblock->magic_number, RRDENG_JF_MAGIC, RRDENG_MAGIC_SZ) ||
  224. strncmp(superblock->version, RRDENG_JF_VER, RRDENG_VER_SZ)) {
  225. error("File has invalid superblock.");
  226. ret = UV_EINVAL;
  227. } else {
  228. ret = 0;
  229. }
  230. error:
  231. free(superblock);
  232. return ret;
  233. }
  234. static void restore_extent_metadata(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile,
  235. void *buf, unsigned max_size)
  236. {
  237. static BITMAP256 page_error_map;
  238. struct page_cache *pg_cache = &ctx->pg_cache;
  239. unsigned i, count, payload_length, descr_size, valid_pages;
  240. struct rrdeng_page_descr *descr;
  241. struct extent_info *extent;
  242. /* persistent structures */
  243. struct rrdeng_jf_store_data *jf_metric_data;
  244. jf_metric_data = buf;
  245. count = jf_metric_data->number_of_pages;
  246. descr_size = sizeof(*jf_metric_data->descr) * count;
  247. payload_length = sizeof(*jf_metric_data) + descr_size;
  248. if (payload_length > max_size) {
  249. error("Corrupted transaction payload.");
  250. return;
  251. }
  252. extent = mallocz(sizeof(*extent) + count * sizeof(extent->pages[0]));
  253. extent->offset = jf_metric_data->extent_offset;
  254. extent->size = jf_metric_data->extent_size;
  255. extent->datafile = journalfile->datafile;
  256. extent->next = NULL;
  257. for (i = 0, valid_pages = 0 ; i < count ; ++i) {
  258. uuid_t *temp_id;
  259. Pvoid_t *PValue;
  260. struct pg_cache_page_index *page_index = NULL;
  261. uint8_t page_type = jf_metric_data->descr[i].type;
  262. if (page_type > PAGE_TYPE_MAX) {
  263. if (!bitmap256_get_bit(&page_error_map, page_type)) {
  264. error("Unknown page type %d encountered.", page_type);
  265. bitmap256_set_bit(&page_error_map, page_type, 1);
  266. }
  267. continue;
  268. }
  269. uint64_t start_time = jf_metric_data->descr[i].start_time;
  270. uint64_t end_time = jf_metric_data->descr[i].end_time;
  271. if (unlikely(start_time > end_time)) {
  272. error("Invalid page encountered, start time %lu > end time %lu", start_time , end_time );
  273. continue;
  274. }
  275. if (unlikely(start_time == end_time)) {
  276. size_t entries = jf_metric_data->descr[i].page_length / page_type_size[page_type];
  277. if (unlikely(entries > 1)) {
  278. error("Invalid page encountered, start time %lu = end time but %zu entries were found", start_time, entries);
  279. continue;
  280. }
  281. }
  282. temp_id = (uuid_t *)jf_metric_data->descr[i].uuid;
  283. uv_rwlock_rdlock(&pg_cache->metrics_index.lock);
  284. PValue = JudyHSGet(pg_cache->metrics_index.JudyHS_array, temp_id, sizeof(uuid_t));
  285. if (likely(NULL != PValue)) {
  286. page_index = *PValue;
  287. }
  288. uv_rwlock_rdunlock(&pg_cache->metrics_index.lock);
  289. if (NULL == PValue) {
  290. /* First time we see the UUID */
  291. uv_rwlock_wrlock(&pg_cache->metrics_index.lock);
  292. PValue = JudyHSIns(&pg_cache->metrics_index.JudyHS_array, temp_id, sizeof(uuid_t), PJE0);
  293. fatal_assert(NULL == *PValue); /* TODO: figure out concurrency model */
  294. *PValue = page_index = create_page_index(temp_id);
  295. page_index->prev = pg_cache->metrics_index.last_page_index;
  296. pg_cache->metrics_index.last_page_index = page_index;
  297. uv_rwlock_wrunlock(&pg_cache->metrics_index.lock);
  298. }
  299. descr = pg_cache_create_descr();
  300. descr->page_length = jf_metric_data->descr[i].page_length;
  301. descr->start_time = start_time;
  302. descr->end_time = end_time;
  303. descr->id = &page_index->id;
  304. descr->extent = extent;
  305. descr->type = page_type;
  306. extent->pages[valid_pages++] = descr;
  307. pg_cache_insert(ctx, page_index, descr);
  308. }
  309. extent->number_of_pages = valid_pages;
  310. if (likely(valid_pages))
  311. df_extent_insert(extent);
  312. else
  313. freez(extent);
  314. }
  315. /*
  316. * Replays transaction by interpreting up to max_size bytes from buf.
  317. * Sets id to the current transaction id or to 0 if unknown.
  318. * Returns size of transaction record or 0 for unknown size.
  319. */
  320. static unsigned replay_transaction(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile,
  321. void *buf, uint64_t *id, unsigned max_size)
  322. {
  323. unsigned payload_length, size_bytes;
  324. int ret;
  325. /* persistent structures */
  326. struct rrdeng_jf_transaction_header *jf_header;
  327. struct rrdeng_jf_transaction_trailer *jf_trailer;
  328. uLong crc;
  329. *id = 0;
  330. jf_header = buf;
  331. if (STORE_PADDING == jf_header->type) {
  332. debug(D_RRDENGINE, "Skipping padding.");
  333. return 0;
  334. }
  335. if (sizeof(*jf_header) > max_size) {
  336. error("Corrupted transaction record, skipping.");
  337. return 0;
  338. }
  339. *id = jf_header->id;
  340. payload_length = jf_header->payload_length;
  341. size_bytes = sizeof(*jf_header) + payload_length + sizeof(*jf_trailer);
  342. if (size_bytes > max_size) {
  343. error("Corrupted transaction record, skipping.");
  344. return 0;
  345. }
  346. jf_trailer = buf + sizeof(*jf_header) + payload_length;
  347. crc = crc32(0L, Z_NULL, 0);
  348. crc = crc32(crc, buf, sizeof(*jf_header) + payload_length);
  349. ret = crc32cmp(jf_trailer->checksum, crc);
  350. debug(D_RRDENGINE, "Transaction %"PRIu64" was read from disk. CRC32 check: %s", *id, ret ? "FAILED" : "SUCCEEDED");
  351. if (unlikely(ret)) {
  352. error("Transaction %"PRIu64" was read from disk. CRC32 check: FAILED", *id);
  353. return size_bytes;
  354. }
  355. switch (jf_header->type) {
  356. case STORE_DATA:
  357. debug(D_RRDENGINE, "Replaying transaction %"PRIu64"", jf_header->id);
  358. restore_extent_metadata(ctx, journalfile, buf + sizeof(*jf_header), payload_length);
  359. break;
  360. default:
  361. error("Unknown transaction type. Skipping record.");
  362. break;
  363. }
  364. return size_bytes;
  365. }
  366. #define READAHEAD_BYTES (RRDENG_BLOCK_SIZE * 256)
  367. /*
  368. * Iterates journal file transactions and populates the page cache.
  369. * Page cache must already be initialized.
  370. * Returns the maximum transaction id it discovered.
  371. */
  372. static uint64_t iterate_transactions(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile)
  373. {
  374. uv_file file;
  375. uint64_t file_size;//, data_file_size;
  376. int ret;
  377. uint64_t pos, pos_i, max_id, id;
  378. unsigned size_bytes;
  379. void *buf;
  380. uv_buf_t iov;
  381. uv_fs_t req;
  382. file = journalfile->file;
  383. file_size = journalfile->pos;
  384. //data_file_size = journalfile->datafile->pos; TODO: utilize this?
  385. max_id = 1;
  386. ret = posix_memalign((void *)&buf, RRDFILE_ALIGNMENT, READAHEAD_BYTES);
  387. if (unlikely(ret)) {
  388. fatal("posix_memalign:%s", strerror(ret));
  389. }
  390. for (pos = sizeof(struct rrdeng_jf_sb) ; pos < file_size ; pos += READAHEAD_BYTES) {
  391. size_bytes = MIN(READAHEAD_BYTES, file_size - pos);
  392. iov = uv_buf_init(buf, size_bytes);
  393. ret = uv_fs_read(NULL, &req, file, &iov, 1, pos, NULL);
  394. if (ret < 0) {
  395. error("uv_fs_read: pos=%"PRIu64", %s", pos, uv_strerror(ret));
  396. uv_fs_req_cleanup(&req);
  397. goto skip_file;
  398. }
  399. fatal_assert(req.result >= 0);
  400. uv_fs_req_cleanup(&req);
  401. ctx->stats.io_read_bytes += size_bytes;
  402. ++ctx->stats.io_read_requests;
  403. //pos_i = pos;
  404. //while (pos_i < pos + size_bytes) {
  405. for (pos_i = 0 ; pos_i < size_bytes ; ) {
  406. unsigned max_size;
  407. max_size = pos + size_bytes - pos_i;
  408. ret = replay_transaction(ctx, journalfile, buf + pos_i, &id, max_size);
  409. if (!ret) /* TODO: support transactions bigger than 4K */
  410. /* unknown transaction size, move on to the next block */
  411. pos_i = ALIGN_BYTES_FLOOR(pos_i + RRDENG_BLOCK_SIZE);
  412. else
  413. pos_i += ret;
  414. max_id = MAX(max_id, id);
  415. }
  416. }
  417. skip_file:
  418. free(buf);
  419. return max_id;
  420. }
  421. int load_journal_file(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile,
  422. struct rrdengine_datafile *datafile)
  423. {
  424. uv_fs_t req;
  425. uv_file file;
  426. int ret, fd, error;
  427. uint64_t file_size, max_id;
  428. char path[RRDENG_PATH_MAX];
  429. generate_journalfilepath(datafile, path, sizeof(path));
  430. fd = open_file_direct_io(path, O_RDWR, &file);
  431. if (fd < 0) {
  432. ++ctx->stats.fs_errors;
  433. rrd_stat_atomic_add(&global_fs_errors, 1);
  434. return fd;
  435. }
  436. info("Loading journal file \"%s\".", path);
  437. ret = check_file_properties(file, &file_size, sizeof(struct rrdeng_df_sb));
  438. if (ret)
  439. goto error;
  440. file_size = ALIGN_BYTES_FLOOR(file_size);
  441. ret = check_journal_file_superblock(file);
  442. if (ret)
  443. goto error;
  444. ctx->stats.io_read_bytes += sizeof(struct rrdeng_jf_sb);
  445. ++ctx->stats.io_read_requests;
  446. journalfile->file = file;
  447. journalfile->pos = file_size;
  448. max_id = iterate_transactions(ctx, journalfile);
  449. ctx->commit_log.transaction_id = MAX(ctx->commit_log.transaction_id, max_id + 1);
  450. info("Journal file \"%s\" loaded (size:%"PRIu64").", path, file_size);
  451. return 0;
  452. error:
  453. error = ret;
  454. ret = uv_fs_close(NULL, &req, file, NULL);
  455. if (ret < 0) {
  456. error("uv_fs_close(%s): %s", path, uv_strerror(ret));
  457. ++ctx->stats.fs_errors;
  458. rrd_stat_atomic_add(&global_fs_errors, 1);
  459. }
  460. uv_fs_req_cleanup(&req);
  461. return error;
  462. }
  463. void init_commit_log(struct rrdengine_instance *ctx)
  464. {
  465. ctx->commit_log.buf = NULL;
  466. ctx->commit_log.buf_pos = 0;
  467. ctx->commit_log.transaction_id = 1;
  468. }