blob: 95b8edafb132fe7702b97e1a6fcd38bc4e956df0 [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
Emeric Brun52a91d32017-08-31 14:41:55 +020053 if ((s->cur_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
Christopher Fauletf3a55db2017-06-09 14:26:38 +020064/* Returns the first pending connection for server <s>, which may be NULL if
65 * nothing is pending.
66 */
67static inline struct pendconn *pendconn_from_srv(const struct server *s) {
68 if (!s->nbpend)
69 return NULL;
70 return LIST_ELEM(s->pendconns.n, struct pendconn *, list);
71}
72
73/* Returns the first pending connection for proxy <px>, which may be NULL if
74 * nothing is pending.
75 */
76static inline struct pendconn *pendconn_from_px(const struct proxy *px) {
77 if (!px->nbpend)
78 return NULL;
79
80 return LIST_ELEM(px->pendconns.n, struct pendconn *, list);
81}
82
83
Willy Tarreaubaaee002006-06-26 02:48:02 +020084/* Detaches the next pending connection from either a server or a proxy, and
Willy Tarreau87b09662015-04-03 00:22:06 +020085 * returns its associated stream. If no pending connection is found, NULL is
Willy Tarreau7c669d72008-06-20 15:04:11 +020086 * returned. Note that neither <srv> nor <px> may be NULL.
Willy Tarreau70089872008-06-13 21:12:51 +020087 * Priority is given to the oldest request in the queue if both <srv> and <px>
88 * have pending requests. This ensures that no request will be left unserved.
Willy Tarreaud132f742010-08-06 10:08:23 +020089 * The <px> queue is not considered if the server (or a tracked server) is not
90 * RUNNING, is disabled, or has a null weight (server going down). The <srv>
Willy Tarreau922a8062008-12-04 09:33:58 +010091 * queue is still considered in this case, because if some connections remain
92 * there, it means that some requests have been forced there after it was seen
93 * down (eg: due to option persist).
Willy Tarreau87b09662015-04-03 00:22:06 +020094 * The stream is immediately marked as "assigned", and both its <srv> and
Willy Tarreau7c669d72008-06-20 15:04:11 +020095 * <srv_conn> are set to <srv>,
Willy Tarreaubaaee002006-06-26 02:48:02 +020096 */
Christopher Faulet87566c92017-06-06 10:34:51 +020097static struct stream *pendconn_get_next_strm(struct server *srv, struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020098{
Willy Tarreau70089872008-06-13 21:12:51 +020099 struct pendconn *ps, *pp;
Willy Tarreau87b09662015-04-03 00:22:06 +0200100 struct stream *strm;
Willy Tarreaud132f742010-08-06 10:08:23 +0200101 struct server *rsrv;
102
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
Willy Tarreau70089872008-06-13 21:12:51 +0200107 ps = pendconn_from_srv(srv);
108 pp = pendconn_from_px(px);
109 /* we want to get the definitive pendconn in <ps> */
Emeric Brun52a91d32017-08-31 14:41:55 +0200110 if (!pp || !srv_currently_usable(rsrv)) {
Willy Tarreau70089872008-06-13 21:12:51 +0200111 if (!ps)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 return NULL;
Willy Tarreau70089872008-06-13 21:12:51 +0200113 } else {
114 /* pendconn exists in the proxy queue */
Willy Tarreau87b09662015-04-03 00:22:06 +0200115 if (!ps || tv_islt(&pp->strm->logs.tv_request, &ps->strm->logs.tv_request))
Willy Tarreau70089872008-06-13 21:12:51 +0200116 ps = pp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 }
Willy Tarreau87b09662015-04-03 00:22:06 +0200118 strm = ps->strm;
Willy Tarreau70089872008-06-13 21:12:51 +0200119 pendconn_free(ps);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200120
Willy Tarreau87b09662015-04-03 00:22:06 +0200121 /* we want to note that the stream has now been assigned a server */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200122 strm->flags |= SF_ASSIGNED;
Willy Tarreau87b09662015-04-03 00:22:06 +0200123 strm->target = &srv->obj_type;
124 stream_add_srv_conn(strm, srv);
Christopher Faulet29f77e82017-06-08 14:04:45 +0200125 HA_ATOMIC_ADD(&srv->served, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200126 HA_ATOMIC_ADD(&srv->proxy->served, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200127 if (px->lbprm.server_take_conn)
128 px->lbprm.server_take_conn(srv);
129
Willy Tarreau87b09662015-04-03 00:22:06 +0200130 return strm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131}
132
Christopher Faulet87566c92017-06-06 10:34:51 +0200133/*
134 * Manages a server's connection queue. This function will try to dequeue as
135 * many pending streams as possible, and wake them up.
136 */
137void process_srv_queue(struct server *s)
138{
139 struct proxy *p = s->proxy;
140 int maxconn;
141
142 /* First, check if we can handle some connections queued at the proxy. We
143 * will take as many as we can handle.
144 */
145
146 maxconn = srv_dynamic_maxconn(s);
147 while (s->served < maxconn) {
148 struct stream *strm = pendconn_get_next_strm(s, p);
149
150 if (strm == NULL)
151 break;
152 task_wakeup(strm->task, TASK_WOKEN_RES);
153 }
154}
155
Willy Tarreau87b09662015-04-03 00:22:06 +0200156/* Adds the stream <strm> to the pending connection list of server <strm>->srv
157 * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau87b09662015-04-03 00:22:06 +0200159 * pendconn itself. If the stream was already marked as served, its flag is
160 * cleared. It is illegal to call this function with a non-NULL strm->srv_conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200162struct pendconn *pendconn_add(struct stream *strm)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163{
164 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100165 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200167 p = pool_alloc2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 if (!p)
169 return NULL;
170
Willy Tarreau87b09662015-04-03 00:22:06 +0200171 strm->pend_pos = p;
172 p->strm = strm;
173 p->srv = srv = objt_server(strm->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200174
Willy Tarreaue7dff022015-04-03 01:14:29 +0200175 if (strm->flags & SF_ASSIGNED && srv) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100176 LIST_ADDQ(&srv->pendconns, &p->list);
177 srv->nbpend++;
Willy Tarreau87b09662015-04-03 00:22:06 +0200178 strm->logs.srv_queue_size += srv->nbpend;
Willy Tarreau827aee92011-03-10 16:55:02 +0100179 if (srv->nbpend > srv->counters.nbpend_max)
180 srv->counters.nbpend_max = srv->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181 } else {
Willy Tarreau87b09662015-04-03 00:22:06 +0200182 LIST_ADDQ(&strm->be->pendconns, &p->list);
183 strm->be->nbpend++;
184 strm->logs.prx_queue_size += strm->be->nbpend;
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200185 HA_ATOMIC_UPDATE_MAX(&strm->be->be_counters.nbpend_max, strm->be->nbpend);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186 }
Willy Tarreau87b09662015-04-03 00:22:06 +0200187 strm->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188 return p;
189}
190
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200191/* Redistribute pending connections when a server goes down. The number of
192 * connections redistributed is returned.
193 */
194int pendconn_redistribute(struct server *s)
195{
196 struct pendconn *pc, *pc_bck;
197 int xferred = 0;
198
199 list_for_each_entry_safe(pc, pc_bck, &s->pendconns, list) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200200 struct stream *strm = pc->strm;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200201
Willy Tarreau87b09662015-04-03 00:22:06 +0200202 if ((strm->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200203 !(strm->flags & SF_FORCE_PRST)) {
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200204 /* The REDISP option was specified. We will ignore
205 * cookie and force to balance or use the dispatcher.
206 */
207
208 /* it's left to the dispatcher to choose a server */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200209 strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200210
211 pendconn_free(pc);
Willy Tarreau87b09662015-04-03 00:22:06 +0200212 task_wakeup(strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200213 xferred++;
214 }
215 }
216 return xferred;
217}
218
219/* Check for pending connections at the backend, and assign some of them to
220 * the server coming up. The server's weight is checked before being assigned
221 * connections it may not be able to handle. The total number of transferred
222 * connections is returned.
223 */
224int pendconn_grab_from_px(struct server *s)
225{
226 int xferred;
227
Emeric Brun52a91d32017-08-31 14:41:55 +0200228 if (!srv_currently_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200229 return 0;
230
231 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200232 struct stream *strm;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200233 struct pendconn *p;
234
235 p = pendconn_from_px(s->proxy);
236 if (!p)
237 break;
Willy Tarreau87b09662015-04-03 00:22:06 +0200238 p->strm->target = &s->obj_type;
239 strm = p->strm;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200240 pendconn_free(p);
Willy Tarreau87b09662015-04-03 00:22:06 +0200241 task_wakeup(strm->task, TASK_WOKEN_RES);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200242 }
243 return xferred;
244}
245
Willy Tarreaubaaee002006-06-26 02:48:02 +0200246/*
247 * Detaches pending connection <p>, decreases the pending count, and frees
248 * the pending connection. The connection might have been queued to a specific
Willy Tarreau87b09662015-04-03 00:22:06 +0200249 * server as well as to the proxy. The stream also gets marked unqueued.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250 */
251void pendconn_free(struct pendconn *p)
252{
253 LIST_DEL(&p->list);
Willy Tarreau87b09662015-04-03 00:22:06 +0200254 p->strm->pend_pos = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255 if (p->srv)
256 p->srv->nbpend--;
257 else
Willy Tarreau87b09662015-04-03 00:22:06 +0200258 p->strm->be->nbpend--;
259 p->strm->be->totpend--;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200260 pool_free2(pool2_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261}
262
263
264/*
265 * Local variables:
266 * c-indent-level: 8
267 * c-basic-offset: 8
268 * End:
269 */