X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=src%2Frrdhost.c;h=81ff96f892415a82a80f6a2ff2f330203a8e9caa;hb=8679670bdbe3c5928ec2e266d9c72e1a758fdf37;hp=3ec71b5835c0ae4e9b56094e6b69492cd1ed0c63;hpb=68c6d537788b9c11670903bce2e9806933a3322a;p=netdata.git diff --git a/src/rrdhost.c b/src/rrdhost.c index 3ec71b58..81ff96f8 100644 --- a/src/rrdhost.c +++ b/src/rrdhost.c @@ -83,7 +83,7 @@ RRDHOST *rrdhost_create(const char *hostname, const char *guid, const char *os, int update_every, - int entries, + long entries, RRD_MEMORY_MODE memory_mode, int health_enabled, int rrdpush_enabled, @@ -91,13 +91,14 @@ RRDHOST *rrdhost_create(const char *hostname, char *rrdpush_api_key, int is_localhost ) { - debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid); + rrd_check_wrlock(); + RRDHOST *host = callocz(1, sizeof(RRDHOST)); host->rrd_update_every = update_every; - host->rrd_history_entries = entries; + host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries); host->rrd_memory_mode = memory_mode; host->health_enabled = (memory_mode == RRD_MEMORY_MODE_NONE)? 0 : health_enabled; host->rrdpush_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key); @@ -208,8 +209,6 @@ RRDHOST *rrdhost_create(const char *hostname, // ------------------------------------------------------------------------ // link it and add it to the index - rrd_wrlock(); - if(is_localhost) { host->next = localhost; localhost = host; @@ -234,7 +233,7 @@ RRDHOST *rrdhost_create(const char *hostname, ", os %s" ", update every %d" ", memory mode %s" - ", history entries %d" + ", history entries %ld" ", streaming %s" " (to '%s' with api key '%s')" ", health %s" @@ -262,7 +261,6 @@ RRDHOST *rrdhost_create(const char *hostname, } rrd_hosts_available++; - rrd_unlock(); return host; } @@ -272,7 +270,7 @@ RRDHOST *rrdhost_find_or_create( , const char *guid , const char *os , int update_every - , int history + , long history , RRD_MEMORY_MODE mode , int health_enabled , int rrdpush_enabled @@ -281,6 +279,7 @@ RRDHOST *rrdhost_find_or_create( ) { debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid); + rrd_wrlock(); RRDHOST *host = rrdhost_find_by_guid(guid, 0); if(!host) { host = rrdhost_create( @@ -311,17 +310,29 @@ RRDHOST *rrdhost_find_or_create( error("Host '%s' has an update frequency of %d seconds, but the wanted one is %d seconds.", host->hostname, host->rrd_update_every, update_every); if(host->rrd_history_entries != history) - error("Host '%s' has history of %d entries, but the wanted one is %d entries.", host->hostname, host->rrd_history_entries, history); + error("Host '%s' has history of %ld entries, but the wanted one is %ld entries.", host->hostname, host->rrd_history_entries, history); if(host->rrd_memory_mode != mode) 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)); } + rrd_unlock(); rrdhost_cleanup_orphan(host); return host; } +static inline int rrdhost_should_be_deleted(RRDHOST *host, RRDHOST *protected, time_t now) { + if(host != protected + && host != localhost + && !host->connected_senders + && host->senders_disconnected_time + && host->senders_disconnected_time + rrdhost_free_orphan_time < now) + return 1; + + return 0; +} + void rrdhost_cleanup_orphan(RRDHOST *protected) { time_t now = now_realtime_sec(); @@ -331,10 +342,7 @@ void rrdhost_cleanup_orphan(RRDHOST *protected) { restart_after_removal: rrdhost_foreach_write(host) { - if(host != protected - && host != localhost - && !host->connected_senders - && host->senders_disconnected_time + rrdhost_free_orphan_time < now) { + if(rrdhost_should_be_deleted(host, protected, now)) { info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid); if(rrdset_flag_check(host, RRDHOST_ORPHAN)) @@ -361,6 +369,7 @@ void rrd_init(char *hostname) { rrdpush_init(); debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname); + rrd_wrlock(); localhost = rrdhost_create( hostname , registry_get_this_machine_guid() @@ -374,13 +383,14 @@ void rrd_init(char *hostname) { , default_rrdpush_api_key , 1 ); + rrd_unlock(); } // ---------------------------------------------------------------------------- // RRDHOST - lock validations // there are only used when NETDATA_INTERNAL_CHECKS is set -void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) { +void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) { debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname); int ret = netdata_rwlock_trywrlock(&host->rrdhost_rwlock); @@ -388,7 +398,7 @@ void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *funct fatal("RRDHOST '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file); } -void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) { +void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) { debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname); int ret = netdata_rwlock_tryrdlock(&host->rrdhost_rwlock); @@ -396,7 +406,7 @@ void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *funct fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file); } -void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line) { +void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line) { debug(D_RRDHOST, "Checking read lock on all RRDs"); int ret = netdata_rwlock_trywrlock(&rrd_rwlock); @@ -404,7 +414,7 @@ void rrd_check_rdlock_int(const char *file, const char *function, const unsigned fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file); } -void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line) { +void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line) { debug(D_RRDHOST, "Checking write lock on all RRDs"); int ret = netdata_rwlock_tryrdlock(&rrd_rwlock);