prevent the charts from moving into the future
Costa Tsaousis (ktsaou) committed
Feb 21, 2017 at 22:26 UTC
19d0657d682c1252137c66cc54683d4b56a7c7af
4 files changed
+42
-10
src/clocks.c
+8
@@ -74,6 +74,14 @@ inline usec_t timeval_usec(struct timeval *tv) {
74
return (usec_t)tv->tv_sec * USEC_PER_SEC + tv->tv_usec;
75
}
76
77
+inline susec_t dt_usec_signed(struct timeval *now, struct timeval *old) {
78
+ usec_t ts1 = timeval_usec(now);
79
+ usec_t ts2 = timeval_usec(old);
80
+
81
+ if(likely(ts1 >= ts2)) return (susec_t)(ts1 - ts2);
82
+ return -((susec_t)(ts2 - ts1));
83
+}
84
+
85
inline usec_t dt_usec(struct timeval *now, struct timeval *old) {
86
usec_t ts1 = timeval_usec(now);
87
usec_t ts2 = timeval_usec(old);
src/clocks.h
+2
@@ -13,6 +13,7 @@ typedef int clockid_t;
13
#endif
14
15
typedef unsigned long long usec_t;
16
+typedef long long susec_t;
17
18
typedef usec_t heartbeat_t;
19
@@ -94,6 +95,7 @@ extern usec_t now_boottime_usec(void);
95
96
extern usec_t timeval_usec(struct timeval *ts);
97
extern usec_t dt_usec(struct timeval *now, struct timeval *old);
98
+extern susec_t dt_usec_signed(struct timeval *now, struct timeval *old);
99
100
extern void heartbeat_init(heartbeat_t *hb);
101
src/rrdpush.c
+6
-1
@@ -60,7 +60,7 @@ static inline void send_chart_definition(RRDSET *st) {
60
}
61
62
static inline void send_chart_metrics(RRDSET *st) {
63
- buffer_sprintf(rrdpush_buffer, "BEGIN %s %llu\n", st->id, st->usec_since_last_update);
63
+ buffer_sprintf(rrdpush_buffer, "BEGIN %s %llu\n", st->id, (st->counter_done > 60)?st->usec_since_last_update:0);
64
65
RRDDIM *rd;
66
rrddim_foreach_read(rd, st) {
@@ -83,6 +83,11 @@ static void reset_all_charts(void) {
83
84
RRDSET *st;
85
rrdset_foreach_read(st, host) {
86
+
87
+ // make it re-align the current time
88
+ // on the remote host
89
+ st->counter_done = 0;
90
+
91
rrdset_rdlock(st);
92
93
RRDDIM *rd;
src/rrdset.c
+26
-9
@@ -529,26 +529,40 @@ inline void rrdset_next_usec(RRDSET *st, usec_t microseconds) {
529
// usec_t now_usec = timeval_usec(&now);
530
// usec_t last_usec = timeval_usec(&st->last_collected_time);
531
//#endif
532
- usec_t since_last_usec = dt_usec(&now, &st->last_collected_time);
532
+ susec_t since_last_usec = dt_usec_signed(&now, &st->last_collected_time);
533
+
534
+ if(unlikely(since_last_usec < 0)) {
535
+ // oops! the database is in the future
536
+ error("Database for chart '%s' on host '%s' is %lld microseconds in the future.", st->id, st->rrdhost->hostname, -since_last_usec);
537
+ memcpy(&st->last_collected_time, &now, sizeof(struct timeval));
538
+ st->last_collected_time.tv_sec -= st->update_every;
539
+
540
+ memcpy(&st->last_updated, &now, sizeof(struct timeval));
541
+ timeval_align(&st->last_updated, st->update_every);
542
+ st->last_updated.tv_sec -= st->update_every;
543
+
544
+ microseconds = st->update_every * USEC_PER_SEC;
545
+ since_last_usec = st->update_every * USEC_PER_SEC;
546
+ }
547
548
// verify the microseconds given is good
535
- if(unlikely(microseconds > since_last_usec)) {
536
- debug(D_RRD_CALLS, "dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - since_last_usec, st->name, st->id);
549
+ if(unlikely(microseconds > (usec_t)since_last_usec)) {
550
+ debug(D_RRD_CALLS, "dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - (usec_t)since_last_usec, st->name, st->id);
551
552
//#ifdef NETDATA_INTERNAL_CHECKS
553
// if(unlikely(last_usec + microseconds > now_usec + 1000))
540
-// error("dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - since_last_usec, st->name, st->id);
554
+// error("dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - (usec_t)since_last_usec, st->name, st->id);
555
//#endif
556
543
- microseconds = since_last_usec;
557
+ microseconds = (usec_t)since_last_usec;
558
}
545
- else if(unlikely(microseconds < since_last_usec * 0.8)) {
546
- debug(D_RRD_CALLS, "dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, since_last_usec, st->name, st->id);
559
+ else if(unlikely(microseconds < (usec_t)since_last_usec * 0.8)) {
560
+ debug(D_RRD_CALLS, "dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, (usec_t)since_last_usec, st->name, st->id);
561
562
//#ifdef NETDATA_INTERNAL_CHECKS
549
-// error("dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, since_last_usec, st->name, st->id);
563
+// error("dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, (usec_t)since_last_usec, st->name, st->id);
564
//#endif
551
- microseconds = since_last_usec;
565
+ microseconds = (usec_t)since_last_usec;
566
}
567
}
568
debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
@@ -596,6 +610,9 @@ static inline void rrdset_done_push_int(RRDSET *st) {
610
}
611
612
rrdset_done_push(st);
613
+
614
+ st->counter++;
615
+ st->counter_done++;
616
}
617
618
void rrdset_done(RRDSET *st) {