5 // gcc -O1 -ggdb -Wall -Wextra -I ../src/ -I ../ -o registry ../src/registry.c ../src/dictionary.o ../src/log.o ../src/avl.o ../src/common.o ../src/appconfig.o ../src/web_buffer.o ../src/storage_number.o -pthread -luuid -lm -DHAVE_CONFIG_H -DVARLIB_DIR="\"/tmp\""
14 #include <sys/types.h>
19 #include "dictionary.h"
20 #include "appconfig.h"
22 #define REGISTRY_URL_FLAGS_DEFAULT 0x00
23 #define REGISTRY_URL_FLAGS_EXPIRED 0x01
25 #define DICTIONARY_FLAGS DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE | DICTIONARY_FLAG_NAME_LINK_DONT_CLONE
27 // ----------------------------------------------------------------------------
31 unsigned long long persons_count;
32 unsigned long long machines_count;
33 unsigned long long usages_count;
34 unsigned long long urls_count;
35 unsigned long long persons_urls_count;
36 unsigned long long machines_urls_count;
41 FILE *registry_log_fp;
43 DICTIONARY *persons; // dictionary of PERSON *, with key the PERSON.guid
44 DICTIONARY *machines; // dictionary of MACHINE *, with key the MACHINE.guid
45 DICTIONARY *urls; // dictionary of URL *, with key the URL.url
50 // ----------------------------------------------------------------------------
52 // Save memory by de-duplicating URLs
59 typedef struct url URL;
62 // ----------------------------------------------------------------------------
65 // For each MACHINE-URL pair we keep this
67 URL *url; // de-duplicated URL
68 // DICTIONARY *persons; // dictionary of PERSON *
71 uint32_t first_t; // the first time we saw this
72 uint32_t last_t; // the last time we saw this
73 uint32_t usages; // how many times this has been accessed
75 typedef struct machine_url MACHINE_URL;
81 DICTIONARY *urls; // MACHINE_URL *
83 uint32_t first_t; // the first time we saw this
84 uint32_t last_t; // the last time we saw this
85 uint32_t usages; // how many times this has been accessed
87 typedef struct machine MACHINE;
90 // ----------------------------------------------------------------------------
93 // for each PERSON-URL pair we keep this
95 URL *url; // de-duplicated URL
99 uint32_t first_t; // the first time we saw this
100 uint32_t last_t; // the last time we saw this
101 uint32_t usages; // how many times this has been accessed
103 typedef struct person_url PERSON_URL;
109 DICTIONARY *urls; // PERSON_URL *
111 uint32_t first_t; // the first time we saw this
112 uint32_t last_t; // the last time we saw this
113 uint32_t usages; // how many times this has been accessed
115 typedef struct person PERSON;
117 extern PERSON *registry_request(const char *person_guid, const char *machine_guid, const char *url, time_t when);
119 // ----------------------------------------------------------------------------
122 static inline URL *registry_url_allocate(const char *url) {
123 size_t len = strlen(url);
125 debug(D_REGISTRY, "Registry: registry_url_allocate('%s'): allocating %zu bytes", url, sizeof(URL) + len);
126 URL *u = malloc(sizeof(URL) + len);
127 if(!u) fatal("Cannot allocate %zu bytes for URL '%s'", sizeof(URL) + len);
133 debug(D_REGISTRY, "Registry: registry_url_allocate('%s'): indexing it", url);
134 dictionary_set(registry.urls, u->url, u, sizeof(URL));
139 static inline URL *registry_url_get(const char *url) {
140 debug(D_REGISTRY, "Registry: registry_url_get('%s')", url);
142 URL *u = dictionary_get(registry.urls, url);
144 u = registry_url_allocate(url);
145 registry.urls_count++;
151 static inline void registry_url_link(URL *u) {
153 debug(D_REGISTRY, "Registry: registry_url_unlink('%s'): This URL has now %u links", u->url, u->links);
156 static inline void registry_url_unlink(URL *u) {
159 debug(D_REGISTRY, "Registry: registry_url_unlink('%s'): No more links for this URL", u->url);
160 dictionary_del(registry.urls, u->url);
164 debug(D_REGISTRY, "Registry: registry_url_unlink('%s'): This URL has %u links left", u->url, u->links);
168 // ----------------------------------------------------------------------------
171 static inline MACHINE *registry_machine_find(const char *machine_guid) {
172 debug(D_REGISTRY, "Registry: registry_machine_find('%s')", machine_guid);
173 return dictionary_get(registry.machines, machine_guid);
176 static inline MACHINE_URL *registry_machine_url_allocate(MACHINE *m, URL *u, time_t when) {
177 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(MACHINE_URL));
179 MACHINE_URL *mu = malloc(sizeof(MACHINE_URL));
180 if(!mu) fatal("registry_machine_link_to_url('%s', '%s'): cannot allocate %zu bytes.", m->guid, u->url, sizeof(MACHINE_URL));
182 // mu->persons = dictionary_create(DICTIONARY_FLAGS);
183 // dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
185 mu->first_t = mu->last_t = when;
188 mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
190 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): indexing URL in machine", m->guid, u->url);
191 dictionary_set(m->urls, u->url, mu, sizeof(MACHINE_URL));
192 registry_url_link(u);
197 static inline MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
198 debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(MACHINE));
200 MACHINE *m = calloc(1, sizeof(MACHINE));
201 if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
203 strncpy(m->guid, machine_guid, 36);
205 debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
206 m->urls = dictionary_create(DICTIONARY_FLAGS);
208 m->first_t = m->last_t = when;
211 dictionary_set(registry.machines, m->guid, m, sizeof(MACHINE));
216 // 1. validate machine GUID
217 // 2. if it is valid, find it or create it and return it
218 // 3. if it is not valid, return NULL
219 static inline MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
222 if(likely(machine_guid && *machine_guid)) {
223 // validate it is a GUID
225 if(uuid_parse(machine_guid, uuid) == -1) {
226 info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
230 uuid_unparse_lower(uuid, buf);
231 if(strcmp(machine_guid, buf))
232 info("Registry: machine guid '%s' and re-generated '%s' differ!", machine_guid, buf);
235 m = registry_machine_find(machine_guid);
237 m = registry_machine_allocate(machine_guid, when);
238 registry.machines_count++;
247 // ----------------------------------------------------------------------------
250 static inline PERSON *registry_person_find(const char *person_guid) {
251 debug(D_REGISTRY, "Registry: registry_person_find('%s')", person_guid);
252 return dictionary_get(registry.persons, person_guid);
255 static inline PERSON_URL *registry_person_url_allocate(PERSON *p, MACHINE *m, URL *u, time_t when) {
256 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): allocating %zu bytes", p->guid, m->guid, u->url, sizeof(PERSON_URL));
257 PERSON_URL *pu = malloc(sizeof(PERSON_URL));
258 if(!pu) fatal("registry_person_link_to_url('%s', '%s', '%s'): cannot allocate %zu bytes.", p->guid, m->guid, u->url, sizeof(PERSON_URL));
261 pu->first_t = pu->last_t = when;
264 pu->flags = REGISTRY_URL_FLAGS_DEFAULT;
266 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): indexing URL in person", p->guid, m->guid, u->url);
267 dictionary_set(p->urls, u->url, pu, sizeof(PERSON_URL));
268 registry_url_link(u);
273 static inline PERSON *registry_person_allocate(const char *person_guid, time_t when) {
276 debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(PERSON));
278 p = calloc(1, sizeof(PERSON));
279 if(!p) fatal("Registry: cannot allocate memory for new person.");
284 if (uuid_generate_time_safe(uuid) == -1)
285 info("Registry: uuid_generate_time_safe() reports UUID generation is not safe for uniqueness.");
287 uuid_unparse_lower(uuid, p->guid);
289 debug(D_REGISTRY, "Registry: Checking if the generated person guid '%s' is unique", p->guid);
290 if (!dictionary_get(registry.persons, p->guid)) {
291 debug(D_REGISTRY, "Registry: generated person guid '%s' is unique", p->guid);
295 info("Registry: generated person guid '%s' found in the registry. Retrying...", p->guid);
299 strncpy(p->guid, person_guid, 36);
303 debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): creating dictionary of urls", p->guid);
304 p->urls = dictionary_create(DICTIONARY_FLAGS);
306 p->first_t = p->last_t = when;
309 dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
314 // 1. validate person GUID
315 // 2. if it is valid, find it
316 // 3. if it is not valid, create a new one
318 static inline PERSON *registry_person_get(const char *person_guid, time_t when) {
321 if(person_guid && *person_guid) {
322 // validate it is a GUID
324 if(uuid_parse(person_guid, uuid) == -1) {
325 info("Registry: person guid '%s' is not a valid guid. Ignoring it.", person_guid);
329 uuid_unparse_lower(uuid, buf);
330 if(strcmp(person_guid, buf))
331 info("Registry: person guid '%s' and re-generated '%s' differ!", person_guid, buf);
334 p = registry_person_find(person_guid);
335 if(!p) person_guid = NULL;
340 p = registry_person_allocate(NULL, when);
341 registry.persons_count++;
347 // ----------------------------------------------------------------------------
348 // LINKING OF OBJECTS
350 static inline PERSON_URL *registry_person_link_to_url(PERSON *p, MACHINE *m, URL *u, time_t when) {
351 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): searching for URL in person", p->guid, m->guid, u->url);
353 PERSON_URL *pu = dictionary_get(p->urls, u->url);
355 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
356 pu = registry_person_url_allocate(p, m, u, when);
357 registry.persons_urls_count++;
360 debug(D_REGISTRY, "registry_person_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
363 if(pu->machine != m) {
364 MACHINE_URL *mu = dictionary_get(pu->machine->urls, u->url);
366 info("registry_person_link_to_url('%s', '%s', '%s'): URL switched machines (old was '%s') - expiring it from previous machine.",
367 p->guid, m->guid, u->url, pu->machine->guid);
368 mu->flags |= REGISTRY_URL_FLAGS_EXPIRED;
378 if(pu->flags & REGISTRY_URL_FLAGS_EXPIRED)
379 info("registry_person_link_to_url('%s', '%s', '%s'): accessing an expired URL.", p->guid, m->guid, u->url);
384 static inline MACHINE_URL *registry_machine_link_to_url(PERSON *p, MACHINE *m, URL *u, time_t when) {
385 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): searching for URL in machine", p->guid, m->guid, u->url);
387 MACHINE_URL *mu = dictionary_get(m->urls, u->url);
389 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): not found", p->guid, m->guid, u->url);
390 mu = registry_machine_url_allocate(m, u, when);
391 registry.machines_urls_count++;
394 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): found", p->guid, m->guid, u->url);
398 //debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s', '%s'): indexing person in machine", p->guid, m->guid, u->url);
399 //dictionary_set(mu->persons, p->guid, p, sizeof(PERSON));
404 if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED)
405 info("registry_person_link_to_url('%s', '%s', '%s'): accessing an expired URL.", p->guid, m->guid, u->url);
410 // ----------------------------------------------------------------------------
411 // REGISTRY LOG LOAD/SAVE
413 static inline void registry_log(const char action, PERSON *p, MACHINE *m, URL *u) {
414 if(likely(registry.registry_log_fp))
415 fprintf(registry.registry_log_fp, "%c\t%08x\t%s\t%s\t%s\n",
424 void registry_log_open(void) {
425 registry.registry_log_fp = fopen(registry.log_filename, "a");
427 if(registry.registry_log_fp) {
428 if (setvbuf(registry.registry_log_fp, NULL, _IOLBF, 0) != 0)
429 error("Cannot set line buffering on registry log file.");
433 void registry_log_close(void) {
434 if(registry.registry_log_fp)
435 fclose(registry.registry_log_fp);
437 registry.registry_log_fp = NULL;
440 void registry_log_recreate(void) {
441 if(registry.registry_log_fp != NULL) {
442 fclose(registry.registry_log_fp);
444 registry.registry_log_fp = fopen(registry.log_filename, "w");
445 if(registry.registry_log_fp) fclose(registry.registry_log_fp);
446 registry.registry_log_fp = NULL;
452 int registry_log_load(void) {
453 char *s, buf[4096 + 1];
456 registry_log_close();
458 debug(D_REGISTRY, "Registry: loading active db from: %s", registry.log_filename);
459 FILE *fp = fopen(registry.log_filename, "r");
461 error("Registry: cannot open registry file: %s", registry.log_filename);
466 while((s = fgets_trim_len(buf, 4096, fp, &len))) {
471 // verify it is valid
472 if(unlikely(len < 85 || s[1] != '\t' || s[10] != '\t' || s[47] != '\t' || s[84] != '\t'))
473 error("Registry log line %u is wrong (len = %zu).", line, len);
475 s[1] = s[10] = s[47] = s[84] = '\0';
476 registry_request(&s[11], &s[48], &s[85], strtoul(&s[2], NULL, 16));
480 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.log_filename, s);
491 // ----------------------------------------------------------------------------
494 PERSON *registry_request(const char *person_guid, const char *machine_guid, const char *url, time_t when) {
495 debug(D_REGISTRY, "registry_request('%s', '%s', '%s'): NEW REQUEST", (person_guid)?person_guid:"", machine_guid, url);
497 MACHINE *m = registry_machine_get(machine_guid, when);
500 URL *u = registry_url_get(url);
501 PERSON *p = registry_person_get(person_guid, when);
503 registry_person_link_to_url(p, m, u, when);
504 registry_machine_link_to_url(p, m, u, when);
506 registry_log('A', p, m, u);
508 registry.usages_count++;
513 // ----------------------------------------------------------------------------
514 // REGISTRY LOAD/SAVE
516 int registry_machine_save_url(void *entry, void *file) {
517 MACHINE_URL *mu = entry;
520 debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
522 int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
533 int registry_machine_save(void *entry, void *file) {
537 debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
539 int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
547 int ret2 = dictionary_get_all(m->urls, registry_machine_save_url, fp);
548 if(ret2 < 0) return ret2;
555 static inline int registry_person_save_url(void *entry, void *file) {
556 PERSON_URL *pu = entry;
559 debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
561 int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\n",
573 static inline int registry_person_save(void *entry, void *file) {
577 debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
579 int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
587 int ret2 = dictionary_get_all(p->urls, registry_person_save_url, fp);
588 if (ret2 < 0) return ret2;
595 static int registry_save(void) {
596 char tmp_filename[FILENAME_MAX + 1];
597 char old_filename[FILENAME_MAX + 1];
599 snprintf(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
600 snprintf(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
602 debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
603 FILE *fp = fopen(tmp_filename, "w");
605 error("Registry: Cannot create file: %s", tmp_filename);
609 debug(D_REGISTRY, "Saving all machines");
610 int bytes1 = dictionary_get_all(registry.machines, registry_machine_save, fp);
612 error("Registry: Cannot save registry machines - return value %d", bytes1);
616 debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
618 debug(D_REGISTRY, "Saving all persons");
619 int bytes2 = dictionary_get_all(registry.persons, registry_person_save, fp);
621 error("Registry: Cannot save registry persons - return value %d", bytes2);
625 debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
631 // remove the .old db
632 debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
633 if(unlink(old_filename) == -1 && errno != ENOENT)
634 error("Registry: cannot remove old registry file '%s'", old_filename);
636 // rename the db to .old
637 debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
638 if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
639 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename, registry.db_filename);
642 // remove the database (it is saved in .old)
643 debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
644 if (unlink(registry.db_filename) == -1 && errno != ENOENT)
645 error("Registry: cannot remove old registry file '%s'", registry.db_filename);
647 // move the .tmp to make it active
648 debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
649 if (link(tmp_filename, registry.db_filename) == -1) {
650 error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
651 registry.db_filename);
653 // move the .old back
654 debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
655 if(link(old_filename, registry.db_filename) == -1)
656 error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
659 debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
660 if(unlink(tmp_filename) == -1)
661 error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
663 // it has been moved successfully
664 // discard the current registry log
665 registry_log_recreate();
672 static inline size_t registry_load(void) {
673 char *s, buf[4096 + 1];
679 debug(D_REGISTRY, "Registry: loading active db from: %s", registry.db_filename);
680 FILE *fp = fopen(registry.db_filename, "r");
682 error("Registry: cannot open registry file: %s", registry.db_filename);
687 while((s = fgets_trim_len(buf, 4096, fp, &len))) {
690 debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
695 // verify it is valid
696 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0'))
697 error("Registry person line %u is wrong (len = %zu).", line, len);
699 s[1] = s[10] = s[19] = s[28] = '\0';
700 p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
701 p->last_t = strtoul(&s[11], NULL, 16);
702 p->usages = strtoul(&s[20], NULL, 16);
703 debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
708 // verify it is valid
709 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0'))
710 error("Registry person line %u is wrong (len = %zu).", line, len);
712 s[1] = s[10] = s[19] = s[28] = '\0';
713 m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
714 m->last_t = strtoul(&s[11], NULL, 16);
715 m->usages = strtoul(&s[20], NULL, 16);
716 debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
719 case 'U': // person URL
721 error("Registry: ignoring line %zu, no person loaded: %s", line, s);
725 // verify it is valid
726 if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t')
727 error("Registry person URL line %u is wrong (len = %zu).", line, len);
729 s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
730 u = registry_url_allocate(&s[69]);
732 time_t first_t = strtoul(&s[2], NULL, 16);
734 m = registry_machine_find(&s[32]);
735 if(!m) m = registry_machine_allocate(&s[32], first_t);
737 PERSON_URL *pu = registry_person_url_allocate(p, m, u, first_t);
738 pu->last_t = strtoul(&s[11], NULL, 16);
739 pu->usages = strtoul(&s[20], NULL, 16);
740 pu->flags = strtoul(&s[29], NULL, 16);
741 debug(D_REGISTRY, "Registry loaded person URL '%s', machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, m->guid, pu->first_t, pu->last_t, pu->usages, pu->flags);
744 case 'V': // machine URL
746 error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
750 // verify it is valid
751 if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t')
752 error("Registry person URL line %u is wrong (len = %zu).", line, len);
754 s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
755 u = registry_url_allocate(&s[32]);
757 MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
758 mu->last_t = strtoul(&s[11], NULL, 16);
759 mu->usages = strtoul(&s[20], NULL, 16);
760 mu->flags = strtoul(&s[29], NULL, 16);
761 debug(D_REGISTRY, "Registry loaded machine URL '%s', machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, m->guid, mu->first_t, mu->last_t, mu->usages, mu->flags);
765 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
776 // ----------------------------------------------------------------------------
779 void registry_init(void) {
780 char filename[FILENAME_MAX + 1];
782 registry.pathname = config_get("registry", "registry db directory", VARLIB_DIR);
783 if(mkdir(registry.pathname, 0644) == -1 && errno != EEXIST)
784 error("Cannot create directory '%s'", registry.pathname);
786 snprintf(filename, FILENAME_MAX, "%s/%s", registry.pathname, "registry.db");
787 registry.db_filename = config_get("registry", "registry db file", filename);
789 snprintf(filename, FILENAME_MAX, "%s/%s", registry.pathname, "registry-log.db");
790 registry.log_filename = config_get("registry", "registry log file", filename);
792 registry.persons_count = 0;
793 registry.machines_count = 0;
794 registry.usages_count = 0;
795 registry.urls_count = 0;
796 registry.persons_urls_count = 0;
797 registry.machines_urls_count = 0;
799 debug(D_REGISTRY, "Registry: creating global registry dictionary for persons.");
800 registry.persons = dictionary_create(DICTIONARY_FLAGS);
802 debug(D_REGISTRY, "Registry: creating global registry dictionary for machines.");
803 registry.machines = dictionary_create(DICTIONARY_FLAGS);
805 debug(D_REGISTRY, "Registry: creating global registry dictionary for urls.");
806 registry.urls = dictionary_create(DICTIONARY_FLAGS);
812 void registry_free(void) {
813 while(registry.persons->values_index.root) {
814 PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
816 // fprintf(stderr, "\nPERSON: '%s', first: %u, last: %u, usages: %u\n", p->guid, p->first_t, p->last_t, p->usages);
818 while(p->urls->values_index.root) {
819 PERSON_URL *pu = ((NAME_VALUE *)p->urls->values_index.root)->value;
821 // fprintf(stderr, "\tURL: '%s', first: %u, last: %u, usages: %u, flags: 0x%02x\n", pu->url->url, pu->first_t, pu->last_t, pu->usages, pu->flags);
823 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", pu->url->url, p->guid);
824 dictionary_del(p->urls, pu->url->url);
826 debug(D_REGISTRY, "Registry: unlinking url '%s' from person", pu->url->url);
827 registry_url_unlink(pu->url);
829 debug(D_REGISTRY, "Registry: freeing person url");
833 debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
834 dictionary_del(registry.persons, p->guid);
836 debug(D_REGISTRY, "Registry: destroying URL dictionary of person '%s'", p->guid);
837 dictionary_destroy(p->urls);
839 debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
843 while(registry.machines->values_index.root) {
844 MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
846 // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
848 while(m->urls->values_index.root) {
849 MACHINE_URL *mu = ((NAME_VALUE *)m->urls->values_index.root)->value;
851 // fprintf(stderr, "\tURL: '%s', first: %u, last: %u, usages: %u, flags: 0x%02x\n", mu->url->url, mu->first_t, mu->last_t, mu->usages, mu->flags);
853 //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
854 //dictionary_destroy(mu->persons);
856 debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
857 dictionary_del(m->urls, mu->url->url);
859 debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
860 registry_url_unlink(mu->url);
862 debug(D_REGISTRY, "Registry: freeing machine url");
866 debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
867 dictionary_del(registry.machines, m->guid);
869 debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
870 dictionary_destroy(m->urls);
872 debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
876 debug(D_REGISTRY, "Registry: destroying persons dictionary");
877 dictionary_destroy(registry.persons);
879 debug(D_REGISTRY, "Registry: destroying machines dictionary");
880 dictionary_destroy(registry.machines);
882 debug(D_REGISTRY, "Registry: destroying urls dictionary");
883 dictionary_destroy(registry.urls);
887 // ----------------------------------------------------------------------------
890 int test1(int argc, char **argv) {
892 void print_stats(uint32_t requests, unsigned long long start, unsigned long long end) {
893 fprintf(stderr, " > SPEED: %u requests served in %0.2f seconds ( >>> %llu per second <<< )\n",
894 requests, (end-start) / 1000000.0, (unsigned long long)requests * 1000000ULL / (end-start));
896 fprintf(stderr, " > DB : persons %llu, machines %llu, unique URLs %llu, accesses %llu, URLs: for persons %llu, for machines %llu\n",
897 registry.persons_count, registry.machines_count, registry.urls_count, registry.usages_count,
898 registry.persons_urls_count, registry.machines_urls_count);
904 uint32_t u, users = 1000000;
905 uint32_t m, machines = 200000;
906 uint32_t machines2 = machines * 2;
908 char **users_guids = malloc(users * sizeof(char *));
909 char **machines_guids = malloc(machines2 * sizeof(char *));
910 char **machines_urls = malloc(machines2 * sizeof(char *));
911 unsigned long long start;
915 fprintf(stderr, "Generating %u machine guids\n", machines2);
916 for(m = 0; m < machines2 ;m++) {
918 machines_guids[m] = malloc(36+1);
920 uuid_unparse(uuid, machines_guids[m]);
922 char buf[FILENAME_MAX + 1];
923 snprintf(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
924 machines_urls[m] = strdup(buf);
926 // fprintf(stderr, "\tmachine %u: '%s', url: '%s'\n", m + 1, machines_guids[m], machines_urls[m]);
930 fprintf(stderr, "\nGenerating %u users accessing %u machines\n", users, machines);
932 time_t now = time(NULL);
933 for(u = 0; u < users ; u++) {
934 if(++m == machines) m = 0;
936 PERSON *p = registry_request(NULL, machines_guids[m], machines_urls[m], now);
937 users_guids[u] = p->guid;
939 print_stats(u, start, timems());
942 fprintf(stderr, "\nAll %u users accessing again the same %u servers\n", users, machines);
945 for(u = 0; u < users ; u++) {
946 if(++m == machines) m = 0;
948 PERSON *p = registry_request(users_guids[u], machines_guids[m], machines_urls[m], now);
950 if(p->guid != users_guids[u])
951 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
953 print_stats(u, start, timems());
956 fprintf(stderr, "\nAll %u users accessing a new server, out of the %u servers\n", users, machines);
959 for(u = 0; u < users ; u++) {
960 if(++m == machines) m = 0;
962 PERSON *p = registry_request(users_guids[u], machines_guids[m], machines_urls[m], now);
964 if(p->guid != users_guids[u])
965 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
967 print_stats(u, start, timems());
970 fprintf(stderr, "\n%u random users accessing a random server, out of the %u servers\n", users, machines);
972 for(u = 0; u < users ; u++) {
973 uint32_t tu = random() * users / RAND_MAX;
974 uint32_t tm = random() * machines / RAND_MAX;
976 PERSON *p = registry_request(users_guids[tu], machines_guids[tm], machines_urls[tm], now);
978 if(p->guid != users_guids[tu])
979 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
981 print_stats(u, start, timems());
984 fprintf(stderr, "\n%u random users accessing a random server, out of %u servers\n", users, machines2);
986 for(u = 0; u < users ; u++) {
987 uint32_t tu = random() * users / RAND_MAX;
988 uint32_t tm = random() * machines2 / RAND_MAX;
990 PERSON *p = registry_request(users_guids[tu], machines_guids[tm], machines_urls[tm], now);
992 if(p->guid != users_guids[tu])
993 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
995 print_stats(u, start, timems());
997 for(m = 0; m < 10; m++) {
1000 "\n%u random user accesses to a random server, out of %u servers,\n > using 1/10000 with a random url, 1/1000 with a mismatched url\n",
1001 users * 2, machines2);
1003 for (u = 0; u < users * 2; u++) {
1004 uint32_t tu = random() * users / RAND_MAX;
1005 uint32_t tm = random() * machines2 / RAND_MAX;
1007 char *url = machines_urls[tm];
1008 char buf[FILENAME_MAX + 1];
1009 if (random() % 10000 == 1234) {
1010 snprintf(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
1013 else if (random() % 1000 == 123)
1014 url = machines_urls[random() * machines2 / RAND_MAX];
1016 PERSON *p = registry_request(users_guids[tu], machines_guids[tm], url, now);
1018 if (p->guid != users_guids[tu])
1019 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
1021 print_stats(u, start, timems());
1024 fprintf(stderr, "\n\nSAVE\n");
1027 print_stats(registry.persons_count, start, timems());
1029 fprintf(stderr, "\n\nCLEANUP\n");
1032 print_stats(registry.persons_count, start, timems());
1036 // ----------------------------------------------------------------------------
1039 int main(int argc, char **argv) {
1040 debug_flags = 0xFFFFFFFF;
1041 //test1(argc, argv);
1050 fprintf(stderr, "\n\nINITIALIZATION\n");
1056 fprintf(stderr, "\n\nADDING ENTRY\n");
1057 // p1 = registry_request("2c95abd0-1542-11e6-8c66-00508db7e9c9", "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", time(NULL));
1061 #ifdef REGISTRY_STDOUT_DUMP
1062 fprintf(stderr, "\n\nADDING ENTRY\n");
1063 #endif /* REGISTRY_STDOUT_DUMP */
1064 p1 = registry_request(NULL, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", time(NULL));
1066 #ifdef REGISTRY_STDOUT_DUMP
1067 fprintf(stderr, "\n\nADDING ANOTHER URL\n");
1068 #endif /* REGISTRY_STDOUT_DUMP */
1069 p1 = registry_request(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://127.0.0.1:19999/", time(NULL));
1071 #ifdef REGISTRY_STDOUT_DUMP
1072 fprintf(stderr, "\n\nADDING ANOTHER URL\n");
1073 #endif /* REGISTRY_STDOUT_DUMP */
1074 p1 = registry_request(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", time(NULL));
1076 #ifdef REGISTRY_STDOUT_DUMP
1077 fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
1078 #endif /* REGISTRY_STDOUT_DUMP */
1079 p1 = registry_request(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", time(NULL));
1081 #ifdef REGISTRY_STDOUT_DUMP
1082 fprintf(stderr, "\n\nADDING ANOTHER PERSON\n");
1083 #endif /* REGISTRY_STDOUT_DUMP */
1084 p2 = registry_request(NULL, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", time(NULL));
1086 #ifdef REGISTRY_STDOUT_DUMP
1087 fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
1088 #endif /* REGISTRY_STDOUT_DUMP */
1089 p2 = registry_request(p2->guid, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", time(NULL));
1092 fprintf(stderr, "\n\nSAVE\n");
1095 fprintf(stderr, "\n\nCLEANUP\n");