]> arthur.barton.de Git - netdata.git/blob - src/registry_init.c
lower registry memory requirements by eliminating DICTIONARY structures for PERSON_URLs
[netdata.git] / src / registry_init.c
1 #include "registry_internals.h"
2
3 int registry_init(void) {
4     char filename[FILENAME_MAX + 1];
5
6     // registry enabled?
7     registry.enabled = config_get_boolean("registry", "enabled", 0);
8
9     // pathnames
10     registry.pathname = config_get("registry", "registry db directory", VARLIB_DIR "/registry");
11     if(mkdir(registry.pathname, 0770) == -1 && errno != EEXIST)
12         fatal("Cannot create directory '%s'.", registry.pathname);
13
14     // filenames
15     snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
16     registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
17     registry_get_this_machine_guid();
18
19     snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
20     registry.db_filename = config_get("registry", "registry db file", filename);
21
22     snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
23     registry.log_filename = config_get("registry", "registry log file", filename);
24
25     // configuration options
26     registry.save_registry_every_entries = (unsigned long long)config_get_number("registry", "registry save db every new entries", 1000000);
27     registry.persons_expiration = config_get_number("registry", "registry expire idle persons days", 365) * 86400;
28     registry.registry_domain = config_get("registry", "registry domain", "");
29     registry.registry_to_announce = config_get("registry", "registry to announce", "https://registry.my-netdata.io");
30     registry.hostname = config_get("registry", "registry hostname", config_get("global", "hostname", localhost.hostname));
31     registry.verify_cookies_redirects = config_get_boolean("registry", "verify browser cookies support", 1);
32
33     setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
34     setenv("NETDATA_REGISTRY_URL", registry.registry_to_announce, 1);
35
36     registry.max_url_length = (size_t)config_get_number("registry", "max URL length", 1024);
37     if(registry.max_url_length < 10) {
38         registry.max_url_length = 10;
39         config_set_number("registry", "max URL length", (long long)registry.max_url_length);
40     }
41
42     registry.max_name_length = (size_t)config_get_number("registry", "max URL name length", 50);
43     if(registry.max_name_length < 10) {
44         registry.max_name_length = 10;
45         config_set_number("registry", "max URL name length", (long long)registry.max_name_length);
46     }
47
48     // initialize entries counters
49     registry.persons_count = 0;
50     registry.machines_count = 0;
51     registry.usages_count = 0;
52     registry.urls_count = 0;
53     registry.persons_urls_count = 0;
54     registry.machines_urls_count = 0;
55
56     // initialize memory counters
57     registry.persons_memory = 0;
58     registry.machines_memory = 0;
59     registry.urls_memory = 0;
60     registry.persons_urls_memory = 0;
61     registry.machines_urls_memory = 0;
62
63     // initialize locks
64     pthread_mutex_init(&registry.lock, NULL);
65
66     // create dictionaries
67     registry.persons = dictionary_create(DICTIONARY_FLAGS);
68     registry.machines = dictionary_create(DICTIONARY_FLAGS);
69     avl_init(&registry.registry_urls_root_index, registry_url_compare);
70
71     // load the registry database
72     if(registry.enabled) {
73         registry_log_open();
74         registry_db_load();
75         registry_log_load();
76
77         if(unlikely(registry_db_should_be_saved()))
78             registry_db_save();
79     }
80
81     return 0;
82 }
83 /*
84 void registry_free(void) {
85     if(!registry.enabled) return;
86
87     // we need to destroy the dictionaries ourselves
88     // since the dictionaries use memory we allocated
89
90     while(registry.persons->values_index.root) {
91         REGISTRY_PERSON *p = ((NAME_VALUE *)registry.persons->values_index.root)->value;
92
93         // fprintf(stderr, "\nPERSON: '%s', first: %u, last: %u, usages: %u\n", p->guid, p->first_t, p->last_t, p->usages);
94
95         while(p->person_urls->values_index.root) {
96             REGISTRY_PERSON_URL *pu = ((NAME_VALUE *)p->person_urls->values_index.root)->value;
97
98             // 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);
99
100             debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", pu->url->url, p->guid);
101             dictionary_del(p->person_urls, pu->url->url);
102
103             debug(D_REGISTRY, "Registry: unlinking url '%s' from person", pu->url->url);
104             registry_url_unlink(pu->url);
105
106             debug(D_REGISTRY, "Registry: freeing person url");
107             freez(pu);
108         }
109
110         debug(D_REGISTRY, "Registry: deleting person '%s' from persons registry", p->guid);
111         dictionary_del(registry.persons, p->guid);
112
113         debug(D_REGISTRY, "Registry: destroying URL dictionary of person '%s'", p->guid);
114         dictionary_destroy(p->person_urls);
115
116         debug(D_REGISTRY, "Registry: freeing person '%s'", p->guid);
117         freez(p);
118     }
119
120     while(registry.machines->values_index.root) {
121         REGISTRY_MACHINE *m = ((NAME_VALUE *)registry.machines->values_index.root)->value;
122
123         // fprintf(stderr, "\nMACHINE: '%s', first: %u, last: %u, usages: %u\n", m->guid, m->first_t, m->last_t, m->usages);
124
125         while(m->machine_urls->values_index.root) {
126             REGISTRY_MACHINE_URL *mu = ((NAME_VALUE *)m->machine_urls->values_index.root)->value;
127
128             // 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);
129
130             //debug(D_REGISTRY, "Registry: destroying persons dictionary from url '%s'", mu->url->url);
131             //dictionary_destroy(mu->persons);
132
133             debug(D_REGISTRY, "Registry: deleting url '%s' from person '%s'", mu->url->url, m->guid);
134             dictionary_del(m->machine_urls, mu->url->url);
135
136             debug(D_REGISTRY, "Registry: unlinking url '%s' from machine", mu->url->url);
137             registry_url_unlink(mu->url);
138
139             debug(D_REGISTRY, "Registry: freeing machine url");
140             freez(mu);
141         }
142
143         debug(D_REGISTRY, "Registry: deleting machine '%s' from machines registry", m->guid);
144         dictionary_del(registry.machines, m->guid);
145
146         debug(D_REGISTRY, "Registry: destroying URL dictionary of machine '%s'", m->guid);
147         dictionary_destroy(m->machine_urls);
148
149         debug(D_REGISTRY, "Registry: freeing machine '%s'", m->guid);
150         freez(m);
151     }
152
153     // and free the memory of remaining dictionary structures
154
155     debug(D_REGISTRY, "Registry: destroying persons dictionary");
156     dictionary_destroy(registry.persons);
157
158     debug(D_REGISTRY, "Registry: destroying machines dictionary");
159     dictionary_destroy(registry.machines);
160 }
161 */