]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stick-tables: Add functions to update some values of a tracked counter
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 6 Oct 2020 11:52:40 +0000 (13:52 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 4 Dec 2020 13:41:49 +0000 (14:41 +0100)
The cumulative numbers of http requests, http errors, bytes received and
sent and their respective rates for a tracked counters are now updated using
specific stream independent functions. These functions are used by the
stream but the aim is to allow the session to do so too. For now, there is
no reason to perform these updates from the session, except from the mux-h2
maybe. But, the mux-h1, on the frontend side, will be able to return some
errors to the client, before the stream creation. In this case, it will be
mandatory to update counters tracked at the session level.

include/haproxy/stick_table.h
include/haproxy/stream.h
src/stream.c

index 3ca08f0c2189799e694eaa6dd4d7944bf63eb627..32475a47948f23333d8d1efd78fc854e846e12ca 100644 (file)
@@ -203,4 +203,130 @@ static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags)
        stkctr->entry = caddr_clr_flags(stkctr->entry, flags);
 }
 
+/* Increase the number of cumulated HTTP requests in the tracked counter
+ * <stkctr>. It returns 0 if the entry pointer does not exist and nothing is
+ * performed. Otherwise it returns 1.
+ */
+static inline int stkctr_inc_http_req_ctr(struct stkctr *stkctr)
+{
+       struct stksess *ts;
+       void *ptr1, *ptr2;
+
+       ts = stkctr_entry(stkctr);
+       if (!ts)
+               return 0;
+
+       HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
+
+       ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
+       if (ptr1)
+               stktable_data_cast(ptr1, http_req_cnt)++;
+
+       ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
+       if (ptr2)
+               update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
+                                      stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
+
+       HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
+
+       /* If data was modified, we need to touch to re-schedule sync */
+       if (ptr1 || ptr2)
+               stktable_touch_local(stkctr->table, ts, 0);
+       return 1;
+}
+
+/* Increase the number of cumulated failed HTTP requests in the tracked counter
+ * <stkctr>. It returns 0 if the entry pointer does not exist and nothing is
+ * performed. Otherwise it returns 1.
+ */
+static inline int stkctr_inc_http_err_ctr(struct stkctr *stkctr)
+{
+       struct stksess *ts;
+       void *ptr1, *ptr2;
+
+       ts = stkctr_entry(stkctr);
+       if (!ts)
+               return 0;
+
+       HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
+
+       ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_CNT);
+       if (ptr1)
+               stktable_data_cast(ptr1, http_err_cnt)++;
+
+       ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_RATE);
+       if (ptr2)
+               update_freq_ctr_period(&stktable_data_cast(ptr2, http_err_rate),
+                                      stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
+
+       HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
+
+       /* If data was modified, we need to touch to re-schedule sync */
+       if (ptr1 || ptr2)
+               stktable_touch_local(stkctr->table, ts, 0);
+       return 1;
+}
+
+/* Increase the number of bytes received in the tracked counter <stkctr>. It
+ * returns 0 if the entry pointer does not exist and nothing is
+ * performed. Otherwise it returns 1.
+ */
+static inline int stkctr_inc_bytes_in_ctr(struct stkctr *stkctr, unsigned long long bytes)
+{
+       struct stksess *ts;
+       void *ptr1, *ptr2;
+
+       ts = stkctr_entry(stkctr);
+       if (!ts)
+               return 0;
+
+       HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
+       ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_CNT);
+       if (ptr1)
+               stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
+
+       ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_RATE);
+       if (ptr2)
+               update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
+                                      stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
+       HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
+
+
+       /* If data was modified, we need to touch to re-schedule sync */
+       if (ptr1 || ptr2)
+               stktable_touch_local(stkctr->table, ts, 0);
+       return 1;
+}
+
+/* Increase the number of bytes sent in the tracked counter <stkctr>. It
+ * returns 0 if the entry pointer does not exist and nothing is
+ * performed. Otherwise it returns 1.
+ */
+static inline int stkctr_inc_bytes_out_ctr(struct stkctr *stkctr, unsigned long long bytes)
+{
+       struct stksess *ts;
+       void *ptr1, *ptr2;
+
+       ts = stkctr_entry(stkctr);
+       if (!ts)
+               return 0;
+
+       HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
+       ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_CNT);
+       if (ptr1)
+               stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
+
+       ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_RATE);
+       if (ptr2)
+               update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
+                                      stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
+       HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
+
+
+       /* If data was modified, we need to touch to re-schedule sync */
+       if (ptr1 || ptr2)
+               stktable_touch_local(stkctr->table, ts, 0);
+       return 1;
+}
+
 #endif /* _HAPROXY_STICK_TABLE_H */
