journalfile.h 6.7 KB

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