blob: 1796ac147f8406d95a0bad92866a4c964f1d60c2 [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
Christopher Faulet8ba59142017-06-27 15:43:53 +020027static void __pendconn_free(struct pendconn *p);
28
Willy Tarreaue4d7e552007-05-13 20:19:55 +020029/* perform minimal intializations, report 0 in case of error, 1 if OK. */
30int init_pendconn()
31{
Willy Tarreaubafbe012017-11-24 17:34:44 +010032 pool_head_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED);
33 return pool_head_pendconn != NULL;
Willy Tarreaue4d7e552007-05-13 20:19:55 +020034}
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010037 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +010038 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
39 * server is currently warming up, the slowstart is also applied to the
40 * resulting value, which can be lower than minconn in this case, but never
41 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020042 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020043unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020044{
Willy Tarreau9909fc12007-11-30 17:42:05 +010045 unsigned int max;
46
Willy Tarreau86034312006-12-29 00:10:33 +010047 if (s->proxy->beconn >= s->proxy->fullconn)
48 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +010049 max = s->maxconn;
50 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +010051 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +010052 max = s->maxconn;
53 else max = MAX(s->minconn,
54 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +010055
Emeric Brun52a91d32017-08-31 14:41:55 +020056 if ((s->cur_state == SRV_ST_STARTING) &&
Willy Tarreau9909fc12007-11-30 17:42:05 +010057 now.tv_sec < s->last_change + s->slowstart &&
58 now.tv_sec >= s->last_change) {
59 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +020060 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
61 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +010062 }
63 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +020064}
65
66
Christopher Fauletf3a55db2017-06-09 14:26:38 +020067/* Returns the first pending connection for server <s>, which may be NULL if
68 * nothing is pending.
69 */
70static inline struct pendconn *pendconn_from_srv(const struct server *s) {
71 if (!s->nbpend)
72 return NULL;
73 return LIST_ELEM(s->pendconns.n, struct pendconn *, list);
74}
75
76/* Returns the first pending connection for proxy <px>, which may be NULL if
77 * nothing is pending.
78 */
79static inline struct pendconn *pendconn_from_px(const struct proxy *px) {
80 if (!px->nbpend)
81 return NULL;
82
83 return LIST_ELEM(px->pendconns.n, struct pendconn *, list);
84}
85
86
Willy Tarreaubaaee002006-06-26 02:48:02 +020087/* Detaches the next pending connection from either a server or a proxy, and
Willy Tarreau87b09662015-04-03 00:22:06 +020088 * returns its associated stream. If no pending connection is found, NULL is
Willy Tarreau7c669d72008-06-20 15:04:11 +020089 * returned. Note that neither <srv> nor <px> may be NULL.
Willy Tarreau70089872008-06-13 21:12:51 +020090 * Priority is given to the oldest request in the queue if both <srv> and <px>
91 * have pending requests. This ensures that no request will be left unserved.
Willy Tarreaud132f742010-08-06 10:08:23 +020092 * The <px> queue is not considered if the server (or a tracked server) is not
93 * RUNNING, is disabled, or has a null weight (server going down). The <srv>
Willy Tarreau922a8062008-12-04 09:33:58 +010094 * queue is still considered in this case, because if some connections remain
95 * there, it means that some requests have been forced there after it was seen
96 * down (eg: due to option persist).
Willy Tarreau87b09662015-04-03 00:22:06 +020097 * The stream is immediately marked as "assigned", and both its <srv> and
Willy Tarreau7c669d72008-06-20 15:04:11 +020098 * <srv_conn> are set to <srv>,
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 */
Christopher Faulet87566c92017-06-06 10:34:51 +0200100static struct stream *pendconn_get_next_strm(struct server *srv, struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101{
Willy Tarreau70089872008-06-13 21:12:51 +0200102 struct pendconn *ps, *pp;
Willy Tarreau87b09662015-04-03 00:22:06 +0200103 struct stream *strm;
Willy Tarreaud132f742010-08-06 10:08:23 +0200104 struct server *rsrv;
105
Willy Tarreau44267702011-10-28 15:35:33 +0200106 rsrv = srv->track;
Willy Tarreaud132f742010-08-06 10:08:23 +0200107 if (!rsrv)
108 rsrv = srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109
Willy Tarreau70089872008-06-13 21:12:51 +0200110 ps = pendconn_from_srv(srv);
111 pp = pendconn_from_px(px);
112 /* we want to get the definitive pendconn in <ps> */
Emeric Brun52a91d32017-08-31 14:41:55 +0200113 if (!pp || !srv_currently_usable(rsrv)) {
Willy Tarreau70089872008-06-13 21:12:51 +0200114 if (!ps)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 return NULL;
Willy Tarreau70089872008-06-13 21:12:51 +0200116 } else {
117 /* pendconn exists in the proxy queue */
Willy Tarreau87b09662015-04-03 00:22:06 +0200118 if (!ps || tv_islt(&pp->strm->logs.tv_request, &ps->strm->logs.tv_request))
Willy Tarreau70089872008-06-13 21:12:51 +0200119 ps = pp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120 }
Willy Tarreau87b09662015-04-03 00:22:06 +0200121 strm = ps->strm;
Christopher Faulet8ba59142017-06-27 15:43:53 +0200122 __pendconn_free(ps);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200123
Willy Tarreau87b09662015-04-03 00:22:06 +0200124 /* we want to note that the stream has now been assigned a server */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200125 strm->flags |= SF_ASSIGNED;
Willy Tarreau87b09662015-04-03 00:22:06 +0200126 strm->target = &srv->obj_type;
127 stream_add_srv_conn(strm, srv);
Christopher Faulet29f77e82017-06-08 14:04:45 +0200128 HA_ATOMIC_ADD(&srv->served, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200129 HA_ATOMIC_ADD(&srv->proxy->served, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200130 if (px->lbprm.server_take_conn)
131 px->lbprm.server_take_conn(srv);
132
Willy Tarreau87b09662015-04-03 00:22:06 +0200133 return strm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134}
135
Christopher Faulet87566c92017-06-06 10:34:51 +0200136/*
137 * Manages a server's connection queue. This function will try to dequeue as
138 * many pending streams as possible, and wake them up.
139 */
140void process_srv_queue(struct server *s)
141{
142 struct proxy *p = s->proxy;
143 int maxconn;
144
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100145 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
146 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200147
Christopher Faulet87566c92017-06-06 10:34:51 +0200148 /* First, check if we can handle some connections queued at the proxy. We
149 * will take as many as we can handle.
150 */
Christopher Faulet87566c92017-06-06 10:34:51 +0200151 maxconn = srv_dynamic_maxconn(s);
152 while (s->served < maxconn) {
153 struct stream *strm = pendconn_get_next_strm(s, p);
154
155 if (strm == NULL)
156 break;
157 task_wakeup(strm->task, TASK_WOKEN_RES);
158 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100159 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
160 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Christopher Faulet87566c92017-06-06 10:34:51 +0200161}
162
Willy Tarreau87b09662015-04-03 00:22:06 +0200163/* Adds the stream <strm> to the pending connection list of server <strm>->srv
164 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200166 * pendconn itself. If the stream was already marked as served, its flag is
167 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200169struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170{
171 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100172 struct server *srv;
Christopher Faulet8ba59142017-06-27 15:43:53 +0200173 int count;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200174
Willy Tarreaubafbe012017-11-24 17:34:44 +0100175 p = pool_alloc(pool_head_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176 if (!p)
177 return NULL;
178
Willy Tarreau87b09662015-04-03 00:22:06 +0200179 strm->pend_pos = p;
180 p->strm = strm;
Christopher Faulet8ba59142017-06-27 15:43:53 +0200181 srv = objt_server(strm->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200182
Christopher Faulet8ba59142017-06-27 15:43:53 +0200183 if ((strm->flags & SF_ASSIGNED) && srv) {
184 p->srv = srv;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100185 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau827aee92011-03-10 16:55:02 +0100186 LIST_ADDQ(&srv->pendconns, &p->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100187 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200188 count = HA_ATOMIC_ADD(&srv->nbpend, 1);
189 strm->logs.srv_queue_size += count;
190 HA_ATOMIC_UPDATE_MAX(&srv->counters.nbpend_max, count);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191 } else {
Christopher Faulet8ba59142017-06-27 15:43:53 +0200192 p->srv = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100193 HA_SPIN_LOCK(PROXY_LOCK, &strm->be->lock);
Willy Tarreau87b09662015-04-03 00:22:06 +0200194 LIST_ADDQ(&strm->be->pendconns, &p->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100195 HA_SPIN_UNLOCK(PROXY_LOCK, &strm->be->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200196 count = HA_ATOMIC_ADD(&strm->be->nbpend, 1);
197 strm->logs.prx_queue_size += count;
198 HA_ATOMIC_UPDATE_MAX(&strm->be->be_counters.nbpend_max, count);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199 }
Christopher Faulet8ba59142017-06-27 15:43:53 +0200200 HA_ATOMIC_ADD(&strm->be->totpend, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201 return p;
202}
203
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200204/* Redistribute pending connections when a server goes down. The number of
205 * connections redistributed is returned.
206 */
207int pendconn_redistribute(struct server *s)
208{
209 struct pendconn *pc, *pc_bck;
210 int xferred = 0;
211
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100212 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200213 list_for_each_entry_safe(pc, pc_bck, &s->pendconns, list) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200214 struct stream *strm = pc->strm;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200215
Willy Tarreau87b09662015-04-03 00:22:06 +0200216 if ((strm->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200217 !(strm->flags & SF_FORCE_PRST)) {
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200218 /* The REDISP option was specified. We will ignore
219 * cookie and force to balance or use the dispatcher.
220 */
221
222 /* it's left to the dispatcher to choose a server */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200223 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200224
Christopher Faulet8ba59142017-06-27 15:43:53 +0200225 __pendconn_free(pc);
Willy Tarreau87b09662015-04-03 00:22:06 +0200226 task_wakeup(strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200227 xferred++;
228 }
229 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100230 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200231 return xferred;
232}
233
234/* Check for pending connections at the backend, and assign some of them to
235 * the server coming up. The server's weight is checked before being assigned
236 * connections it may not be able to handle. The total number of transferred
237 * connections is returned.
238 */
239int pendconn_grab_from_px(struct server *s)
240{
241 int xferred;
242
Emeric Brun52a91d32017-08-31 14:41:55 +0200243 if (!srv_currently_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200244 return 0;
245
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100246 HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200247 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200248 struct stream *strm;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200249 struct pendconn *p;
250
251 p = pendconn_from_px(s->proxy);
252 if (!p)
253 break;
Willy Tarreau87b09662015-04-03 00:22:06 +0200254 p->strm->target = &s->obj_type;
255 strm = p->strm;
Christopher Faulet8ba59142017-06-27 15:43:53 +0200256 __pendconn_free(p);
Willy Tarreau87b09662015-04-03 00:22:06 +0200257 task_wakeup(strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200258 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100259 HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200260 return xferred;
261}
262
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263/*
264 * Detaches pending connection <p>, decreases the pending count, and frees
265 * the pending connection. The connection might have been queued to a specific
Willy Tarreau87b09662015-04-03 00:22:06 +0200266 * server as well as to the proxy. The stream also gets marked unqueued.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200267 */
268void pendconn_free(struct pendconn *p)
269{
Christopher Faulet8ba59142017-06-27 15:43:53 +0200270 if (p->srv) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100271 HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200272 LIST_DEL(&p->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100273 HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200274 HA_ATOMIC_SUB(&p->srv->nbpend, 1);
275 }
276 else {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100277 HA_SPIN_LOCK(SERVER_LOCK, &p->strm->be->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200278 LIST_DEL(&p->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100279 HA_SPIN_UNLOCK(SERVER_LOCK, &p->strm->be->lock);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200280 HA_ATOMIC_SUB(&p->strm->be->nbpend, 1);
281 }
Willy Tarreau87b09662015-04-03 00:22:06 +0200282 p->strm->pend_pos = NULL;
Christopher Faulet8ba59142017-06-27 15:43:53 +0200283 HA_ATOMIC_SUB(&p->strm->be->totpend, 1);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100284 pool_free(pool_head_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285}
286
Christopher Faulet8ba59142017-06-27 15:43:53 +0200287/* Lock-free version of pendconn_free. */
288static void __pendconn_free(struct pendconn *p)
289{
290 if (p->srv) {
291 LIST_DEL(&p->list);
292 HA_ATOMIC_SUB(&p->srv->nbpend, 1);
293 }
294 else {
295 LIST_DEL(&p->list);
296 HA_ATOMIC_SUB(&p->strm->be->nbpend, 1);
297 }
298 p->strm->pend_pos = NULL;
299 HA_ATOMIC_SUB(&p->strm->be->totpend, 1);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100300 pool_free(pool_head_pendconn, p);
Christopher Faulet8ba59142017-06-27 15:43:53 +0200301}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302
303/*
304 * Local variables:
305 * c-indent-level: 8
306 * c-basic-offset: 8
307 * End:
308 */