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