Simplify loop in alert checkpoint (#15065)
simplify loop in alert checkpoint
Emmanuel Vasilakis committed
May 19, 2023 at 11:12 UTC
51373d68116cf5d03549edd2465a038167e4afa5
1 file changed
+12
-26
database/sqlite/sqlite_aclk_alert.c
+12
-26
@@ -1038,6 +1038,7 @@ static inline int compare_active_alerts(const void * a, const void * b) {
1038
return strcmp(active_alerts_a->name, active_alerts_b->name);
1039
}
1040
1041
+#define BATCH_ALLOCATED 10
1042
void aclk_push_alarm_checkpoint(RRDHOST *host __maybe_unused)
1043
{
1044
#ifdef ENABLE_ACLK
@@ -1047,7 +1048,6 @@ void aclk_push_alarm_checkpoint(RRDHOST *host __maybe_unused)
1048
return;
1049
}
1050
1050
- //TODO: make sure all pending events are sent.
1051
if (rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS)) {
1052
//postpone checkpoint send
1053
wc->alert_checkpoint_req++;
@@ -1055,13 +1055,11 @@ void aclk_push_alarm_checkpoint(RRDHOST *host __maybe_unused)
1055
return;
1056
}
1057
1058
- //TODO: lock rc here, or make sure it's called when health decides
1059
- //count them
1058
RRDCALC *rc;
1059
uint32_t cnt = 0;
1060
size_t len = 0;
1063
- active_alerts_t *active_alerts = NULL;
1061
1062
+ active_alerts_t *active_alerts = callocz(BATCH_ALLOCATED, sizeof(active_alerts_t));
1063
foreach_rrdcalc_in_rrdhost_read(host, rc) {
1064
if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
1065
continue;
@@ -1069,33 +1067,21 @@ void aclk_push_alarm_checkpoint(RRDHOST *host __maybe_unused)
1067
if (rc->status == RRDCALC_STATUS_WARNING ||
1068
rc->status == RRDCALC_STATUS_CRITICAL) {
1069
1070
+ if (cnt && !(cnt % BATCH_ALLOCATED)) {
1071
+ active_alerts = reallocz(active_alerts, (BATCH_ALLOCATED * ((cnt / BATCH_ALLOCATED) + 1)) * sizeof(active_alerts_t));
1072
+ }
1073
+
1074
+ active_alerts[cnt].name = (char *)rrdcalc_name(rc);
1075
+ len += string_strlen(rc->name);
1076
+ active_alerts[cnt].chart = (char *)rrdcalc_chart_name(rc);
1077
+ len += string_strlen(rc->chart);
1078
+ active_alerts[cnt].status = rc->status;
1079
+ len++;
1080
cnt++;
1081
}
1082
}
1083
foreach_rrdcalc_in_rrdhost_done(rc);
1084
1077
- if (cnt) {
1078
- active_alerts = callocz(cnt, sizeof(active_alerts_t));
1079
- cnt = 0;
1080
- foreach_rrdcalc_in_rrdhost_read(host, rc) {
1081
- if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
1082
- continue;
1083
-
1084
- if (rc->status == RRDCALC_STATUS_WARNING ||
1085
- rc->status == RRDCALC_STATUS_CRITICAL) {
1086
-
1087
- active_alerts[cnt].name = (char *)rrdcalc_name(rc);
1088
- len += string_strlen(rc->name);
1089
- active_alerts[cnt].chart = (char *)rrdcalc_chart_name(rc);
1090
- len += string_strlen(rc->chart);
1091
- active_alerts[cnt].status = rc->status;
1092
- len++;
1093
- cnt++;
1094
- }
1095
- }
1096
- foreach_rrdcalc_in_rrdhost_done(rc);
1097
- }
1098
-
1085
BUFFER *alarms_to_hash;
1086
if (cnt) {
1087
qsort (active_alerts, cnt, sizeof(active_alerts_t), compare_active_alerts);