blob: 1f27c498b0a08cafee61e816dada2f4dc1a425f7 [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016
Willy Tarreaubaaee002006-06-26 02:48:02 +020017#include <proto/queue.h>
18#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020019#include <proto/stream.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010020#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <proto/task.h>
22
23
Willy Tarreaue4d7e552007-05-13 20:19:55 +020024struct pool_head *pool2_pendconn;
25
26/* perform minimal intializations, report 0 in case of error, 1 if OK. */
27int init_pendconn()
28{
29 pool2_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED);
30 return pool2_pendconn != NULL;
31}
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
33/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010034 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +010035 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
36 * server is currently warming up, the slowstart is also applied to the
37 * resulting value, which can be lower than minconn in this case, but never
38 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020039 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020040unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020041{
Willy Tarreau9909fc12007-11-30 17:42:05 +010042 unsigned int max;
43
Willy Tarreau86034312006-12-29 00:10:33 +010044 if (s->proxy->beconn >= s->proxy->fullconn)
45 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +010046 max = s->maxconn;
47 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +010048 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +010049 max = s->maxconn;
50 else max = MAX(s->minconn,
51 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +010052
Willy Tarreau892337c2014-05-13 23:41:20 +020053 if ((s->state == SRV_ST_STARTING) &&
Willy Tarreau9909fc12007-11-30 17:42:05 +010054 now.tv_sec < s->last_change + s->slowstart &&
55 now.tv_sec >= s->last_change) {
56 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +020057 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
58 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +010059 }
60 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +020061}
62
63
64/*
Willy Tarreau7c669d72008-06-20 15:04:11 +020065 * Manages a server's connection queue. This function will try to dequeue as
Willy Tarreau87b09662015-04-03 00:22:06 +020066 * many pending streams as possible, and wake them up.
Willy Tarreaubaaee002006-06-26 02:48:02 +020067 */
Willy Tarreau7c669d72008-06-20 15:04:11 +020068void process_srv_queue(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020069{
Willy Tarreaubaaee002006-06-26 02:48:02 +020070 struct proxy *p = s->proxy;
Willy Tarreau7c669d72008-06-20 15:04:11 +020071 int maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
73 /* First, check if we can handle some connections queued at the proxy. We
74 * will take as many as we can handle.
75 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020076
Willy Tarreau7c669d72008-06-20 15:04:11 +020077 maxconn = srv_dynamic_maxconn(s);
78 while (s->served < maxconn) {
Willy Tarreau87b09662015-04-03 00:22:06 +020079 struct stream *strm = pendconn_get_next_strm(s, p);
80
81 if (strm == NULL)
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 break;
Willy Tarreau87b09662015-04-03 00:22:06 +020083 task_wakeup(strm->task, TASK_WOKEN_RES);
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020085}
86
87/* 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 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200100struct 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> */
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200113 if (!pp || !srv_is_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;
Willy Tarreau70089872008-06-13 21:12:51 +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);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200128 srv->served++;
129 if (px->lbprm.server_take_conn)
130 px->lbprm.server_take_conn(srv);
131
Willy Tarreau87b09662015-04-03 00:22:06 +0200132 return strm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133}
134
Willy Tarreau87b09662015-04-03 00:22:06 +0200135/* Adds the stream <strm> to the pending connection list of server <strm>->srv
136 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200138 * pendconn itself. If the stream was already marked as served, its flag is
139 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200140 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200141struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200142{
143 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100144 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200146 p = pool_alloc2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147 if (!p)
148 return NULL;
149
Willy Tarreau87b09662015-04-03 00:22:06 +0200150 strm->pend_pos = p;
151 p->strm = strm;
152 p->srv = srv = objt_server(strm->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200153
Willy Tarreaue7dff022015-04-03 01:14:29 +0200154 if (strm->flags & SF_ASSIGNED && srv) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100155 LIST_ADDQ(&srv->pendconns, &p->list);
156 srv->nbpend++;
Willy Tarreau87b09662015-04-03 00:22:06 +0200157 strm->logs.srv_queue_size += srv->nbpend;
Willy Tarreau827aee92011-03-10 16:55:02 +0100158 if (srv->nbpend > srv->counters.nbpend_max)
159 srv->counters.nbpend_max = srv->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200160 } else {
Willy Tarreau87b09662015-04-03 00:22:06 +0200161 LIST_ADDQ(&strm->be->pendconns, &p->list);
162 strm->be->nbpend++;
163 strm->logs.prx_queue_size += strm->be->nbpend;
164 if (strm->be->nbpend > strm->be->be_counters.nbpend_max)
165 strm->be->be_counters.nbpend_max = strm->be->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 }
Willy Tarreau87b09662015-04-03 00:22:06 +0200167 strm->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 return p;
169}
170
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200171/* Redistribute pending connections when a server goes down. The number of
172 * connections redistributed is returned.
173 */
174int pendconn_redistribute(struct server *s)
175{
176 struct pendconn *pc, *pc_bck;
177 int xferred = 0;
178
179 list_for_each_entry_safe(pc, pc_bck, &s->pendconns, list) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200180 struct stream *strm = pc->strm;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200181
Willy Tarreau87b09662015-04-03 00:22:06 +0200182 if ((strm->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200183 !(strm->flags & SF_FORCE_PRST)) {
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200184 /* The REDISP option was specified. We will ignore
185 * cookie and force to balance or use the dispatcher.
186 */
187
188 /* it's left to the dispatcher to choose a server */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200189 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200190
191 pendconn_free(pc);
Willy Tarreau87b09662015-04-03 00:22:06 +0200192 task_wakeup(strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200193 xferred++;
194 }
195 }
196 return xferred;
197}
198
199/* Check for pending connections at the backend, and assign some of them to
200 * the server coming up. The server's weight is checked before being assigned
201 * connections it may not be able to handle. The total number of transferred
202 * connections is returned.
203 */
204int pendconn_grab_from_px(struct server *s)
205{
206 int xferred;
207
Willy Tarreau9943d312014-05-22 16:20:59 +0200208 if (!srv_is_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200209 return 0;
210
211 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200212 struct stream *strm;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200213 struct pendconn *p;
214
215 p = pendconn_from_px(s->proxy);
216 if (!p)
217 break;
Willy Tarreau87b09662015-04-03 00:22:06 +0200218 p->strm->target = &s->obj_type;
219 strm = p->strm;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200220 pendconn_free(p);
Willy Tarreau87b09662015-04-03 00:22:06 +0200221 task_wakeup(strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200222 }
223 return xferred;
224}
225
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226/*
227 * Detaches pending connection <p>, decreases the pending count, and frees
228 * the pending connection. The connection might have been queued to a specific
Willy Tarreau87b09662015-04-03 00:22:06 +0200229 * server as well as to the proxy. The stream also gets marked unqueued.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230 */
231void pendconn_free(struct pendconn *p)
232{
233 LIST_DEL(&p->list);
Willy Tarreau87b09662015-04-03 00:22:06 +0200234 p->strm->pend_pos = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235 if (p->srv)
236 p->srv->nbpend--;
237 else
Willy Tarreau87b09662015-04-03 00:22:06 +0200238 p->strm->be->nbpend--;
239 p->strm->be->totpend--;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200240 pool_free2(pool2_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241}
242
243
244/*
245 * Local variables:
246 * c-indent-level: 8
247 * c-basic-offset: 8
248 * End:
249 */