exporting_engine.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "exporting_engine.h"
  3. static struct engine *engine = NULL;
  4. /**
  5. * Exporting Clean Engine
  6. *
  7. * Clean all variables allocated inside engine structure
  8. *
  9. * @param en a pointer to the structure that will be cleaned.
  10. */
  11. static void exporting_clean_engine()
  12. {
  13. if (!engine)
  14. return;
  15. #if HAVE_KINESIS
  16. if (engine->aws_sdk_initialized)
  17. aws_sdk_shutdown();
  18. #endif
  19. #if ENABLE_PROMETHEUS_REMOTE_WRITE
  20. if (engine->protocol_buffers_initialized)
  21. protocol_buffers_shutdown();
  22. #endif
  23. //Cleanup web api
  24. prometheus_clean_server_root();
  25. for (struct instance *instance = engine->instance_root; instance;) {
  26. struct instance *current_instance = instance;
  27. instance = instance->next;
  28. clean_instance(current_instance);
  29. }
  30. freez((void *)engine->config.hostname);
  31. freez(engine);
  32. }
  33. /**
  34. * Clean up the main exporting thread and all connector workers on Netdata exit
  35. *
  36. * @param ptr thread data.
  37. */
  38. static void exporting_main_cleanup(void *ptr)
  39. {
  40. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  41. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  42. info("cleaning up...");
  43. if (!engine) {
  44. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  45. return;
  46. }
  47. engine->exit = 1;
  48. int found = 0;
  49. usec_t max = 2 * USEC_PER_SEC, step = 50000;
  50. for (struct instance *instance = engine->instance_root; instance; instance = instance->next) {
  51. if (!instance->exited) {
  52. found++;
  53. info("stopping worker for instance %s", instance->config.name);
  54. uv_mutex_unlock(&instance->mutex);
  55. instance->data_is_ready = 1;
  56. uv_cond_signal(&instance->cond_var);
  57. } else
  58. info("found stopped worker for instance %s", instance->config.name);
  59. }
  60. while (found && max > 0) {
  61. max -= step;
  62. info("Waiting %d exporting connectors to finish...", found);
  63. sleep_usec(step);
  64. found = 0;
  65. for (struct instance *instance = engine->instance_root; instance; instance = instance->next) {
  66. if (!instance->exited)
  67. found++;
  68. }
  69. }
  70. exporting_clean_engine();
  71. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  72. }
  73. /**
  74. * Exporting engine main
  75. *
  76. * The main thread used to control the exporting engine.
  77. *
  78. * @param ptr a pointer to netdata_static_structure.
  79. *
  80. * @return It always returns NULL.
  81. */
  82. void *exporting_main(void *ptr)
  83. {
  84. netdata_thread_cleanup_push(exporting_main_cleanup, ptr);
  85. engine = read_exporting_config();
  86. if (!engine) {
  87. info("EXPORTING: no exporting connectors configured");
  88. goto cleanup;
  89. }
  90. if (init_connectors(engine) != 0) {
  91. error("EXPORTING: cannot initialize exporting connectors");
  92. send_statistics("EXPORTING_START", "FAIL", "-");
  93. goto cleanup;
  94. }
  95. RRDSET *st_main_rusage = NULL;
  96. RRDDIM *rd_main_user = NULL;
  97. RRDDIM *rd_main_system = NULL;
  98. create_main_rusage_chart(&st_main_rusage, &rd_main_user, &rd_main_system);
  99. usec_t step_ut = localhost->rrd_update_every * USEC_PER_SEC;
  100. heartbeat_t hb;
  101. heartbeat_init(&hb);
  102. while (!netdata_exit) {
  103. heartbeat_next(&hb, step_ut);
  104. engine->now = now_realtime_sec();
  105. if (mark_scheduled_instances(engine))
  106. prepare_buffers(engine);
  107. send_main_rusage(st_main_rusage, rd_main_user, rd_main_system);
  108. #ifdef UNIT_TESTING
  109. return NULL;
  110. #endif
  111. }
  112. cleanup:
  113. netdata_thread_cleanup_pop(1);
  114. return NULL;
  115. }