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 | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 81 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 82 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 83 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 84 | #include <haproxy/thread.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 85 | #include <haproxy/time.h> |
Willy Tarreau | c1a689f | 2021-05-08 13:59:05 +0200 | [diff] [blame] | 86 | #include <haproxy/tools.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | |
| 88 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 89 | #define NOW_OFFSET_BOUNDARY() ((now_ms - (TIMER_LOOK_BACK >> 12)) & 0xfffff) |
| 90 | #define KEY_CLASS(key) ((u32)key & 0xfff00000) |
| 91 | #define KEY_OFFSET(key) ((u32)key & 0x000fffff) |
| 92 | #define KEY_CLASS_OFFSET_BOUNDARY(key) (KEY_CLASS(key) | NOW_OFFSET_BOUNDARY()) |
| 93 | #define MAKE_KEY(class, offset) (((u32)(class + 0x7ff) << 20) | ((u32)(now_ms + offset) & 0xfffff)) |
| 94 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 95 | DECLARE_POOL(pool_head_pendconn, "pendconn", sizeof(struct pendconn)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | |
| 97 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 98 | * 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] | 99 | * expected that 0 < s->minconn <= s->maxconn when this is called. If the |
| 100 | * server is currently warming up, the slowstart is also applied to the |
| 101 | * resulting value, which can be lower than minconn in this case, but never |
| 102 | * less than 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 104 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 106 | unsigned int max; |
| 107 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 108 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 109 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 110 | max = s->maxconn; |
| 111 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 112 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 113 | max = s->maxconn; |
| 114 | else max = MAX(s->minconn, |
| 115 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 116 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 117 | if ((s->cur_state == SRV_ST_STARTING) && |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 118 | now.tv_sec < s->last_change + s->slowstart && |
| 119 | now.tv_sec >= s->last_change) { |
| 120 | unsigned int ratio; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 121 | ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; |
| 122 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 123 | } |
| 124 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 127 | /* 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] | 128 | * is not really dequeued. It will be done during the process_stream. It is |
| 129 | * up to the caller to atomically decrement the pending counts. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 130 | * |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 131 | * The caller must own the lock on the server queue. The pendconn must still be |
| 132 | * 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] | 133 | */ |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 134 | static void __pendconn_unlink_srv(struct pendconn *p) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 135 | { |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 136 | p->strm->logs.srv_queue_pos += p->srv->queue_idx - p->queue_idx; |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 137 | eb32_delete(&p->node); |
| 138 | } |
| 139 | |
| 140 | /* 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] | 141 | * is not really dequeued. It will be done during the process_stream. It is |
| 142 | * up to the caller to atomically decrement the pending counts. |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 143 | * |
| 144 | * The caller must own the lock on the proxy queue. The pendconn must still be |
| 145 | * queued (p->node.leaf_p != NULL) and must be in the proxy (p->srv == NULL). |
| 146 | */ |
| 147 | static void __pendconn_unlink_prx(struct pendconn *p) |
| 148 | { |
| 149 | p->strm->logs.prx_queue_pos += p->px->queue_idx - p->queue_idx; |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 150 | eb32_delete(&p->node); |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 153 | /* Locks the queue the pendconn element belongs to. This relies on both p->px |
| 154 | * and p->srv to be properly initialized (which is always the case once the |
| 155 | * element has been added). |
| 156 | */ |
| 157 | static inline void pendconn_queue_lock(struct pendconn *p) |
| 158 | { |
| 159 | if (p->srv) |
| 160 | HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock); |
| 161 | else |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 162 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->px->lock); |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Unlocks the queue the pendconn element belongs to. This relies on both p->px |
| 166 | * and p->srv to be properly initialized (which is always the case once the |
| 167 | * element has been added). |
| 168 | */ |
| 169 | static inline void pendconn_queue_unlock(struct pendconn *p) |
| 170 | { |
| 171 | if (p->srv) |
| 172 | HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock); |
| 173 | else |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 174 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->px->lock); |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 177 | /* Removes the pendconn from the server/proxy queue. At this stage, the |
| 178 | * connection is not really dequeued. It will be done during process_stream(). |
Willy Tarreau | 9ada030 | 2019-11-14 14:58:39 +0100 | [diff] [blame] | 179 | * This function takes all the required locks for the operation. The pendconn |
| 180 | * must be valid, though it doesn't matter if it was already unlinked. Prefer |
| 181 | * pendconn_cond_unlink() to first check <p>. When the locks are already held, |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 182 | * please use __pendconn_unlink_{srv,prx}() instead. |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 183 | */ |
| 184 | void pendconn_unlink(struct pendconn *p) |
| 185 | { |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 186 | int done = 0; |
| 187 | |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 188 | if (p->srv) { |
| 189 | /* queued in the server */ |
| 190 | HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 191 | if (p->node.node.leaf_p) { |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 192 | __pendconn_unlink_srv(p); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 193 | done = 1; |
| 194 | } |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 195 | HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 196 | if (done) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 197 | _HA_ATOMIC_DEC(&p->srv->nbpend); |
| 198 | _HA_ATOMIC_DEC(&p->px->totpend); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 199 | } |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 200 | } |
| 201 | else { |
| 202 | /* queued in the proxy */ |
| 203 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->px->lock); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 204 | if (p->node.node.leaf_p) { |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 205 | __pendconn_unlink_prx(p); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 206 | done = 1; |
| 207 | } |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 208 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->px->lock); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 209 | if (done) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 210 | _HA_ATOMIC_DEC(&p->px->nbpend); |
| 211 | _HA_ATOMIC_DEC(&p->px->totpend); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 212 | } |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 213 | } |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 216 | /* Retrieve the first pendconn from tree <pendconns>. Classes are always |
| 217 | * considered first, then the time offset. The time does wrap, so the |
| 218 | * lookup is performed twice, one to retrieve the first class and a second |
| 219 | * time to retrieve the earliest time in this class. |
| 220 | */ |
| 221 | static struct pendconn *pendconn_first(struct eb_root *pendconns) |
| 222 | { |
| 223 | struct eb32_node *node, *node2 = NULL; |
| 224 | u32 key; |
| 225 | |
| 226 | node = eb32_first(pendconns); |
| 227 | if (!node) |
| 228 | return NULL; |
| 229 | |
| 230 | key = KEY_CLASS_OFFSET_BOUNDARY(node->key); |
| 231 | node2 = eb32_lookup_ge(pendconns, key); |
| 232 | |
| 233 | if (!node2 || |
| 234 | KEY_CLASS(node2->key) != KEY_CLASS(node->key)) { |
| 235 | /* no other key in the tree, or in this class */ |
| 236 | return eb32_entry(node, struct pendconn, node); |
| 237 | } |
| 238 | |
| 239 | /* found a better key */ |
| 240 | return eb32_entry(node2, struct pendconn, node); |
| 241 | } |
| 242 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 243 | /* 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] | 244 | * returns a strictly positive value on success (see below). If no pending |
| 245 | * connection is found, 0 is returned. Note that neither <srv> nor <px> may be |
| 246 | * NULL. Priority is given to the oldest request in the queue if both <srv> and |
| 247 | * <px> have pending requests. This ensures that no request will be left |
| 248 | * unserved. The <px> queue is not considered if the server (or a tracked |
| 249 | * server) is not RUNNING, is disabled, or has a null weight (server going |
| 250 | * down). The <srv> queue is still considered in this case, because if some |
| 251 | * connections remain there, it means that some requests have been forced there |
| 252 | * after it was seen down (eg: due to option persist). The stream is |
| 253 | * immediately marked as "assigned", and both its <srv> and <srv_conn> are set |
| 254 | * to <srv>. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 255 | * |
| 256 | * This function must only be called if the server queue _AND_ the proxy queue |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 257 | * are locked. Today it is only called by process_srv_queue. When a pending |
| 258 | * connection is dequeued, this function returns 1 if the pending connection can |
| 259 | * be handled by the current thread, else it returns 2. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 260 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 261 | static int pendconn_process_next_strm(struct server *srv, struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 262 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 263 | struct pendconn *p = NULL; |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 264 | struct pendconn *pp = NULL; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 265 | struct server *rsrv; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 266 | u32 pkey, ppkey; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 267 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 268 | rsrv = srv->track; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 269 | if (!rsrv) |
| 270 | rsrv = srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 271 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 272 | p = NULL; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 273 | if (srv->nbpend) |
| 274 | p = pendconn_first(&srv->pendconns); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 275 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 276 | pp = NULL; |
Willy Tarreau | a869465 | 2018-08-07 10:44:58 +0200 | [diff] [blame] | 277 | if (srv_currently_usable(rsrv) && px->nbpend && |
| 278 | (!(srv->flags & SRV_F_BACKUP) || |
| 279 | (!px->srv_act && |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 280 | (srv == px->lbprm.fbck || (px->options & PR_O_USE_ALL_BK))))) |
| 281 | pp = pendconn_first(&px->pendconns); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 282 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 283 | if (!p && !pp) |
| 284 | return 0; |
Christopher Faulet | cd7126b | 2021-02-11 11:13:33 +0100 | [diff] [blame] | 285 | else if (!pp) |
| 286 | goto use_p; /* p != NULL */ |
| 287 | else if (!p) |
| 288 | goto use_pp; /* pp != NULL */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 289 | |
Christopher Faulet | cd7126b | 2021-02-11 11:13:33 +0100 | [diff] [blame] | 290 | /* p != NULL && pp != NULL*/ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 291 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 292 | if (KEY_CLASS(p->node.key) < KEY_CLASS(pp->node.key)) |
| 293 | goto use_p; |
| 294 | |
| 295 | if (KEY_CLASS(pp->node.key) < KEY_CLASS(p->node.key)) |
| 296 | goto use_pp; |
| 297 | |
| 298 | pkey = KEY_OFFSET(p->node.key); |
| 299 | ppkey = KEY_OFFSET(pp->node.key); |
| 300 | |
| 301 | if (pkey < NOW_OFFSET_BOUNDARY()) |
| 302 | pkey += 0x100000; // key in the future |
| 303 | |
| 304 | if (ppkey < NOW_OFFSET_BOUNDARY()) |
| 305 | ppkey += 0x100000; // key in the future |
| 306 | |
| 307 | if (pkey <= ppkey) |
| 308 | goto use_p; |
| 309 | |
| 310 | use_pp: |
| 311 | /* Let's switch from the server pendconn to the proxy pendconn */ |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 312 | __pendconn_unlink_prx(pp); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 313 | _HA_ATOMIC_DEC(&px->nbpend); |
| 314 | _HA_ATOMIC_DEC(&px->totpend); |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 315 | px->queue_idx++; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 316 | p = pp; |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 317 | goto unlinked; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 318 | use_p: |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 319 | __pendconn_unlink_srv(p); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 320 | _HA_ATOMIC_DEC(&srv->nbpend); |
| 321 | _HA_ATOMIC_DEC(&px->totpend); |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 322 | srv->queue_idx++; |
| 323 | unlinked: |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 324 | p->strm_flags |= SF_ASSIGNED; |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 325 | p->target = srv; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 326 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 327 | _HA_ATOMIC_INC(&srv->served); |
| 328 | _HA_ATOMIC_INC(&srv->proxy->served); |
Olivier Houchard | b4df492 | 2019-03-08 18:54:16 +0100 | [diff] [blame] | 329 | __ha_barrier_atomic_store(); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 330 | if (px->lbprm.server_take_conn) |
Willy Tarreau | 59b0fec | 2021-02-17 16:01:37 +0100 | [diff] [blame] | 331 | px->lbprm.server_take_conn(srv, 1); |
Willy Tarreau | 751153e | 2021-02-17 13:33:24 +0100 | [diff] [blame] | 332 | stream_add_srv_conn(p->strm, srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 333 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 334 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 335 | |
Olivier Houchard | ecfe673 | 2018-07-26 18:47:27 +0200 | [diff] [blame] | 336 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 339 | /* Manages a server's connection queue. This function will try to dequeue as |
Amaury Denoyelle | caaafd0 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 340 | * many pending streams as possible, and wake them up. <server_locked> must |
| 341 | * only be set if the caller already hold the server lock. |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 342 | */ |
Amaury Denoyelle | caaafd0 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 343 | void process_srv_queue(struct server *s, int server_locked) |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 344 | { |
| 345 | struct proxy *p = s->proxy; |
Olivier Houchard | ecfe673 | 2018-07-26 18:47:27 +0200 | [diff] [blame] | 346 | int maxconn; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 347 | |
Amaury Denoyelle | caaafd0 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 348 | if (!server_locked) |
| 349 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 350 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 351 | maxconn = srv_dynamic_maxconn(s); |
| 352 | while (s->served < maxconn) { |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 353 | int ret = pendconn_process_next_strm(s, p); |
| 354 | if (!ret) |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 355 | break; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 356 | } |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 357 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
Amaury Denoyelle | caaafd0 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 358 | if (!server_locked) |
| 359 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 362 | /* 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] | 363 | * 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] | 364 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 365 | * pendconn itself. If the stream was already marked as served, its flag is |
| 366 | * 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] | 367 | * The stream's queue position is counted with an offset of -1 because we want |
| 368 | * 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] | 369 | * |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 370 | * The queue is sorted by the composition of the priority_class, and the current |
| 371 | * timestamp offset by strm->priority_offset. The timestamp is in milliseconds |
| 372 | * and truncated to 20 bits, so will wrap every 17m28s575ms. |
| 373 | * The offset can be positive or negative, and an offset of 0 puts it in the |
| 374 | * middle of this range (~ 8 min). Note that this also means if the adjusted |
| 375 | * timestamp wraps around, the request will be misinterpreted as being of |
Joseph Herlant | d8499ec | 2018-11-25 11:26:48 -0800 | [diff] [blame] | 376 | * the highest priority for that priority class. |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 377 | * |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 378 | * This function must be called by the stream itself, so in the context of |
| 379 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 380 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 381 | struct pendconn *pendconn_add(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 382 | { |
| 383 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 384 | struct proxy *px; |
| 385 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 386 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 387 | p = pool_alloc(pool_head_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 388 | if (!p) |
| 389 | return NULL; |
| 390 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 391 | if (strm->flags & SF_ASSIGNED) |
| 392 | srv = objt_server(strm->target); |
| 393 | else |
| 394 | srv = NULL; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 395 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 396 | px = strm->be; |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 397 | p->target = NULL; |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 398 | p->srv = srv; |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 399 | p->node.key = MAKE_KEY(strm->priority_class, strm->priority_offset); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 400 | p->px = px; |
| 401 | p->strm = strm; |
| 402 | p->strm_flags = strm->flags; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 403 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 404 | if (srv) { |
Willy Tarreau | 56c1cfb | 2020-10-21 11:45:44 +0200 | [diff] [blame] | 405 | unsigned int old_max, new_max; |
| 406 | |
Willy Tarreau | 1db4273 | 2021-04-06 11:44:07 +0200 | [diff] [blame] | 407 | new_max = _HA_ATOMIC_ADD_FETCH(&srv->nbpend, 1); |
Willy Tarreau | 56c1cfb | 2020-10-21 11:45:44 +0200 | [diff] [blame] | 408 | old_max = srv->counters.nbpend_max; |
| 409 | while (new_max > old_max) { |
| 410 | if (likely(_HA_ATOMIC_CAS(&srv->counters.nbpend_max, &old_max, new_max))) |
| 411 | break; |
| 412 | } |
| 413 | __ha_barrier_atomic_store(); |
| 414 | |
Willy Tarreau | c7eedf7 | 2020-10-21 11:31:12 +0200 | [diff] [blame] | 415 | HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock); |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 416 | p->queue_idx = srv->queue_idx - 1; // for increment |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 417 | eb32_insert(&srv->pendconns, &p->node); |
Willy Tarreau | c7eedf7 | 2020-10-21 11:31:12 +0200 | [diff] [blame] | 418 | HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 419 | } |
| 420 | else { |
Willy Tarreau | 56c1cfb | 2020-10-21 11:45:44 +0200 | [diff] [blame] | 421 | unsigned int old_max, new_max; |
| 422 | |
Willy Tarreau | 1db4273 | 2021-04-06 11:44:07 +0200 | [diff] [blame] | 423 | new_max = _HA_ATOMIC_ADD_FETCH(&px->nbpend, 1); |
Willy Tarreau | 56c1cfb | 2020-10-21 11:45:44 +0200 | [diff] [blame] | 424 | old_max = px->be_counters.nbpend_max; |
| 425 | while (new_max > old_max) { |
| 426 | if (likely(_HA_ATOMIC_CAS(&px->be_counters.nbpend_max, &old_max, new_max))) |
| 427 | break; |
| 428 | } |
| 429 | __ha_barrier_atomic_store(); |
| 430 | |
Willy Tarreau | c7eedf7 | 2020-10-21 11:31:12 +0200 | [diff] [blame] | 431 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->px->lock); |
Patrick Hemmer | da282f4 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 432 | p->queue_idx = px->queue_idx - 1; // for increment |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 433 | eb32_insert(&px->pendconns, &p->node); |
Willy Tarreau | c7eedf7 | 2020-10-21 11:31:12 +0200 | [diff] [blame] | 434 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->px->lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 435 | } |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 436 | strm->pend_pos = p; |
| 437 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 438 | _HA_ATOMIC_INC(&px->totpend); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 439 | return p; |
| 440 | } |
| 441 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 442 | /* Redistribute pending connections when a server goes down. The number of |
Willy Tarreau | deca26c | 2018-08-21 18:11:03 +0200 | [diff] [blame] | 443 | * connections redistributed is returned. It must be called with the server |
| 444 | * lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 445 | */ |
| 446 | int pendconn_redistribute(struct server *s) |
| 447 | { |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 448 | struct pendconn *p; |
Willy Tarreau | bff005a | 2019-05-27 08:10:11 +0200 | [diff] [blame] | 449 | struct eb32_node *node, *nodeb; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 450 | int xferred = 0; |
| 451 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 452 | /* The REDISP option was specified. We will ignore cookie and force to |
| 453 | * balance or use the dispatcher. */ |
| 454 | if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP) |
| 455 | return 0; |
| 456 | |
Willy Tarreau | bff005a | 2019-05-27 08:10:11 +0200 | [diff] [blame] | 457 | for (node = eb32_first(&s->pendconns); node; node = nodeb) { |
| 458 | nodeb = eb32_next(node); |
| 459 | |
| 460 | p = eb32_entry(node, struct pendconn, node); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 461 | if (p->strm_flags & SF_FORCE_PRST) |
| 462 | continue; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 463 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 464 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 465 | __pendconn_unlink_srv(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 466 | p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 467 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 468 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Willy Tarreau | ef71f01 | 2020-10-21 11:54:38 +0200 | [diff] [blame] | 469 | xferred++; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 470 | } |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 471 | if (xferred) { |
Willy Tarreau | 5472aa5 | 2020-10-24 12:57:41 +0200 | [diff] [blame] | 472 | _HA_ATOMIC_SUB(&s->nbpend, xferred); |
| 473 | _HA_ATOMIC_SUB(&s->proxy->totpend, xferred); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 474 | } |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 475 | return xferred; |
| 476 | } |
| 477 | |
| 478 | /* Check for pending connections at the backend, and assign some of them to |
| 479 | * the server coming up. The server's weight is checked before being assigned |
| 480 | * connections it may not be able to handle. The total number of transferred |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 481 | * connections is returned. It must be called with the server lock held, and |
| 482 | * will take the proxy's lock. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 483 | */ |
| 484 | int pendconn_grab_from_px(struct server *s) |
| 485 | { |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 486 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 487 | int maxconn, xferred = 0; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 488 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 489 | if (!srv_currently_usable(s)) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 490 | return 0; |
| 491 | |
Willy Tarreau | a869465 | 2018-08-07 10:44:58 +0200 | [diff] [blame] | 492 | /* if this is a backup server and there are active servers or at |
| 493 | * least another backup server was elected, then this one must |
| 494 | * not dequeue requests from the proxy. |
| 495 | */ |
| 496 | if ((s->flags & SRV_F_BACKUP) && |
| 497 | (s->proxy->srv_act || |
| 498 | ((s != s->proxy->lbprm.fbck) && !(s->proxy->options & PR_O_USE_ALL_BK)))) |
| 499 | return 0; |
| 500 | |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 501 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &s->proxy->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 502 | maxconn = srv_dynamic_maxconn(s); |
Patrick Hemmer | 248cb4c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 503 | while ((p = pendconn_first(&s->proxy->pendconns))) { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 504 | if (s->maxconn && s->served + xferred >= maxconn) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 505 | break; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 506 | |
Willy Tarreau | 3e3ae25 | 2020-10-21 11:20:07 +0200 | [diff] [blame] | 507 | __pendconn_unlink_prx(p); |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 508 | p->target = s; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 509 | |
| 510 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 511 | xferred++; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 512 | } |
Willy Tarreau | ac66d6b | 2020-10-20 17:24:27 +0200 | [diff] [blame] | 513 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &s->proxy->lock); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 514 | if (xferred) { |
Willy Tarreau | 5472aa5 | 2020-10-24 12:57:41 +0200 | [diff] [blame] | 515 | _HA_ATOMIC_SUB(&s->proxy->nbpend, xferred); |
| 516 | _HA_ATOMIC_SUB(&s->proxy->totpend, xferred); |
Willy Tarreau | 96bca33 | 2020-10-21 12:01:28 +0200 | [diff] [blame] | 517 | } |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 518 | return xferred; |
| 519 | } |
| 520 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 521 | /* Try to dequeue pending connection attached to the stream <strm>. It must |
| 522 | * always exists here. If the pendconn is still linked to the server or the |
| 523 | * proxy queue, nothing is done and the function returns 1. Otherwise, |
| 524 | * <strm>->flags and <strm>->target are updated, the pendconn is released and 0 |
| 525 | * is returned. |
| 526 | * |
| 527 | * This function must be called by the stream itself, so in the context of |
| 528 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 529 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 530 | int pendconn_dequeue(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 531 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 532 | struct pendconn *p; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 533 | int is_unlinked; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 534 | |
| 535 | if (unlikely(!strm->pend_pos)) { |
| 536 | /* unexpected case because it is called by the stream itself and |
| 537 | * only the stream can release a pendconn. So it is only |
| 538 | * possible if a pendconn is released by someone else or if the |
| 539 | * stream is supposed to be queued but without its associated |
| 540 | * pendconn. In both cases it is a bug! */ |
| 541 | abort(); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 542 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 543 | p = strm->pend_pos; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 544 | |
| 545 | /* note below : we need to grab the queue's lock to check for emptiness |
| 546 | * because we don't want a partial _grab_from_px() or _redistribute() |
| 547 | * to be called in parallel and show an empty list without having the |
| 548 | * time to finish. With this we know that if we see the element |
| 549 | * unlinked, these functions were completely done. |
| 550 | */ |
| 551 | pendconn_queue_lock(p); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 552 | is_unlinked = !p->node.node.leaf_p; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 553 | pendconn_queue_unlock(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 554 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 555 | if (!is_unlinked) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 556 | return 1; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 557 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 558 | /* the pendconn is not queued anymore and will not be so we're safe |
| 559 | * to proceed. |
| 560 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 561 | strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 562 | strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
Willy Tarreau | a6cce19 | 2021-06-16 08:42:23 +0200 | [diff] [blame] | 563 | |
Willy Tarreau | 0a55591 | 2021-12-24 11:27:53 +0100 | [diff] [blame] | 564 | /* the entry might have been redistributed to another server */ |
| 565 | if (!(strm->flags & SF_ADDR_SET)) |
| 566 | sockaddr_free(&strm->target_addr); |
| 567 | |
Willy Tarreau | a6cce19 | 2021-06-16 08:42:23 +0200 | [diff] [blame] | 568 | if (p->target) { |
| 569 | /* a server picked this pendconn, it must skip LB */ |
| 570 | strm->target = &p->target->obj_type; |
| 571 | strm->flags |= SF_ASSIGNED; |
| 572 | } |
| 573 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 574 | strm->pend_pos = NULL; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 575 | pool_free(pool_head_pendconn, p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 576 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 577 | } |
| 578 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 579 | static enum act_return action_set_priority_class(struct act_rule *rule, struct proxy *px, |
| 580 | struct session *sess, struct stream *s, int flags) |
| 581 | { |
| 582 | struct sample *smp; |
| 583 | |
| 584 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 585 | if (!smp) |
| 586 | return ACT_RET_CONT; |
| 587 | |
| 588 | s->priority_class = queue_limit_class(smp->data.u.sint); |
| 589 | return ACT_RET_CONT; |
| 590 | } |
| 591 | |
| 592 | static enum act_return action_set_priority_offset(struct act_rule *rule, struct proxy *px, |
| 593 | struct session *sess, struct stream *s, int flags) |
| 594 | { |
| 595 | struct sample *smp; |
| 596 | |
| 597 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 598 | if (!smp) |
| 599 | return ACT_RET_CONT; |
| 600 | |
| 601 | s->priority_offset = queue_limit_offset(smp->data.u.sint); |
| 602 | |
| 603 | return ACT_RET_CONT; |
| 604 | } |
| 605 | |
| 606 | static enum act_parse_ret parse_set_priority_class(const char **args, int *arg, struct proxy *px, |
| 607 | struct act_rule *rule, char **err) |
| 608 | { |
| 609 | unsigned int where = 0; |
| 610 | |
| 611 | 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] | 612 | px->conf.args.line, err, &px->conf.args, NULL); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 613 | if (!rule->arg.expr) |
| 614 | return ACT_RET_PRS_ERR; |
| 615 | |
| 616 | if (px->cap & PR_CAP_FE) |
| 617 | where |= SMP_VAL_FE_HRQ_HDR; |
| 618 | if (px->cap & PR_CAP_BE) |
| 619 | where |= SMP_VAL_BE_HRQ_HDR; |
| 620 | |
| 621 | if (!(rule->arg.expr->fetch->val & where)) { |
| 622 | memprintf(err, |
| 623 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 624 | args[0], sample_src_names(rule->arg.expr->fetch->use)); |
| 625 | free(rule->arg.expr); |
| 626 | return ACT_RET_PRS_ERR; |
| 627 | } |
| 628 | |
| 629 | rule->action = ACT_CUSTOM; |
| 630 | rule->action_ptr = action_set_priority_class; |
| 631 | return ACT_RET_PRS_OK; |
| 632 | } |
| 633 | |
| 634 | static enum act_parse_ret parse_set_priority_offset(const char **args, int *arg, struct proxy *px, |
| 635 | struct act_rule *rule, char **err) |
| 636 | { |
| 637 | unsigned int where = 0; |
| 638 | |
| 639 | 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] | 640 | px->conf.args.line, err, &px->conf.args, NULL); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 641 | if (!rule->arg.expr) |
| 642 | return ACT_RET_PRS_ERR; |
| 643 | |
| 644 | if (px->cap & PR_CAP_FE) |
| 645 | where |= SMP_VAL_FE_HRQ_HDR; |
| 646 | if (px->cap & PR_CAP_BE) |
| 647 | where |= SMP_VAL_BE_HRQ_HDR; |
| 648 | |
| 649 | if (!(rule->arg.expr->fetch->val & where)) { |
| 650 | memprintf(err, |
| 651 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 652 | args[0], sample_src_names(rule->arg.expr->fetch->use)); |
| 653 | free(rule->arg.expr); |
| 654 | return ACT_RET_PRS_ERR; |
| 655 | } |
| 656 | |
| 657 | rule->action = ACT_CUSTOM; |
| 658 | rule->action_ptr = action_set_priority_offset; |
| 659 | return ACT_RET_PRS_OK; |
| 660 | } |
| 661 | |
| 662 | static struct action_kw_list tcp_cont_kws = {ILH, { |
| 663 | { "set-priority-class", parse_set_priority_class }, |
| 664 | { "set-priority-offset", parse_set_priority_offset }, |
| 665 | { /* END */ } |
| 666 | }}; |
| 667 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 668 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_cont_kws); |
| 669 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 670 | static struct action_kw_list http_req_kws = {ILH, { |
| 671 | { "set-priority-class", parse_set_priority_class }, |
| 672 | { "set-priority-offset", parse_set_priority_offset }, |
| 673 | { /* END */ } |
| 674 | }}; |
| 675 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 676 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws); |
| 677 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 678 | static int |
| 679 | smp_fetch_priority_class(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 680 | { |
| 681 | if (!smp->strm) |
| 682 | return 0; |
| 683 | |
| 684 | smp->data.type = SMP_T_SINT; |
| 685 | smp->data.u.sint = smp->strm->priority_class; |
| 686 | |
| 687 | return 1; |
| 688 | } |
| 689 | |
| 690 | static int |
| 691 | smp_fetch_priority_offset(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 692 | { |
| 693 | if (!smp->strm) |
| 694 | return 0; |
| 695 | |
| 696 | smp->data.type = SMP_T_SINT; |
| 697 | smp->data.u.sint = smp->strm->priority_offset; |
| 698 | |
| 699 | return 1; |
| 700 | } |
| 701 | |
| 702 | |
| 703 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 704 | { "prio_class", smp_fetch_priority_class, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 705 | { "prio_offset", smp_fetch_priority_offset, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 706 | { /* END */}, |
| 707 | }}; |
| 708 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 709 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 710 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 711 | /* |
| 712 | * Local variables: |
| 713 | * c-indent-level: 8 |
| 714 | * c-basic-offset: 8 |
| 715 | * End: |
| 716 | */ |