]> arthur.barton.de Git - netdata.git/blobdiff - src/backends.c
multi-host rrdset and rrdfamily operations
[netdata.git] / src / backends.c
index 1272d0473d585d01c38ab77de132025170884dbc..e1c886dc7063bb8b43952d5301b4a039cb5a1932 100644 (file)
@@ -8,9 +8,8 @@ static inline calculated_number backend_calculate_value_from_stored_data(RRDSET
     time_t first_t = rrdset_first_entry_t(st);
     time_t last_t = rrdset_last_entry_t(st);
 
-    if(unlikely(before - after < st->update_every && after != after - after % st->update_every))
-        // when st->update_every is bigger than the frequency we send data to backend
-        // skip the iterations that are not aligned to the database
+    if(unlikely(before < first_t || after > last_t))
+        // the chart has not been updated in the wanted timeframe
         return NAN;
 
     // align the time-frame
@@ -238,31 +237,31 @@ void *backends_main(void *ptr) {
             chart_backend_reconnects = 0,
             chart_backend_latency = 0;
 
-    RRDSET *chart_metrics = rrdset_find("netdata.backend_metrics");
+    RRDSET *chart_metrics = rrdset_find_localhost("netdata.backend_metrics");
     if(!chart_metrics) {
         chart_metrics = rrdset_create("netdata", "backend_metrics", NULL, "backend", NULL, "Netdata Buffered Metrics", "metrics", 130600, frequency, RRDSET_TYPE_LINE);
-        rrddim_add(chart_metrics, "buffered", NULL,  1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_metrics, "lost",     NULL,  1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_metrics, "sent",     NULL,  1, 1, RRDDIM_ABSOLUTE);
+        rrddim_add(chart_metrics, "buffered", NULL,  1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_metrics, "lost",     NULL,  1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_metrics, "sent",     NULL,  1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
     }
 
-    RRDSET *chart_bytes = rrdset_find("netdata.backend_bytes");
+    RRDSET *chart_bytes = rrdset_find_localhost("netdata.backend_bytes");
     if(!chart_bytes) {
         chart_bytes = rrdset_create("netdata", "backend_bytes", NULL, "backend", NULL, "Netdata Backend Data Size", "KB", 130610, frequency, RRDSET_TYPE_AREA);
-        rrddim_add(chart_bytes, "buffered", NULL, 1, 1024, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_bytes, "lost",     NULL, 1, 1024, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_bytes, "sent",     NULL, 1, 1024, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_bytes, "received", NULL, 1, 1024, RRDDIM_ABSOLUTE);
+        rrddim_add(chart_bytes, "buffered", NULL, 1, 1024, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_bytes, "lost",     NULL, 1, 1024, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_bytes, "sent",     NULL, 1, 1024, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_bytes, "received", NULL, 1, 1024, RRDDIM_ALGORITHM_ABSOLUTE);
     }
 
-    RRDSET *chart_ops = rrdset_find("netdata.backend_ops");
+    RRDSET *chart_ops = rrdset_find_localhost("netdata.backend_ops");
     if(!chart_ops) {
         chart_ops = rrdset_create("netdata", "backend_ops", NULL, "backend", NULL, "Netdata Backend Operations", "operations", 130630, frequency, RRDSET_TYPE_LINE);
-        rrddim_add(chart_ops, "write",     NULL, 1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_ops, "discard",   NULL, 1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_ops, "reconnect", NULL, 1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_ops, "failure",   NULL, 1, 1, RRDDIM_ABSOLUTE);
-        rrddim_add(chart_ops, "read",      NULL, 1, 1, RRDDIM_ABSOLUTE);
+        rrddim_add(chart_ops, "write",     NULL, 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_ops, "discard",   NULL, 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_ops, "reconnect", NULL, 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_ops, "failure",   NULL, 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
+        rrddim_add(chart_ops, "read",      NULL, 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
     }
 
     /*
@@ -272,18 +271,18 @@ void *backends_main(void *ptr) {
      *
      * issue #1432 and https://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html
      *
-    RRDSET *chart_latency = rrdset_find("netdata.backend_latency");
+    RRDSET *chart_latency = rrdset_find_localhost("netdata.backend_latency");
     if(!chart_latency) {
         chart_latency = rrdset_create("netdata", "backend_latency", NULL, "backend", NULL, "Netdata Backend Latency", "ms", 130620, frequency, RRDSET_TYPE_AREA);
-        rrddim_add(chart_latency, "latency",   NULL,  1, 1000, RRDDIM_ABSOLUTE);
+        rrddim_add(chart_latency, "latency",   NULL,  1, 1000, RRDDIM_ALGORITHM_ABSOLUTE);
     }
     */
 
-    RRDSET *chart_rusage = rrdset_find("netdata.backend_thread_cpu");
+    RRDSET *chart_rusage = rrdset_find_localhost("netdata.backend_thread_cpu");
     if(!chart_rusage) {
         chart_rusage = rrdset_create("netdata", "backend_thread_cpu", NULL, "backend", NULL, "NetData Backend Thread CPU usage", "milliseconds/s", 130630, frequency, RRDSET_TYPE_STACKED);
-        rrddim_add(chart_rusage, "user",   NULL, 1, 1000, RRDDIM_INCREMENTAL);
-        rrddim_add(chart_rusage, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
+        rrddim_add(chart_rusage, "user",   NULL, 1, 1000, RRDDIM_ALGORITHM_INCREMENTAL);
+        rrddim_add(chart_rusage, "system", NULL, 1, 1000, RRDDIM_ALGORITHM_INCREMENTAL);
     }
 
     // ------------------------------------------------------------------------
@@ -292,30 +291,19 @@ void *backends_main(void *ptr) {
     info("BACKEND configured ('%s' on '%s' sending '%s' data, every %d seconds, as host '%s', with prefix '%s')", type, destination, source, frequency, hostname, prefix);
 
     usec_t step_ut = frequency * USEC_PER_SEC;
-    usec_t random_ut = now_realtime_usec() % (step_ut / 2);
-    time_t before = (time_t)((now_realtime_usec() - step_ut) / USEC_PER_SEC);
-    time_t after = before;
+    time_t after = now_realtime_sec();
     int failures = 0;
+    heartbeat_t hb;
+    heartbeat_init(&hb);
 
     for(;;) {
         // ------------------------------------------------------------------------
-        // wait for the next iteration point
-
-        usec_t now_ut = now_realtime_usec();
-        usec_t next_ut = now_ut - (now_ut % step_ut) + step_ut;
-        before = (time_t)(next_ut / USEC_PER_SEC);
-
-        // add a little delay (1/4 of the step) plus some randomness
-        next_ut += (step_ut / 4) + random_ut;
-
-        while(now_ut < next_ut) {
-            sleep_usec(next_ut - now_ut);
-            now_ut = now_realtime_usec();
-        }
+        // Wait for the next iteration point.
+        heartbeat_next(&hb, step_ut);
+        time_t before = now_realtime_sec();
 
         // ------------------------------------------------------------------------
         // add to the buffer the data we need to send to the backend
-
         RRDSET *st;
         int pthreadoldcancelstate;
 
@@ -360,6 +348,10 @@ void *backends_main(void *ptr) {
         //fprintf(stderr, "\nBACKEND BEGIN:\n%s\nBACKEND END\n", buffer_tostring(b)); // FIXME
         //fprintf(stderr, "after = %lu, before = %lu\n", after, before);
 
+        // prepare for the next iteration
+        // to add incrementally data to buffer
+        after = before;
+
         // ------------------------------------------------------------------------
         // if we are connected, receive a response, without blocking
 
@@ -399,7 +391,7 @@ void *backends_main(void *ptr) {
         // if we are not connected, connect to a backend server
 
         if(unlikely(sock == -1)) {
-            usec_t start_ut = now_realtime_usec();
+            usec_t start_ut = now_monotonic_usec();
             const char *s = destination;
             while(*s) {
                 const char *e = s;
@@ -420,7 +412,7 @@ void *backends_main(void *ptr) {
                 if(sock != -1) break;
                 s = e;
             }
-            chart_backend_latency += now_realtime_usec() - start_ut;
+            chart_backend_latency += now_monotonic_usec() - start_ut;
         }
 
         if(unlikely(netdata_exit)) break;
@@ -430,14 +422,14 @@ void *backends_main(void *ptr) {
 
         if(likely(sock != -1)) {
             size_t len = buffer_strlen(b);
-            usec_t start_ut = now_realtime_usec();
+            usec_t start_ut = now_monotonic_usec();
             int flags = 0;
 #ifdef MSG_NOSIGNAL
             flags += MSG_NOSIGNAL;
 #endif
 
             ssize_t written = send(sock, buffer_tostring(b), len, flags);
-            chart_backend_latency += now_realtime_usec() - start_ut;
+            chart_backend_latency += now_monotonic_usec() - start_ut;
             if(written != -1 && (size_t)written == len) {
                 // we sent the data successfully
                 chart_transmission_successes++;
@@ -465,12 +457,6 @@ void *backends_main(void *ptr) {
                 close(sock);
                 sock = -1;
             }
-
-            // either the buffer is empty
-            // or is holding the data we couldn't send
-            // so, make sure the next iteration will continue
-            // from where we are now
-            after = before;
         }
         else {
             error("Failed to update database backend '%s'", destination);