Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Queue management functions. |
| 3 | * |
Willy Tarreau | ac68c5d | 2009-10-04 23:12:44 +0200 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 13 | /* Short explanation on the locking, which is far from being trivial : a |
| 14 | * pendconn is a list element which necessarily is associated with an existing |
| 15 | * stream. It has pendconn->strm always valid. A pendconn may only be in one of |
| 16 | * these three states : |
| 17 | * - unlinked : in this case it is an empty list head ; |
| 18 | * - linked into the server's queue ; |
| 19 | * - linked into the proxy's queue. |
| 20 | * |
| 21 | * A stream does not necessarily have such a pendconn. Thus the pendconn is |
| 22 | * designated by the stream->pend_pos pointer. This results in some properties : |
| 23 | * - pendconn->strm->pend_pos is never NULL for any valid pendconn |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 24 | * - if p->node.node.leaf_p is NULL, the element is unlinked, |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 25 | * otherwise it necessarily belongs to one of the other lists ; this may |
| 26 | * not be atomically checked under threads though ; |
| 27 | * - pendconn->px is never NULL if pendconn->list is not empty |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 28 | * - pendconn->srv is never NULL if pendconn->list is in the server's queue, |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 29 | * and is always NULL if pendconn->list is in the backend's queue or empty. |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 30 | * - pendconn->target is NULL while the element is queued, and points to the |
| 31 | * assigned server when the pendconn is picked. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 32 | * |
| 33 | * Threads complicate the design a little bit but rules remain simple : |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 34 | * - the server's queue lock must be held at least when manipulating the |
| 35 | * server's queue, which is when adding a pendconn to the queue and when |
| 36 | * removing a pendconn from the queue. It protects the queue's integrity. |
| 37 | * |
| 38 | * - the proxy's queue lock must be held at least when manipulating the |
| 39 | * proxy's queue, which is when adding a pendconn to the queue and when |
| 40 | * removing a pendconn from the queue. It protects the queue's integrity. |
| 41 | * |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 42 | * - both locks are compatible and may be held at the same time. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 43 | * |
| 44 | * - a pendconn_add() is only performed by the stream which will own the |
| 45 | * pendconn ; the pendconn is allocated at this moment and returned ; it is |
| 46 | * added to either the server or the proxy's queue while holding this |
Willy Tarreau | 4980160 | 2020-06-04 22:50:02 +0200 | [diff] [blame] | 47 | s * queue's lock. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 48 | * |
| 49 | * - the pendconn is then met by a thread walking over the proxy or server's |
| 50 | * queue with the respective lock held. This lock is exclusive and the |
| 51 | * pendconn can only appear in one queue so by definition a single thread |
| 52 | * may find this pendconn at a time. |
| 53 | * |
| 54 | * - the pendconn is unlinked either by its own stream upon success/abort/ |
| 55 | * free, or by another one offering it its server slot. This is achieved by |
| 56 | * pendconn_process_next_strm() under either the server or proxy's lock, |
| 57 | * pendconn_redistribute() under the server's lock, pendconn_grab_from_px() |
| 58 | * under the proxy's lock, or pendconn_unlink() under either the proxy's or |
| 59 | * the server's lock depending on the queue the pendconn is attached to. |
| 60 | * |
| 61 | * - no single operation except the pendconn initialisation prior to the |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 62 | * insertion are performed without eithre a queue lock held or the element |
| 63 | * being unlinked and visible exclusively to its stream. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 64 | * |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 65 | * - pendconn_grab_from_px() and pendconn_process_next_strm() assign ->target |
| 66 | * so that the stream knows what server to work with (via |
| 67 | * pendconn_dequeue() which sets it on strm->target). |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 68 | * |
| 69 | * - a pendconn doesn't switch between queues, it stays where it is. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 70 | */ |
| 71 | |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 72 | #include <import/eb32tree.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 73 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 74 | #include <haproxy/backend.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 75 | #include <haproxy/http_rules.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 76 | #include <haproxy/pool.h> |
Willy Tarreau | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 77 | #include <haproxy/queue.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 78 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 79 | #include <haproxy/server-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 80 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 81 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 82 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 83 | #include <haproxy/thread.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 84 | #include <haproxy/time.h> |
Willy Tarreau | c1a689f | 2021-05-08 13:59:05 +0200 | [diff] [blame] | 85 | #include <haproxy/tools.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | |
| 87 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 88 | #define NOW_OFFSET_BOUNDARY() ((now_ms - (TIMER_LOOK_BACK >> 12)) & 0xfffff) |
| 89 | #define KEY_CLASS(key) ((u32)key & 0xfff00000) |
| 90 | #define KEY_OFFSET(key) ((u32)key & 0x000fffff) |
| 91 | #define KEY_CLASS_OFFSET_BOUNDARY(key) (KEY_CLASS(key) | NOW_OFFSET_BOUNDARY()) |
| 92 | #define MAKE_KEY(class, offset) (((u32)(class + 0x7ff) << 20) | ((u32)(now_ms + offset) & 0xfffff)) |
| 93 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 94 | DECLARE_POOL(pool_head_pendconn, "pendconn", sizeof(struct pendconn)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | |
| 96 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 97 | * and the proxy's usage relative to its dynamic connections limit. It is |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 98 | * expected that 0 < s->minconn <= s->maxconn when this is called. If the |
| 99 | * server is currently warming up, the slowstart is also applied to the |
| 100 | * resulting value, which can be lower than minconn in this case, but never |
| 101 | * less than 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 103 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 105 | unsigned int max; |
| 106 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 107 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 108 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 109 | max = s->maxconn; |
| 110 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 111 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 112 | max = s->maxconn; |
| 113 | else max = MAX(s->minconn, |
| 114 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 115 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 116 | if ((s->cur_state == SRV_ST_STARTING) && |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 117 | ns_to_sec(now_ns) < s->last_change + s->slowstart && |
| 118 | ns_to_sec(now_ns) >= s->last_change) { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 119 | unsigned int ratio; |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 120 | ratio = 100 * (ns_to_sec(now_ns) - s->last_change) / s->slowstart; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 121 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 122 | } |
| 123 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 126 | /* Remove the pendconn from the server's queue. At this stage, the connection |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 127 | * is not really dequeued. It will be done during the process_stream. It is |
| 128 | * up to the caller to atomically decrement the pending counts. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 129 | * |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 130 | * The caller must own the lock on the server queue. The pendconn must still be |
| 131 | * queued (p->node.leaf_p != NULL) and must be in a server (p->srv != NULL). |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 132 | */ |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 133 | static void __pendconn_unlink_srv(struct pendconn *p) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 134 | { |
Willy Tarreau | 51c63f0 | 2021-06-23 16:43:45 +0200 | [diff] [blame] | 135 | p->strm->logs.srv_queue_pos += _HA_ATOMIC_LOAD(&p->queue->idx) - p->queue_idx; |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 136 | eb32_delete(&p->node); |
| 137 | } |
| 138 | |
| 139 | /* Remove the pendconn from the proxy's queue. At this stage, the connection |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 140 | * is not really dequeued. It will be done during the process_stream. It is |
| 141 | * up to the caller to atomically decrement the pending counts. |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 142 | * |
| 143 | * The caller must own the lock on the proxy queue. The pendconn must still be |
| 144 | * queued (p->node.leaf_p != NULL) and must be in the proxy (p->srv == NULL). |
| 145 | */ |
| 146 | static void __pendconn_unlink_prx(struct pendconn *p) |
| 147 | { |
Willy Tarreau | 51c63f0 | 2021-06-23 16:43:45 +0200 | [diff] [blame] | 148 | p->strm->logs.prx_queue_pos += _HA_ATOMIC_LOAD(&p->queue->idx) - p->queue_idx; |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 149 | eb32_delete(&p->node); |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 152 | /* Locks the queue the pendconn element belongs to. This relies on both p->px |
| 153 | * and p->srv to be properly initialized (which is always the case once the |
| 154 | * element has been added). |
| 155 | */ |
| 156 | static inline void pendconn_queue_lock(struct pendconn *p) |
| 157 | { |
Willy Tarreau | 51c63f0 | 2021-06-23 16:43:45 +0200 | [diff] [blame] | 158 | HA_SPIN_LOCK(QUEUE_LOCK, &p->queue->lock); |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* Unlocks the queue the pendconn element belongs to. This relies on both p->px |
| 162 | * and p->srv to be properly initialized (which is always the case once the |
| 163 | * element has been added). |
| 164 | */ |
| 165 | static inline void pendconn_queue_unlock(struct pendconn *p) |
| 166 | { |
Willy Tarreau | 51c63f0 | 2021-06-23 16:43:45 +0200 | [diff] [blame] | 167 | HA_SPIN_UNLOCK(QUEUE_LOCK, &p->queue->lock); |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 170 | /* Removes the pendconn from the server/proxy queue. At this stage, the |
| 171 | * connection is not really dequeued. It will be done during process_stream(). |
Willy Tarreau | 9ada030 | 2019-11-14 14:58:39 +0100 | [diff] [blame] | 172 | * This function takes all the required locks for the operation. The pendconn |
| 173 | * must be valid, though it doesn't matter if it was already unlinked. Prefer |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 174 | * pendconn_cond_unlink() to first check <p>. It also forces a serialization |
| 175 | * on p->del_lock to make sure another thread currently waking it up finishes |
| 176 | * first. |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 177 | */ |
| 178 | void pendconn_unlink(struct pendconn *p) |
| 179 | { |
Willy Tarreau | 51c63f0 | 2021-06-23 16:43:45 +0200 | [diff] [blame] | 180 | struct queue *q = p->queue; |
| 181 | struct proxy *px = q->px; |
| 182 | struct server *sv = q->sv; |
Willy Tarreau | d03adce | 2021-06-23 16:54:16 +0200 | [diff] [blame] | 183 | uint oldidx; |
| 184 | int done = 0; |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 185 | |
Willy Tarreau | d03adce | 2021-06-23 16:54:16 +0200 | [diff] [blame] | 186 | oldidx = _HA_ATOMIC_LOAD(&p->queue->idx); |
| 187 | HA_SPIN_LOCK(QUEUE_LOCK, &q->lock); |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 188 | HA_SPIN_LOCK(QUEUE_LOCK, &p->del_lock); |
| 189 | |
Willy Tarreau | d03adce | 2021-06-23 16:54:16 +0200 | [diff] [blame] | 190 | if (p->node.node.leaf_p) { |
| 191 | eb32_delete(&p->node); |
| 192 | done = 1; |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 193 | } |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 194 | |
| 195 | HA_SPIN_UNLOCK(QUEUE_LOCK, &p->del_lock); |
Willy Tarreau | d03adce | 2021-06-23 16:54:16 +0200 | [diff] [blame] | 196 | HA_SPIN_UNLOCK(QUEUE_LOCK, &q->lock); |
| 197 | |
| 198 | if (done) { |
| 199 | oldidx -= p->queue_idx; |
| 200 | if (sv) |
| 201 | p->strm->logs.srv_queue_pos += oldidx; |
| 202 | else |
| 203 | p->strm->logs.prx_queue_pos += oldidx; |
| 204 | |
| 205 | _HA_ATOMIC_DEC(&q->length); |
| 206 | _HA_ATOMIC_DEC(&px->totpend); |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 207 | } |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 210 | /* Retrieve the first pendconn from tree <pendconns>. Classes are always |
| 211 | * considered first, then the time offset. The time does wrap, so the |
| 212 | * lookup is performed twice, one to retrieve the first class and a second |
| 213 | * time to retrieve the earliest time in this class. |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 214 | */ |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 215 | static struct pendconn *pendconn_first(struct eb_root *pendconns) |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 216 | { |
| 217 | struct eb32_node *node, *node2 = NULL; |
| 218 | u32 key; |
| 219 | |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 220 | node = eb32_first(pendconns); |
| 221 | if (!node) |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 222 | return NULL; |
| 223 | |
| 224 | key = KEY_CLASS_OFFSET_BOUNDARY(node->key); |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 225 | node2 = eb32_lookup_ge(pendconns, key); |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 226 | |
| 227 | if (!node2 || |
| 228 | KEY_CLASS(node2->key) != KEY_CLASS(node->key)) { |
| 229 | /* no other key in the tree, or in this class */ |
| 230 | return eb32_entry(node, struct pendconn, node); |
| 231 | } |
| 232 | |
| 233 | /* found a better key */ |
| 234 | return eb32_entry(node2, struct pendconn, node); |
| 235 | } |
| 236 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 237 | /* Process the next pending connection from either a server or a proxy, and |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 238 | * returns a strictly positive value on success (see below). If no pending |
| 239 | * connection is found, 0 is returned. Note that neither <srv> nor <px> may be |
| 240 | * NULL. Priority is given to the oldest request in the queue if both <srv> and |
| 241 | * <px> have pending requests. This ensures that no request will be left |
| 242 | * unserved. The <px> queue is not considered if the server (or a tracked |
| 243 | * server) is not RUNNING, is disabled, or has a null weight (server going |
| 244 | * down). The <srv> queue is still considered in this case, because if some |
| 245 | * connections remain there, it means that some requests have been forced there |
| 246 | * after it was seen down (eg: due to option persist). The stream is |
| 247 | * immediately marked as "assigned", and both its <srv> and <srv_conn> are set |
| 248 | * to <srv>. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 249 | * |
Willy Tarreau | a0e9c55 | 2021-06-18 19:45:17 +0200 | [diff] [blame] | 250 | * The proxy's queue will be consulted only if px_ok is non-zero. |
| 251 | * |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 252 | * This function must only be called if the server queue is locked _AND_ the |
| 253 | * proxy queue is not. Today it is only called by process_srv_queue. |
Willy Tarreau | a0e9c55 | 2021-06-18 19:45:17 +0200 | [diff] [blame] | 254 | * When a pending connection is dequeued, this function returns 1 if a pendconn |
| 255 | * is dequeued, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 256 | */ |
Willy Tarreau | a0e9c55 | 2021-06-18 19:45:17 +0200 | [diff] [blame] | 257 | static int pendconn_process_next_strm(struct server *srv, struct proxy *px, int px_ok) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 258 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 259 | struct pendconn *p = NULL; |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 260 | struct pendconn *pp = NULL; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 261 | u32 pkey, ppkey; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 263 | p = NULL; |
Willy Tarreau | 90a160a | 2021-06-24 07:21:59 +0200 | [diff] [blame] | 264 | if (srv->queue.length) |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 265 | p = pendconn_first(&srv->queue.head); |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 266 | |
| 267 | pp = NULL; |
Willy Tarreau | 49667c1 | 2021-06-24 08:04:24 +0200 | [diff] [blame] | 268 | if (px_ok && px->queue.length) { |
| 269 | /* the lock only remains held as long as the pp is |
| 270 | * in the proxy's queue. |
| 271 | */ |
Willy Tarreau | 47ee44f | 2021-06-24 16:00:18 +0200 | [diff] [blame] | 272 | HA_SPIN_LOCK(QUEUE_LOCK, &px->queue.lock); |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 273 | pp = pendconn_first(&px->queue.head); |
Willy Tarreau | 49667c1 | 2021-06-24 08:04:24 +0200 | [diff] [blame] | 274 | if (!pp) |
Willy Tarreau | 47ee44f | 2021-06-24 16:00:18 +0200 | [diff] [blame] | 275 | HA_SPIN_UNLOCK(QUEUE_LOCK, &px->queue.lock); |
Willy Tarreau | 49667c1 | 2021-06-24 08:04:24 +0200 | [diff] [blame] | 276 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 277 | |
Willy Tarreau | 5343d8e | 2021-06-24 07:22:03 +0200 | [diff] [blame] | 278 | if (!p && !pp) |
Willy Tarreau | a48905b | 2021-06-24 07:27:01 +0200 | [diff] [blame] | 279 | return 0; |
Christopher Faulet | cd7126b | 2021-02-11 11:13:33 +0100 | [diff] [blame] | 280 | else if (!pp) |
| 281 | goto use_p; /* p != NULL */ |
| 282 | else if (!p) |
| 283 | goto use_pp; /* pp != NULL */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 284 | |
Christopher Faulet | cd7126b | 2021-02-11 11:13:33 +0100 | [diff] [blame] | 285 | /* p != NULL && pp != NULL*/ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 286 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 287 | if (KEY_CLASS(p->node.key) < KEY_CLASS(pp->node.key)) |
| 288 | goto use_p; |
| 289 | |
| 290 | if (KEY_CLASS(pp->node.key) < KEY_CLASS(p->node.key)) |
| 291 | goto use_pp; |
| 292 | |
| 293 | pkey = KEY_OFFSET(p->node.key); |
| 294 | ppkey = KEY_OFFSET(pp->node.key); |
| 295 | |
| 296 | if (pkey < NOW_OFFSET_BOUNDARY()) |
| 297 | pkey += 0x100000; // key in the future |
| 298 | |
| 299 | if (ppkey < NOW_OFFSET_BOUNDARY()) |
| 300 | ppkey += 0x100000; // key in the future |
| 301 | |
| 302 | if (pkey <= ppkey) |
| 303 | goto use_p; |
| 304 | |
| 305 | use_pp: |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 306 | /* we'd like to release the proxy lock ASAP to let other threads |
| 307 | * work with other servers. But for this we must first hold the |
| 308 | * pendconn alive to prevent a removal from its owning stream. |
| 309 | */ |
| 310 | HA_SPIN_LOCK(QUEUE_LOCK, &pp->del_lock); |
| 311 | |
| 312 | /* now the element won't go, we can release the proxy */ |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 313 | __pendconn_unlink_prx(pp); |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 314 | HA_SPIN_UNLOCK(QUEUE_LOCK, &px->queue.lock); |
| 315 | |
| 316 | pp->strm_flags |= SF_ASSIGNED; |
| 317 | pp->target = srv; |
| 318 | stream_add_srv_conn(pp->strm, srv); |
| 319 | |
| 320 | /* we must wake the task up before releasing the lock as it's the only |
| 321 | * way to make sure the task still exists. The pendconn cannot vanish |
| 322 | * under us since the task will need to take the lock anyway and to wait |
| 323 | * if it wakes up on a different thread. |
| 324 | */ |
Willy Tarreau | 3fdacdd | 2022-06-16 16:10:05 +0200 | [diff] [blame] | 325 | task_wakeup(pp->strm->task, TASK_WOKEN_RES); |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 326 | HA_SPIN_UNLOCK(QUEUE_LOCK, &pp->del_lock); |
| 327 | |
Willy Tarreau | 7f3c1df | 2021-06-18 09:22:21 +0200 | [diff] [blame] | 328 | _HA_ATOMIC_DEC(&px->queue.length); |
Willy Tarreau | 98c8910 | 2021-06-18 10:51:58 +0200 | [diff] [blame] | 329 | _HA_ATOMIC_INC(&px->queue.idx); |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 330 | return 1; |
| 331 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 332 | use_p: |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 333 | /* we don't need the px queue lock anymore, we have the server's lock */ |
Willy Tarreau | 49667c1 | 2021-06-24 08:04:24 +0200 | [diff] [blame] | 334 | if (pp) |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 335 | HA_SPIN_UNLOCK(QUEUE_LOCK, &px->queue.lock); |
| 336 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 337 | p->strm_flags |= SF_ASSIGNED; |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 338 | p->target = srv; |
Willy Tarreau | a48905b | 2021-06-24 07:27:01 +0200 | [diff] [blame] | 339 | stream_add_srv_conn(p->strm, srv); |
| 340 | |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 341 | /* we must wake the task up before releasing the lock as it's the only |
| 342 | * way to make sure the task still exists. The pendconn cannot vanish |
| 343 | * under us since the task will need to take the lock anyway and to wait |
| 344 | * if it wakes up on a different thread. |
| 345 | */ |
Willy Tarreau | 3fdacdd | 2022-06-16 16:10:05 +0200 | [diff] [blame] | 346 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 347 | __pendconn_unlink_srv(p); |
Willy Tarreau | a48905b | 2021-06-24 07:27:01 +0200 | [diff] [blame] | 348 | |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 349 | _HA_ATOMIC_DEC(&srv->queue.length); |
| 350 | _HA_ATOMIC_INC(&srv->queue.idx); |
Willy Tarreau | a48905b | 2021-06-24 07:27:01 +0200 | [diff] [blame] | 351 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 354 | /* Manages a server's connection queue. This function will try to dequeue as |
Willy Tarreau | 9ab7829 | 2021-06-22 18:47:51 +0200 | [diff] [blame] | 355 | * many pending streams as possible, and wake them up. |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 356 | */ |
Willy Tarreau | 9ab7829 | 2021-06-22 18:47:51 +0200 | [diff] [blame] | 357 | void process_srv_queue(struct server *s) |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 358 | { |
Willy Tarreau | a0e9c55 | 2021-06-18 19:45:17 +0200 | [diff] [blame] | 359 | struct server *ref = s->track ? s->track : s; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 360 | struct proxy *p = s->proxy; |
Olivier Houchard | ecfe673 | 2018-07-26 18:47:27 +0200 | [diff] [blame] | 361 | int maxconn; |
Willy Tarreau | 19c5581 | 2021-06-24 15:51:12 +0200 | [diff] [blame] | 362 | int stop = 0; |
Willy Tarreau | 9cef43a | 2021-06-24 07:47:08 +0200 | [diff] [blame] | 363 | int done = 0; |
Willy Tarreau | a0e9c55 | 2021-06-18 19:45:17 +0200 | [diff] [blame] | 364 | int px_ok; |
| 365 | |
| 366 | /* if a server is not usable or backup and must not be used |
| 367 | * to dequeue backend requests. |
| 368 | */ |
| 369 | px_ok = srv_currently_usable(ref) && |
| 370 | (!(s->flags & SRV_F_BACKUP) || |
| 371 | (!p->srv_act && |
| 372 | (s == p->lbprm.fbck || (p->options & PR_O_USE_ALL_BK)))); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 373 | |
Willy Tarreau | ae0b12e | 2021-06-24 08:30:07 +0200 | [diff] [blame] | 374 | /* let's repeat that under the lock on each round. Threads competing |
| 375 | * for the same server will give up, knowing that at least one of |
Willy Tarreau | ddab05b | 2022-07-30 09:53:22 +0200 | [diff] [blame] | 376 | * them will check the conditions again before quitting. In order |
| 377 | * to avoid the deadly situation where one thread spends its time |
| 378 | * dequeueing for others, we limit the number of rounds it does. |
| 379 | * However we still re-enter the loop for one pass if there's no |
| 380 | * more served, otherwise we could end up with no other thread |
| 381 | * trying to dequeue them. |
Willy Tarreau | ae0b12e | 2021-06-24 08:30:07 +0200 | [diff] [blame] | 382 | */ |
Willy Tarreau | ddab05b | 2022-07-30 09:53:22 +0200 | [diff] [blame] | 383 | while (!stop && (done < global.tune.maxpollevents || !s->served) && |
| 384 | s->served < (maxconn = srv_dynamic_maxconn(s))) { |
Willy Tarreau | 47ee44f | 2021-06-24 16:00:18 +0200 | [diff] [blame] | 385 | if (HA_SPIN_TRYLOCK(QUEUE_LOCK, &s->queue.lock) != 0) |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 386 | break; |
Willy Tarreau | ae0b12e | 2021-06-24 08:30:07 +0200 | [diff] [blame] | 387 | |
| 388 | while (s->served < maxconn) { |
Willy Tarreau | 19c5581 | 2021-06-24 15:51:12 +0200 | [diff] [blame] | 389 | stop = !pendconn_process_next_strm(s, p, px_ok); |
| 390 | if (stop) |
Willy Tarreau | ae0b12e | 2021-06-24 08:30:07 +0200 | [diff] [blame] | 391 | break; |
| 392 | _HA_ATOMIC_INC(&s->served); |
| 393 | done++; |
Willy Tarreau | ddab05b | 2022-07-30 09:53:22 +0200 | [diff] [blame] | 394 | if (done >= global.tune.maxpollevents) |
| 395 | break; |
Willy Tarreau | ae0b12e | 2021-06-24 08:30:07 +0200 | [diff] [blame] | 396 | } |
Willy Tarreau | 47ee44f | 2021-06-24 16:00:18 +0200 | [diff] [blame] | 397 | HA_SPIN_UNLOCK(QUEUE_LOCK, &s->queue.lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 398 | } |
Willy Tarreau | 9cef43a | 2021-06-24 07:47:08 +0200 | [diff] [blame] | 399 | |
| 400 | if (done) { |
| 401 | _HA_ATOMIC_SUB(&p->totpend, done); |
| 402 | _HA_ATOMIC_ADD(&p->served, done); |
| 403 | __ha_barrier_atomic_store(); |
| 404 | if (p->lbprm.server_take_conn) |
| 405 | p->lbprm.server_take_conn(s); |
| 406 | } |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 409 | /* Adds the stream <strm> to the pending connection queue of server <strm>->srv |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 410 | * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 412 | * pendconn itself. If the stream was already marked as served, its flag is |
| 413 | * cleared. It is illegal to call this function with a non-NULL strm->srv_conn. |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 414 | * The stream's queue position is counted with an offset of -1 because we want |
| 415 | * to make sure that being at the first position in the queue reports 1. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 416 | * |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 417 | * The queue is sorted by the composition of the priority_class, and the current |
| 418 | * timestamp offset by strm->priority_offset. The timestamp is in milliseconds |
| 419 | * and truncated to 20 bits, so will wrap every 17m28s575ms. |
| 420 | * The offset can be positive or negative, and an offset of 0 puts it in the |
| 421 | * middle of this range (~ 8 min). Note that this also means if the adjusted |
| 422 | * timestamp wraps around, the request will be misinterpreted as being of |
Joseph Herlant | d8499ec | 2018-11-25 11:26:48 -0800 | [diff] [blame] | 423 | * the highest priority for that priority class. |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 424 | * |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 425 | * This function must be called by the stream itself, so in the context of |
| 426 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 427 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 428 | struct pendconn *pendconn_add(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 429 | { |
| 430 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 431 | struct proxy *px; |
| 432 | struct server *srv; |
Willy Tarreau | 12529c0 | 2021-06-18 10:21:20 +0200 | [diff] [blame] | 433 | struct queue *q; |
| 434 | unsigned int *max_ptr; |
| 435 | unsigned int old_max, new_max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 436 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 437 | p = pool_alloc(pool_head_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 438 | if (!p) |
| 439 | return NULL; |
| 440 | |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 441 | p->target = NULL; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 442 | p->node.key = MAKE_KEY(strm->priority_class, strm->priority_offset); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 443 | p->strm = strm; |
| 444 | p->strm_flags = strm->flags; |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 445 | HA_SPIN_INIT(&p->del_lock); |
Willy Tarreau | 901972e | 2021-06-18 10:33:47 +0200 | [diff] [blame] | 446 | strm->pend_pos = p; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 447 | |
Willy Tarreau | 51c63f0 | 2021-06-23 16:43:45 +0200 | [diff] [blame] | 448 | px = strm->be; |
| 449 | if (strm->flags & SF_ASSIGNED) |
| 450 | srv = objt_server(strm->target); |
| 451 | else |
| 452 | srv = NULL; |
| 453 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 454 | if (srv) { |
Willy Tarreau | 12529c0 | 2021-06-18 10:21:20 +0200 | [diff] [blame] | 455 | q = &srv->queue; |
| 456 | max_ptr = &srv->counters.nbpend_max; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 457 | } |
| 458 | else { |
Willy Tarreau | 12529c0 | 2021-06-18 10:21:20 +0200 | [diff] [blame] | 459 | q = &px->queue; |
| 460 | max_ptr = &px->be_counters.nbpend_max; |
| 461 | } |
Willy Tarreau | 3eecdb6 | 2021-06-18 10:21:20 +0200 | [diff] [blame] | 462 | |
Willy Tarreau | 8429097 | 2021-06-23 16:33:52 +0200 | [diff] [blame] | 463 | p->queue = q; |
Willy Tarreau | 98c8910 | 2021-06-18 10:51:58 +0200 | [diff] [blame] | 464 | p->queue_idx = _HA_ATOMIC_LOAD(&q->idx) - 1; // for logging only |
Willy Tarreau | 12529c0 | 2021-06-18 10:21:20 +0200 | [diff] [blame] | 465 | new_max = _HA_ATOMIC_ADD_FETCH(&q->length, 1); |
| 466 | old_max = _HA_ATOMIC_LOAD(max_ptr); |
| 467 | while (new_max > old_max) { |
| 468 | if (likely(_HA_ATOMIC_CAS(max_ptr, &old_max, new_max))) |
| 469 | break; |
Willy Tarreau | 58f4dfb | 2021-06-24 07:22:15 +0200 | [diff] [blame] | 470 | } |
Willy Tarreau | 12529c0 | 2021-06-18 10:21:20 +0200 | [diff] [blame] | 471 | __ha_barrier_atomic_store(); |
| 472 | |
| 473 | HA_SPIN_LOCK(QUEUE_LOCK, &q->lock); |
Willy Tarreau | 12529c0 | 2021-06-18 10:21:20 +0200 | [diff] [blame] | 474 | eb32_insert(&q->head, &p->node); |
| 475 | HA_SPIN_UNLOCK(QUEUE_LOCK, &q->lock); |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 476 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 477 | _HA_ATOMIC_INC(&px->totpend); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 478 | return p; |
| 479 | } |
| 480 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 481 | /* Redistribute pending connections when a server goes down. The number of |
Willy Tarreau | 16fbdda | 2021-06-18 09:45:27 +0200 | [diff] [blame] | 482 | * connections redistributed is returned. It will take the server queue lock |
| 483 | * and does not use nor depend on other locks. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 484 | */ |
| 485 | int pendconn_redistribute(struct server *s) |
| 486 | { |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 487 | struct pendconn *p; |
Willy Tarreau | bff005a | 2019-05-27 08:10:11 +0200 | [diff] [blame] | 488 | struct eb32_node *node, *nodeb; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 489 | int xferred = 0; |
| 490 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 491 | /* The REDISP option was specified. We will ignore cookie and force to |
| 492 | * balance or use the dispatcher. */ |
| 493 | if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP) |
| 494 | return 0; |
| 495 | |
Willy Tarreau | 47ee44f | 2021-06-24 16:00:18 +0200 | [diff] [blame] | 496 | HA_SPIN_LOCK(QUEUE_LOCK, &s->queue.lock); |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 497 | for (node = eb32_first(&s->queue.head); node; node = nodeb) { |
Willy Tarreau | bff005a | 2019-05-27 08:10:11 +0200 | [diff] [blame] | 498 | nodeb = eb32_next(node); |
| 499 | |
| 500 | p = eb32_entry(node, struct pendconn, node); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 501 | if (p->strm_flags & SF_FORCE_PRST) |
| 502 | continue; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 503 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 504 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 505 | __pendconn_unlink_srv(p); |
Willy Tarreau | 03bd395 | 2022-05-02 16:36:47 +0200 | [diff] [blame] | 506 | p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 507 | |
Willy Tarreau | 3fdacdd | 2022-06-16 16:10:05 +0200 | [diff] [blame] | 508 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Willy Tarreau | ef71f01 | 2020-10-21 11:54:38 +0200 | [diff] [blame] | 509 | xferred++; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 510 | } |
Willy Tarreau | 47ee44f | 2021-06-24 16:00:18 +0200 | [diff] [blame] | 511 | HA_SPIN_UNLOCK(QUEUE_LOCK, &s->queue.lock); |
Willy Tarreau | 16fbdda | 2021-06-18 09:45:27 +0200 | [diff] [blame] | 512 | |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 513 | if (xferred) { |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 514 | _HA_ATOMIC_SUB(&s->queue.length, xferred); |
Willy Tarreau | 5472aa5 | 2020-10-24 12:57:41 +0200 | [diff] [blame] | 515 | _HA_ATOMIC_SUB(&s->proxy->totpend, xferred); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 516 | } |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 517 | return xferred; |
| 518 | } |
| 519 | |
| 520 | /* Check for pending connections at the backend, and assign some of them to |
| 521 | * the server coming up. The server's weight is checked before being assigned |
| 522 | * connections it may not be able to handle. The total number of transferred |
Willy Tarreau | 16fbdda | 2021-06-18 09:45:27 +0200 | [diff] [blame] | 523 | * connections is returned. It will take the proxy's queue lock and will not |
| 524 | * use nor depend on other locks. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 525 | */ |
| 526 | int pendconn_grab_from_px(struct server *s) |
| 527 | { |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 528 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 529 | int maxconn, xferred = 0; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 530 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 531 | if (!srv_currently_usable(s)) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 532 | return 0; |
| 533 | |
Willy Tarreau | a869465 | 2018-08-07 10:44:58 +0200 | [diff] [blame] | 534 | /* if this is a backup server and there are active servers or at |
| 535 | * least another backup server was elected, then this one must |
| 536 | * not dequeue requests from the proxy. |
| 537 | */ |
| 538 | if ((s->flags & SRV_F_BACKUP) && |
| 539 | (s->proxy->srv_act || |
| 540 | ((s != s->proxy->lbprm.fbck) && !(s->proxy->options & PR_O_USE_ALL_BK)))) |
| 541 | return 0; |
| 542 | |
Willy Tarreau | 16fbdda | 2021-06-18 09:45:27 +0200 | [diff] [blame] | 543 | HA_SPIN_LOCK(QUEUE_LOCK, &s->proxy->queue.lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 544 | maxconn = srv_dynamic_maxconn(s); |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 545 | while ((p = pendconn_first(&s->proxy->queue.head))) { |
| 546 | if (s->maxconn && s->served + xferred >= maxconn) |
| 547 | break; |
Willy Tarreau | 772e968 | 2021-06-18 20:32:50 +0200 | [diff] [blame] | 548 | |
Willy Tarreau | 2bf3f2c | 2021-06-24 07:20:26 +0200 | [diff] [blame] | 549 | __pendconn_unlink_prx(p); |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 550 | p->target = s; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 551 | |
Willy Tarreau | 3fdacdd | 2022-06-16 16:10:05 +0200 | [diff] [blame] | 552 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 553 | xferred++; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 554 | } |
Willy Tarreau | 16fbdda | 2021-06-18 09:45:27 +0200 | [diff] [blame] | 555 | HA_SPIN_UNLOCK(QUEUE_LOCK, &s->proxy->queue.lock); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 556 | if (xferred) { |
Willy Tarreau | 7f3c1df | 2021-06-18 09:22:21 +0200 | [diff] [blame] | 557 | _HA_ATOMIC_SUB(&s->proxy->queue.length, xferred); |
Willy Tarreau | 5472aa5 | 2020-10-24 12:57:41 +0200 | [diff] [blame] | 558 | _HA_ATOMIC_SUB(&s->proxy->totpend, xferred); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 559 | } |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 560 | return xferred; |
| 561 | } |
| 562 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 563 | /* Try to dequeue pending connection attached to the stream <strm>. It must |
| 564 | * always exists here. If the pendconn is still linked to the server or the |
| 565 | * proxy queue, nothing is done and the function returns 1. Otherwise, |
| 566 | * <strm>->flags and <strm>->target are updated, the pendconn is released and 0 |
| 567 | * is returned. |
| 568 | * |
| 569 | * This function must be called by the stream itself, so in the context of |
| 570 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 571 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 572 | int pendconn_dequeue(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 573 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 574 | struct pendconn *p; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 575 | int is_unlinked; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 576 | |
Tim Duesterhus | cc8348f | 2022-02-28 19:16:31 +0100 | [diff] [blame] | 577 | /* unexpected case because it is called by the stream itself and |
| 578 | * only the stream can release a pendconn. So it is only |
| 579 | * possible if a pendconn is released by someone else or if the |
| 580 | * stream is supposed to be queued but without its associated |
| 581 | * pendconn. In both cases it is a bug! */ |
| 582 | BUG_ON(!strm->pend_pos); |
| 583 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 584 | p = strm->pend_pos; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 585 | |
| 586 | /* note below : we need to grab the queue's lock to check for emptiness |
| 587 | * because we don't want a partial _grab_from_px() or _redistribute() |
| 588 | * to be called in parallel and show an empty list without having the |
| 589 | * time to finish. With this we know that if we see the element |
| 590 | * unlinked, these functions were completely done. |
| 591 | */ |
| 592 | pendconn_queue_lock(p); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 593 | is_unlinked = !p->node.node.leaf_p; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 594 | pendconn_queue_unlock(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 595 | |
Willy Tarreau | 87154e3 | 2021-08-31 17:21:39 +0200 | [diff] [blame] | 596 | /* serialize to make sure the element was finished processing */ |
| 597 | HA_SPIN_LOCK(QUEUE_LOCK, &p->del_lock); |
| 598 | HA_SPIN_UNLOCK(QUEUE_LOCK, &p->del_lock); |
| 599 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 600 | if (!is_unlinked) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 601 | return 1; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 602 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 603 | /* the pendconn is not queued anymore and will not be so we're safe |
| 604 | * to proceed. |
| 605 | */ |
Willy Tarreau | 03bd395 | 2022-05-02 16:36:47 +0200 | [diff] [blame] | 606 | strm->flags &= ~(SF_DIRECT | SF_ASSIGNED); |
| 607 | strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED); |
Willy Tarreau | 7867ceb | 2021-06-16 08:42:23 +0200 | [diff] [blame] | 608 | |
Willy Tarreau | 266d540 | 2021-12-24 11:27:53 +0100 | [diff] [blame] | 609 | /* the entry might have been redistributed to another server */ |
Willy Tarreau | 03bd395 | 2022-05-02 16:36:47 +0200 | [diff] [blame] | 610 | if (!(strm->flags & SF_ASSIGNED)) |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 611 | sockaddr_free(&strm->scb->dst); |
Willy Tarreau | 266d540 | 2021-12-24 11:27:53 +0100 | [diff] [blame] | 612 | |
Willy Tarreau | 7867ceb | 2021-06-16 08:42:23 +0200 | [diff] [blame] | 613 | if (p->target) { |
| 614 | /* a server picked this pendconn, it must skip LB */ |
| 615 | strm->target = &p->target->obj_type; |
| 616 | strm->flags |= SF_ASSIGNED; |
| 617 | } |
| 618 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 619 | strm->pend_pos = NULL; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 620 | pool_free(pool_head_pendconn, p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 621 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 624 | static enum act_return action_set_priority_class(struct act_rule *rule, struct proxy *px, |
| 625 | struct session *sess, struct stream *s, int flags) |
| 626 | { |
| 627 | struct sample *smp; |
| 628 | |
| 629 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 630 | if (!smp) |
| 631 | return ACT_RET_CONT; |
| 632 | |
| 633 | s->priority_class = queue_limit_class(smp->data.u.sint); |
| 634 | return ACT_RET_CONT; |
| 635 | } |
| 636 | |
| 637 | static enum act_return action_set_priority_offset(struct act_rule *rule, struct proxy *px, |
| 638 | struct session *sess, struct stream *s, int flags) |
| 639 | { |
| 640 | struct sample *smp; |
| 641 | |
| 642 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 643 | if (!smp) |
| 644 | return ACT_RET_CONT; |
| 645 | |
| 646 | s->priority_offset = queue_limit_offset(smp->data.u.sint); |
| 647 | |
| 648 | return ACT_RET_CONT; |
| 649 | } |
| 650 | |
| 651 | static enum act_parse_ret parse_set_priority_class(const char **args, int *arg, struct proxy *px, |
| 652 | struct act_rule *rule, char **err) |
| 653 | { |
| 654 | unsigned int where = 0; |
| 655 | |
| 656 | rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 657 | px->conf.args.line, err, &px->conf.args, NULL); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 658 | if (!rule->arg.expr) |
| 659 | return ACT_RET_PRS_ERR; |
| 660 | |
| 661 | if (px->cap & PR_CAP_FE) |
| 662 | where |= SMP_VAL_FE_HRQ_HDR; |
| 663 | if (px->cap & PR_CAP_BE) |
| 664 | where |= SMP_VAL_BE_HRQ_HDR; |
| 665 | |
| 666 | if (!(rule->arg.expr->fetch->val & where)) { |
| 667 | memprintf(err, |
| 668 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 669 | args[0], sample_src_names(rule->arg.expr->fetch->use)); |
| 670 | free(rule->arg.expr); |
| 671 | return ACT_RET_PRS_ERR; |
| 672 | } |
| 673 | |
| 674 | rule->action = ACT_CUSTOM; |
| 675 | rule->action_ptr = action_set_priority_class; |
| 676 | return ACT_RET_PRS_OK; |
| 677 | } |
| 678 | |
| 679 | static enum act_parse_ret parse_set_priority_offset(const char **args, int *arg, struct proxy *px, |
| 680 | struct act_rule *rule, char **err) |
| 681 | { |
| 682 | unsigned int where = 0; |
| 683 | |
| 684 | rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 685 | px->conf.args.line, err, &px->conf.args, NULL); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 686 | if (!rule->arg.expr) |
| 687 | return ACT_RET_PRS_ERR; |
| 688 | |
| 689 | if (px->cap & PR_CAP_FE) |
| 690 | where |= SMP_VAL_FE_HRQ_HDR; |
| 691 | if (px->cap & PR_CAP_BE) |
| 692 | where |= SMP_VAL_BE_HRQ_HDR; |
| 693 | |
| 694 | if (!(rule->arg.expr->fetch->val & where)) { |
| 695 | memprintf(err, |
| 696 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 697 | args[0], sample_src_names(rule->arg.expr->fetch->use)); |
| 698 | free(rule->arg.expr); |
| 699 | return ACT_RET_PRS_ERR; |
| 700 | } |
| 701 | |
| 702 | rule->action = ACT_CUSTOM; |
| 703 | rule->action_ptr = action_set_priority_offset; |
| 704 | return ACT_RET_PRS_OK; |
| 705 | } |
| 706 | |
| 707 | static struct action_kw_list tcp_cont_kws = {ILH, { |
| 708 | { "set-priority-class", parse_set_priority_class }, |
| 709 | { "set-priority-offset", parse_set_priority_offset }, |
| 710 | { /* END */ } |
| 711 | }}; |
| 712 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 713 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_cont_kws); |
| 714 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 715 | static struct action_kw_list http_req_kws = {ILH, { |
| 716 | { "set-priority-class", parse_set_priority_class }, |
| 717 | { "set-priority-offset", parse_set_priority_offset }, |
| 718 | { /* END */ } |
| 719 | }}; |
| 720 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 721 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws); |
| 722 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 723 | static int |
| 724 | smp_fetch_priority_class(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 725 | { |
| 726 | if (!smp->strm) |
| 727 | return 0; |
| 728 | |
| 729 | smp->data.type = SMP_T_SINT; |
| 730 | smp->data.u.sint = smp->strm->priority_class; |
| 731 | |
| 732 | return 1; |
| 733 | } |
| 734 | |
| 735 | static int |
| 736 | smp_fetch_priority_offset(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 737 | { |
| 738 | if (!smp->strm) |
| 739 | return 0; |
| 740 | |
| 741 | smp->data.type = SMP_T_SINT; |
| 742 | smp->data.u.sint = smp->strm->priority_offset; |
| 743 | |
| 744 | return 1; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 749 | { "prio_class", smp_fetch_priority_class, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 750 | { "prio_offset", smp_fetch_priority_offset, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 751 | { /* END */}, |
| 752 | }}; |
| 753 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 754 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 755 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 756 | /* |
| 757 | * Local variables: |
| 758 | * c-indent-level: 8 |
| 759 | * c-basic-offset: 8 |
| 760 | * End: |
| 761 | */ |