@cryptotaxi247 / netdata-1 / commits / 111fe53e1

prevents hosts from being removed during initialization

Costa Tsaousis (ktsaou) committed Mar 12, 2017 at 23:56 UTC 111fe53e19aa54e8a5fad4cce795c0783cc9257c
1 file changed +18 -8
src/rrdhost.c
+18 -8
@@ -91,9 +91,10 @@ RRDHOST *rrdhost_create(const char *hostname,
91 char *rrdpush_api_key,
92 int is_localhost
93 ) {
94 -
94 debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
95
96 + rrd_check_wrlock();
97 +
98 RRDHOST *host = callocz(1, sizeof(RRDHOST));
99
100 host->rrd_update_every = update_every;
@@ -208,8 +209,6 @@ RRDHOST *rrdhost_create(const char *hostname,
209 // ------------------------------------------------------------------------
210 // link it and add it to the index
211
211 - rrd_wrlock();
212 -
212 if(is_localhost) {
213 host->next = localhost;
214 localhost = host;
@@ -262,7 +261,6 @@ RRDHOST *rrdhost_create(const char *hostname,
261 }
262
263 rrd_hosts_available++;
265 - rrd_unlock();
264
265 return host;
266 }
@@ -281,6 +279,7 @@ RRDHOST *rrdhost_find_or_create(
279 ) {
280 debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
281
282 + rrd_wrlock();
283 RRDHOST *host = rrdhost_find_by_guid(guid, 0);
284 if(!host) {
285 host = rrdhost_create(
@@ -316,12 +315,24 @@ RRDHOST *rrdhost_find_or_create(
315 if(host->rrd_memory_mode != mode)
316 error("Host '%s' has memory mode '%s', but the wanted one is '%s'.", host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
317 }
318 + rrd_unlock();
319
320 rrdhost_cleanup_orphan(host);
321
322 return host;
323 }
324
325 +static inline int rrdhost_should_be_deleted(RRDHOST *host, RRDHOST *protected, time_t now) {
326 + if(host != protected
327 + && host != localhost
328 + && !host->connected_senders
329 + && host->senders_disconnected_time
330 + && host->senders_disconnected_time + rrdhost_free_orphan_time < now)
331 + return 1;
332 +
333 + return 0;
334 +}
335 +
336 void rrdhost_cleanup_orphan(RRDHOST *protected) {
337 time_t now = now_realtime_sec();
338
@@ -331,10 +342,7 @@ void rrdhost_cleanup_orphan(RRDHOST *protected) {
342
343 restart_after_removal:
344 rrdhost_foreach_write(host) {
334 - if(host != protected
335 - && host != localhost
336 - && !host->connected_senders
337 - && host->senders_disconnected_time + rrdhost_free_orphan_time < now) {
345 + if(rrdhost_should_be_deleted(host, protected, now)) {
346 info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid);
347
348 if(rrdset_flag_check(host, RRDHOST_ORPHAN))
@@ -361,6 +369,7 @@ void rrd_init(char *hostname) {
369 rrdpush_init();
370
371 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
372 + rrd_wrlock();
373 localhost = rrdhost_create(
374 hostname
375 , registry_get_this_machine_guid()
@@ -374,6 +383,7 @@ void rrd_init(char *hostname) {
383 , default_rrdpush_api_key
384 , 1
385 );
386 + rrd_unlock();
387 }
388
389 // ----------------------------------------------------------------------------