journalfile.c 17 KB

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