Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Queue management functions. |
| 3 | * |
Willy Tarreau | ac68c5d | 2009-10-04 23:12:44 +0200 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 13 | #include <common/config.h> |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 14 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 15 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 17 | #include <proto/queue.h> |
| 18 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 19 | #include <proto/stream.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 20 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | #include <proto/task.h> |
| 22 | |
| 23 | |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 24 | struct pool_head *pool2_pendconn; |
| 25 | |
| 26 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 27 | int init_pendconn() |
| 28 | { |
| 29 | pool2_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED); |
| 30 | return pool2_pendconn != NULL; |
| 31 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | |
| 33 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 34 | * and the proxy's usage relative to its dynamic connections limit. It is |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 35 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 40 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 42 | unsigned int max; |
| 43 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 44 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 45 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 46 | max = s->maxconn; |
| 47 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 48 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 49 | max = s->maxconn; |
| 50 | else max = MAX(s->minconn, |
| 51 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 52 | |
Willy Tarreau | 892337c | 2014-05-13 23:41:20 +0200 | [diff] [blame] | 53 | if ((s->state == SRV_ST_STARTING) && |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 54 | now.tv_sec < s->last_change + s->slowstart && |
| 55 | now.tv_sec >= s->last_change) { |
| 56 | unsigned int ratio; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 57 | ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; |
| 58 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 59 | } |
| 60 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | /* |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 65 | * Manages a server's connection queue. This function will try to dequeue as |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 66 | * many pending streams as possible, and wake them up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 68 | void process_srv_queue(struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | struct proxy *p = s->proxy; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 71 | int maxconn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 77 | maxconn = srv_dynamic_maxconn(s); |
| 78 | while (s->served < maxconn) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 79 | struct stream *strm = pendconn_get_next_strm(s, p); |
| 80 | |
| 81 | if (strm == NULL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | break; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 83 | task_wakeup(strm->task, TASK_WOKEN_RES); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Detaches the next pending connection from either a server or a proxy, and |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 88 | * returns its associated stream. If no pending connection is found, NULL is |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 89 | * returned. Note that neither <srv> nor <px> may be NULL. |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 90 | * 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 Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 92 | * 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 Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 94 | * 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 Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 97 | * The stream is immediately marked as "assigned", and both its <srv> and |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 98 | * <srv_conn> are set to <srv>, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 100 | struct stream *pendconn_get_next_strm(struct server *srv, struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | { |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 102 | struct pendconn *ps, *pp; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 103 | struct stream *strm; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 104 | struct server *rsrv; |
| 105 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 106 | rsrv = srv->track; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 107 | if (!rsrv) |
| 108 | rsrv = srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 110 | ps = pendconn_from_srv(srv); |
| 111 | pp = pendconn_from_px(px); |
| 112 | /* we want to get the definitive pendconn in <ps> */ |
Willy Tarreau | 87eb1d6 | 2014-05-13 18:51:40 +0200 | [diff] [blame] | 113 | if (!pp || !srv_is_usable(rsrv)) { |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 114 | if (!ps) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | return NULL; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 116 | } else { |
| 117 | /* pendconn exists in the proxy queue */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 118 | if (!ps || tv_islt(&pp->strm->logs.tv_request, &ps->strm->logs.tv_request)) |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 119 | ps = pp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 120 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 121 | strm = ps->strm; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 122 | pendconn_free(ps); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 123 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 124 | /* we want to note that the stream has now been assigned a server */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 125 | strm->flags |= SF_ASSIGNED; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 126 | strm->target = &srv->obj_type; |
| 127 | stream_add_srv_conn(strm, srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 128 | srv->served++; |
Andrew Rodland | e168feb | 2016-10-25 12:48:17 -0400 | [diff] [blame] | 129 | srv->proxy->served++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 130 | if (px->lbprm.server_take_conn) |
| 131 | px->lbprm.server_take_conn(srv); |
| 132 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 133 | return strm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 136 | /* Adds the stream <strm> to the pending connection list of server <strm>->srv |
| 137 | * or to the one of <strm>->proxy if srv is NULL. All counters and back pointers |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 138 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 139 | * pendconn itself. If the stream was already marked as served, its flag is |
| 140 | * cleared. It is illegal to call this function with a non-NULL strm->srv_conn. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 141 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 142 | struct pendconn *pendconn_add(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | { |
| 144 | struct pendconn *p; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 145 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 146 | |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 147 | p = pool_alloc2(pool2_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 148 | if (!p) |
| 149 | return NULL; |
| 150 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 151 | strm->pend_pos = p; |
| 152 | p->strm = strm; |
| 153 | p->srv = srv = objt_server(strm->target); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 154 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 155 | if (strm->flags & SF_ASSIGNED && srv) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 156 | LIST_ADDQ(&srv->pendconns, &p->list); |
| 157 | srv->nbpend++; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 158 | strm->logs.srv_queue_size += srv->nbpend; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 159 | if (srv->nbpend > srv->counters.nbpend_max) |
| 160 | srv->counters.nbpend_max = srv->nbpend; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | } else { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 162 | LIST_ADDQ(&strm->be->pendconns, &p->list); |
| 163 | strm->be->nbpend++; |
| 164 | strm->logs.prx_queue_size += strm->be->nbpend; |
| 165 | if (strm->be->nbpend > strm->be->be_counters.nbpend_max) |
| 166 | strm->be->be_counters.nbpend_max = strm->be->nbpend; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 168 | strm->be->totpend++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | return p; |
| 170 | } |
| 171 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 172 | /* Redistribute pending connections when a server goes down. The number of |
| 173 | * connections redistributed is returned. |
| 174 | */ |
| 175 | int pendconn_redistribute(struct server *s) |
| 176 | { |
| 177 | struct pendconn *pc, *pc_bck; |
| 178 | int xferred = 0; |
| 179 | |
| 180 | list_for_each_entry_safe(pc, pc_bck, &s->pendconns, list) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 181 | struct stream *strm = pc->strm; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 182 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 183 | if ((strm->be->options & (PR_O_REDISP|PR_O_PERSIST)) == PR_O_REDISP && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 184 | !(strm->flags & SF_FORCE_PRST)) { |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 185 | /* The REDISP option was specified. We will ignore |
| 186 | * cookie and force to balance or use the dispatcher. |
| 187 | */ |
| 188 | |
| 189 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 190 | strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 191 | |
| 192 | pendconn_free(pc); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 193 | task_wakeup(strm->task, TASK_WOKEN_RES); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 194 | xferred++; |
| 195 | } |
| 196 | } |
| 197 | return xferred; |
| 198 | } |
| 199 | |
| 200 | /* Check for pending connections at the backend, and assign some of them to |
| 201 | * the server coming up. The server's weight is checked before being assigned |
| 202 | * connections it may not be able to handle. The total number of transferred |
| 203 | * connections is returned. |
| 204 | */ |
| 205 | int pendconn_grab_from_px(struct server *s) |
| 206 | { |
| 207 | int xferred; |
| 208 | |
Willy Tarreau | 9943d31 | 2014-05-22 16:20:59 +0200 | [diff] [blame] | 209 | if (!srv_is_usable(s)) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 210 | return 0; |
| 211 | |
| 212 | for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 213 | struct stream *strm; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 214 | struct pendconn *p; |
| 215 | |
| 216 | p = pendconn_from_px(s->proxy); |
| 217 | if (!p) |
| 218 | break; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 219 | p->strm->target = &s->obj_type; |
| 220 | strm = p->strm; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 221 | pendconn_free(p); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 222 | task_wakeup(strm->task, TASK_WOKEN_RES); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 223 | } |
| 224 | return xferred; |
| 225 | } |
| 226 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 227 | /* |
| 228 | * Detaches pending connection <p>, decreases the pending count, and frees |
| 229 | * the pending connection. The connection might have been queued to a specific |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 230 | * server as well as to the proxy. The stream also gets marked unqueued. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 231 | */ |
| 232 | void pendconn_free(struct pendconn *p) |
| 233 | { |
| 234 | LIST_DEL(&p->list); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 235 | p->strm->pend_pos = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 236 | if (p->srv) |
| 237 | p->srv->nbpend--; |
| 238 | else |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 239 | p->strm->be->nbpend--; |
| 240 | p->strm->be->totpend--; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 241 | pool_free2(pool2_pendconn, p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
| 245 | /* |
| 246 | * Local variables: |
| 247 | * c-indent-level: 8 |
| 248 | * c-basic-offset: 8 |
| 249 | * End: |
| 250 | */ |