blob: 74fa659212ec85561fdd354b97159d4c7b55a03c [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>
Simon Hormanaf514952011-06-21 14:34:57 +090019#include <proto/session.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
66 * many pending sessions 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) {
79 struct session *sess = pendconn_get_next_sess(s, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +020080 if (sess == NULL)
81 break;
Willy Tarreaufdccded2008-08-29 18:19:04 +020082 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubaaee002006-06-26 02:48:02 +020083 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020084}
85
86/* Detaches the next pending connection from either a server or a proxy, and
87 * returns its associated session. If no pending connection is found, NULL is
Willy Tarreau7c669d72008-06-20 15:04:11 +020088 * returned. Note that neither <srv> nor <px> may be NULL.
Willy Tarreau70089872008-06-13 21:12:51 +020089 * Priority is given to the oldest request in the queue if both <srv> and <px>
90 * have pending requests. This ensures that no request will be left unserved.
Willy Tarreaud132f742010-08-06 10:08:23 +020091 * The <px> queue is not considered if the server (or a tracked server) is not
92 * RUNNING, is disabled, or has a null weight (server going down). The <srv>
Willy Tarreau922a8062008-12-04 09:33:58 +010093 * queue is still considered in this case, because if some connections remain
94 * there, it means that some requests have been forced there after it was seen
95 * down (eg: due to option persist).
Willy Tarreau7c669d72008-06-20 15:04:11 +020096 * The session is immediately marked as "assigned", and both its <srv> and
97 * <srv_conn> are set to <srv>,
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 */
99struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
100{
Willy Tarreau70089872008-06-13 21:12:51 +0200101 struct pendconn *ps, *pp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102 struct session *sess;
Willy Tarreaud132f742010-08-06 10:08:23 +0200103 struct server *rsrv;
104
Willy Tarreau44267702011-10-28 15:35:33 +0200105 rsrv = srv->track;
Willy Tarreaud132f742010-08-06 10:08:23 +0200106 if (!rsrv)
107 rsrv = srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108
Willy Tarreau70089872008-06-13 21:12:51 +0200109 ps = pendconn_from_srv(srv);
110 pp = pendconn_from_px(px);
111 /* we want to get the definitive pendconn in <ps> */
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200112 if (!pp || !srv_is_usable(rsrv)) {
Willy Tarreau70089872008-06-13 21:12:51 +0200113 if (!ps)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 return NULL;
Willy Tarreau70089872008-06-13 21:12:51 +0200115 } else {
116 /* pendconn exists in the proxy queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100117 if (!ps || tv_islt(&pp->sess->logs.tv_request, &ps->sess->logs.tv_request))
Willy Tarreau70089872008-06-13 21:12:51 +0200118 ps = pp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 }
Willy Tarreau70089872008-06-13 21:12:51 +0200120 sess = ps->sess;
121 pendconn_free(ps);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200122
123 /* we want to note that the session has now been assigned a server */
124 sess->flags |= SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100125 sess->target = &srv->obj_type;
Simon Hormanaf514952011-06-21 14:34:57 +0900126 session_add_srv_conn(sess, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200127 srv->served++;
128 if (px->lbprm.server_take_conn)
129 px->lbprm.server_take_conn(srv);
130
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131 return sess;
132}
133
134/* Adds the session <sess> to the pending connection list of server <sess>->srv
135 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
136 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200137 * pendconn itself. If the session was already marked as served, its flag is
138 * cleared. It is illegal to call this function with a non-NULL sess->srv_conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139 */
140struct pendconn *pendconn_add(struct session *sess)
141{
142 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100143 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200145 p = pool_alloc2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146 if (!p)
147 return NULL;
148
149 sess->pend_pos = p;
150 p->sess = sess;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100151 p->srv = srv = objt_server(sess->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200152
Willy Tarreau827aee92011-03-10 16:55:02 +0100153 if (sess->flags & SN_ASSIGNED && srv) {
154 LIST_ADDQ(&srv->pendconns, &p->list);
155 srv->nbpend++;
156 sess->logs.srv_queue_size += srv->nbpend;
157 if (srv->nbpend > srv->counters.nbpend_max)
158 srv->counters.nbpend_max = srv->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200160 LIST_ADDQ(&sess->be->pendconns, &p->list);
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200161 sess->be->nbpend++;
Willy Tarreau7a63abd2008-06-13 21:48:18 +0200162 sess->logs.prx_queue_size += sess->be->nbpend;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100163 if (sess->be->nbpend > sess->be->be_counters.nbpend_max)
164 sess->be->be_counters.nbpend_max = sess->be->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200166 sess->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167 return p;
168}
169
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200170/* Redistribute pending connections when a server goes down. The number of
171 * connections redistributed is returned.
172 */
173int pendconn_redistribute(struct server *s)
174{
175 struct pendconn *pc, *pc_bck;
176 int xferred = 0;
177
178 list_for_each_entry_safe(pc, pc_bck, &s->pendconns, list) {
179 struct session *sess = pc->sess;
180
181 if ((sess->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP &&
182 !(sess->flags & SN_FORCE_PRST)) {
183 /* The REDISP option was specified. We will ignore
184 * cookie and force to balance or use the dispatcher.
185 */
186
187 /* it's left to the dispatcher to choose a server */
188 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
189
190 pendconn_free(pc);
191 task_wakeup(sess->task, TASK_WOKEN_RES);
192 xferred++;
193 }
194 }
195 return xferred;
196}
197
198/* Check for pending connections at the backend, and assign some of them to
199 * the server coming up. The server's weight is checked before being assigned
200 * connections it may not be able to handle. The total number of transferred
201 * connections is returned.
202 */
203int pendconn_grab_from_px(struct server *s)
204{
205 int xferred;
206
Willy Tarreau9943d312014-05-22 16:20:59 +0200207 if (!srv_is_usable(s))
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200208 return 0;
209
210 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
211 struct session *sess;
212 struct pendconn *p;
213
214 p = pendconn_from_px(s->proxy);
215 if (!p)
216 break;
217 p->sess->target = &s->obj_type;
218 sess = p->sess;
219 pendconn_free(p);
220 task_wakeup(sess->task, TASK_WOKEN_RES);
221 }
222 return xferred;
223}
224
Willy Tarreaubaaee002006-06-26 02:48:02 +0200225/*
226 * Detaches pending connection <p>, decreases the pending count, and frees
227 * the pending connection. The connection might have been queued to a specific
228 * server as well as to the proxy. The session also gets marked unqueued.
229 */
230void pendconn_free(struct pendconn *p)
231{
232 LIST_DEL(&p->list);
233 p->sess->pend_pos = NULL;
234 if (p->srv)
235 p->srv->nbpend--;
236 else
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200237 p->sess->be->nbpend--;
238 p->sess->be->totpend--;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200239 pool_free2(pool2_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240}
241
242
243/*
244 * Local variables:
245 * c-indent-level: 8
246 * c-basic-offset: 8
247 * End:
248 */