rrdengine.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRDENGINE_H
  3. #define NETDATA_RRDENGINE_H
  4. #ifndef _GNU_SOURCE
  5. #define _GNU_SOURCE
  6. #endif
  7. #include <fcntl.h>
  8. #include <lz4.h>
  9. #include <Judy.h>
  10. #include <openssl/sha.h>
  11. #include <openssl/evp.h>
  12. #include "daemon/common.h"
  13. #include "../rrd.h"
  14. #include "rrddiskprotocol.h"
  15. #include "rrdenginelib.h"
  16. #include "datafile.h"
  17. #include "journalfile.h"
  18. #include "metadata_log/metadatalog.h"
  19. #include "rrdengineapi.h"
  20. #include "pagecache.h"
  21. #include "rrdenglocking.h"
  22. #ifdef NETDATA_RRD_INTERNALS
  23. #endif /* NETDATA_RRD_INTERNALS */
  24. /* Forward declarations */
  25. struct rrdengine_instance;
  26. #define MAX_PAGES_PER_EXTENT (64) /* TODO: can go higher only when journal supports bigger than 4KiB transactions */
  27. #define RRDENG_FILE_NUMBER_SCAN_TMPL "%1u-%10u"
  28. #define RRDENG_FILE_NUMBER_PRINT_TMPL "%1.1u-%10.10u"
  29. typedef enum {
  30. RRDENGINE_STATUS_UNINITIALIZED = 0,
  31. RRDENGINE_STATUS_INITIALIZING,
  32. RRDENGINE_STATUS_INITIALIZED
  33. } rrdengine_state_t;
  34. enum rrdeng_opcode {
  35. /* can be used to return empty status or flush the command queue */
  36. RRDENG_NOOP = 0,
  37. RRDENG_READ_PAGE,
  38. RRDENG_READ_EXTENT,
  39. RRDENG_COMMIT_PAGE,
  40. RRDENG_FLUSH_PAGES,
  41. RRDENG_SHUTDOWN,
  42. RRDENG_INVALIDATE_OLDEST_MEMORY_PAGE,
  43. RRDENG_QUIESCE,
  44. RRDENG_MAX_OPCODE
  45. };
  46. struct rrdeng_read_page {
  47. struct rrdeng_page_descr *page_cache_descr;
  48. };
  49. struct rrdeng_read_extent {
  50. struct rrdeng_page_descr *page_cache_descr[MAX_PAGES_PER_EXTENT];
  51. int page_count;
  52. };
  53. struct rrdeng_cmd {
  54. enum rrdeng_opcode opcode;
  55. union {
  56. struct rrdeng_read_page read_page;
  57. struct rrdeng_read_extent read_extent;
  58. struct completion *completion;
  59. };
  60. };
  61. #define RRDENG_CMD_Q_MAX_SIZE (2048)
  62. struct rrdeng_cmdqueue {
  63. unsigned head, tail;
  64. struct rrdeng_cmd cmd_array[RRDENG_CMD_Q_MAX_SIZE];
  65. };
  66. struct extent_io_descriptor {
  67. uv_fs_t req;
  68. uv_buf_t iov;
  69. void *buf;
  70. uint64_t pos;
  71. unsigned bytes;
  72. struct completion *completion;
  73. unsigned descr_count;
  74. int release_descr;
  75. struct rrdeng_page_descr *descr_array[MAX_PAGES_PER_EXTENT];
  76. Word_t descr_commit_idx_array[MAX_PAGES_PER_EXTENT];
  77. struct extent_io_descriptor *next; /* multiple requests to be served by the same cached extent */
  78. };
  79. struct generic_io_descriptor {
  80. uv_fs_t req;
  81. uv_buf_t iov;
  82. void *buf;
  83. uint64_t pos;
  84. unsigned bytes;
  85. struct completion *completion;
  86. };
  87. struct extent_cache_element {
  88. struct extent_info *extent; /* The ABA problem is avoided with the help of fileno below */
  89. unsigned fileno;
  90. struct extent_cache_element *prev; /* LRU */
  91. struct extent_cache_element *next; /* LRU */
  92. struct extent_io_descriptor *inflight_io_descr; /* I/O descriptor for in-flight extent */
  93. uint8_t pages[MAX_PAGES_PER_EXTENT * RRDENG_BLOCK_SIZE];
  94. };
  95. #define MAX_CACHED_EXTENTS 16 /* cannot be over 32 to fit in 32-bit architectures */
  96. /* Initialize by setting the structure to zero */
  97. struct extent_cache {
  98. struct extent_cache_element extent_array[MAX_CACHED_EXTENTS];
  99. unsigned allocation_bitmap; /* 1 if the corresponding position in the extent_array is allocated */
  100. unsigned inflight_bitmap; /* 1 if the corresponding position in the extent_array is waiting for I/O */
  101. struct extent_cache_element *replaceQ_head; /* LRU */
  102. struct extent_cache_element *replaceQ_tail; /* MRU */
  103. };
  104. struct rrdengine_worker_config {
  105. struct rrdengine_instance *ctx;
  106. uv_thread_t thread;
  107. uv_loop_t* loop;
  108. uv_async_t async;
  109. /* file deletion thread */
  110. uv_thread_t *now_deleting_files;
  111. unsigned long cleanup_thread_deleting_files; /* set to 0 when now_deleting_files is still running */
  112. /* dirty page deletion thread */
  113. uv_thread_t *now_invalidating_dirty_pages;
  114. /* set to 0 when now_invalidating_dirty_pages is still running */
  115. unsigned long cleanup_thread_invalidating_dirty_pages;
  116. unsigned inflight_dirty_pages;
  117. /* FIFO command queue */
  118. uv_mutex_t cmd_mutex;
  119. uv_cond_t cmd_cond;
  120. volatile unsigned queue_size;
  121. struct rrdeng_cmdqueue cmd_queue;
  122. struct extent_cache xt_cache;
  123. int error;
  124. };
  125. /*
  126. * Debug statistics not used by code logic.
  127. * They only describe operations since DB engine instance load time.
  128. */
  129. struct rrdengine_statistics {
  130. rrdeng_stats_t metric_API_producers;
  131. rrdeng_stats_t metric_API_consumers;
  132. rrdeng_stats_t pg_cache_insertions;
  133. rrdeng_stats_t pg_cache_deletions;
  134. rrdeng_stats_t pg_cache_hits;
  135. rrdeng_stats_t pg_cache_misses;
  136. rrdeng_stats_t pg_cache_backfills;
  137. rrdeng_stats_t pg_cache_evictions;
  138. rrdeng_stats_t before_decompress_bytes;
  139. rrdeng_stats_t after_decompress_bytes;
  140. rrdeng_stats_t before_compress_bytes;
  141. rrdeng_stats_t after_compress_bytes;
  142. rrdeng_stats_t io_write_bytes;
  143. rrdeng_stats_t io_write_requests;
  144. rrdeng_stats_t io_read_bytes;
  145. rrdeng_stats_t io_read_requests;
  146. rrdeng_stats_t io_write_extent_bytes;
  147. rrdeng_stats_t io_write_extents;
  148. rrdeng_stats_t io_read_extent_bytes;
  149. rrdeng_stats_t io_read_extents;
  150. rrdeng_stats_t datafile_creations;
  151. rrdeng_stats_t datafile_deletions;
  152. rrdeng_stats_t journalfile_creations;
  153. rrdeng_stats_t journalfile_deletions;
  154. rrdeng_stats_t page_cache_descriptors;
  155. rrdeng_stats_t io_errors;
  156. rrdeng_stats_t fs_errors;
  157. rrdeng_stats_t pg_cache_over_half_dirty_events;
  158. rrdeng_stats_t flushing_pressure_page_deletions;
  159. };
  160. /* I/O errors global counter */
  161. extern rrdeng_stats_t global_io_errors;
  162. /* File-System errors global counter */
  163. extern rrdeng_stats_t global_fs_errors;
  164. /* number of File-Descriptors that have been reserved by dbengine */
  165. extern rrdeng_stats_t rrdeng_reserved_file_descriptors;
  166. /* inability to flush global counters */
  167. extern rrdeng_stats_t global_pg_cache_over_half_dirty_events;
  168. extern rrdeng_stats_t global_flushing_pressure_page_deletions; /* number of deleted pages */
  169. #define NO_QUIESCE (0) /* initial state when all operations function normally */
  170. #define SET_QUIESCE (1) /* set it before shutting down the instance, quiesce long running operations */
  171. #define QUIESCED (2) /* is set after all threads have finished running */
  172. struct rrdengine_instance {
  173. struct metalog_instance *metalog_ctx;
  174. struct rrdengine_worker_config worker_config;
  175. struct completion rrdengine_completion;
  176. struct page_cache pg_cache;
  177. uint8_t drop_metrics_under_page_cache_pressure; /* boolean */
  178. uint8_t global_compress_alg;
  179. struct transaction_commit_log commit_log;
  180. struct rrdengine_datafile_list datafiles;
  181. RRDHOST *host; /* the legacy host, or NULL for multi-host DB */
  182. char dbfiles_path[FILENAME_MAX + 1];
  183. char machine_guid[GUID_LEN + 1]; /* the unique ID of the corresponding host, or localhost for multihost DB */
  184. uint64_t disk_space;
  185. uint64_t max_disk_space;
  186. unsigned last_fileno; /* newest index of datafile and journalfile */
  187. unsigned long max_cache_pages;
  188. unsigned long cache_pages_low_watermark;
  189. unsigned long metric_API_max_producers;
  190. uint8_t quiesce; /* set to SET_QUIESCE before shutdown of the engine */
  191. struct rrdengine_statistics stats;
  192. };
  193. extern int init_rrd_files(struct rrdengine_instance *ctx);
  194. extern void finalize_rrd_files(struct rrdengine_instance *ctx);
  195. extern void rrdeng_test_quota(struct rrdengine_worker_config* wc);
  196. extern void rrdeng_worker(void* arg);
  197. extern void rrdeng_enq_cmd(struct rrdengine_worker_config* wc, struct rrdeng_cmd *cmd);
  198. extern struct rrdeng_cmd rrdeng_deq_cmd(struct rrdengine_worker_config* wc);
  199. #endif /* NETDATA_RRDENGINE_H */