]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: proxy: add and use a separate last_change variable for internal use
authorAurelien DARRAGON <adarragon@haproxy.com>
Mon, 30 Jun 2025 13:45:44 +0000 (15:45 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Mon, 30 Jun 2025 14:26:31 +0000 (16:26 +0200)
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.

include/haproxy/proxy-t.h
src/backend.c
src/proxy.c
src/server.c

index 993751c5c53fe6f7ec6831e3758c2aebccbd316c..2d08a7852c8f145c31376305ea09cf82768a0506 100644 (file)
@@ -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 */
 
index 5479d4ddf821d2dd211ed822298271b0bbc20266..56a6d88b0614ec779b22d16a70c495d2a630556a 100644 (file)
@@ -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;
 }
 
 /*
index 5e09641a35e89e2b8a5c916347a30b5c0def5e01..e3a56af4c14e02c667060f5b305c41c7e4a7e624 100644 (file)
@@ -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);
index 3fc1cd9979bb82144904d341f7872a89953403a4..73870574bf08a5fb6a2b9f868464723d3d2143b7 100644 (file)
@@ -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));
        }
 }