@cryptotaxi247 / netdata-1 / commits / 6acc6a3e9

Optimize linking of foreach alarms to dimensions. (#12813)

* Optimize linking of foreach alarms to dimensions. Keep the write-lock on host but use read-lock for charts because it's easy to verify that they aren't modified by the linking of foreach alarms to dimensions. * Protect alarm log modifications with write-lock.

vkalintiris committed May 4, 2022 at 22:00 UTC 6acc6a3e9c55c8a21c20e5d574693ae8bb3c367b
2 files changed +24 -20
database/rrddim.c
+3
@@ -179,7 +179,10 @@ void rrdcalc_link_to_rrddim(RRDDIM *rd, RRDSET *st, RRDHOST *host) {
179 continue;
180 }
181
182 + netdata_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
183 RRDCALC *child = rrdcalc_create_from_rrdcalc(rrdc, host, name, rd->name);
184 + netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
185 +
186 if (child) {
187 rrdcalc_add_to_host(host, child);
188 RRDCALC *rdcmp = (RRDCALC *) avl_insert_lock(&(host)->alarms_idx_health_log,(avl_t *)child);
health/health.c
+21 -20
@@ -453,9 +453,11 @@ static inline void health_alarm_log_process(RRDHOST *host) {
453 // remember this for the next iteration
454 host->health_last_processed_id = first_waiting;
455
456 + bool cleanup_excess_log_entries = host->health_log.count > host->health_log.max;
457 +
458 netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
459
458 - if(host->health_log.count <= host->health_log.max)
460 + if (!cleanup_excess_log_entries)
461 return;
462
463 // cleanup excess entries in the log
@@ -653,35 +655,34 @@ static int update_disabled_silenced(RRDHOST *host, RRDCALC *rc) {
655 // Create alarms for dimensions that have been added to charts
656 // since the previous iteration.
657 static void init_pending_foreach_alarms(RRDHOST *host) {
656 - rrdhost_wrlock(host);
658 + RRDSET *st;
659 + RRDDIM *rd;
660
658 - if (host->alarms_with_foreach || host->alarms_template_with_foreach) {
659 - if (rrdhost_flag_check(host, RRDHOST_FLAG_PENDING_FOREACH_ALARMS)) {
660 - RRDSET *st;
661 + if (!rrdhost_flag_check(host, RRDHOST_FLAG_PENDING_FOREACH_ALARMS))
662 + return;
663
662 - rrdset_foreach_read(st, host) {
663 - rrdset_wrlock(st);
664 + rrdhost_wrlock(host);
665
665 - if (rrdset_flag_check(st, RRDSET_FLAG_PENDING_FOREACH_ALARMS)) {
666 - RRDDIM *rd;
666 + rrdset_foreach_write(st, host) {
667 + if (!rrdset_flag_check(st, RRDSET_FLAG_PENDING_FOREACH_ALARMS))
668 + continue;
669
668 - rrddim_foreach_write(rd, st) {
669 - if (rrddim_flag_check(rd, RRDDIM_FLAG_PENDING_FOREACH_ALARM)) {
670 - rrdcalc_link_to_rrddim(rd, st, host);
671 - rrddim_flag_clear(rd, RRDDIM_FLAG_PENDING_FOREACH_ALARM);
672 - }
673 - }
670 + rrdset_rdlock(st);
671
675 - rrdset_flag_clear(st, RRDSET_FLAG_PENDING_FOREACH_ALARMS);
676 - }
672 + rrddim_foreach_read(rd, st) {
673 + if (!rrddim_flag_check(rd, RRDDIM_FLAG_PENDING_FOREACH_ALARM))
674 + continue;
675
678 - rrdset_unlock(st);
679 - }
676 + rrdcalc_link_to_rrddim(rd, st, host);
677
681 - rrdhost_flag_clear(host, RRDHOST_FLAG_PENDING_FOREACH_ALARMS);
678 + rrddim_flag_clear(rd, RRDDIM_FLAG_PENDING_FOREACH_ALARM);
679 }
680 +
681 + rrdset_flag_clear(st, RRDSET_FLAG_PENDING_FOREACH_ALARMS);
682 + rrdset_unlock(st);
683 }
684
685 + rrdhost_flag_clear(host, RRDHOST_FLAG_PENDING_FOREACH_ALARMS);
686 rrdhost_unlock(host);
687 }
688