journalfile.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. struct page_cache *pg_cache = &ctx->pg_cache;
  238. unsigned i, count, payload_length, descr_size, valid_pages;
  239. struct rrdeng_page_descr *descr;
  240. struct extent_info *extent;
  241. /* persistent structures */
  242. struct rrdeng_jf_store_data *jf_metric_data;
  243. jf_metric_data = buf;
  244. count = jf_metric_data->number_of_pages;
  245. descr_size = sizeof(*jf_metric_data->descr) * count;
  246. payload_length = sizeof(*jf_metric_data) + descr_size;
  247. if (payload_length > max_size) {
  248. error("Corrupted transaction payload.");
  249. return;
  250. }
  251. extent = mallocz(sizeof(*extent) + count * sizeof(extent->pages[0]));
  252. extent->offset = jf_metric_data->extent_offset;
  253. extent->size = jf_metric_data->extent_size;
  254. extent->datafile = journalfile->datafile;
  255. extent->next = NULL;
  256. for (i = 0, valid_pages = 0 ; i < count ; ++i) {
  257. uuid_t *temp_id;
  258. Pvoid_t *PValue;
  259. struct pg_cache_page_index *page_index = NULL;
  260. if (PAGE_METRICS != jf_metric_data->descr[i].type) {
  261. error("Unknown page type encountered.");
  262. continue;
  263. }
  264. temp_id = (uuid_t *)jf_metric_data->descr[i].uuid;
  265. uv_rwlock_rdlock(&pg_cache->metrics_index.lock);
  266. PValue = JudyHSGet(pg_cache->metrics_index.JudyHS_array, temp_id, sizeof(uuid_t));
  267. if (likely(NULL != PValue)) {
  268. page_index = *PValue;
  269. }
  270. uv_rwlock_rdunlock(&pg_cache->metrics_index.lock);
  271. if (NULL == PValue) {
  272. /* First time we see the UUID */
  273. uv_rwlock_wrlock(&pg_cache->metrics_index.lock);
  274. PValue = JudyHSIns(&pg_cache->metrics_index.JudyHS_array, temp_id, sizeof(uuid_t), PJE0);
  275. fatal_assert(NULL == *PValue); /* TODO: figure out concurrency model */
  276. *PValue = page_index = create_page_index(temp_id);
  277. page_index->prev = pg_cache->metrics_index.last_page_index;
  278. pg_cache->metrics_index.last_page_index = page_index;
  279. uv_rwlock_wrunlock(&pg_cache->metrics_index.lock);
  280. }
  281. descr = pg_cache_create_descr();
  282. descr->page_length = jf_metric_data->descr[i].page_length;
  283. descr->start_time = jf_metric_data->descr[i].start_time;
  284. descr->end_time = jf_metric_data->descr[i].end_time;
  285. descr->id = &page_index->id;
  286. descr->extent = extent;
  287. extent->pages[valid_pages++] = descr;
  288. pg_cache_insert(ctx, page_index, descr);
  289. }
  290. extent->number_of_pages = valid_pages;
  291. if (likely(valid_pages))
  292. df_extent_insert(extent);
  293. else
  294. freez(extent);
  295. }
  296. /*
  297. * Replays transaction by interpreting up to max_size bytes from buf.
  298. * Sets id to the current transaction id or to 0 if unknown.
  299. * Returns size of transaction record or 0 for unknown size.
  300. */
  301. static unsigned replay_transaction(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile,
  302. void *buf, uint64_t *id, unsigned max_size)
  303. {
  304. unsigned payload_length, size_bytes;
  305. int ret;
  306. /* persistent structures */
  307. struct rrdeng_jf_transaction_header *jf_header;
  308. struct rrdeng_jf_transaction_trailer *jf_trailer;
  309. uLong crc;
  310. *id = 0;
  311. jf_header = buf;
  312. if (STORE_PADDING == jf_header->type) {
  313. debug(D_RRDENGINE, "Skipping padding.");
  314. return 0;
  315. }
  316. if (sizeof(*jf_header) > max_size) {
  317. error("Corrupted transaction record, skipping.");
  318. return 0;
  319. }
  320. *id = jf_header->id;
  321. payload_length = jf_header->payload_length;
  322. size_bytes = sizeof(*jf_header) + payload_length + sizeof(*jf_trailer);
  323. if (size_bytes > max_size) {
  324. error("Corrupted transaction record, skipping.");
  325. return 0;
  326. }
  327. jf_trailer = buf + sizeof(*jf_header) + payload_length;
  328. crc = crc32(0L, Z_NULL, 0);
  329. crc = crc32(crc, buf, sizeof(*jf_header) + payload_length);
  330. ret = crc32cmp(jf_trailer->checksum, crc);
  331. debug(D_RRDENGINE, "Transaction %"PRIu64" was read from disk. CRC32 check: %s", *id, ret ? "FAILED" : "SUCCEEDED");
  332. if (unlikely(ret)) {
  333. error("Transaction %"PRIu64" was read from disk. CRC32 check: FAILED", *id);
  334. return size_bytes;
  335. }
  336. switch (jf_header->type) {
  337. case STORE_DATA:
  338. debug(D_RRDENGINE, "Replaying transaction %"PRIu64"", jf_header->id);
  339. restore_extent_metadata(ctx, journalfile, buf + sizeof(*jf_header), payload_length);
  340. break;
  341. default:
  342. error("Unknown transaction type. Skipping record.");
  343. break;
  344. }
  345. return size_bytes;
  346. }
  347. #define READAHEAD_BYTES (RRDENG_BLOCK_SIZE * 256)
  348. /*
  349. * Iterates journal file transactions and populates the page cache.
  350. * Page cache must already be initialized.
  351. * Returns the maximum transaction id it discovered.
  352. */
  353. static uint64_t iterate_transactions(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile)
  354. {
  355. uv_file file;
  356. uint64_t file_size;//, data_file_size;
  357. int ret;
  358. uint64_t pos, pos_i, max_id, id;
  359. unsigned size_bytes;
  360. void *buf;
  361. uv_buf_t iov;
  362. uv_fs_t req;
  363. file = journalfile->file;
  364. file_size = journalfile->pos;
  365. //data_file_size = journalfile->datafile->pos; TODO: utilize this?
  366. max_id = 1;
  367. ret = posix_memalign((void *)&buf, RRDFILE_ALIGNMENT, READAHEAD_BYTES);
  368. if (unlikely(ret)) {
  369. fatal("posix_memalign:%s", strerror(ret));
  370. }
  371. for (pos = sizeof(struct rrdeng_jf_sb) ; pos < file_size ; pos += READAHEAD_BYTES) {
  372. size_bytes = MIN(READAHEAD_BYTES, file_size - pos);
  373. iov = uv_buf_init(buf, size_bytes);
  374. ret = uv_fs_read(NULL, &req, file, &iov, 1, pos, NULL);
  375. if (ret < 0) {
  376. error("uv_fs_read: pos=%"PRIu64", %s", pos, uv_strerror(ret));
  377. uv_fs_req_cleanup(&req);
  378. goto skip_file;
  379. }
  380. fatal_assert(req.result >= 0);
  381. uv_fs_req_cleanup(&req);
  382. ctx->stats.io_read_bytes += size_bytes;
  383. ++ctx->stats.io_read_requests;
  384. //pos_i = pos;
  385. //while (pos_i < pos + size_bytes) {
  386. for (pos_i = 0 ; pos_i < size_bytes ; ) {
  387. unsigned max_size;
  388. max_size = pos + size_bytes - pos_i;
  389. ret = replay_transaction(ctx, journalfile, buf + pos_i, &id, max_size);
  390. if (!ret) /* TODO: support transactions bigger than 4K */
  391. /* unknown transaction size, move on to the next block */
  392. pos_i = ALIGN_BYTES_FLOOR(pos_i + RRDENG_BLOCK_SIZE);
  393. else
  394. pos_i += ret;
  395. max_id = MAX(max_id, id);
  396. }
  397. }
  398. skip_file:
  399. free(buf);
  400. return max_id;
  401. }
  402. int load_journal_file(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile,
  403. struct rrdengine_datafile *datafile)
  404. {
  405. uv_fs_t req;
  406. uv_file file;
  407. int ret, fd, error;
  408. uint64_t file_size, max_id;
  409. char path[RRDENG_PATH_MAX];
  410. generate_journalfilepath(datafile, path, sizeof(path));
  411. fd = open_file_direct_io(path, O_RDWR, &file);
  412. if (fd < 0) {
  413. ++ctx->stats.fs_errors;
  414. rrd_stat_atomic_add(&global_fs_errors, 1);
  415. return fd;
  416. }
  417. info("Loading journal file \"%s\".", path);
  418. ret = check_file_properties(file, &file_size, sizeof(struct rrdeng_df_sb));
  419. if (ret)
  420. goto error;
  421. file_size = ALIGN_BYTES_FLOOR(file_size);
  422. ret = check_journal_file_superblock(file);
  423. if (ret)
  424. goto error;
  425. ctx->stats.io_read_bytes += sizeof(struct rrdeng_jf_sb);
  426. ++ctx->stats.io_read_requests;
  427. journalfile->file = file;
  428. journalfile->pos = file_size;
  429. max_id = iterate_transactions(ctx, journalfile);
  430. ctx->commit_log.transaction_id = MAX(ctx->commit_log.transaction_id, max_id + 1);
  431. info("Journal file \"%s\" loaded (size:%"PRIu64").", path, file_size);
  432. return 0;
  433. error:
  434. error = ret;
  435. ret = uv_fs_close(NULL, &req, file, NULL);
  436. if (ret < 0) {
  437. error("uv_fs_close(%s): %s", path, uv_strerror(ret));
  438. ++ctx->stats.fs_errors;
  439. rrd_stat_atomic_add(&global_fs_errors, 1);
  440. }
  441. uv_fs_req_cleanup(&req);
  442. return error;
  443. }
  444. void init_commit_log(struct rrdengine_instance *ctx)
  445. {
  446. ctx->commit_log.buf = NULL;
  447. ctx->commit_log.buf_pos = 0;
  448. ctx->commit_log.transaction_id = 1;
  449. }