registry_machine.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "daemon/common.h"
  3. #include "registry_internals.h"
  4. // ----------------------------------------------------------------------------
  5. // MACHINE
  6. REGISTRY_MACHINE *registry_machine_find(const char *machine_guid) {
  7. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_find('%s')", machine_guid);
  8. return dictionary_get(registry.machines, machine_guid);
  9. }
  10. REGISTRY_MACHINE_URL *registry_machine_url_find(REGISTRY_MACHINE *m, STRING *url) {
  11. REGISTRY_MACHINE_URL *mu;
  12. for(mu = m->machine_urls; mu ;mu = mu->next)
  13. if(mu->url == url)
  14. break;
  15. return mu;
  16. }
  17. void registry_machine_url_unlink_from_machine_and_free(REGISTRY_MACHINE *m, REGISTRY_MACHINE_URL *mu) {
  18. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(m->machine_urls, mu, prev, next);
  19. string_freez(mu->url);
  20. aral_freez(registry.machine_urls_aral, mu);
  21. }
  22. REGISTRY_MACHINE_URL *registry_machine_url_allocate(REGISTRY_MACHINE *m, STRING *u, time_t when) {
  23. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_url_allocate('%s', '%s'): allocating %zu bytes", m->guid, string2str(u), sizeof(REGISTRY_MACHINE_URL));
  24. REGISTRY_MACHINE_URL *mu = aral_mallocz(registry.machine_urls_aral);
  25. mu->first_t = mu->last_t = (uint32_t)when;
  26. mu->usages = 1;
  27. mu->url = string_dup(u);
  28. mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
  29. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_url_allocate('%s', '%s'): indexing URL in machine", m->guid, string2str(u));
  30. DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(m->machine_urls, mu, prev, next);
  31. return mu;
  32. }
  33. REGISTRY_MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
  34. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(REGISTRY_MACHINE));
  35. REGISTRY_MACHINE *m = aral_mallocz(registry.machines_aral);
  36. strncpyz(m->guid, machine_guid, GUID_LEN);
  37. m->machine_urls = NULL;
  38. m->first_t = m->last_t = (uint32_t)when;
  39. m->usages = 0;
  40. m->links = 0;
  41. registry.machines_count++;
  42. dictionary_set(registry.machines, m->guid, m, sizeof(REGISTRY_MACHINE));
  43. return m;
  44. }
  45. // 1. validate machine GUID
  46. // 2. if it is valid, find it or create it and return it
  47. // 3. if it is not valid, return NULL
  48. REGISTRY_MACHINE *registry_machine_find_or_create(const char *machine_guid, time_t when, bool is_dummy __maybe_unused) {
  49. REGISTRY_MACHINE *m = NULL;
  50. if(likely(machine_guid && *machine_guid)) {
  51. // validate it is a GUID
  52. char buf[GUID_LEN + 1];
  53. if(unlikely(regenerate_guid(machine_guid, buf) == -1))
  54. netdata_log_info("REGISTRY: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
  55. else {
  56. machine_guid = buf;
  57. m = registry_machine_find(machine_guid);
  58. if(!m) m = registry_machine_allocate(machine_guid, when);
  59. }
  60. }
  61. return m;
  62. }
  63. // ----------------------------------------------------------------------------
  64. // LINKING OF OBJECTS
  65. REGISTRY_MACHINE_URL *registry_machine_link_to_url(REGISTRY_MACHINE *m, STRING *url, time_t when) {
  66. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_link_to_url('%s', '%s'): searching for URL in machine", m->guid, string2str(url));
  67. REGISTRY_MACHINE_URL *mu = registry_machine_url_find(m, url);
  68. if(!mu) {
  69. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_link_to_url('%s', '%s'): not found", m->guid, string2str(url));
  70. mu = registry_machine_url_allocate(m, url, when);
  71. registry.machines_urls_count++;
  72. }
  73. else {
  74. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_link_to_url('%s', '%s'): found", m->guid, string2str(url));
  75. mu->usages++;
  76. if(likely(mu->last_t < (uint32_t)when)) mu->last_t = (uint32_t)when;
  77. }
  78. m->usages++;
  79. if(likely(m->last_t < (uint32_t)when)) m->last_t = (uint32_t)when;
  80. if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
  81. netdata_log_debug(D_REGISTRY, "REGISTRY: registry_machine_link_to_url('%s', '%s'): accessing an expired URL.", m->guid, string2str(url));
  82. mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
  83. }
  84. return mu;
  85. }