journalfile.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. posix_memfree(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. posix_memfree(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. posix_memfree(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_ut = jf_metric_data->descr[i].start_time_ut;
  270. uint64_t end_time_ut = jf_metric_data->descr[i].end_time_ut;
  271. size_t entries = jf_metric_data->descr[i].page_length / page_type_size[page_type];
  272. time_t update_every_s = (entries > 1) ? ((end_time_ut - start_time_ut) / USEC_PER_SEC / (entries - 1)) : 0;
  273. if (unlikely(start_time_ut > end_time_ut)) {
  274. ctx->load_errors[LOAD_ERRORS_PAGE_FLIPPED_TIME].counter++;
  275. if(ctx->load_errors[LOAD_ERRORS_PAGE_FLIPPED_TIME].latest_end_time_ut < end_time_ut)
  276. ctx->load_errors[LOAD_ERRORS_PAGE_FLIPPED_TIME].latest_end_time_ut = end_time_ut;
  277. continue;
  278. }
  279. if (unlikely(start_time_ut == end_time_ut && entries != 1)) {
  280. ctx->load_errors[LOAD_ERRORS_PAGE_EQUAL_TIME].counter++;
  281. if(ctx->load_errors[LOAD_ERRORS_PAGE_EQUAL_TIME].latest_end_time_ut < end_time_ut)
  282. ctx->load_errors[LOAD_ERRORS_PAGE_EQUAL_TIME].latest_end_time_ut = end_time_ut;
  283. continue;
  284. }
  285. if (unlikely(!entries)) {
  286. ctx->load_errors[LOAD_ERRORS_PAGE_ZERO_ENTRIES].counter++;
  287. if(ctx->load_errors[LOAD_ERRORS_PAGE_ZERO_ENTRIES].latest_end_time_ut < end_time_ut)
  288. ctx->load_errors[LOAD_ERRORS_PAGE_ZERO_ENTRIES].latest_end_time_ut = end_time_ut;
  289. continue;
  290. }
  291. if(entries > 1 && update_every_s == 0) {
  292. ctx->load_errors[LOAD_ERRORS_PAGE_UPDATE_ZERO].counter++;
  293. if(ctx->load_errors[LOAD_ERRORS_PAGE_UPDATE_ZERO].latest_end_time_ut < end_time_ut)
  294. ctx->load_errors[LOAD_ERRORS_PAGE_UPDATE_ZERO].latest_end_time_ut = end_time_ut;
  295. continue;
  296. }
  297. if(start_time_ut + update_every_s * USEC_PER_SEC * (entries - 1) != end_time_ut) {
  298. ctx->load_errors[LOAD_ERRORS_PAGE_FLEXY_TIME].counter++;
  299. if(ctx->load_errors[LOAD_ERRORS_PAGE_FLEXY_TIME].latest_end_time_ut < end_time_ut)
  300. ctx->load_errors[LOAD_ERRORS_PAGE_FLEXY_TIME].latest_end_time_ut = end_time_ut;
  301. // let this be
  302. // end_time_ut = start_time_ut + update_every_s * USEC_PER_SEC * (entries - 1);
  303. }
  304. temp_id = (uuid_t *)jf_metric_data->descr[i].uuid;
  305. uv_rwlock_rdlock(&pg_cache->metrics_index.lock);
  306. PValue = JudyHSGet(pg_cache->metrics_index.JudyHS_array, temp_id, sizeof(uuid_t));
  307. if (likely(NULL != PValue)) {
  308. page_index = *PValue;
  309. }
  310. uv_rwlock_rdunlock(&pg_cache->metrics_index.lock);
  311. if (NULL == PValue) {
  312. /* First time we see the UUID */
  313. uv_rwlock_wrlock(&pg_cache->metrics_index.lock);
  314. PValue = JudyHSIns(&pg_cache->metrics_index.JudyHS_array, temp_id, sizeof(uuid_t), PJE0);
  315. fatal_assert(NULL == *PValue); /* TODO: figure out concurrency model */
  316. *PValue = page_index = create_page_index(temp_id, ctx);
  317. page_index->prev = pg_cache->metrics_index.last_page_index;
  318. pg_cache->metrics_index.last_page_index = page_index;
  319. uv_rwlock_wrunlock(&pg_cache->metrics_index.lock);
  320. }
  321. descr = pg_cache_create_descr();
  322. descr->page_length = jf_metric_data->descr[i].page_length;
  323. descr->start_time_ut = start_time_ut;
  324. descr->end_time_ut = end_time_ut;
  325. descr->update_every_s = (update_every_s > 0) ? (uint32_t)update_every_s : (page_index->latest_update_every_s);
  326. descr->id = &page_index->id;
  327. descr->extent = extent;
  328. descr->type = page_type;
  329. extent->pages[valid_pages++] = descr;
  330. pg_cache_insert(ctx, page_index, descr);
  331. if(page_index->latest_time_ut == descr->end_time_ut)
  332. page_index->latest_update_every_s = descr->update_every_s;
  333. if(descr->update_every_s == 0)
  334. fatal(
  335. "DBENGINE: page descriptor update every is zero, end_time_ut = %llu, start_time_ut = %llu, entries = %zu",
  336. (unsigned long long)end_time_ut, (unsigned long long)start_time_ut, entries);
  337. }
  338. extent->number_of_pages = valid_pages;
  339. if (likely(valid_pages))
  340. df_extent_insert(extent);
  341. else {
  342. freez(extent);
  343. ctx->load_errors[LOAD_ERRORS_DROPPED_EXTENT].counter++;
  344. }
  345. }
  346. /*
  347. * Replays transaction by interpreting up to max_size bytes from buf.
  348. * Sets id to the current transaction id or to 0 if unknown.
  349. * Returns size of transaction record or 0 for unknown size.
  350. */
  351. static unsigned replay_transaction(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile,
  352. void *buf, uint64_t *id, unsigned max_size)
  353. {
  354. unsigned payload_length, size_bytes;
  355. int ret;
  356. /* persistent structures */
  357. struct rrdeng_jf_transaction_header *jf_header;
  358. struct rrdeng_jf_transaction_trailer *jf_trailer;
  359. uLong crc;
  360. *id = 0;
  361. jf_header = buf;
  362. if (STORE_PADDING == jf_header->type) {
  363. debug(D_RRDENGINE, "Skipping padding.");
  364. return 0;
  365. }
  366. if (sizeof(*jf_header) > max_size) {
  367. error("Corrupted transaction record, skipping.");
  368. return 0;
  369. }
  370. *id = jf_header->id;
  371. payload_length = jf_header->payload_length;
  372. size_bytes = sizeof(*jf_header) + payload_length + sizeof(*jf_trailer);
  373. if (size_bytes > max_size) {
  374. error("Corrupted transaction record, skipping.");
  375. return 0;
  376. }
  377. jf_trailer = buf + sizeof(*jf_header) + payload_length;
  378. crc = crc32(0L, Z_NULL, 0);
  379. crc = crc32(crc, buf, sizeof(*jf_header) + payload_length);
  380. ret = crc32cmp(jf_trailer->checksum, crc);
  381. debug(D_RRDENGINE, "Transaction %"PRIu64" was read from disk. CRC32 check: %s", *id, ret ? "FAILED" : "SUCCEEDED");
  382. if (unlikely(ret)) {
  383. error("Transaction %"PRIu64" was read from disk. CRC32 check: FAILED", *id);
  384. return size_bytes;
  385. }
  386. switch (jf_header->type) {
  387. case STORE_DATA:
  388. debug(D_RRDENGINE, "Replaying transaction %"PRIu64"", jf_header->id);
  389. restore_extent_metadata(ctx, journalfile, buf + sizeof(*jf_header), payload_length);
  390. break;
  391. default:
  392. error("Unknown transaction type. Skipping record.");
  393. break;
  394. }
  395. return size_bytes;
  396. }
  397. #define READAHEAD_BYTES (RRDENG_BLOCK_SIZE * 256)
  398. /*
  399. * Iterates journal file transactions and populates the page cache.
  400. * Page cache must already be initialized.
  401. * Returns the maximum transaction id it discovered.
  402. */
  403. static uint64_t iterate_transactions(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile)
  404. {
  405. uv_file file;
  406. uint64_t file_size;//, data_file_size;
  407. int ret;
  408. uint64_t pos, pos_i, max_id, id;
  409. unsigned size_bytes;
  410. void *buf;
  411. uv_buf_t iov;
  412. uv_fs_t req;
  413. file = journalfile->file;
  414. file_size = journalfile->pos;
  415. //data_file_size = journalfile->datafile->pos; TODO: utilize this?
  416. max_id = 1;
  417. bool journal_is_mmapped = (journalfile->data != NULL);
  418. if (unlikely(!journal_is_mmapped)) {
  419. ret = posix_memalign((void *)&buf, RRDFILE_ALIGNMENT, READAHEAD_BYTES);
  420. if (unlikely(ret))
  421. fatal("posix_memalign:%s", strerror(ret));
  422. }
  423. else
  424. buf = journalfile->data + sizeof(struct rrdeng_jf_sb);
  425. for (pos = sizeof(struct rrdeng_jf_sb) ; pos < file_size ; pos += READAHEAD_BYTES) {
  426. size_bytes = MIN(READAHEAD_BYTES, file_size - pos);
  427. if (unlikely(!journal_is_mmapped)) {
  428. iov = uv_buf_init(buf, size_bytes);
  429. ret = uv_fs_read(NULL, &req, file, &iov, 1, pos, NULL);
  430. if (ret < 0) {
  431. error("uv_fs_read: pos=%" PRIu64 ", %s", pos, uv_strerror(ret));
  432. uv_fs_req_cleanup(&req);
  433. goto skip_file;
  434. }
  435. fatal_assert(req.result >= 0);
  436. uv_fs_req_cleanup(&req);
  437. ++ctx->stats.io_read_requests;
  438. ctx->stats.io_read_bytes += size_bytes;
  439. }
  440. for (pos_i = 0 ; pos_i < size_bytes ; ) {
  441. unsigned max_size;
  442. max_size = pos + size_bytes - pos_i;
  443. ret = replay_transaction(ctx, journalfile, buf + pos_i, &id, max_size);
  444. if (!ret) /* TODO: support transactions bigger than 4K */
  445. /* unknown transaction size, move on to the next block */
  446. pos_i = ALIGN_BYTES_FLOOR(pos_i + RRDENG_BLOCK_SIZE);
  447. else
  448. pos_i += ret;
  449. max_id = MAX(max_id, id);
  450. }
  451. if (likely(journal_is_mmapped))
  452. buf += size_bytes;
  453. }
  454. skip_file:
  455. if (unlikely(!journal_is_mmapped))
  456. posix_memfree(buf);
  457. return max_id;
  458. }
  459. int load_journal_file(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile,
  460. struct rrdengine_datafile *datafile)
  461. {
  462. uv_fs_t req;
  463. uv_file file;
  464. int ret, fd, error;
  465. uint64_t file_size, max_id;
  466. char path[RRDENG_PATH_MAX];
  467. generate_journalfilepath(datafile, path, sizeof(path));
  468. fd = open_file_direct_io(path, O_RDWR, &file);
  469. if (fd < 0) {
  470. ++ctx->stats.fs_errors;
  471. rrd_stat_atomic_add(&global_fs_errors, 1);
  472. return fd;
  473. }
  474. info("Loading journal file \"%s\".", path);
  475. ret = check_file_properties(file, &file_size, sizeof(struct rrdeng_df_sb));
  476. if (ret)
  477. goto error;
  478. file_size = ALIGN_BYTES_FLOOR(file_size);
  479. ret = check_journal_file_superblock(file);
  480. if (ret)
  481. goto error;
  482. ctx->stats.io_read_bytes += sizeof(struct rrdeng_jf_sb);
  483. ++ctx->stats.io_read_requests;
  484. journalfile->file = file;
  485. journalfile->pos = file_size;
  486. journalfile->data = netdata_mmap(path, file_size, MAP_SHARED, 0);
  487. info("Loading journal file \"%s\" using %s.", path, journalfile->data?"MMAP":"uv_fs_read");
  488. max_id = iterate_transactions(ctx, journalfile);
  489. ctx->commit_log.transaction_id = MAX(ctx->commit_log.transaction_id, max_id + 1);
  490. info("Journal file \"%s\" loaded (size:%"PRIu64").", path, file_size);
  491. if (likely(journalfile->data))
  492. netdata_munmap(journalfile->data, file_size);
  493. return 0;
  494. error:
  495. error = ret;
  496. ret = uv_fs_close(NULL, &req, file, NULL);
  497. if (ret < 0) {
  498. error("uv_fs_close(%s): %s", path, uv_strerror(ret));
  499. ++ctx->stats.fs_errors;
  500. rrd_stat_atomic_add(&global_fs_errors, 1);
  501. }
  502. uv_fs_req_cleanup(&req);
  503. return error;
  504. }
  505. void init_commit_log(struct rrdengine_instance *ctx)
  506. {
  507. ctx->commit_log.buf = NULL;
  508. ctx->commit_log.buf_pos = 0;
  509. ctx->commit_log.transaction_id = 1;
  510. }