index e5c9c2937f7631bcd7dc0cfedd80e9526ac241f4..766740c6265a6145124f52c81e15c64476640bf8 100644 (file)
@@ -230,36 +230,11 @@ static inline void stream_track_stkctr(struct stkctr *ctr, struct stktable *t, s
 /* Increase the number of cumulated HTTP requests in the tracked counters */
 static inline void stream_inc_http_req_ctr(struct stream *s)
 {
-       struct stksess *ts;
-       void *ptr;
        int i;
 
        for (i = 0; i < MAX_SESS_STKCTR; i++) {
-               struct stkctr *stkctr = &s->stkctr[i];
-
-               ts = stkctr_entry(stkctr);
-               if (!ts) {
-                       stkctr = &s->sess->stkctr[i];
-                       ts = stkctr_entry(stkctr);
-                       if (!ts)
-                               continue;
-               }
-
-               HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
-
-               ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
-               if (ptr)
-                       stktable_data_cast(ptr, http_req_cnt)++;
-
-               ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
-               if (ptr)
-                       update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
-                                              stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
-
-               HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
-
-               /* If data was modified, we need to touch to re-schedule sync */
-               stktable_touch_local(stkctr->table, ts, 0);
+               if (!stkctr_inc_http_req_ctr(&s->stkctr[i]))
+                       stkctr_inc_http_req_ctr(&s->sess->stkctr[i]);
        }
 }
 
@@ -268,35 +243,13 @@ static inline void stream_inc_http_req_ctr(struct stream *s)
  */
 static inline void stream_inc_be_http_req_ctr(struct stream *s)
 {
-       struct stksess *ts;
-       void *ptr;
        int i;
 
        for (i = 0; i < MAX_SESS_STKCTR; i++) {
-               struct stkctr *stkctr = &s->stkctr[i];
-
-               ts = stkctr_entry(stkctr);
-               if (!ts)
-                       continue;
-
-               if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
+               if (!stkctr_entry(&s->stkctr[i]) || !(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
                        continue;
 
-               HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
-
-               ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_CNT);
-               if (ptr)
-                       stktable_data_cast(ptr, http_req_cnt)++;
-
-               ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_REQ_RATE);
-               if (ptr)
-                       update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
-                                              stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
-
-               HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
-
-               /* If data was modified, we need to touch to re-schedule sync */
-               stktable_touch_local(stkctr->table, ts, 0);
+               stkctr_inc_http_req_ctr(&s->stkctr[i]);
        }
 }
 
@@ -308,36 +261,11 @@ static inline void stream_inc_be_http_req_ctr(struct stream *s)
  */
 static inline void stream_inc_http_err_ctr(struct stream *s)
 {
-       struct stksess *ts;
-       void *ptr;
        int i;
 
        for (i = 0; i < MAX_SESS_STKCTR; i++) {
-               struct stkctr *stkctr = &s->stkctr[i];
-
-               ts = stkctr_entry(stkctr);
-               if (!ts) {
-                       stkctr = &s->sess->stkctr[i];
-                       ts = stkctr_entry(stkctr);
-                       if (!ts)
-                               continue;
-               }
-
-               HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
-
-               ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_CNT);
-               if (ptr)
-                       stktable_data_cast(ptr, http_err_cnt)++;
-
-               ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_HTTP_ERR_RATE);
-               if (ptr)
-                       update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
-                                              stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
-
-               HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
-
-               /* If data was modified, we need to touch to re-schedule sync */
-               stktable_touch_local(stkctr->table, ts, 0);
+               if (!stkctr_inc_http_err_ctr(&s->stkctr[i]))
+                       stkctr_inc_http_err_ctr(&s->sess->stkctr[i]);
        }
 }
 
index 0ca54afe03d3d50159d7ca6ca3e912aa203c3ffa..d121fbe7bf241e9fc484df5ea4becfbe83d99b25 100644 (file)
@@ -757,8 +757,6 @@ void stream_process_counters(struct stream *s)
 {
        struct session *sess = s->sess;
        unsigned long long bytes;
-       void *ptr1,*ptr2;
-       struct stksess *ts;
        int i;
 
        bytes = s->req.total - s->logs.bytes_in;
@@ -774,30 +772,8 @@ void stream_process_counters(struct stream *s)
                        _HA_ATOMIC_ADD(&sess->listener->counters->bytes_in, bytes);
 
                for (i = 0; i < MAX_SESS_STKCTR; i++) {
-                       struct stkctr *stkctr = &s->stkctr[i];
-
-                       ts = stkctr_entry(stkctr);
-                       if (!ts) {
-                               stkctr = &sess->stkctr[i];
-                               ts = stkctr_entry(stkctr);
-                               if (!ts)
-                                       continue;
-                       }
-
-                       HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
-                       ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_CNT);
-                       if (ptr1)
-                               stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
-
-                       ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_RATE);
-                       if (ptr2)
-                               update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
-                                                      stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
-                       HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
-
-                       /* If data was modified, we need to touch to re-schedule sync */
-                       if (ptr1 || ptr2)
-                               stktable_touch_local(stkctr->table, ts, 0);
+                       if (!stkctr_inc_bytes_in_ctr(&s->stkctr[i], bytes))
+                               stkctr_inc_bytes_in_ctr(&sess->stkctr[i], bytes);
                }
        }
 
@@ -814,30 +790,8 @@ void stream_process_counters(struct stream *s)
                        _HA_ATOMIC_ADD(&sess->listener->counters->bytes_out, bytes);
 
                for (i = 0; i < MAX_SESS_STKCTR; i++) {
-                       struct stkctr *stkctr = &s->stkctr[i];
-
-                       ts = stkctr_entry(stkctr);
-                       if (!ts) {
-                               stkctr = &sess->stkctr[i];
-                               ts = stkctr_entry(stkctr);
-                               if (!ts)
-                                       continue;
-                       }
-
-                       HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
-                       ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_CNT);
-                       if (ptr1)
-                               stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
-
-                       ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_RATE);
-                       if (ptr2)
-                               update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
-                                                      stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
-                       HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
-
-                       /* If data was modified, we need to touch to re-schedule sync */
-                       if (ptr1 || ptr2)
-                               stktable_touch_local(stkctr->table, stkctr_entry(stkctr), 0);
+                       if (!stkctr_inc_bytes_out_ctr(&s->stkctr[i], bytes))
+                               stkctr_inc_bytes_out_ctr(&sess->stkctr[i], bytes);
                }
        }
 }