journalfile.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_JOURNALFILE_H
  3. #define NETDATA_JOURNALFILE_H
  4. #include "rrdengine.h"
  5. /* Forward declarations */
  6. struct rrdengine_instance;
  7. struct rrdengine_worker_config;
  8. struct rrdengine_datafile;
  9. struct rrdengine_journalfile;
  10. #define WALFILE_PREFIX "journalfile-"
  11. #define WALFILE_EXTENSION ".njf"
  12. #define WALFILE_EXTENSION_V2 ".njfv2"
  13. #define is_descr_journal_v2(descr) ((descr)->extent_entry != NULL)
  14. typedef enum __attribute__ ((__packed__)) {
  15. JOURNALFILE_FLAG_IS_AVAILABLE = (1 << 0),
  16. JOURNALFILE_FLAG_IS_MOUNTED = (1 << 1),
  17. JOURNALFILE_FLAG_MOUNTED_FOR_RETENTION = (1 << 2),
  18. } JOURNALFILE_FLAGS;
  19. /* only one event loop is supported for now */
  20. struct rrdengine_journalfile {
  21. struct {
  22. SPINLOCK spinlock;
  23. void *data; // MMAPed file of journal v2
  24. uint32_t size; // Total file size mapped
  25. int fd;
  26. } mmap;
  27. struct {
  28. SPINLOCK spinlock;
  29. JOURNALFILE_FLAGS flags;
  30. int32_t refcount;
  31. time_t first_time_s;
  32. time_t last_time_s;
  33. time_t not_needed_since_s;
  34. } v2;
  35. struct {
  36. SPINLOCK spinlock;
  37. uint64_t pos;
  38. } unsafe;
  39. uv_file file;
  40. struct rrdengine_datafile *datafile;
  41. };
  42. static inline uint64_t journalfile_current_size(struct rrdengine_journalfile *journalfile) {
  43. netdata_spinlock_lock(&journalfile->unsafe.spinlock);
  44. uint64_t size = journalfile->unsafe.pos;
  45. netdata_spinlock_unlock(&journalfile->unsafe.spinlock);
  46. return size;
  47. }
  48. // Journal v2 structures
  49. #define JOURVAL_V2_MAGIC (0x01230317)
  50. #define JOURVAL_V2_REBUILD_MAGIC (0x00230317)
  51. #define JOURVAL_V2_SKIP_MAGIC (0x02230317)
  52. struct journal_v2_block_trailer {
  53. union {
  54. uint8_t checksum[CHECKSUM_SZ]; /* CRC32 */
  55. uint32_t crc;
  56. };
  57. };
  58. // Journal V2
  59. // 28 bytes
  60. struct journal_page_header {
  61. union {
  62. uint8_t checksum[CHECKSUM_SZ]; // CRC check
  63. uint32_t crc;
  64. };
  65. uint32_t uuid_offset; // Points back to the UUID list which should point here (UUIDs should much)
  66. uint32_t entries; // Entries
  67. uuid_t uuid; // Which UUID this is
  68. };
  69. // 20 bytes
  70. struct journal_page_list {
  71. uint32_t delta_start_s; // relative to the start time of journal
  72. uint32_t delta_end_s; // relative to delta_start
  73. uint32_t extent_index; // Index to the extent (extent list) (bytes from BASE)
  74. uint32_t update_every_s;
  75. uint16_t page_length;
  76. uint8_t type;
  77. };
  78. // UUID_LIST
  79. // 36 bytes
  80. struct journal_metric_list {
  81. uuid_t uuid;
  82. uint32_t entries; // Number of entries
  83. uint32_t page_offset; // OFFSET that contains entries * struct( journal_page_list )
  84. uint32_t delta_start_s; // Min time of metric
  85. uint32_t delta_end_s; // Max time of metric (to be used to populate page_index)
  86. uint32_t update_every_s; // Last update every for this metric in this journal (last page collected)
  87. };
  88. // 16 bytes
  89. struct journal_extent_list {
  90. uint64_t datafile_offset; // Datafile offset to find the extent
  91. uint32_t datafile_size; // Size of the extent
  92. uint16_t file_index; // which file index is this datafile[index]
  93. uint8_t pages; // number of pages (not all are necesssarily valid)
  94. };
  95. // 72 bytes
  96. struct journal_v2_header {
  97. uint32_t magic;
  98. usec_t start_time_ut; // Min start time of journal
  99. usec_t end_time_ut; // Maximum end time of journal
  100. uint32_t extent_count; // Count of extents
  101. uint32_t extent_offset;
  102. uint32_t metric_count; // Count of metrics (unique UUIDS)
  103. uint32_t metric_offset;
  104. uint32_t page_count; // Total count of pages (descriptors @ time)
  105. uint32_t page_offset;
  106. uint32_t extent_trailer_offset; // CRC for entent list
  107. uint32_t metric_trailer_offset; // CRC for metric list
  108. uint32_t journal_v1_file_size; // This is the original journal file
  109. uint32_t journal_v2_file_size; // This is the total file size
  110. void *data; // Used when building the index
  111. };
  112. #define JOURNAL_V2_HEADER_PADDING_SZ (RRDENG_BLOCK_SIZE - (sizeof(struct journal_v2_header)))
  113. struct wal;
  114. void journalfile_v1_generate_path(struct rrdengine_datafile *datafile, char *str, size_t maxlen);
  115. void journalfile_v2_generate_path(struct rrdengine_datafile *datafile, char *str, size_t maxlen);
  116. struct rrdengine_journalfile *journalfile_alloc_and_init(struct rrdengine_datafile *datafile);
  117. void journalfile_v1_extent_write(struct rrdengine_instance *ctx, struct rrdengine_datafile *datafile, struct wal *wal, uv_loop_t *loop);
  118. int journalfile_close(struct rrdengine_journalfile *journalfile, struct rrdengine_datafile *datafile);
  119. int journalfile_unlink(struct rrdengine_journalfile *journalfile);
  120. int journalfile_destroy_unsafe(struct rrdengine_journalfile *journalfile, struct rrdengine_datafile *datafile);
  121. int journalfile_create(struct rrdengine_journalfile *journalfile, struct rrdengine_datafile *datafile);
  122. int journalfile_load(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile,
  123. struct rrdengine_datafile *datafile);
  124. void journalfile_v2_populate_retention_to_mrg(struct rrdengine_instance *ctx, struct rrdengine_journalfile *journalfile);
  125. void journalfile_migrate_to_v2_callback(Word_t section, unsigned datafile_fileno __maybe_unused, uint8_t type __maybe_unused,
  126. Pvoid_t JudyL_metrics, Pvoid_t JudyL_extents_pos,
  127. size_t number_of_extents, size_t number_of_metrics, size_t number_of_pages, void *user_data);
  128. bool journalfile_v2_data_available(struct rrdengine_journalfile *journalfile);
  129. size_t journalfile_v2_data_size_get(struct rrdengine_journalfile *journalfile);
  130. void journalfile_v2_data_set(struct rrdengine_journalfile *journalfile, int fd, void *journal_data, uint32_t journal_data_size);
  131. struct journal_v2_header *journalfile_v2_data_acquire(struct rrdengine_journalfile *journalfile, size_t *data_size, time_t wanted_first_time_s, time_t wanted_last_time_s);
  132. void journalfile_v2_data_release(struct rrdengine_journalfile *journalfile);
  133. void journalfile_v2_data_unmount_cleanup(time_t now_s);
  134. #endif /* NETDATA_JOURNALFILE_H */