blob: 1ef0607d04eff0e22da25a9dbe25838db4915bce [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Queue management functions.
3 *
Willy Tarreauac68c5d2009-10-04 23:12:44 +02004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreau6bdd05c2018-07-25 15:21:00 +020013/* 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 Hemmer0355dab2018-05-11 12:52:31 -040024 * - if p->node.node.leaf_p is NULL, the element is unlinked,
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020025 * 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 Tarreau88930dd2018-07-26 07:38:54 +020028 * - pendconn->srv is never NULL if pendconn->list is in the server's queue,
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020029 * and is always NULL if pendconn->list is in the backend's queue or empty.
Willy Tarreau88930dd2018-07-26 07:38:54 +020030 * - pendconn->target is NULL while the element is queued, and points to the
31 * assigned server when the pendconn is picked.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020032 *
33 * Threads complicate the design a little bit but rules remain simple :
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020034 * - 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 Tarreau3201e4e2018-07-26 08:23:24 +020042 * - both locks are compatible and may be held at the same time.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020043 *
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 Tarreau49801602020-06-04 22:50:02 +020047s * queue's lock.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020048 *
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 Tarreau3201e4e2018-07-26 08:23:24 +020062 * insertion are performed without eithre a queue lock held or the element
63 * being unlinked and visible exclusively to its stream.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020064 *
Willy Tarreau88930dd2018-07-26 07:38:54 +020065 * - 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 Tarreau6bdd05c2018-07-25 15:21:00 +020068 *
69 * - a pendconn doesn't switch between queues, it stays where it is.
Willy Tarreau6bdd05c2018-07-25 15:21:00 +020070 */
71
Willy Tarreaudfd3de82020-06-04 23:46:14 +020072#include <import/eb32tree.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020073#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020074#include <haproxy/backend.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020075#include <haproxy/http_rules.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020076#include <haproxy/pool.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020077#include <haproxy/queue.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020078#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020079#include <haproxy/server-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020080#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020081#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020082#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020083#include <haproxy/tcp_rules.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020084#include <haproxy/thread.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020085#include <haproxy/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
87
Patrick Hemmer248cb4c2018-05-11 12:52:31 -040088#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 Tarreau8ceae722018-11-26 11:58:30 +010094DECLARE_POOL(pool_head_pendconn, "pendconn", sizeof(struct pendconn));
Willy Tarreaubaaee002006-06-26 02:48:02 +020095
96/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010097 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +010098 * 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 Tarreaubaaee002006-06-26 02:48:02 +0200102 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200103unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104{
Willy Tarreau9909fc12007-11-30 17:42:05 +0100105 unsigned int max;
106
Willy Tarreau86034312006-12-29 00:10:33 +0100107 if (s->proxy->beconn >= s->proxy->fullconn)
108 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100109 max = s->maxconn;
110 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +0100111 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100112 max = s->maxconn;
113 else max = MAX(s->minconn,
114 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +0100115
Emeric Brun52a91d32017-08-31 14:41:55 +0200116 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreau9909fc12007-11-30 17:42:05 +0100117 now.tv_sec < s->last_change + s->slowstart &&
118 now.tv_sec >= s->last_change) {
119 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +0200120 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
121 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +0100122 }
123 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124}
125
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200126/* Remove the pendconn from the server's queue. At this stage, the connection
Willy Tarreau96bca332020-10-21 12:01:28 +0200127 * 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 Faulet5cd4bbd2018-03-14 16:18:06 +0100129 *
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200130 * 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 Fauletf3a55db2017-06-09 14:26:38 +0200132 */
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200133static void __pendconn_unlink_srv(struct pendconn *p)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100134{
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200135 p->strm->logs.srv_queue_pos += p->srv->queue_idx - p->queue_idx;
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200136 eb32_delete(&p->node);
137}
138
139/* Remove the pendconn from the proxy's queue. At this stage, the connection
Willy Tarreau96bca332020-10-21 12:01:28 +0200140 * 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 Tarreau3e3ae252020-10-21 11:20:07 +0200142 *
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 */
146static void __pendconn_unlink_prx(struct pendconn *p)
147{
148 p->strm->logs.prx_queue_pos += p->px->queue_idx - p->queue_idx;
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400149 eb32_delete(&p->node);
Christopher Fauletf3a55db2017-06-09 14:26:38 +0200150}
151
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200152/* 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 */
156static inline void pendconn_queue_lock(struct pendconn *p)
157{
158 if (p->srv)
159 HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock);
160 else
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200161 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->px->lock);
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200162}
163
164/* Unlocks the queue the pendconn element belongs to. This relies on both p->px
165 * and p->srv to be properly initialized (which is always the case once the
166 * element has been added).
167 */
168static inline void pendconn_queue_unlock(struct pendconn *p)
169{
170 if (p->srv)
171 HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock);
172 else
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200173 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->px->lock);
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200174}
175
Willy Tarreau9624fae2018-07-25 08:04:20 +0200176/* Removes the pendconn from the server/proxy queue. At this stage, the
177 * connection is not really dequeued. It will be done during process_stream().
Willy Tarreau9ada0302019-11-14 14:58:39 +0100178 * This function takes all the required locks for the operation. The pendconn
179 * must be valid, though it doesn't matter if it was already unlinked. Prefer
180 * pendconn_cond_unlink() to first check <p>. When the locks are already held,
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200181 * please use __pendconn_unlink_{srv,prx}() instead.
Willy Tarreau9624fae2018-07-25 08:04:20 +0200182 */
183void pendconn_unlink(struct pendconn *p)
184{
Willy Tarreau96bca332020-10-21 12:01:28 +0200185 int done = 0;
186
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200187 if (p->srv) {
188 /* queued in the server */
189 HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock);
Willy Tarreau96bca332020-10-21 12:01:28 +0200190 if (p->node.node.leaf_p) {
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200191 __pendconn_unlink_srv(p);
Willy Tarreau96bca332020-10-21 12:01:28 +0200192 done = 1;
193 }
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200194 HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock);
Willy Tarreau96bca332020-10-21 12:01:28 +0200195 if (done) {
196 _HA_ATOMIC_SUB(&p->srv->nbpend, 1);
197 _HA_ATOMIC_SUB(&p->px->totpend, 1);
198 }
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200199 }
200 else {
201 /* queued in the proxy */
202 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->px->lock);
Willy Tarreau96bca332020-10-21 12:01:28 +0200203 if (p->node.node.leaf_p) {
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200204 __pendconn_unlink_prx(p);
Willy Tarreau96bca332020-10-21 12:01:28 +0200205 done = 1;
206 }
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200207 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->px->lock);
Willy Tarreau96bca332020-10-21 12:01:28 +0200208 if (done) {
209 _HA_ATOMIC_SUB(&p->px->nbpend, 1);
210 _HA_ATOMIC_SUB(&p->px->totpend, 1);
211 }
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200212 }
Willy Tarreau9624fae2018-07-25 08:04:20 +0200213}
214
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400215/* Retrieve the first pendconn from tree <pendconns>. Classes are always
216 * considered first, then the time offset. The time does wrap, so the
217 * lookup is performed twice, one to retrieve the first class and a second
218 * time to retrieve the earliest time in this class.
219 */
220static struct pendconn *pendconn_first(struct eb_root *pendconns)
221{
222 struct eb32_node *node, *node2 = NULL;
223 u32 key;
224
225 node = eb32_first(pendconns);
226 if (!node)
227 return NULL;
228
229 key = KEY_CLASS_OFFSET_BOUNDARY(node->key);
230 node2 = eb32_lookup_ge(pendconns, key);
231
232 if (!node2 ||
233 KEY_CLASS(node2->key) != KEY_CLASS(node->key)) {
234 /* no other key in the tree, or in this class */
235 return eb32_entry(node, struct pendconn, node);
236 }
237
238 /* found a better key */
239 return eb32_entry(node2, struct pendconn, node);
240}
241
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100242/* Process the next pending connection from either a server or a proxy, and
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100243 * returns a strictly positive value on success (see below). If no pending
244 * connection is found, 0 is returned. Note that neither <srv> nor <px> may be
245 * NULL. Priority is given to the oldest request in the queue if both <srv> and
246 * <px> have pending requests. This ensures that no request will be left
247 * unserved. The <px> queue is not considered if the server (or a tracked
248 * server) is not RUNNING, is disabled, or has a null weight (server going
249 * down). The <srv> queue is still considered in this case, because if some
250 * connections remain there, it means that some requests have been forced there
251 * after it was seen down (eg: due to option persist). The stream is
252 * immediately marked as "assigned", and both its <srv> and <srv_conn> are set
253 * to <srv>.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100254 *
255 * This function must only be called if the server queue _AND_ the proxy queue
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100256 * are locked. Today it is only called by process_srv_queue. When a pending
257 * connection is dequeued, this function returns 1 if the pending connection can
258 * be handled by the current thread, else it returns 2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100260static int pendconn_process_next_strm(struct server *srv, struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100262 struct pendconn *p = NULL;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400263 struct pendconn *pp = NULL;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100264 struct server *rsrv;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400265 u32 pkey, ppkey;
Willy Tarreaud132f742010-08-06 10:08:23 +0200266
Willy Tarreau44267702011-10-28 15:35:33 +0200267 rsrv = srv->track;
Willy Tarreaud132f742010-08-06 10:08:23 +0200268 if (!rsrv)
269 rsrv = srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200271 p = NULL;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400272 if (srv->nbpend)
273 p = pendconn_first(&srv->pendconns);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200274
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400275 pp = NULL;
Willy Tarreaua8694652018-08-07 10:44:58 +0200276 if (srv_currently_usable(rsrv) && px->nbpend &&
277 (!(srv->flags & SRV_F_BACKUP) ||
278 (!px->srv_act &&
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400279 (srv == px->lbprm.fbck || (px->options & PR_O_USE_ALL_BK)))))
280 pp = pendconn_first(&px->pendconns);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100281
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400282 if (!p && !pp)
283 return 0;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200284
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400285 if (p && !pp)
286 goto use_p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100287
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400288 if (pp && !p)
289 goto use_pp;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100290
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400291 if (KEY_CLASS(p->node.key) < KEY_CLASS(pp->node.key))
292 goto use_p;
293
294 if (KEY_CLASS(pp->node.key) < KEY_CLASS(p->node.key))
295 goto use_pp;
296
297 pkey = KEY_OFFSET(p->node.key);
298 ppkey = KEY_OFFSET(pp->node.key);
299
300 if (pkey < NOW_OFFSET_BOUNDARY())
301 pkey += 0x100000; // key in the future
302
303 if (ppkey < NOW_OFFSET_BOUNDARY())
304 ppkey += 0x100000; // key in the future
305
306 if (pkey <= ppkey)
307 goto use_p;
308
309 use_pp:
310 /* Let's switch from the server pendconn to the proxy pendconn */
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200311 __pendconn_unlink_prx(pp);
Willy Tarreau96bca332020-10-21 12:01:28 +0200312 _HA_ATOMIC_SUB(&px->nbpend, 1);
313 _HA_ATOMIC_SUB(&px->totpend, 1);
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200314 px->queue_idx++;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400315 p = pp;
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200316 goto unlinked;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400317 use_p:
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200318 __pendconn_unlink_srv(p);
Willy Tarreau96bca332020-10-21 12:01:28 +0200319 _HA_ATOMIC_SUB(&srv->nbpend, 1);
320 _HA_ATOMIC_SUB(&px->totpend, 1);
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200321 srv->queue_idx++;
322 unlinked:
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100323 p->strm_flags |= SF_ASSIGNED;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200324 p->target = srv;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100325
Olivier Houchardb4df4922019-03-08 18:54:16 +0100326 _HA_ATOMIC_ADD(&srv->served, 1);
327 _HA_ATOMIC_ADD(&srv->proxy->served, 1);
328 __ha_barrier_atomic_store();
Willy Tarreau7c669d72008-06-20 15:04:11 +0200329 if (px->lbprm.server_take_conn)
Willy Tarreau2b437002021-02-17 16:01:37 +0100330 px->lbprm.server_take_conn(srv, 1);
Willy Tarreau49e10802021-02-17 13:33:24 +0100331 stream_add_srv_conn(p->strm, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200332
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100333 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100334
Olivier Houchardecfe6732018-07-26 18:47:27 +0200335 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336}
337
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100338/* Manages a server's connection queue. This function will try to dequeue as
Amaury Denoyelle43b0c622021-06-18 11:11:36 +0200339 * many pending streams as possible, and wake them up. <server_locked> must
340 * only be set if the caller already hold the server lock.
Christopher Faulet87566c92017-06-06 10:34:51 +0200341 */
Amaury Denoyelle43b0c622021-06-18 11:11:36 +0200342void process_srv_queue(struct server *s, int server_locked)
Christopher Faulet87566c92017-06-06 10:34:51 +0200343{
344 struct proxy *p = s->proxy;
Olivier Houchardecfe6732018-07-26 18:47:27 +0200345 int maxconn;
Christopher Faulet87566c92017-06-06 10:34:51 +0200346
Amaury Denoyelle43b0c622021-06-18 11:11:36 +0200347 if (!server_locked)
348 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200349 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200350 maxconn = srv_dynamic_maxconn(s);
351 while (s->served < maxconn) {
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100352 int ret = pendconn_process_next_strm(s, p);
353 if (!ret)
Christopher Faulet87566c92017-06-06 10:34:51 +0200354 break;
Christopher Faulet87566c92017-06-06 10:34:51 +0200355 }
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200356 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
Amaury Denoyelle43b0c622021-06-18 11:11:36 +0200357 if (!server_locked)
358 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200359}
360
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400361/* Adds the stream <strm> to the pending connection queue of server <strm>->srv
Willy Tarreau87b09662015-04-03 00:22:06 +0200362 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200364 * pendconn itself. If the stream was already marked as served, its flag is
365 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Patrick Hemmerda282f42018-05-11 12:52:31 -0400366 * The stream's queue position is counted with an offset of -1 because we want
367 * to make sure that being at the first position in the queue reports 1.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100368 *
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400369 * The queue is sorted by the composition of the priority_class, and the current
370 * timestamp offset by strm->priority_offset. The timestamp is in milliseconds
371 * and truncated to 20 bits, so will wrap every 17m28s575ms.
372 * The offset can be positive or negative, and an offset of 0 puts it in the
373 * middle of this range (~ 8 min). Note that this also means if the adjusted
374 * timestamp wraps around, the request will be misinterpreted as being of
Joseph Herlantd8499ec2018-11-25 11:26:48 -0800375 * the highest priority for that priority class.
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400376 *
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100377 * This function must be called by the stream itself, so in the context of
378 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200380struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381{
382 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100383 struct proxy *px;
384 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200385
Willy Tarreaubafbe012017-11-24 17:34:44 +0100386 p = pool_alloc(pool_head_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200387 if (!p)
388 return NULL;
389
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200390 if (strm->flags & SF_ASSIGNED)
391 srv = objt_server(strm->target);
392 else
393 srv = NULL;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100394
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200395 px = strm->be;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200396 p->target = NULL;
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200397 p->srv = srv;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400398 p->node.key = MAKE_KEY(strm->priority_class, strm->priority_offset);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100399 p->px = px;
400 p->strm = strm;
401 p->strm_flags = strm->flags;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200402
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200403 if (srv) {
Willy Tarreau56c1cfb2020-10-21 11:45:44 +0200404 unsigned int old_max, new_max;
405
406 new_max = _HA_ATOMIC_ADD(&srv->nbpend, 1);
407 old_max = srv->counters.nbpend_max;
408 while (new_max > old_max) {
409 if (likely(_HA_ATOMIC_CAS(&srv->counters.nbpend_max, &old_max, new_max)))
410 break;
411 }
412 __ha_barrier_atomic_store();
413
Willy Tarreauc7eedf72020-10-21 11:31:12 +0200414 HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock);
Patrick Hemmerda282f42018-05-11 12:52:31 -0400415 p->queue_idx = srv->queue_idx - 1; // for increment
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400416 eb32_insert(&srv->pendconns, &p->node);
Willy Tarreauc7eedf72020-10-21 11:31:12 +0200417 HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100418 }
419 else {
Willy Tarreau56c1cfb2020-10-21 11:45:44 +0200420 unsigned int old_max, new_max;
421
422 new_max = _HA_ATOMIC_ADD(&px->nbpend, 1);
423 old_max = px->be_counters.nbpend_max;
424 while (new_max > old_max) {
425 if (likely(_HA_ATOMIC_CAS(&px->be_counters.nbpend_max, &old_max, new_max)))
426 break;
427 }
428 __ha_barrier_atomic_store();
429
Willy Tarreauc7eedf72020-10-21 11:31:12 +0200430 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->px->lock);
Patrick Hemmerda282f42018-05-11 12:52:31 -0400431 p->queue_idx = px->queue_idx - 1; // for increment
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400432 eb32_insert(&px->pendconns, &p->node);
Willy Tarreauc7eedf72020-10-21 11:31:12 +0200433 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->px->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434 }
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200435 strm->pend_pos = p;
436
Olivier Houchardb4df4922019-03-08 18:54:16 +0100437 _HA_ATOMIC_ADD(&px->totpend, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 return p;
439}
440
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200441/* Redistribute pending connections when a server goes down. The number of
Willy Tarreaudeca26c2018-08-21 18:11:03 +0200442 * connections redistributed is returned. It must be called with the server
443 * lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200444 */
445int pendconn_redistribute(struct server *s)
446{
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400447 struct pendconn *p;
Willy Tarreaubff005a2019-05-27 08:10:11 +0200448 struct eb32_node *node, *nodeb;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200449 int xferred = 0;
450
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100451 /* The REDISP option was specified. We will ignore cookie and force to
452 * balance or use the dispatcher. */
453 if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP)
454 return 0;
455
Willy Tarreaubff005a2019-05-27 08:10:11 +0200456 for (node = eb32_first(&s->pendconns); node; node = nodeb) {
457 nodeb = eb32_next(node);
458
459 p = eb32_entry(node, struct pendconn, node);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100460 if (p->strm_flags & SF_FORCE_PRST)
461 continue;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200462
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100463 /* it's left to the dispatcher to choose a server */
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200464 __pendconn_unlink_srv(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100465 p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200466
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100467 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Willy Tarreauef71f012020-10-21 11:54:38 +0200468 xferred++;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200469 }
Willy Tarreau96bca332020-10-21 12:01:28 +0200470 if (xferred) {
Willy Tarreau5472aa52020-10-24 12:57:41 +0200471 _HA_ATOMIC_SUB(&s->nbpend, xferred);
472 _HA_ATOMIC_SUB(&s->proxy->totpend, xferred);
Willy Tarreau96bca332020-10-21 12:01:28 +0200473 }
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200474 return xferred;
475}
476
477/* Check for pending connections at the backend, and assign some of them to
478 * the server coming up. The server's weight is checked before being assigned
479 * connections it may not be able to handle. The total number of transferred
Willy Tarreau5e83d992019-07-30 11:59:34 +0200480 * connections is returned. It must be called with the server lock held, and
481 * will take the proxy's lock.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200482 */
483int pendconn_grab_from_px(struct server *s)
484{
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400485 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100486 int maxconn, xferred = 0;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200487
Emeric Brun52a91d32017-08-31 14:41:55 +0200488 if (!srv_currently_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200489 return 0;
490
Willy Tarreaua8694652018-08-07 10:44:58 +0200491 /* if this is a backup server and there are active servers or at
492 * least another backup server was elected, then this one must
493 * not dequeue requests from the proxy.
494 */
495 if ((s->flags & SRV_F_BACKUP) &&
496 (s->proxy->srv_act ||
497 ((s != s->proxy->lbprm.fbck) && !(s->proxy->options & PR_O_USE_ALL_BK))))
498 return 0;
499
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200500 HA_RWLOCK_WRLOCK(PROXY_LOCK, &s->proxy->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100501 maxconn = srv_dynamic_maxconn(s);
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400502 while ((p = pendconn_first(&s->proxy->pendconns))) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100503 if (s->maxconn && s->served + xferred >= maxconn)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200504 break;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100505
Willy Tarreau3e3ae252020-10-21 11:20:07 +0200506 __pendconn_unlink_prx(p);
Willy Tarreau88930dd2018-07-26 07:38:54 +0200507 p->target = s;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100508
509 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100510 xferred++;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200511 }
Willy Tarreauac66d6b2020-10-20 17:24:27 +0200512 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &s->proxy->lock);
Willy Tarreau96bca332020-10-21 12:01:28 +0200513 if (xferred) {
Willy Tarreau5472aa52020-10-24 12:57:41 +0200514 _HA_ATOMIC_SUB(&s->proxy->nbpend, xferred);
515 _HA_ATOMIC_SUB(&s->proxy->totpend, xferred);
Willy Tarreau96bca332020-10-21 12:01:28 +0200516 }
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200517 return xferred;
518}
519
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100520/* Try to dequeue pending connection attached to the stream <strm>. It must
521 * always exists here. If the pendconn is still linked to the server or the
522 * proxy queue, nothing is done and the function returns 1. Otherwise,
523 * <strm>->flags and <strm>->target are updated, the pendconn is released and 0
524 * is returned.
525 *
526 * This function must be called by the stream itself, so in the context of
527 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100529int pendconn_dequeue(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100531 struct pendconn *p;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200532 int is_unlinked;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100533
534 if (unlikely(!strm->pend_pos)) {
535 /* unexpected case because it is called by the stream itself and
536 * only the stream can release a pendconn. So it is only
537 * possible if a pendconn is released by someone else or if the
538 * stream is supposed to be queued but without its associated
539 * pendconn. In both cases it is a bug! */
540 abort();
Christopher Faulet8ba59142017-06-27 15:43:53 +0200541 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100542 p = strm->pend_pos;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200543
544 /* note below : we need to grab the queue's lock to check for emptiness
545 * because we don't want a partial _grab_from_px() or _redistribute()
546 * to be called in parallel and show an empty list without having the
547 * time to finish. With this we know that if we see the element
548 * unlinked, these functions were completely done.
549 */
550 pendconn_queue_lock(p);
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400551 is_unlinked = !p->node.node.leaf_p;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200552 pendconn_queue_unlock(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100553
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200554 if (!is_unlinked)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100555 return 1;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100556
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200557 /* the pendconn is not queued anymore and will not be so we're safe
558 * to proceed.
559 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100560 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
561 strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreaubce92432021-06-16 08:42:23 +0200562
563 if (p->target) {
564 /* a server picked this pendconn, it must skip LB */
565 strm->target = &p->target->obj_type;
566 strm->flags |= SF_ASSIGNED;
567 }
568
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100569 strm->pend_pos = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100570 pool_free(pool_head_pendconn, p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100571 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572}
573
Patrick Hemmer268a7072018-05-11 12:52:31 -0400574static enum act_return action_set_priority_class(struct act_rule *rule, struct proxy *px,
575 struct session *sess, struct stream *s, int flags)
576{
577 struct sample *smp;
578
579 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
580 if (!smp)
581 return ACT_RET_CONT;
582
583 s->priority_class = queue_limit_class(smp->data.u.sint);
584 return ACT_RET_CONT;
585}
586
587static enum act_return action_set_priority_offset(struct act_rule *rule, struct proxy *px,
588 struct session *sess, struct stream *s, int flags)
589{
590 struct sample *smp;
591
592 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
593 if (!smp)
594 return ACT_RET_CONT;
595
596 s->priority_offset = queue_limit_offset(smp->data.u.sint);
597
598 return ACT_RET_CONT;
599}
600
601static enum act_parse_ret parse_set_priority_class(const char **args, int *arg, struct proxy *px,
602 struct act_rule *rule, char **err)
603{
604 unsigned int where = 0;
605
606 rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100607 px->conf.args.line, err, &px->conf.args, NULL);
Patrick Hemmer268a7072018-05-11 12:52:31 -0400608 if (!rule->arg.expr)
609 return ACT_RET_PRS_ERR;
610
611 if (px->cap & PR_CAP_FE)
612 where |= SMP_VAL_FE_HRQ_HDR;
613 if (px->cap & PR_CAP_BE)
614 where |= SMP_VAL_BE_HRQ_HDR;
615
616 if (!(rule->arg.expr->fetch->val & where)) {
617 memprintf(err,
618 "fetch method '%s' extracts information from '%s', none of which is available here",
619 args[0], sample_src_names(rule->arg.expr->fetch->use));
620 free(rule->arg.expr);
621 return ACT_RET_PRS_ERR;
622 }
623
624 rule->action = ACT_CUSTOM;
625 rule->action_ptr = action_set_priority_class;
626 return ACT_RET_PRS_OK;
627}
628
629static enum act_parse_ret parse_set_priority_offset(const char **args, int *arg, struct proxy *px,
630 struct act_rule *rule, char **err)
631{
632 unsigned int where = 0;
633
634 rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100635 px->conf.args.line, err, &px->conf.args, NULL);
Patrick Hemmer268a7072018-05-11 12:52:31 -0400636 if (!rule->arg.expr)
637 return ACT_RET_PRS_ERR;
638
639 if (px->cap & PR_CAP_FE)
640 where |= SMP_VAL_FE_HRQ_HDR;
641 if (px->cap & PR_CAP_BE)
642 where |= SMP_VAL_BE_HRQ_HDR;
643
644 if (!(rule->arg.expr->fetch->val & where)) {
645 memprintf(err,
646 "fetch method '%s' extracts information from '%s', none of which is available here",
647 args[0], sample_src_names(rule->arg.expr->fetch->use));
648 free(rule->arg.expr);
649 return ACT_RET_PRS_ERR;
650 }
651
652 rule->action = ACT_CUSTOM;
653 rule->action_ptr = action_set_priority_offset;
654 return ACT_RET_PRS_OK;
655}
656
657static struct action_kw_list tcp_cont_kws = {ILH, {
658 { "set-priority-class", parse_set_priority_class },
659 { "set-priority-offset", parse_set_priority_offset },
660 { /* END */ }
661}};
662
Willy Tarreau0108d902018-11-25 19:14:37 +0100663INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_cont_kws);
664
Patrick Hemmer268a7072018-05-11 12:52:31 -0400665static struct action_kw_list http_req_kws = {ILH, {
666 { "set-priority-class", parse_set_priority_class },
667 { "set-priority-offset", parse_set_priority_offset },
668 { /* END */ }
669}};
670
Willy Tarreau0108d902018-11-25 19:14:37 +0100671INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
672
Patrick Hemmer268a7072018-05-11 12:52:31 -0400673static int
674smp_fetch_priority_class(const struct arg *args, struct sample *smp, const char *kw, void *private)
675{
676 if (!smp->strm)
677 return 0;
678
679 smp->data.type = SMP_T_SINT;
680 smp->data.u.sint = smp->strm->priority_class;
681
682 return 1;
683}
684
685static int
686smp_fetch_priority_offset(const struct arg *args, struct sample *smp, const char *kw, void *private)
687{
688 if (!smp->strm)
689 return 0;
690
691 smp->data.type = SMP_T_SINT;
692 smp->data.u.sint = smp->strm->priority_offset;
693
694 return 1;
695}
696
697
698static struct sample_fetch_kw_list smp_kws = {ILH, {
699 { "prio_class", smp_fetch_priority_class, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, },
700 { "prio_offset", smp_fetch_priority_offset, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, },
701 { /* END */},
702}};
703
Willy Tarreau0108d902018-11-25 19:14:37 +0100704INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
Patrick Hemmer268a7072018-05-11 12:52:31 -0400705
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706/*
707 * Local variables:
708 * c-indent-level: 8
709 * c-basic-offset: 8
710 * End:
711 */