@cryptotaxi247 / netdata-1 / commits / c130f88d0

work in progress: registry

Costa Tsaousis committed May 7, 2016 at 04:52 UTC c130f88d0430f6b47c9cb43137afd8c799095130
5 files changed +256 -3
configure.ac
+4
@@ -92,6 +92,10 @@ if test -z "${MATH_LIBS}"; then
92 )
93 fi
94
95 +AC_CHECK_LIB([uuid], [uuid_generate_time_safe], [], \
96 + [AC_MSG_ERROR([Function uuid_generate_time_safe was not found in libuuid.
97 +Is uuid-dev installed? Try running 'sudo apt-get install uuid-dev'.])])
98 +
99 if test "${enable_plugin_nfacct}" = "yes"; then
100 PKG_CHECK_MODULES(
101 [NFACCT],
src/dictionary.c
+8 -3
@@ -1,6 +1,7 @@
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 +
5 #include <pthread.h>
6 #include <stdlib.h>
7 #include <string.h>
@@ -58,9 +59,13 @@ static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const c
59 NAME_VALUE *nv = calloc(1, sizeof(NAME_VALUE));
60 if(unlikely(!nv)) fatal("Cannot allocate name_value of size %z", sizeof(NAME_VALUE));
61
61 - nv->name = strdup(name);
62 - if(unlikely(!nv->name))
63 - fatal("Cannot allocate name_value.name of size %z", strlen(name));
62 + if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
63 + nv->name = (char *)name;
64 + else {
65 + nv->name = strdup(name);
66 + if (unlikely(!nv->name))
67 + fatal("Cannot allocate name_value.name of size %z", strlen(name));
68 + }
69
70 nv->hash = (hash)?hash:simple_hash(nv->name);
71
src/dictionary.h
+1
@@ -30,6 +30,7 @@ typedef struct dictionary {
30 #define DICTIONARY_FLAG_DEFAULT 0x00000000
31 #define DICTIONARY_FLAG_SINGLE_THREADED 0x00000001
32 #define DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE 0x00000002
33 +#define DICTIONARY_FLAG_NAME_LINK_DONT_CLONE 0x00000004
34
35 extern DICTIONARY *dictionary_create(uint32_t flags);
36 extern void dictionary_destroy(DICTIONARY *dict);
src/log.h
+1
@@ -26,6 +26,7 @@
26 #define D_DICTIONARY 0x00040000
27 #define D_MEMORY 0x00080000
28 #define D_CGROUP 0x00100000
29 +#define D_REGISTRY 0x00200000
30
31 //#define DEBUG (D_WEB_CLIENT_ACCESS|D_LISTENER|D_RRD_STATS)
32 //#define DEBUG 0xffffffff
src/registry.c new
+242
@@ -0,0 +1,242 @@
1 +#ifdef HAVE_CONFIG_H
2 +#include <config.h>
3 +#endif
4 +
5 +// gcc -O3 -Wall -Wextra -I ../src/ -I ../ -o registry ../src/registry.c ../src/dictionary.o ../src/log.o ../src/avl.o ../src/common.o -pthread -luuid -DHAVE_CONFIG_H
6 +
7 +#include <uuid/uuid.h>
8 +#include <inttypes.h>
9 +#include <stdlib.h>
10 +#include <string.h>
11 +
12 +#include "log.h"
13 +#include "common.h"
14 +#include "dictionary.h"
15 +
16 +struct machine {
17 + char guid[36 + 1];
18 +
19 + time_t first_t;
20 + time_t last_t;
21 + size_t usages;
22 +
23 + DICTIONARY *urls;
24 +};
25 +typedef struct machine MACHINE;
26 +
27 +
28 +struct person {
29 + char guid[36 + 1];
30 +
31 + time_t first_t;
32 + time_t last_t;
33 + size_t usages;
34 +
35 + DICTIONARY *urls;
36 +};
37 +typedef struct person PERSON;
38 +
39 +
40 +struct url {
41 + MACHINE *machine;
42 + PERSON *person;
43 +
44 + time_t first_t;
45 + time_t last_t;
46 + size_t usages;
47 +
48 + size_t url_length;
49 + char url[];
50 +};
51 +typedef struct url URL;
52 +
53 +
54 +struct registry {
55 + DICTIONARY *persons;
56 + DICTIONARY *machines;
57 +} registry;
58 +
59 +
60 +// ----------------------------------------------------------------------------
61 +// MACHINE
62 +
63 +MACHINE *registry_machine_load(const char *machine_guid) {
64 + (void)machine_guid;
65 +
66 + return NULL;
67 +}
68 +
69 +int registry_machine_save(MACHINE *m) {
70 + (void)m;
71 +
72 + return -1;
73 +}
74 +
75 +MACHINE *registry_machine_find(const char *machine_guid) {
76 + MACHINE *m = dictionary_get(registry.machines, machine_guid);
77 + if(!m) m = registry_machine_load(machine_guid);
78 + return m;
79 +}
80 +
81 +
82 +MACHINE *registry_machine_get(const char *machine_guid) {
83 + MACHINE *m = registry_machine_find(machine_guid);
84 + if(!m) {
85 + debug(D_REGISTRY, "Registry: creating new machine '%s'", machine_guid);
86 +
87 + m = calloc(1, sizeof(MACHINE));
88 + if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
89 +
90 + strncpy(m->guid, machine_guid, 36);
91 +
92 + dictionary_set(registry.machines, m->guid, m, sizeof(MACHINE));
93 +
94 + m->first_t = time(NULL);
95 + m->usages = 0;
96 + }
97 +
98 + return m;
99 +}
100 +
101 +
102 +// ----------------------------------------------------------------------------
103 +// PERSON
104 +
105 +PERSON *registry_person_load(const char *person_guid) {
106 + (void)person_guid;
107 +
108 + return NULL;
109 +}
110 +
111 +int registry_person_save(PERSON *p) {
112 + (void)p;
113 +
114 + return -1;
115 +}
116 +
117 +PERSON *registry_person_find(const char *person_guid) {
118 + PERSON *p = dictionary_get(registry.persons, person_guid);
119 + if(!p) p = registry_person_load(person_guid);
120 + return p;
121 +}
122 +
123 +PERSON *registry_person_get(const char *person_guid) {
124 + PERSON *p = NULL;
125 +
126 + if(person_guid && *person_guid)
127 + p = registry_person_find(person_guid);
128 +
129 + if(!p) {
130 + if(person_guid && *person_guid)
131 + error("Registry: discarding unknown person guid '%s'. Will give a new PERSONID.", person_guid);
132 +
133 + debug(D_REGISTRY, "Registry: creating new person");
134 +
135 + p = calloc(1, sizeof(PERSON));
136 + if(!p) fatal("Registry: cannot allocate memory for new person.");
137 +
138 + uuid_t uuid;
139 + if(uuid_generate_time_safe(uuid) == -1)
140 + info("Registry: uuid_generate_time_safe() reports UUID generation is not safe for uniqueness.");
141 +
142 + uuid_unparse_lower(uuid, p->guid);
143 +
144 + dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
145 +
146 + p->first_t = time(NULL);
147 + p->usages = 0;
148 + }
149 +
150 + return p;
151 +}
152 +
153 +
154 +// ----------------------------------------------------------------------------
155 +// URL
156 +
157 +URL *registry_url_update(PERSON *p, MACHINE *m, const char *url) {
158 + URL *pu = dictionary_get(p->urls, url);
159 + URL *mu = dictionary_get(m->urls, url);
160 + URL *u = NULL;
161 +
162 + if(pu != mu || pu == NULL || mu == NULL)
163 + error("Registry: person/machine discrepancy on url '%s', for person '%s' and machine '%s'", url, p->guid, m->guid);
164 + else
165 + u = pu;
166 +
167 + if(!u) {
168 + size_t len = strlen(url);
169 +
170 + URL *u = calloc(1, sizeof(URL) + len);
171 + if(!u) fatal("Registry: cannot allocate memory for person '%s', machine '%s', url '%s'.", p->guid, m->guid, url);
172 +
173 + strcpy(u->url, url);
174 + u->url_length = len;
175 + u->person = p;
176 + u->machine = m;
177 + u->first_t = time(NULL);
178 + u->usages = 1;
179 +
180 + dictionary_set(p->urls, url, u, sizeof(URL) + u->url_length);
181 + dictionary_set(m->urls, url, u, sizeof(URL) + u->url_length);
182 + if(pu) free(pu);
183 + if(mu) free(mu);
184 + }
185 + else
186 + u->usages++;
187 +
188 + p->usages++;
189 + m->usages++;
190 + p->last_t = m->last_t = u->last_t = time(NULL);
191 +
192 + return u;
193 +}
194 +
195 +
196 +// ----------------------------------------------------------------------------
197 +// REGISTRY
198 +
199 +int registry_save(void) {
200 + return -1;
201 +}
202 +
203 +char *registry_request(const char *person_guid, const char *machine_guid, const char *url) {
204 + PERSON *p = NULL;
205 + MACHINE *m = NULL;
206 +
207 + // --- PERSON ---
208 + p = registry_person_get(person_guid);
209 + person_guid = p->guid;
210 +
211 + // --- MACHINE ---
212 + m = registry_machine_get(machine_guid);
213 + machine_guid = m->guid;
214 +
215 + // --- URL ---
216 + registry_url_update(p, m, url);
217 +
218 + registry_person_save(p);
219 + registry_machine_save(m);
220 + registry_save();
221 +
222 + return NULL;
223 +}
224 +
225 +void registry_init(void) {
226 + registry.persons = dictionary_create(DICTIONARY_FLAG_DEFAULT);
227 + if(!registry.persons)
228 + fatal("Registry: cannot create persons registry");
229 +
230 + registry.machines = dictionary_create(DICTIONARY_FLAG_DEFAULT);
231 + if(!registry.machines)
232 + fatal("Registry: cannot create machines registry");
233 +}
234 +
235 +int main(int argc, char **argv) {
236 + (void)argc;
237 + (void)argv;
238 +
239 + registry_init();
240 +
241 + return 0;
242 +}