blob: aa40fba729cd82334c83e41858c2484693ced28a [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 Tarreaue3ba5f02006-06-29 18:54:54 +020013#include <common/config.h>
Willy Tarreaue4d7e552007-05-13 20:19:55 +020014#include <common/memory.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020015#include <common/time.h>
Christopher Faulet8ba59142017-06-27 15:43:53 +020016#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020017
Willy Tarreaubaaee002006-06-26 02:48:02 +020018#include <proto/queue.h>
19#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020020#include <proto/stream.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010021#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <proto/task.h>
23
24
Willy Tarreaubafbe012017-11-24 17:34:44 +010025struct pool_head *pool_head_pendconn;
Willy Tarreaue4d7e552007-05-13 20:19:55 +020026
27/* perform minimal intializations, report 0 in case of error, 1 if OK. */
28int init_pendconn()
29{
Willy Tarreaubafbe012017-11-24 17:34:44 +010030 pool_head_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED);
31 return pool_head_pendconn != NULL;
Willy Tarreaue4d7e552007-05-13 20:19:55 +020032}
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010035 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +010036 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
37 * server is currently warming up, the slowstart is also applied to the
38 * resulting value, which can be lower than minconn in this case, but never
39 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020040 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020041unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020042{
Willy Tarreau9909fc12007-11-30 17:42:05 +010043 unsigned int max;
44
Willy Tarreau86034312006-12-29 00:10:33 +010045 if (s->proxy->beconn >= s->proxy->fullconn)
46 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +010047 max = s->maxconn;
48 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +010049 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +010050 max = s->maxconn;
51 else max = MAX(s->minconn,
52 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +010053
Emeric Brun52a91d32017-08-31 14:41:55 +020054 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreau9909fc12007-11-30 17:42:05 +010055 now.tv_sec < s->last_change + s->slowstart &&
56 now.tv_sec >= s->last_change) {
57 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +020058 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
59 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +010060 }
61 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +020062}
63
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +010064/* Remove the pendconn from the server/proxy queue. At this stage, the
65 * connection is not really dequeued. It will be done during the
66 * process_stream. This function must be called by function owning the locks on
67 * the pendconn _AND_ the server/proxy. It also decreases the pending count.
68 *
69 * The caller must own the lock on the pendconn _AND_ the queue containing the
70 * pendconn. The pendconn must still be queued.
Christopher Fauletf3a55db2017-06-09 14:26:38 +020071 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +010072static void pendconn_unlink(struct pendconn *p)
73{
74 if (p->srv)
75 p->srv->nbpend--;
76 else
77 p->px->nbpend--;
78 HA_ATOMIC_SUB(&p->px->totpend, 1);
79 LIST_DEL(&p->list);
80 LIST_INIT(&p->list);
Christopher Fauletf3a55db2017-06-09 14:26:38 +020081}
82
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +010083/* Process the next pending connection from either a server or a proxy, and
84 * returns 0 on success. If no pending connection is found, 1 is returned.
85 * Note that neither <srv> nor <px> may be NULL. Priority is given to the
86 * oldest request in the queue if both <srv> and <px> have pending
87 * requests. This ensures that no request will be left unserved. The <px> queue
88 * is not considered if the server (or a tracked server) is not RUNNING, is
89 * disabled, or has a null weight (server going down). The <srv> queue is still
90 * considered in this case, because if some connections remain there, it means
91 * that some requests have been forced there after it was seen down (eg: due to
92 * option persist). The stream is immediately marked as "assigned", and both
93 * its <srv> and <srv_conn> are set to <srv>.
94 *
95 * This function must only be called if the server queue _AND_ the proxy queue
96 * are locked. Today it is only called by process_srv_queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +010098static int pendconn_process_next_strm(struct server *srv, struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020099{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100100 struct pendconn *p = NULL;
101 struct server *rsrv;
Willy Tarreaud132f742010-08-06 10:08:23 +0200102
Willy Tarreau44267702011-10-28 15:35:33 +0200103 rsrv = srv->track;
Willy Tarreaud132f742010-08-06 10:08:23 +0200104 if (!rsrv)
105 rsrv = srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100107 if (srv->nbpend) {
108 list_for_each_entry(p, &srv->pendconns, list) {
109 if (!HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock))
110 goto ps_found;
111 }
112 p = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200114
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100115 ps_found:
116 if (srv_currently_usable(rsrv) && px->nbpend) {
117 struct pendconn *pp;
118
119 list_for_each_entry(pp, &px->pendconns, list) {
120 /* If the server pendconn is older than the proxy one,
121 * we process the server one. */
122 if (p && !tv_islt(&pp->strm->logs.tv_request, &p->strm->logs.tv_request))
123 goto pendconn_found;
124
125 if (!HA_SPIN_TRYLOCK(PENDCONN_LOCK, &pp->lock)) {
126 /* Let's switch from the server pendconn to the
127 * proxy pendconn. Don't forget to unlock the
128 * server pendconn, if any. */
129 if (p)
130 HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock);
131 p = pp;
132 goto pendconn_found;
133 }
134 }
135 }
136
137 if (!p)
138 return 1;
139
140 pendconn_found:
141 pendconn_unlink(p);
142 p->strm_flags |= SF_ASSIGNED;
143 p->srv = srv;
144
Christopher Faulet29f77e82017-06-08 14:04:45 +0200145 HA_ATOMIC_ADD(&srv->served, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200146 HA_ATOMIC_ADD(&srv->proxy->served, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200147 if (px->lbprm.server_take_conn)
148 px->lbprm.server_take_conn(srv);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100149 __stream_add_srv_conn(p->strm, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200150
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100151 task_wakeup(p->strm->task, TASK_WOKEN_RES);
152 HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock);
153 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154}
155
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100156/* Manages a server's connection queue. This function will try to dequeue as
Christopher Faulet87566c92017-06-06 10:34:51 +0200157 * many pending streams as possible, and wake them up.
158 */
159void process_srv_queue(struct server *s)
160{
161 struct proxy *p = s->proxy;
162 int maxconn;
163
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100164 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
165 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200166 maxconn = srv_dynamic_maxconn(s);
167 while (s->served < maxconn) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100168 if (pendconn_process_next_strm(s, p))
Christopher Faulet87566c92017-06-06 10:34:51 +0200169 break;
Christopher Faulet87566c92017-06-06 10:34:51 +0200170 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100171 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
172 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200173}
174
Willy Tarreau87b09662015-04-03 00:22:06 +0200175/* Adds the stream <strm> to the pending connection list of server <strm>->srv
176 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200178 * pendconn itself. If the stream was already marked as served, its flag is
179 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100180 *
181 * This function must be called by the stream itself, so in the context of
182 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200184struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185{
186 struct pendconn *p;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100187 struct proxy *px;
188 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
Willy Tarreaubafbe012017-11-24 17:34:44 +0100190 p = pool_alloc(pool_head_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191 if (!p)
192 return NULL;
193
Christopher Faulet8ba59142017-06-27 15:43:53 +0200194 srv = objt_server(strm->target);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100195 px = strm->be;
196
197 p->srv = NULL;
198 p->px = px;
199 p->strm = strm;
200 p->strm_flags = strm->flags;
201 HA_SPIN_INIT(&p->lock);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200202
Christopher Faulet8ba59142017-06-27 15:43:53 +0200203 if ((strm->flags & SF_ASSIGNED) && srv) {
204 p->srv = srv;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100205 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100206 srv->nbpend++;
207 strm->logs.srv_queue_size += srv->nbpend;
208 if (srv->nbpend > srv->counters.nbpend_max)
209 srv->counters.nbpend_max = srv->nbpend;
Willy Tarreau827aee92011-03-10 16:55:02 +0100210 LIST_ADDQ(&srv->pendconns, &p->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100211 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100212 }
213 else {
214 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
215 px->nbpend++;
216 strm->logs.prx_queue_size += px->nbpend;
217 if (px->nbpend > px->be_counters.nbpend_max)
218 px->be_counters.nbpend_max = px->nbpend;
219 LIST_ADDQ(&px->pendconns, &p->list);
220 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100222 HA_ATOMIC_ADD(&px->totpend, 1);
223 strm->pend_pos = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224 return p;
225}
226
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200227/* Redistribute pending connections when a server goes down. The number of
228 * connections redistributed is returned.
229 */
230int pendconn_redistribute(struct server *s)
231{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100232 struct pendconn *p, *pback;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200233 int xferred = 0;
234
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100235 /* The REDISP option was specified. We will ignore cookie and force to
236 * balance or use the dispatcher. */
237 if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP)
238 return 0;
239
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100240 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100241 list_for_each_entry_safe(p, pback, &s->pendconns, list) {
242 if (p->strm_flags & SF_FORCE_PRST)
243 continue;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200244
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100245 if (HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock))
246 continue;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200247
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100248 /* it's left to the dispatcher to choose a server */
249 pendconn_unlink(p);
250 p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200251
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100252 task_wakeup(p->strm->task, TASK_WOKEN_RES);
253 HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200254 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100255 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200256 return xferred;
257}
258
259/* Check for pending connections at the backend, and assign some of them to
260 * the server coming up. The server's weight is checked before being assigned
261 * connections it may not be able to handle. The total number of transferred
262 * connections is returned.
263 */
264int pendconn_grab_from_px(struct server *s)
265{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100266 struct pendconn *p, *pback;
267 int maxconn, xferred = 0;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200268
Emeric Brun52a91d32017-08-31 14:41:55 +0200269 if (!srv_currently_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200270 return 0;
271
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100272 HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100273 maxconn = srv_dynamic_maxconn(s);
274 list_for_each_entry_safe(p, pback, &s->proxy->pendconns, list) {
275 if (s->maxconn && s->served + xferred >= maxconn)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200276 break;
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100277
278 if (HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock))
279 continue;
280
281 pendconn_unlink(p);
282 p->srv = s;
283
284 task_wakeup(p->strm->task, TASK_WOKEN_RES);
285 HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock);
286 xferred++;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200287 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100288 HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200289 return xferred;
290}
291
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100292/* Try to dequeue pending connection attached to the stream <strm>. It must
293 * always exists here. If the pendconn is still linked to the server or the
294 * proxy queue, nothing is done and the function returns 1. Otherwise,
295 * <strm>->flags and <strm>->target are updated, the pendconn is released and 0
296 * is returned.
297 *
298 * This function must be called by the stream itself, so in the context of
299 * process_stream.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200300 */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100301int pendconn_dequeue(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100303 struct pendconn *p;
304
305 if (unlikely(!strm->pend_pos)) {
306 /* unexpected case because it is called by the stream itself and
307 * only the stream can release a pendconn. So it is only
308 * possible if a pendconn is released by someone else or if the
309 * stream is supposed to be queued but without its associated
310 * pendconn. In both cases it is a bug! */
311 abort();
Christopher Faulet8ba59142017-06-27 15:43:53 +0200312 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100313 p = strm->pend_pos;
314 HA_SPIN_LOCK(PENDCONN_LOCK, &p->lock);
315
316 /* the pendconn is still linked to the server/proxy queue, so unlock it
317 * and go away. */
318 if (!LIST_ISEMPTY(&p->list)) {
319 HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock);
320 return 1;
Christopher Faulet8ba59142017-06-27 15:43:53 +0200321 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100322
323 /* the pendconn must be dequeued now */
324 if (p->srv)
325 strm->target = &p->srv->obj_type;
326
327 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
328 strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
329 strm->pend_pos = NULL;
330 HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100331 pool_free(pool_head_pendconn, p);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100332 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333}
334
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100335/* Release the pending connection <p>, and decreases the pending count if
336 * needed. The connection might have been queued to a specific server as well as
337 * to the proxy. The stream also gets marked unqueued. <p> must always be
338 * defined here. So it is the caller responsibility to check its existance.
339 *
340 * This function must be called by the stream itself, so in the context of
341 * process_stream.
342 */
343void pendconn_free(struct pendconn *p)
Christopher Faulet8ba59142017-06-27 15:43:53 +0200344{
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100345 struct stream *strm = p->strm;
346
347 HA_SPIN_LOCK(PENDCONN_LOCK, &p->lock);
348
349 /* The pendconn was already unlinked, just release it. */
350 if (LIST_ISEMPTY(&p->list))
351 goto release;
352
Christopher Faulet8ba59142017-06-27 15:43:53 +0200353 if (p->srv) {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100354 HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock);
355 p->srv->nbpend--;
Christopher Faulet8ba59142017-06-27 15:43:53 +0200356 LIST_DEL(&p->list);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100357 HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200358 }
359 else {
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100360 HA_SPIN_LOCK(PROXY_LOCK, &p->px->lock);
361 p->px->nbpend--;
Christopher Faulet8ba59142017-06-27 15:43:53 +0200362 LIST_DEL(&p->list);
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100363 HA_SPIN_UNLOCK(PROXY_LOCK, &p->px->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200364 }
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100365 HA_ATOMIC_SUB(&p->px->totpend, 1);
366
367 release:
368 strm->pend_pos = NULL;
369 HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100370 pool_free(pool_head_pendconn, p);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200371}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372
373/*
374 * Local variables:
375 * c-indent-level: 8
376 * c-basic-offset: 8
377 * End:
378 */