X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_tsdb.c;h=99c84278906d1a9fb8b52c9a8354bb69c245bb62;hb=cb262c0e23692b6374d57ad3b3decb0d6c397949;hp=49a3b4142f417c21b1e4f01d2003f6585fef4d8f;hpb=269ee450433e15290dc50724acba62f2c58d6c36;p=collectd.git diff --git a/src/write_tsdb.c b/src/write_tsdb.c index 49a3b414..99c84278 100644 --- a/src/write_tsdb.c +++ b/src/write_tsdb.c @@ -45,8 +45,8 @@ #include "common.h" #include "plugin.h" - #include "utils_cache.h" +#include "utils_random.h" #include @@ -67,20 +67,6 @@ #define WT_SEND_BUF_SIZE 1428 #endif -/* Default configuration */ - -/* WRITE_TSDB_DEFAULT_DNS_TTL is the time we keep the dns cached info - * (seconds) - */ -#define WRITE_TSDB_DEFAULT_DNS_TTL 600 - -/* WRITE_TSDB_DEFAULT_DNS_RANDOM_TTL helps define the max random - * time we keep the dns cached info : - * min = 0 - * max = WRITE_TSDB_DEFAULT_DNS_RANDOM_TTL * get_plugin_interval() - */ -#define WRITE_TSDB_DEFAULT_DNS_RANDOM_TTL 15 - /* * Private variables */ @@ -108,8 +94,8 @@ struct wt_callback { cdtime_t next_random_ttl; }; -static cdtime_t dnsttl = TIME_T_TO_CDTIME_T_STATIC(WRITE_TSDB_DEFAULT_DNS_TTL); -static cdtime_t dnsrandomttl = 0; +static cdtime_t resolve_interval = 0; +static cdtime_t resolve_jitter = 0; /* * Functions @@ -168,12 +154,10 @@ static int wt_flush_nolock(cdtime_t timeout, struct wt_callback *cb) { } static cdtime_t new_random_ttl() { - if (dnsrandomttl == 0) + if (resolve_jitter == 0) return 0; - time_t ttl = (time_t)(CDTIME_T_TO_DOUBLE(dnsrandomttl) * ((double)random()) / - (((double)RAND_MAX) + 1.0)); - return TIME_T_TO_CDTIME_T(ttl); + return (cdtime_t)cdrand_range(0, (long)resolve_jitter); } static int wt_callback_init(struct wt_callback *cb) { @@ -194,7 +178,7 @@ static int wt_callback_init(struct wt_callback *cb) { * If there is no more attempts, we need to flush the cache. */ - if ((cb->ai_last_update + dnsttl + cb->next_random_ttl) < now) { + if ((cb->ai_last_update + resolve_interval + cb->next_random_ttl) < now) { cb->next_random_ttl = new_random_ttl(); if (cb->connect_dns_failed_attempts_remaining > 0) { /* Warning : this is run under send_lock mutex. @@ -210,7 +194,7 @@ static int wt_callback_init(struct wt_callback *cb) { } if (cb->ai == NULL) { - if ((cb->ai_last_update + dnsttl + cb->next_random_ttl) >= now) { + if ((cb->ai_last_update + resolve_interval + cb->next_random_ttl) >= now) { DEBUG("write_tsdb plugin: too many getaddrinfo(%s, %s) failures", node, service); return (-1); @@ -638,34 +622,28 @@ static int wt_config_tsd(oconfig_item_t *ci) { } static int wt_config(oconfig_item_t *ci) { - _Bool config_random_ttl = 0; + if ((resolve_interval == 0) && (resolve_jitter == 0)) + resolve_interval = resolve_jitter = plugin_get_interval(); for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp("Node", child->key) == 0) wt_config_tsd(child); - else if (strcasecmp("DNS_Cache_TTL", child->key) == 0) - cf_util_get_cdtime(child, &dnsttl); - else if (strcasecmp("DNS_Random_Cache_TTL", child->key) == 0) { - config_random_ttl = 1; - cf_util_get_cdtime(child, &dnsrandomttl); - } else { + else if (strcasecmp("ResolveInterval", child->key) == 0) + cf_util_get_cdtime(child, &resolve_interval); + else if (strcasecmp("ResolveJitter", child->key) == 0) + cf_util_get_cdtime(child, &resolve_jitter); + else { ERROR("write_tsdb plugin: Invalid configuration " "option: %s.", child->key); } } - if (!config_random_ttl) - dnsrandomttl = CDTIME_T_TO_DOUBLE(WRITE_TSDB_DEFAULT_DNS_RANDOM_TTL * - plugin_get_interval()); - return 0; } void module_register(void) { plugin_register_complex_config("write_tsdb", wt_config); } - -/* vim: set sw=4 ts=4 sts=4 tw=78 et : */