@cryptotaxi247 / netdata-1 / commits / 7b272cbea

query engine optimizations and cleanup (#12978)

* move number unpacking close to next_metric * dont miss group value flags

Costa Tsaousis committed May 21, 2022 at 12:29 UTC 7b272cbea802e5670e5a268232ff9dcfd8a1dc0c
1 file changed +34 -18
web/api/queries/query.c
+34 -18
@@ -599,12 +599,29 @@ static inline void do_dimension_fixedstep(
599 #endif
600
601 db_now = now; // this is needed to set db_now in case the next_metric implementation does not set it
602 +
603 storage_number n;
603 - if (unlikely(rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE && now <= first_time_t))
604 + calculated_number value;
605 +
606 + if (unlikely(rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE && now <= first_time_t)) {
607 n = SN_EMPTY_SLOT;
605 - else
608 + value = NAN;
609 + }
610 + else {
611 + // load the metric value
612 n = next_metric(&handle, &db_now);
613
614 + // and unpack it
615 + if(likely(does_storage_number_exist(n))) {
616 + if (options & RRDR_OPTION_ANOMALY_BIT)
617 + value = (n & SN_ANOMALY_BIT) ? 0.0 : 100.0;
618 + else
619 + value = unpack_storage_number(n);
620 + }
621 + else
622 + value = NAN;
623 + }
624 +
625 if(unlikely(db_now > before_wanted)) {
626 #ifdef NETDATA_INTERNAL_CHECKS
627 r->internal.log = "stopped, because attempted to access the db after 'wanted before'";
@@ -613,26 +630,25 @@ static inline void do_dimension_fixedstep(
630 }
631
632 for ( ; now <= db_now ; now += dt) {
616 - calculated_number value = NAN;
633 + if(likely(does_storage_number_exist(n))) {
634
618 - if(likely(now >= db_now && does_storage_number_exist(n))) {
635 #if defined(NETDATA_INTERNAL_CHECKS) && defined(ENABLE_DBENGINE)
620 - struct rrdeng_query_handle* rrd_handle = (struct rrdeng_query_handle*)handle.handle;
621 - if ((rd->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) && (now != rrd_handle->now)) {
622 - error("INTERNAL CHECK: Unaligned query for %s, database time: %ld, expected time: %ld", rd->id, (long)rrd_handle->now, (long)now);
636 + if(now >= db_now) {
637 + struct rrdeng_query_handle *rrd_handle = (struct rrdeng_query_handle *)handle.handle;
638 + if ((rd->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) && (now != rrd_handle->now))
639 + error(
640 + "INTERNAL CHECK: Unaligned query for %s, database time: %ld, expected time: %ld",
641 + rd->id,
642 + (long)rrd_handle->now,
643 + (long)now);
644 }
645 #endif
625 - if (options & RRDR_OPTION_ANOMALY_BIT)
626 - value = (n & SN_ANOMALY_BIT) ? 0.0 : 100.0;
627 - else
628 - value = unpack_storage_number(n);
646
647 if(likely(value != 0.0))
648 values_in_group_non_zero++;
649
650 if(unlikely(did_storage_number_reset(n)))
651 group_value_flags |= RRDR_VALUE_RESET;
635 -
652 }
653
654 // add this value for grouping
@@ -657,21 +673,21 @@ static inline void do_dimension_fixedstep(
673 // store the specific point options
674 *rrdr_value_options_ptr = group_value_flags;
675
660 - // store the value
661 - value = grouping_flush(r, rrdr_value_options_ptr);
662 - r->v[rrdr_o_v_index] = value;
676 + // store the group value
677 + calculated_number group_value = grouping_flush(r, rrdr_value_options_ptr);
678 + r->v[rrdr_o_v_index] = group_value;
679
680 if(likely(points_added || dim_id_in_rrdr)) {
681 // find the min/max across all dimensions
682
667 - if(unlikely(value < min)) min = value;
668 - if(unlikely(value > max)) max = value;
683 + if(unlikely(group_value < min)) min = group_value;
684 + if(unlikely(group_value > max)) max = group_value;
685
686 }
687 else {
688 // runs only when dim_id_in_rrdr == 0 && points_added == 0
689 // so, on the first point added for the query.
674 - min = max = value;
690 + min = max = group_value;
691 }
692
693 points_added++;