blob: a5f129d50367659872dd8d464df7bb4cba610f9d [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 Tarreau3201e4e2018-07-26 08:23:24 +020047 * 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 Tarreaue3ba5f02006-06-29 18:54:54 +020072#include <common/config.h>
Willy Tarreaue4d7e552007-05-13 20:19:55 +020073#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020074#include <common/time.h>
Christopher Faulet8ba59142017-06-27 15:43:53 +020075#include <common/hathreads.h>
Patrick Hemmer0355dab2018-05-11 12:52:31 -040076#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Willy Tarreau61c112a2018-10-02 16:43:32 +020078#include <proto/http_rules.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040079#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020080#include <proto/queue.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040081#include <proto/sample.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020082#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020083#include <proto/stream.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010084#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020085#include <proto/task.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040086#include <proto/tcp_rules.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
88
Patrick Hemmer248cb4c2018-05-11 12:52:31 -040089#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 Tarreaubafbe012017-11-24 17:34:44 +010095struct pool_head *pool_head_pendconn;
Willy Tarreaue4d7e552007-05-13 20:19:55 +020096
97/* perform minimal intializations, report 0 in case of error, 1 if OK. */
98int init_pendconn()
99{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100100 pool_head_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED);
101 return pool_head_pendconn != NULL;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200102}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103
104/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +0100105 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +0100106 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
107 * server is currently warming up, the slowstart is also applied to the
108 * resulting value, which can be lower than minconn in this case, but never
109 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200111unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112{
Willy Tarreau9909fc12007-11-30 17:42:05 +0100113 unsigned int max;
114
Willy Tarreau86034312006-12-29 00:10:33 +0100115 if (s->proxy->beconn >= s->proxy->fullconn)
116 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100117 max = s->maxconn;
118 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +0100119 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100120 max = s->maxconn;
121 else max = MAX(s->minconn,
122 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +0100123
Emeric Brun52a91d32017-08-31 14:41:55 +0200124 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreau9909fc12007-11-30 17:42:05 +0100125 now.tv_sec < s->last_change + s->slowstart &&
126 now.tv_sec >= s->last_change) {
127 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +0200128 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
129 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +0100130 }
131 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132}
133
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100134/* Remove the pendconn from the server/proxy queue. At this stage, the
135 * connection is not really dequeued. It will be done during the
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200136 * process_stream. It also decreases the pending count.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100137 *
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200138 * The caller must own the lock on the queue containing the pendconn. The
139 * pendconn must still be queued.
Christopher Fauletf3a55db2017-06-09 14:26:38 +0200140 */
Willy Tarreau9624fae2018-07-25 08:04:20 +0200141static void __pendconn_unlink(struct pendconn *p)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100142{
Patrick Hemmerda282f42018-05-11 12:52:31 -0400143 if (p->srv) {
144 p->strm->logs.srv_queue_pos += p->srv->queue_idx - p->queue_idx;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100145 p->srv->nbpend--;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400146 } else {
147 p->strm->logs.prx_queue_pos += p->px->queue_idx - p->queue_idx;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100148 p->px->nbpend--;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400149 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100150 HA_ATOMIC_SUB(&p->px->totpend, 1);
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400151 eb32_delete(&p->node);
Christopher Fauletf3a55db2017-06-09 14:26:38 +0200152}
153
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200154/* Locks the queue the pendconn element belongs to. This relies on both p->px
155 * and p->srv to be properly initialized (which is always the case once the
156 * element has been added).
157 */
158static inline void pendconn_queue_lock(struct pendconn *p)
159{
160 if (p->srv)
161 HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock);
162 else
163 HA_SPIN_LOCK(PROXY_LOCK, &p->px->lock);
164}
165
166/* Unlocks the queue the pendconn element belongs to. This relies on both p->px
167 * and p->srv to be properly initialized (which is always the case once the
168 * element has been added).
169 */
170static inline void pendconn_queue_unlock(struct pendconn *p)
171{
172 if (p->srv)
173 HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock);
174 else
175 HA_SPIN_UNLOCK(PROXY_LOCK, &p->px->lock);
176}
177
Willy Tarreau9624fae2018-07-25 08:04:20 +0200178/* Removes the pendconn from the server/proxy queue. At this stage, the
179 * connection is not really dequeued. It will be done during process_stream().
180 * This function takes all the required locks for the operation. The caller is
181 * responsible for ensuring that <p> is valid and still in the queue. Use
182 * pendconn_cond_unlink() if unsure. When the locks are already held, please
183 * use __pendconn_unlink() instead.
184 */
185void pendconn_unlink(struct pendconn *p)
186{
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200187 pendconn_queue_lock(p);
Willy Tarreau9624fae2018-07-25 08:04:20 +0200188
189 __pendconn_unlink(p);
190
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200191 pendconn_queue_unlock(p);
Willy Tarreau9624fae2018-07-25 08:04:20 +0200192}
193
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400194/* Retrieve the first pendconn from tree <pendconns>. Classes are always
195 * considered first, then the time offset. The time does wrap, so the
196 * lookup is performed twice, one to retrieve the first class and a second
197 * time to retrieve the earliest time in this class.
198 */
199static struct pendconn *pendconn_first(struct eb_root *pendconns)
200{
201 struct eb32_node *node, *node2 = NULL;
202 u32 key;
203
204 node = eb32_first(pendconns);
205 if (!node)
206 return NULL;
207
208 key = KEY_CLASS_OFFSET_BOUNDARY(node->key);
209 node2 = eb32_lookup_ge(pendconns, key);
210
211 if (!node2 ||
212 KEY_CLASS(node2->key) != KEY_CLASS(node->key)) {
213 /* no other key in the tree, or in this class */
214 return eb32_entry(node, struct pendconn, node);
215 }
216
217 /* found a better key */
218 return eb32_entry(node2, struct pendconn, node);
219}
220
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100221/* Process the next pending connection from either a server or a proxy, and
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100222 * returns a strictly positive value on success (see below). If no pending
223 * connection is found, 0 is returned. Note that neither <srv> nor <px> may be
224 * NULL. Priority is given to the oldest request in the queue if both <srv> and
225 * <px> have pending requests. This ensures that no request will be left
226 * unserved. The <px> queue is not considered if the server (or a tracked
227 * server) is not RUNNING, is disabled, or has a null weight (server going
228 * down). The <srv> queue is still considered in this case, because if some
229 * connections remain there, it means that some requests have been forced there
230 * after it was seen down (eg: due to option persist). The stream is
231 * immediately marked as "assigned", and both its <srv> and <srv_conn> are set
232 * to <srv>.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100233 *
234 * This function must only be called if the server queue _AND_ the proxy queue
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100235 * are locked. Today it is only called by process_srv_queue. When a pending
236 * connection is dequeued, this function returns 1 if the pending connection can
237 * be handled by the current thread, else it returns 2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100239static int pendconn_process_next_strm(struct server *srv, struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100241 struct pendconn *p = NULL;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400242 struct pendconn *pp = NULL;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100243 struct server *rsrv;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400244 u32 pkey, ppkey;
Willy Tarreaud132f742010-08-06 10:08:23 +0200245
Willy Tarreau44267702011-10-28 15:35:33 +0200246 rsrv = srv->track;
Willy Tarreaud132f742010-08-06 10:08:23 +0200247 if (!rsrv)
248 rsrv = srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200250 p = NULL;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400251 if (srv->nbpend)
252 p = pendconn_first(&srv->pendconns);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200253
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400254 pp = NULL;
Willy Tarreaua8694652018-08-07 10:44:58 +0200255 if (srv_currently_usable(rsrv) && px->nbpend &&
256 (!(srv->flags & SRV_F_BACKUP) ||
257 (!px->srv_act &&
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400258 (srv == px->lbprm.fbck || (px->options & PR_O_USE_ALL_BK)))))
259 pp = pendconn_first(&px->pendconns);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100260
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400261 if (!p && !pp)
262 return 0;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200263
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400264 if (p && !pp)
265 goto use_p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100266
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400267 if (pp && !p)
268 goto use_pp;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100269
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400270 if (KEY_CLASS(p->node.key) < KEY_CLASS(pp->node.key))
271 goto use_p;
272
273 if (KEY_CLASS(pp->node.key) < KEY_CLASS(p->node.key))
274 goto use_pp;
275
276 pkey = KEY_OFFSET(p->node.key);
277 ppkey = KEY_OFFSET(pp->node.key);
278
279 if (pkey < NOW_OFFSET_BOUNDARY())
280 pkey += 0x100000; // key in the future
281
282 if (ppkey < NOW_OFFSET_BOUNDARY())
283 ppkey += 0x100000; // key in the future
284
285 if (pkey <= ppkey)
286 goto use_p;
287
288 use_pp:
289 /* Let's switch from the server pendconn to the proxy pendconn */
290 p = pp;
291 use_p:
Willy Tarreau9624fae2018-07-25 08:04:20 +0200292 __pendconn_unlink(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100293 p->strm_flags |= SF_ASSIGNED;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200294 p->target = srv;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100295
Patrick Hemmerda282f42018-05-11 12:52:31 -0400296 if (p != pp)
297 srv->queue_idx++;
298 else
299 px->queue_idx++;
300
Christopher Faulet29f77e82017-06-08 14:04:45 +0200301 HA_ATOMIC_ADD(&srv->served, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200302 HA_ATOMIC_ADD(&srv->proxy->served, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200303 if (px->lbprm.server_take_conn)
304 px->lbprm.server_take_conn(srv);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100305 __stream_add_srv_conn(p->strm, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200306
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100307 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100308
Olivier Houchardecfe6732018-07-26 18:47:27 +0200309 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310}
311
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100312/* Manages a server's connection queue. This function will try to dequeue as
Christopher Faulet87566c92017-06-06 10:34:51 +0200313 * many pending streams as possible, and wake them up.
314 */
315void process_srv_queue(struct server *s)
316{
317 struct proxy *p = s->proxy;
Olivier Houchardecfe6732018-07-26 18:47:27 +0200318 int maxconn;
Christopher Faulet87566c92017-06-06 10:34:51 +0200319
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100320 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
321 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200322 maxconn = srv_dynamic_maxconn(s);
323 while (s->served < maxconn) {
Christopher Fauletfd83f0b2018-03-19 15:22:09 +0100324 int ret = pendconn_process_next_strm(s, p);
325 if (!ret)
Christopher Faulet87566c92017-06-06 10:34:51 +0200326 break;
Christopher Faulet87566c92017-06-06 10:34:51 +0200327 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100328 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
329 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200330}
331
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400332/* Adds the stream <strm> to the pending connection queue of server <strm>->srv
Willy Tarreau87b09662015-04-03 00:22:06 +0200333 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200335 * pendconn itself. If the stream was already marked as served, its flag is
336 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Patrick Hemmerda282f42018-05-11 12:52:31 -0400337 * The stream's queue position is counted with an offset of -1 because we want
338 * to make sure that being at the first position in the queue reports 1.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100339 *
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400340 * The queue is sorted by the composition of the priority_class, and the current
341 * timestamp offset by strm->priority_offset. The timestamp is in milliseconds
342 * and truncated to 20 bits, so will wrap every 17m28s575ms.
343 * The offset can be positive or negative, and an offset of 0 puts it in the
344 * middle of this range (~ 8 min). Note that this also means if the adjusted
345 * timestamp wraps around, the request will be misinterpreted as being of
346 * the higest priority for that priority class.
347 *
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100348 * This function must be called by the stream itself, so in the context of
349 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200351struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352{
353 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100354 struct proxy *px;
355 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356
Willy Tarreaubafbe012017-11-24 17:34:44 +0100357 p = pool_alloc(pool_head_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358 if (!p)
359 return NULL;
360
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200361 if (strm->flags & SF_ASSIGNED)
362 srv = objt_server(strm->target);
363 else
364 srv = NULL;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100365
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200366 px = strm->be;
Willy Tarreau88930dd2018-07-26 07:38:54 +0200367 p->target = NULL;
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200368 p->srv = srv;
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400369 p->node.key = MAKE_KEY(strm->priority_class, strm->priority_offset);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100370 p->px = px;
371 p->strm = strm;
372 p->strm_flags = strm->flags;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200373
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200374 pendconn_queue_lock(p);
375
376 if (srv) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100377 srv->nbpend++;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100378 if (srv->nbpend > srv->counters.nbpend_max)
379 srv->counters.nbpend_max = srv->nbpend;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400380 p->queue_idx = srv->queue_idx - 1; // for increment
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400381 eb32_insert(&srv->pendconns, &p->node);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100382 }
383 else {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100384 px->nbpend++;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100385 if (px->nbpend > px->be_counters.nbpend_max)
386 px->be_counters.nbpend_max = px->nbpend;
Patrick Hemmerda282f42018-05-11 12:52:31 -0400387 p->queue_idx = px->queue_idx - 1; // for increment
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400388 eb32_insert(&px->pendconns, &p->node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389 }
Willy Tarreau7c6f8a22018-07-26 08:03:14 +0200390 strm->pend_pos = p;
391
392 pendconn_queue_unlock(p);
393
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100394 HA_ATOMIC_ADD(&px->totpend, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395 return p;
396}
397
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200398/* Redistribute pending connections when a server goes down. The number of
Willy Tarreaudeca26c2018-08-21 18:11:03 +0200399 * connections redistributed is returned. It must be called with the server
400 * lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200401 */
402int pendconn_redistribute(struct server *s)
403{
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400404 struct pendconn *p;
405 struct eb32_node *node;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200406 int xferred = 0;
407
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100408 /* The REDISP option was specified. We will ignore cookie and force to
409 * balance or use the dispatcher. */
410 if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP)
411 return 0;
412
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400413 for (node = eb32_first(&s->pendconns); node; node = eb32_next(node)) {
414 p = eb32_entry(&node, struct pendconn, node);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100415 if (p->strm_flags & SF_FORCE_PRST)
416 continue;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200417
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100418 /* it's left to the dispatcher to choose a server */
Willy Tarreau9624fae2018-07-25 08:04:20 +0200419 __pendconn_unlink(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100420 p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200421
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100422 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200423 }
424 return xferred;
425}
426
427/* Check for pending connections at the backend, and assign some of them to
428 * the server coming up. The server's weight is checked before being assigned
429 * connections it may not be able to handle. The total number of transferred
430 * connections is returned.
431 */
432int pendconn_grab_from_px(struct server *s)
433{
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400434 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100435 int maxconn, xferred = 0;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200436
Emeric Brun52a91d32017-08-31 14:41:55 +0200437 if (!srv_currently_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200438 return 0;
439
Willy Tarreaua8694652018-08-07 10:44:58 +0200440 /* if this is a backup server and there are active servers or at
441 * least another backup server was elected, then this one must
442 * not dequeue requests from the proxy.
443 */
444 if ((s->flags & SRV_F_BACKUP) &&
445 (s->proxy->srv_act ||
446 ((s != s->proxy->lbprm.fbck) && !(s->proxy->options & PR_O_USE_ALL_BK))))
447 return 0;
448
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100449 HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100450 maxconn = srv_dynamic_maxconn(s);
Patrick Hemmer248cb4c2018-05-11 12:52:31 -0400451 while ((p = pendconn_first(&s->proxy->pendconns))) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100452 if (s->maxconn && s->served + xferred >= maxconn)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200453 break;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100454
Willy Tarreau9624fae2018-07-25 08:04:20 +0200455 __pendconn_unlink(p);
Willy Tarreau88930dd2018-07-26 07:38:54 +0200456 p->target = s;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100457
458 task_wakeup(p->strm->task, TASK_WOKEN_RES);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100459 xferred++;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200460 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100461 HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200462 return xferred;
463}
464
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100465/* Try to dequeue pending connection attached to the stream <strm>. It must
466 * always exists here. If the pendconn is still linked to the server or the
467 * proxy queue, nothing is done and the function returns 1. Otherwise,
468 * <strm>->flags and <strm>->target are updated, the pendconn is released and 0
469 * is returned.
470 *
471 * This function must be called by the stream itself, so in the context of
472 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100474int pendconn_dequeue(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100476 struct pendconn *p;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200477 int is_unlinked;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100478
479 if (unlikely(!strm->pend_pos)) {
480 /* unexpected case because it is called by the stream itself and
481 * only the stream can release a pendconn. So it is only
482 * possible if a pendconn is released by someone else or if the
483 * stream is supposed to be queued but without its associated
484 * pendconn. In both cases it is a bug! */
485 abort();
Christopher Faulet8ba59142017-06-27 15:43:53 +0200486 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100487 p = strm->pend_pos;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200488
489 /* note below : we need to grab the queue's lock to check for emptiness
490 * because we don't want a partial _grab_from_px() or _redistribute()
491 * to be called in parallel and show an empty list without having the
492 * time to finish. With this we know that if we see the element
493 * unlinked, these functions were completely done.
494 */
495 pendconn_queue_lock(p);
Patrick Hemmer0355dab2018-05-11 12:52:31 -0400496 is_unlinked = !p->node.node.leaf_p;
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200497 pendconn_queue_unlock(p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100498
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200499 if (!is_unlinked)
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100500 return 1;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100501
Willy Tarreau3201e4e2018-07-26 08:23:24 +0200502 /* the pendconn is not queued anymore and will not be so we're safe
503 * to proceed.
504 */
Willy Tarreau88930dd2018-07-26 07:38:54 +0200505 if (p->target)
506 strm->target = &p->target->obj_type;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100507
508 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
509 strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
510 strm->pend_pos = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100511 pool_free(pool_head_pendconn, p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100512 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513}
514
Patrick Hemmer268a7072018-05-11 12:52:31 -0400515static enum act_return action_set_priority_class(struct act_rule *rule, struct proxy *px,
516 struct session *sess, struct stream *s, int flags)
517{
518 struct sample *smp;
519
520 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
521 if (!smp)
522 return ACT_RET_CONT;
523
524 s->priority_class = queue_limit_class(smp->data.u.sint);
525 return ACT_RET_CONT;
526}
527
528static enum act_return action_set_priority_offset(struct act_rule *rule, struct proxy *px,
529 struct session *sess, struct stream *s, int flags)
530{
531 struct sample *smp;
532
533 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT);
534 if (!smp)
535 return ACT_RET_CONT;
536
537 s->priority_offset = queue_limit_offset(smp->data.u.sint);
538
539 return ACT_RET_CONT;
540}
541
542static enum act_parse_ret parse_set_priority_class(const char **args, int *arg, struct proxy *px,
543 struct act_rule *rule, char **err)
544{
545 unsigned int where = 0;
546
547 rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
548 px->conf.args.line, err, &px->conf.args);
549 if (!rule->arg.expr)
550 return ACT_RET_PRS_ERR;
551
552 if (px->cap & PR_CAP_FE)
553 where |= SMP_VAL_FE_HRQ_HDR;
554 if (px->cap & PR_CAP_BE)
555 where |= SMP_VAL_BE_HRQ_HDR;
556
557 if (!(rule->arg.expr->fetch->val & where)) {
558 memprintf(err,
559 "fetch method '%s' extracts information from '%s', none of which is available here",
560 args[0], sample_src_names(rule->arg.expr->fetch->use));
561 free(rule->arg.expr);
562 return ACT_RET_PRS_ERR;
563 }
564
565 rule->action = ACT_CUSTOM;
566 rule->action_ptr = action_set_priority_class;
567 return ACT_RET_PRS_OK;
568}
569
570static enum act_parse_ret parse_set_priority_offset(const char **args, int *arg, struct proxy *px,
571 struct act_rule *rule, char **err)
572{
573 unsigned int where = 0;
574
575 rule->arg.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
576 px->conf.args.line, err, &px->conf.args);
577 if (!rule->arg.expr)
578 return ACT_RET_PRS_ERR;
579
580 if (px->cap & PR_CAP_FE)
581 where |= SMP_VAL_FE_HRQ_HDR;
582 if (px->cap & PR_CAP_BE)
583 where |= SMP_VAL_BE_HRQ_HDR;
584
585 if (!(rule->arg.expr->fetch->val & where)) {
586 memprintf(err,
587 "fetch method '%s' extracts information from '%s', none of which is available here",
588 args[0], sample_src_names(rule->arg.expr->fetch->use));
589 free(rule->arg.expr);
590 return ACT_RET_PRS_ERR;
591 }
592
593 rule->action = ACT_CUSTOM;
594 rule->action_ptr = action_set_priority_offset;
595 return ACT_RET_PRS_OK;
596}
597
598static struct action_kw_list tcp_cont_kws = {ILH, {
599 { "set-priority-class", parse_set_priority_class },
600 { "set-priority-offset", parse_set_priority_offset },
601 { /* END */ }
602}};
603
604static struct action_kw_list http_req_kws = {ILH, {
605 { "set-priority-class", parse_set_priority_class },
606 { "set-priority-offset", parse_set_priority_offset },
607 { /* END */ }
608}};
609
610static int
611smp_fetch_priority_class(const struct arg *args, struct sample *smp, const char *kw, void *private)
612{
613 if (!smp->strm)
614 return 0;
615
616 smp->data.type = SMP_T_SINT;
617 smp->data.u.sint = smp->strm->priority_class;
618
619 return 1;
620}
621
622static int
623smp_fetch_priority_offset(const struct arg *args, struct sample *smp, const char *kw, void *private)
624{
625 if (!smp->strm)
626 return 0;
627
628 smp->data.type = SMP_T_SINT;
629 smp->data.u.sint = smp->strm->priority_offset;
630
631 return 1;
632}
633
634
635static struct sample_fetch_kw_list smp_kws = {ILH, {
636 { "prio_class", smp_fetch_priority_class, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, },
637 { "prio_offset", smp_fetch_priority_offset, 0, NULL, SMP_T_SINT, SMP_USE_INTRN, },
638 { /* END */},
639}};
640
641__attribute__((constructor))
642static void __queue_init(void)
643{
644 tcp_req_cont_keywords_register(&tcp_cont_kws);
645 http_req_keywords_register(&http_req_kws);
646 sample_register_fetches(&smp_kws);
647}
648
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649/*
650 * Local variables:
651 * c-indent-level: 8
652 * c-basic-offset: 8
653 * End:
654 */