]> arthur.barton.de Git - netdata.git/blob - src/registry_db.c
removed over-optimization at the registry to simplify its logic and remove race condi...
[netdata.git] / src / registry_db.c
1 #include "registry_internals.h"
2
3 int registry_db_should_be_saved(void) {
4     debug(D_REGISTRY, "log entries %llu, max %llu", registry.log_count, registry.save_registry_every_entries);
5     return registry.log_count > registry.save_registry_every_entries;
6 }
7
8 // ----------------------------------------------------------------------------
9 // INTERNAL FUNCTIONS FOR SAVING REGISTRY OBJECTS
10
11 static int registry_machine_save_url(void *entry, void *file) {
12     REGISTRY_MACHINE_URL *mu = entry;
13     FILE *fp = file;
14
15     debug(D_REGISTRY, "Registry: registry_machine_save_url('%s')", mu->url->url);
16
17     int ret = fprintf(fp, "V\t%08x\t%08x\t%08x\t%02x\t%s\n",
18             mu->first_t,
19             mu->last_t,
20             mu->usages,
21             mu->flags,
22             mu->url->url
23     );
24
25     // error handling is done at registry_db_save()
26
27     return ret;
28 }
29
30 static int registry_machine_save(void *entry, void *file) {
31     REGISTRY_MACHINE *m = entry;
32     FILE *fp = file;
33
34     debug(D_REGISTRY, "Registry: registry_machine_save('%s')", m->guid);
35
36     int ret = fprintf(fp, "M\t%08x\t%08x\t%08x\t%s\n",
37             m->first_t,
38             m->last_t,
39             m->usages,
40             m->guid
41     );
42
43     if(ret >= 0) {
44         int ret2 = dictionary_get_all(m->machine_urls, registry_machine_save_url, fp);
45         if(ret2 < 0) return ret2;
46         ret += ret2;
47     }
48
49     // error handling is done at registry_db_save()
50
51     return ret;
52 }
53
54 static inline int registry_person_save_url(void *entry, void *file) {
55     REGISTRY_PERSON_URL *pu = entry;
56     FILE *fp = file;
57
58     debug(D_REGISTRY, "Registry: registry_person_save_url('%s')", pu->url->url);
59
60     int ret = fprintf(fp, "U\t%08x\t%08x\t%08x\t%02x\t%s\t%s\t%s\n",
61             pu->first_t,
62             pu->last_t,
63             pu->usages,
64             pu->flags,
65             pu->machine->guid,
66             pu->machine_name,
67             pu->url->url
68     );
69
70     // error handling is done at registry_db_save()
71
72     return ret;
73 }
74
75 static inline int registry_person_save(void *entry, void *file) {
76     REGISTRY_PERSON *p = entry;
77     FILE *fp = file;
78
79     debug(D_REGISTRY, "Registry: registry_person_save('%s')", p->guid);
80
81     int ret = fprintf(fp, "P\t%08x\t%08x\t%08x\t%s\n",
82             p->first_t,
83             p->last_t,
84             p->usages,
85             p->guid
86     );
87
88     if(ret >= 0) {
89         int ret2 = dictionary_get_all(p->person_urls, registry_person_save_url, fp);
90         if (ret2 < 0) return ret2;
91         ret += ret2;
92     }
93
94     // error handling is done at registry_db_save()
95
96     return ret;
97 }
98
99 // ----------------------------------------------------------------------------
100 // SAVE THE REGISTRY DATABASE
101
102 int registry_db_save(void) {
103     if(unlikely(!registry.enabled))
104         return -1;
105
106     if(unlikely(!registry_db_should_be_saved()))
107         return -2;
108
109     error_log_limit_unlimited();
110
111     char tmp_filename[FILENAME_MAX + 1];
112     char old_filename[FILENAME_MAX + 1];
113
114     snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
115     snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
116
117     debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
118     FILE *fp = fopen(tmp_filename, "w");
119     if(!fp) {
120         error("Registry: Cannot create file: %s", tmp_filename);
121         error_log_limit_reset();
122         return -1;
123     }
124
125     // dictionary_get_all() has its own locking, so this is safe to do
126
127     debug(D_REGISTRY, "Saving all machines");
128     int bytes1 = dictionary_get_all(registry.machines, registry_machine_save, fp);
129     if(bytes1 < 0) {
130         error("Registry: Cannot save registry machines - return value %d", bytes1);
131         fclose(fp);
132         error_log_limit_reset();
133         return bytes1;
134     }
135     debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
136
137     debug(D_REGISTRY, "Saving all persons");
138     int bytes2 = dictionary_get_all(registry.persons, registry_person_save, fp);
139     if(bytes2 < 0) {
140         error("Registry: Cannot save registry persons - return value %d", bytes2);
141         fclose(fp);
142         error_log_limit_reset();
143         return bytes2;
144     }
145     debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
146
147     // save the totals
148     fprintf(fp, "T\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\t%016llx\n",
149             registry.persons_count,
150             registry.machines_count,
151             registry.usages_count + 1, // this is required - it is lost on db rotation
152             registry.urls_count,
153             registry.persons_urls_count,
154             registry.machines_urls_count
155     );
156
157     fclose(fp);
158
159     errno = 0;
160
161     // remove the .old db
162     debug(D_REGISTRY, "Registry: Removing old db '%s'", old_filename);
163     if(unlink(old_filename) == -1 && errno != ENOENT)
164         error("Registry: cannot remove old registry file '%s'", old_filename);
165
166     // rename the db to .old
167     debug(D_REGISTRY, "Registry: Link current db '%s' to .old: '%s'", registry.db_filename, old_filename);
168     if(link(registry.db_filename, old_filename) == -1 && errno != ENOENT)
169         error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", registry.db_filename, old_filename);
170
171     else {
172         // remove the database (it is saved in .old)
173         debug(D_REGISTRY, "Registry: removing db '%s'", registry.db_filename);
174         if (unlink(registry.db_filename) == -1 && errno != ENOENT)
175             error("Registry: cannot remove old registry file '%s'", registry.db_filename);
176
177         // move the .tmp to make it active
178         debug(D_REGISTRY, "Registry: linking tmp db '%s' to active db '%s'", tmp_filename, registry.db_filename);
179         if (link(tmp_filename, registry.db_filename) == -1) {
180             error("Registry: cannot move file '%s' to '%s'. Saving registry DB failed!", tmp_filename,
181                     registry.db_filename);
182
183             // move the .old back
184             debug(D_REGISTRY, "Registry: linking old db '%s' to active db '%s'", old_filename, registry.db_filename);
185             if(link(old_filename, registry.db_filename) == -1)
186                 error("Registry: cannot move file '%s' to '%s'. Recovering the old registry DB failed!", old_filename, registry.db_filename);
187         }
188         else {
189             debug(D_REGISTRY, "Registry: removing tmp db '%s'", tmp_filename);
190             if(unlink(tmp_filename) == -1)
191                 error("Registry: cannot remove tmp registry file '%s'", tmp_filename);
192
193             // it has been moved successfully
194             // discard the current registry log
195             registry_log_recreate();
196             registry.log_count = 0;
197         }
198     }
199
200     // continue operations
201     error_log_limit_reset();
202
203     return -1;
204 }
205
206 // ----------------------------------------------------------------------------
207 // LOAD THE REGISTRY DATABASE
208
209 size_t registry_db_load(void) {
210     char *s, buf[4096 + 1];
211     REGISTRY_PERSON *p = NULL;
212     REGISTRY_MACHINE *m = NULL;
213     REGISTRY_URL *u = NULL;
214     size_t line = 0;
215
216     debug(D_REGISTRY, "Registry: loading active db from: '%s'", registry.db_filename);
217     FILE *fp = fopen(registry.db_filename, "r");
218     if(!fp) {
219         error("Registry: cannot open registry file: '%s'", registry.db_filename);
220         return 0;
221     }
222
223     size_t len = 0;
224     buf[4096] = '\0';
225     while((s = fgets_trim_len(buf, 4096, fp, &len))) {
226         line++;
227
228         debug(D_REGISTRY, "Registry: read line %zu to length %zu: %s", line, len, s);
229         switch(*s) {
230             case 'T': // totals
231                 if(unlikely(len != 103 || s[1] != '\t' || s[18] != '\t' || s[35] != '\t' || s[52] != '\t' || s[69] != '\t' || s[86] != '\t' || s[103] != '\0')) {
232                     error("Registry totals line %zu is wrong (len = %zu).", line, len);
233                     continue;
234                 }
235                 registry.persons_count = strtoull(&s[2], NULL, 16);
236                 registry.machines_count = strtoull(&s[19], NULL, 16);
237                 registry.usages_count = strtoull(&s[36], NULL, 16);
238                 registry.urls_count = strtoull(&s[53], NULL, 16);
239                 registry.persons_urls_count = strtoull(&s[70], NULL, 16);
240                 registry.machines_urls_count = strtoull(&s[87], NULL, 16);
241                 break;
242
243             case 'P': // person
244                 m = NULL;
245                 // verify it is valid
246                 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
247                     error("Registry person line %zu is wrong (len = %zu).", line, len);
248                     continue;
249                 }
250
251                 s[1] = s[10] = s[19] = s[28] = '\0';
252                 p = registry_person_allocate(&s[29], strtoul(&s[2], NULL, 16));
253                 p->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
254                 p->usages = (uint32_t)strtoul(&s[20], NULL, 16);
255                 debug(D_REGISTRY, "Registry loaded person '%s', first: %u, last: %u, usages: %u", p->guid, p->first_t, p->last_t, p->usages);
256                 break;
257
258             case 'M': // machine
259                 p = NULL;
260                 // verify it is valid
261                 if(unlikely(len != 65 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[65] != '\0')) {
262                     error("Registry person line %zu is wrong (len = %zu).", line, len);
263                     continue;
264                 }
265
266                 s[1] = s[10] = s[19] = s[28] = '\0';
267                 m = registry_machine_allocate(&s[29], strtoul(&s[2], NULL, 16));
268                 m->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
269                 m->usages = (uint32_t)strtoul(&s[20], NULL, 16);
270                 debug(D_REGISTRY, "Registry loaded machine '%s', first: %u, last: %u, usages: %u", m->guid, m->first_t, m->last_t, m->usages);
271                 break;
272
273             case 'U': // person URL
274                 if(unlikely(!p)) {
275                     error("Registry: ignoring line %zu, no person loaded: %s", line, s);
276                     continue;
277                 }
278
279                 // verify it is valid
280                 if(len < 69 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t' || s[68] != '\t') {
281                     error("Registry person URL line %zu is wrong (len = %zu).", line, len);
282                     continue;
283                 }
284
285                 s[1] = s[10] = s[19] = s[28] = s[31] = s[68] = '\0';
286
287                 // skip the name to find the url
288                 char *url = &s[69];
289                 while(*url && *url != '\t') url++;
290                 if(!*url) {
291                     error("Registry person URL line %zu does not have a url.", line);
292                     continue;
293                 }
294                 *url++ = '\0';
295
296                 // u = registry_url_allocate_nolock(url, strlen(url));
297                 u = registry_url_get(url, strlen(url));
298
299                 time_t first_t = strtoul(&s[2], NULL, 16);
300
301                 m = registry_machine_find(&s[32]);
302                 if(!m) m = registry_machine_allocate(&s[32], first_t);
303
304                 REGISTRY_PERSON_URL *pu = registry_person_url_allocate(p, m, u, &s[69], strlen(&s[69]), first_t);
305                 pu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
306                 pu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
307                 pu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
308                 debug(D_REGISTRY, "Registry loaded person URL '%s' with name '%s' of machine '%s', first: %u, last: %u, usages: %u, flags: %02x", u->url, pu->machine_name, m->guid, pu->first_t, pu->last_t, pu->usages, pu->flags);
309                 break;
310
311             case 'V': // machine URL
312                 if(unlikely(!m)) {
313                     error("Registry: ignoring line %zu, no machine loaded: %s", line, s);
314                     continue;
315                 }
316
317                 // verify it is valid
318                 if(len < 32 || s[1] != '\t' || s[10] != '\t' || s[19] != '\t' || s[28] != '\t' || s[31] != '\t') {
319                     error("Registry person URL line %zu is wrong (len = %zu).", line, len);
320                     continue;
321                 }
322
323                 s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
324                 // u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
325                 u = registry_url_get(&s[32], strlen(&s[32]));
326
327                 REGISTRY_MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
328                 mu->last_t = (uint32_t)strtoul(&s[11], NULL, 16);
329                 mu->usages = (uint32_t)strtoul(&s[20], NULL, 16);
330                 mu->flags = (uint8_t)strtoul(&s[29], NULL, 16);
331                 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);
332                 break;
333
334             default:
335                 error("Registry: ignoring line %zu of filename '%s': %s.", line, registry.db_filename, s);
336                 break;
337         }
338     }
339     fclose(fp);
340
341     return line;
342 }