blob: 48380fcd8e86b448cdb95535f1cd678a4c69dab6 [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 Tarreau9e000c62011-03-10 14:03:36 +010019#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020#include <proto/task.h>
21
22
Willy Tarreaue4d7e552007-05-13 20:19:55 +020023struct pool_head *pool2_pendconn;
24
25/* perform minimal intializations, report 0 in case of error, 1 if OK. */
26int init_pendconn()
27{
28 pool2_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED);
29 return pool2_pendconn != NULL;
30}
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
32/* returns the effective dynamic maxconn for a server, considering the minconn
Willy Tarreau86034312006-12-29 00:10:33 +010033 * and the proxy's usage relative to its dynamic connections limit. It is
Willy Tarreau9909fc12007-11-30 17:42:05 +010034 * expected that 0 < s->minconn <= s->maxconn when this is called. If the
35 * server is currently warming up, the slowstart is also applied to the
36 * resulting value, which can be lower than minconn in this case, but never
37 * less than 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +020038 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020039unsigned int srv_dynamic_maxconn(const struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020040{
Willy Tarreau9909fc12007-11-30 17:42:05 +010041 unsigned int max;
42
Willy Tarreau86034312006-12-29 00:10:33 +010043 if (s->proxy->beconn >= s->proxy->fullconn)
44 /* no fullconn or proxy is full */
Willy Tarreau9909fc12007-11-30 17:42:05 +010045 max = s->maxconn;
46 else if (s->minconn == s->maxconn)
Willy Tarreau86034312006-12-29 00:10:33 +010047 /* static limit */
Willy Tarreau9909fc12007-11-30 17:42:05 +010048 max = s->maxconn;
49 else max = MAX(s->minconn,
50 s->proxy->beconn * s->maxconn / s->proxy->fullconn);
Willy Tarreau86034312006-12-29 00:10:33 +010051
Willy Tarreau9909fc12007-11-30 17:42:05 +010052 if ((s->state & SRV_WARMINGUP) &&
53 now.tv_sec < s->last_change + s->slowstart &&
54 now.tv_sec >= s->last_change) {
55 unsigned int ratio;
Willy Tarreau28a9e522008-09-14 17:43:27 +020056 ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart;
57 max = MAX(1, max * ratio / 100);
Willy Tarreau9909fc12007-11-30 17:42:05 +010058 }
59 return max;
Willy Tarreaubaaee002006-06-26 02:48:02 +020060}
61
62
63/*
Willy Tarreau7c669d72008-06-20 15:04:11 +020064 * Manages a server's connection queue. This function will try to dequeue as
65 * many pending sessions as possible, and wake them up.
Willy Tarreaubaaee002006-06-26 02:48:02 +020066 */
Willy Tarreau7c669d72008-06-20 15:04:11 +020067void process_srv_queue(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +020068{
Willy Tarreaubaaee002006-06-26 02:48:02 +020069 struct proxy *p = s->proxy;
Willy Tarreau7c669d72008-06-20 15:04:11 +020070 int maxconn;
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
72 /* First, check if we can handle some connections queued at the proxy. We
73 * will take as many as we can handle.
74 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreau7c669d72008-06-20 15:04:11 +020076 maxconn = srv_dynamic_maxconn(s);
77 while (s->served < maxconn) {
78 struct session *sess = pendconn_get_next_sess(s, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +020079 if (sess == NULL)
80 break;
Willy Tarreaufdccded2008-08-29 18:19:04 +020081 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020083}
84
85/* Detaches the next pending connection from either a server or a proxy, and
86 * returns its associated session. If no pending connection is found, NULL is
Willy Tarreau7c669d72008-06-20 15:04:11 +020087 * returned. Note that neither <srv> nor <px> may be NULL.
Willy Tarreau70089872008-06-13 21:12:51 +020088 * Priority is given to the oldest request in the queue if both <srv> and <px>
89 * have pending requests. This ensures that no request will be left unserved.
Willy Tarreaud132f742010-08-06 10:08:23 +020090 * The <px> queue is not considered if the server (or a tracked server) is not
91 * RUNNING, is disabled, or has a null weight (server going down). The <srv>
Willy Tarreau922a8062008-12-04 09:33:58 +010092 * queue is still considered in this case, because if some connections remain
93 * there, it means that some requests have been forced there after it was seen
94 * down (eg: due to option persist).
Willy Tarreau7c669d72008-06-20 15:04:11 +020095 * The session is immediately marked as "assigned", and both its <srv> and
96 * <srv_conn> are set to <srv>,
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 */
98struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px)
99{
Willy Tarreau70089872008-06-13 21:12:51 +0200100 struct pendconn *ps, *pp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 struct session *sess;
Willy Tarreaud132f742010-08-06 10:08:23 +0200102 struct server *rsrv;
103
104 rsrv = srv->tracked;
105 if (!rsrv)
106 rsrv = srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107
Willy Tarreau70089872008-06-13 21:12:51 +0200108 ps = pendconn_from_srv(srv);
109 pp = pendconn_from_px(px);
110 /* we want to get the definitive pendconn in <ps> */
Willy Tarreaud132f742010-08-06 10:08:23 +0200111 if (!pp || !(rsrv->state & SRV_RUNNING) || (rsrv->state & (SRV_GOINGDOWN|SRV_MAINTAIN))) {
Willy Tarreau70089872008-06-13 21:12:51 +0200112 if (!ps)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 return NULL;
Willy Tarreau70089872008-06-13 21:12:51 +0200114 } else {
115 /* pendconn exists in the proxy queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100116 if (!ps || tv_islt(&pp->sess->logs.tv_request, &ps->sess->logs.tv_request))
Willy Tarreau70089872008-06-13 21:12:51 +0200117 ps = pp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118 }
Willy Tarreau70089872008-06-13 21:12:51 +0200119 sess = ps->sess;
120 pendconn_free(ps);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200121
122 /* we want to note that the session has now been assigned a server */
123 sess->flags |= SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100124 set_target_server(&sess->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200125 sess->srv_conn = srv;
126 srv->served++;
127 if (px->lbprm.server_take_conn)
128 px->lbprm.server_take_conn(srv);
129
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130 return sess;
131}
132
133/* Adds the session <sess> to the pending connection list of server <sess>->srv
134 * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers
135 * are updated accordingly. Returns NULL if no memory is available, otherwise the
Willy Tarreau7c669d72008-06-20 15:04:11 +0200136 * pendconn itself. If the session was already marked as served, its flag is
137 * cleared. It is illegal to call this function with a non-NULL sess->srv_conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138 */
139struct pendconn *pendconn_add(struct session *sess)
140{
141 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100142 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200144 p = pool_alloc2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145 if (!p)
146 return NULL;
147
148 sess->pend_pos = p;
149 p->sess = sess;
Willy Tarreau827aee92011-03-10 16:55:02 +0100150 p->srv = srv = target_srv(&sess->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200151
Willy Tarreau827aee92011-03-10 16:55:02 +0100152 if (sess->flags & SN_ASSIGNED && srv) {
153 LIST_ADDQ(&srv->pendconns, &p->list);
154 srv->nbpend++;
155 sess->logs.srv_queue_size += srv->nbpend;
156 if (srv->nbpend > srv->counters.nbpend_max)
157 srv->counters.nbpend_max = srv->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200159 LIST_ADDQ(&sess->be->pendconns, &p->list);
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200160 sess->be->nbpend++;
Willy Tarreau7a63abd2008-06-13 21:48:18 +0200161 sess->logs.prx_queue_size += sess->be->nbpend;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100162 if (sess->be->nbpend > sess->be->be_counters.nbpend_max)
163 sess->be->be_counters.nbpend_max = sess->be->nbpend;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200165 sess->be->totpend++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 return p;
167}
168
169/*
170 * Detaches pending connection <p>, decreases the pending count, and frees
171 * the pending connection. The connection might have been queued to a specific
172 * server as well as to the proxy. The session also gets marked unqueued.
173 */
174void pendconn_free(struct pendconn *p)
175{
176 LIST_DEL(&p->list);
177 p->sess->pend_pos = NULL;
178 if (p->srv)
179 p->srv->nbpend--;
180 else
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200181 p->sess->be->nbpend--;
182 p->sess->be->totpend--;
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200183 pool_free2(pool2_pendconn, p);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184}
185
186
187/*
188 * Local variables:
189 * c-indent-level: 8
190 * c-basic-offset: 8
191 * End:
192 */