@cryptotaxi247 / netdata-1 / commits / 6b893ecaf

Improve health log cleanup (#20213)

* Use a try lock in health_log cleanup * Release lock before updating an existing alarm log entry in the databse

Stelios Fragkakis committed Apr 30, 2025 at 16:42 UTC 6b893ecafb258f61835a7e539db24886874f99b7
1 file changed +10 -4
src/health/health_log.c
+10 -4
@@ -253,6 +253,7 @@ inline void health_alarm_log_add_entry(RRDHOST *host, ALARM_ENTRY *ae, bool asyn
253
254 // match previous alarms
255 rw_spinlock_read_lock(&host->health_log.spinlock);
256 + ALARM_ENTRY *update_ae = NULL;
257 for(ALARM_ENTRY *t = host->health_log.alarms ; t ; t = t->next) {
258 if(t != ae && t->alarm_id == ae->alarm_id) {
259 if(!(t->flags & HEALTH_ENTRY_FLAG_UPDATED) && !t->updated_by_id) {
@@ -264,7 +265,7 @@ inline void health_alarm_log_add_entry(RRDHOST *host, ALARM_ENTRY *ae, bool asyn
265 (t->old_status == RRDCALC_STATUS_WARNING || t->old_status == RRDCALC_STATUS_CRITICAL))
266 ae->non_clear_duration += t->non_clear_duration;
267
267 - health_alarm_log_save(host, t, async);
268 + update_ae = t;
269 }
270
271 // no need to continue
@@ -272,6 +273,8 @@ inline void health_alarm_log_add_entry(RRDHOST *host, ALARM_ENTRY *ae, bool asyn
273 }
274 }
275 rw_spinlock_read_unlock(&host->health_log.spinlock);
276 + if (update_ae)
277 + health_alarm_log_save(host, update_ae, async);
278
279 health_alarm_log_save(host, ae, async);
280 }
@@ -328,11 +331,14 @@ void health_alarm_log_cleanup(RRDHOST *host) {
331 if(!host->health_log.alarms)
332 return;
333
334 + if (!rw_spinlock_trywrite_lock(&host->health_log.spinlock)) {
335 + // If we can't get the lock, just return
336 + return;
337 + }
338 +
339 time_t now = now_realtime_sec();
340 time_t retention = host->health_log.health_log_retention_s;
333 -
334 - rw_spinlock_write_lock(&host->health_log.spinlock);
335 -
341 +
342 ALARM_ENTRY *ae = host->health_log.alarms;
343 while(ae) {
344 // Check if entry is old enough to be deleted