From: Aurelien DARRAGON Date: Mon, 30 Jun 2025 13:45:44 +0000 (+0200) Subject: MEDIUM: proxy: add and use a separate last_change variable for internal use X-Git-Tag: v3.3-dev3~73 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5b1480c9d40dc6f9e4ebe8d0531a1709d9f6c244;p=thirdparty%2Fhaproxy.git MEDIUM: proxy: add and use a separate last_change variable for internal use Same motivation as previous commit, proxy last_change is "abused" because it is used for 2 different purposes, one for stats, and the other one for process-local internal use. Let's add a separate proxy-only last_change variable for internal use, and leave the last_change shared (and thread-grouped) counter for statistics. --- diff --git a/include/haproxy/proxy-t.h b/include/haproxy/proxy-t.h index 993751c5c..2d08a7852 100644 --- a/include/haproxy/proxy-t.h +++ b/include/haproxy/proxy-t.h @@ -311,7 +311,7 @@ struct proxy { char flags; /* bit field PR_FL_* */ enum pr_mode mode; /* mode = PR_MODE_TCP, PR_MODE_HTTP, ... */ char cap; /* supported capabilities (PR_CAP_*) */ - /* 4-bytes hole */ + unsigned long last_change; /* internal use only: last time the proxy state was changed */ struct list global_list; /* list member for global proxy list */ diff --git a/src/backend.c b/src/backend.c index 5479d4ddf..56a6d88b0 100644 --- a/src/backend.c +++ b/src/backend.c @@ -2894,6 +2894,7 @@ void back_handle_st_rdy(struct stream *s) */ void set_backend_down(struct proxy *be) { + be->last_change = ns_to_sec(now_ns); HA_ATOMIC_STORE(&be->be_counters.shared->tg[tgid - 1]->last_change, ns_to_sec(now_ns)); _HA_ATOMIC_INC(&be->be_counters.shared->tg[tgid - 1]->down_trans); @@ -2967,12 +2968,10 @@ no_cookie: } int be_downtime(struct proxy *px) { - unsigned long last_change = COUNTERS_SHARED_LAST(px->be_counters.shared->tg, last_change); - - if (px->lbprm.tot_weight && last_change < ns_to_sec(now_ns)) // ignore negative time + if (px->lbprm.tot_weight && px->last_change < ns_to_sec(now_ns)) // ignore negative time return px->down_time; - return ns_to_sec(now_ns) - last_change + px->down_time; + return ns_to_sec(now_ns) - px->last_change + px->down_time; } /* diff --git a/src/proxy.c b/src/proxy.c index 5e09641a3..e3a56af4c 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1722,6 +1722,7 @@ int setup_new_proxy(struct proxy *px, const char *name, unsigned int cap, char * } px->cap = cap; + px->last_change = ns_to_sec(now_ns); if (name && !(cap & PR_CAP_INT)) proxy_store_name(px); diff --git a/src/server.c b/src/server.c index 3fc1cd997..73870574b 100644 --- a/src/server.c +++ b/src/server.c @@ -7064,13 +7064,14 @@ static void srv_update_status(struct server *s, int type, int cause) if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) set_backend_down(s->proxy); /* backend going down */ else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) { - unsigned long last_change = COUNTERS_SHARED_LAST(s->proxy->be_counters.shared->tg, last_change); + unsigned long last_change = s->proxy->last_change; /* backend was down and is back up again: * no helper function, updating last_change and backend downtime stats */ if (last_change < ns_to_sec(now_ns)) // ignore negative times s->proxy->down_time += ns_to_sec(now_ns) - last_change; + s->proxy->last_change = ns_to_sec(now_ns); HA_ATOMIC_STORE(&s->proxy->be_counters.shared->tg[tgid - 1]->last_change, ns_to_sec(now_ns)); } }