]> arthur.barton.de Git - netdata.git/blob - src/registry_machine.c
Merge pull request #1998 from ktsaou/master
[netdata.git] / src / registry_machine.c
1 #include "registry_internals.h"
2
3 // ----------------------------------------------------------------------------
4 // MACHINE
5
6 REGISTRY_MACHINE *registry_machine_find(const char *machine_guid) {
7     debug(D_REGISTRY, "Registry: registry_machine_find('%s')", machine_guid);
8     return dictionary_get(registry.machines, machine_guid);
9 }
10
11 REGISTRY_MACHINE_URL *registry_machine_url_allocate(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
12     debug(D_REGISTRY, "registry_machine_url_allocate('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(REGISTRY_MACHINE_URL));
13
14     REGISTRY_MACHINE_URL *mu = mallocz(sizeof(REGISTRY_MACHINE_URL));
15
16     mu->first_t = mu->last_t = (uint32_t)when;
17     mu->usages = 1;
18     mu->url = u;
19     mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
20
21     registry.machines_urls_memory += sizeof(REGISTRY_MACHINE_URL);
22
23     debug(D_REGISTRY, "registry_machine_url_allocate('%s', '%s'): indexing URL in machine", m->guid, u->url);
24     dictionary_set(m->machine_urls, u->url, mu, sizeof(REGISTRY_MACHINE_URL));
25
26     registry_url_link(u);
27
28     return mu;
29 }
30
31 REGISTRY_MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
32     debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(REGISTRY_MACHINE));
33
34     REGISTRY_MACHINE *m = mallocz(sizeof(REGISTRY_MACHINE));
35
36     strncpyz(m->guid, machine_guid, GUID_LEN);
37
38     debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
39     m->machine_urls = dictionary_create(DICTIONARY_FLAGS);
40
41     m->first_t = m->last_t = (uint32_t)when;
42     m->usages = 0;
43
44     registry.machines_memory += sizeof(REGISTRY_MACHINE);
45
46     registry.machines_count++;
47     dictionary_set(registry.machines, m->guid, m, sizeof(REGISTRY_MACHINE));
48
49     return m;
50 }
51
52 // 1. validate machine GUID
53 // 2. if it is valid, find it or create it and return it
54 // 3. if it is not valid, return NULL
55 REGISTRY_MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
56     REGISTRY_MACHINE *m = NULL;
57
58     if(likely(machine_guid && *machine_guid)) {
59         // validate it is a GUID
60         char buf[GUID_LEN + 1];
61         if(unlikely(regenerate_guid(machine_guid, buf) == -1))
62             info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
63         else {
64             machine_guid = buf;
65             m = registry_machine_find(machine_guid);
66             if(!m) m = registry_machine_allocate(machine_guid, when);
67         }
68     }
69
70     return m;
71 }
72
73
74 // ----------------------------------------------------------------------------
75 // LINKING OF OBJECTS
76
77 REGISTRY_MACHINE_URL *registry_machine_link_to_url(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
78     debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): searching for URL in machine", m->guid, u->url);
79
80     REGISTRY_MACHINE_URL *mu = dictionary_get(m->machine_urls, u->url);
81     if(!mu) {
82         debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): not found", m->guid, u->url);
83         mu = registry_machine_url_allocate(m, u, when);
84         registry.machines_urls_count++;
85     }
86     else {
87         debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): found", m->guid, u->url);
88         mu->usages++;
89         if(likely(mu->last_t < (uint32_t)when)) mu->last_t = (uint32_t)when;
90     }
91
92     m->usages++;
93     if(likely(m->last_t < (uint32_t)when)) m->last_t = (uint32_t)when;
94
95     if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
96         debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): accessing an expired URL.", m->guid, u->url);
97         mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;
98     }
99
100     return mu;
101 }