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 | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 13 | /* Short explanation on the locking, which is far from being trivial : a |
| 14 | * pendconn is a list element which necessarily is associated with an existing |
| 15 | * stream. It has pendconn->strm always valid. A pendconn may only be in one of |
| 16 | * these three states : |
| 17 | * - unlinked : in this case it is an empty list head ; |
| 18 | * - linked into the server's queue ; |
| 19 | * - linked into the proxy's queue. |
| 20 | * |
| 21 | * A stream does not necessarily have such a pendconn. Thus the pendconn is |
| 22 | * designated by the stream->pend_pos pointer. This results in some properties : |
| 23 | * - pendconn->strm->pend_pos is never NULL for any valid pendconn |
| 24 | * - if LIST_ISEMPTY(pendconn->list) is true, the element is unlinked, |
| 25 | * otherwise it necessarily belongs to one of the other lists ; this may |
| 26 | * not be atomically checked under threads though ; |
| 27 | * - pendconn->px is never NULL if pendconn->list is not empty |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 28 | * - pendconn->srv is never NULL if pendconn->list is in the server's queue, |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 29 | * and is always NULL if pendconn->list is in the backend's queue or empty. |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 30 | * - pendconn->target is NULL while the element is queued, and points to the |
| 31 | * assigned server when the pendconn is picked. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 32 | * |
| 33 | * Threads complicate the design a little bit but rules remain simple : |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 34 | * - the server's queue lock must be held at least when manipulating the |
| 35 | * server's queue, which is when adding a pendconn to the queue and when |
| 36 | * removing a pendconn from the queue. It protects the queue's integrity. |
| 37 | * |
| 38 | * - the proxy's queue lock must be held at least when manipulating the |
| 39 | * proxy's queue, which is when adding a pendconn to the queue and when |
| 40 | * removing a pendconn from the queue. It protects the queue's integrity. |
| 41 | * |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 42 | * - both locks are compatible and may be held at the same time. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 43 | * |
| 44 | * - a pendconn_add() is only performed by the stream which will own the |
| 45 | * pendconn ; the pendconn is allocated at this moment and returned ; it is |
| 46 | * added to either the server or the proxy's queue while holding this |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 47 | * queue's lock. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 48 | * |
| 49 | * - the pendconn is then met by a thread walking over the proxy or server's |
| 50 | * queue with the respective lock held. This lock is exclusive and the |
| 51 | * pendconn can only appear in one queue so by definition a single thread |
| 52 | * may find this pendconn at a time. |
| 53 | * |
| 54 | * - the pendconn is unlinked either by its own stream upon success/abort/ |
| 55 | * free, or by another one offering it its server slot. This is achieved by |
| 56 | * pendconn_process_next_strm() under either the server or proxy's lock, |
| 57 | * pendconn_redistribute() under the server's lock, pendconn_grab_from_px() |
| 58 | * under the proxy's lock, or pendconn_unlink() under either the proxy's or |
| 59 | * the server's lock depending on the queue the pendconn is attached to. |
| 60 | * |
| 61 | * - no single operation except the pendconn initialisation prior to the |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 62 | * insertion are performed without eithre a queue lock held or the element |
| 63 | * being unlinked and visible exclusively to its stream. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 64 | * |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 65 | * - pendconn_grab_from_px() and pendconn_process_next_strm() assign ->target |
| 66 | * so that the stream knows what server to work with (via |
| 67 | * pendconn_dequeue() which sets it on strm->target). |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 68 | * |
| 69 | * - a pendconn doesn't switch between queues, it stays where it is. |
Willy Tarreau | 6bdd05c | 2018-07-25 15:21:00 +0200 | [diff] [blame] | 70 | */ |
| 71 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 72 | #include <common/config.h> |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 73 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 74 | #include <common/time.h> |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 75 | #include <common/hathreads.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | #include <proto/queue.h> |
| 78 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 79 | #include <proto/stream.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 80 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | #include <proto/task.h> |
| 82 | |
| 83 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 84 | struct pool_head *pool_head_pendconn; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 85 | |
| 86 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 87 | int init_pendconn() |
| 88 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 89 | pool_head_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED); |
| 90 | return pool_head_pendconn != NULL; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 91 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | |
| 93 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 94 | * 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] | 95 | * expected that 0 < s->minconn <= s->maxconn when this is called. If the |
| 96 | * server is currently warming up, the slowstart is also applied to the |
| 97 | * resulting value, which can be lower than minconn in this case, but never |
| 98 | * less than 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 100 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 102 | unsigned int max; |
| 103 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 104 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 105 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 106 | max = s->maxconn; |
| 107 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 108 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 109 | max = s->maxconn; |
| 110 | else max = MAX(s->minconn, |
| 111 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 112 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 113 | if ((s->cur_state == SRV_ST_STARTING) && |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 114 | now.tv_sec < s->last_change + s->slowstart && |
| 115 | now.tv_sec >= s->last_change) { |
| 116 | unsigned int ratio; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 117 | ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; |
| 118 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 119 | } |
| 120 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 123 | /* Remove the pendconn from the server/proxy queue. At this stage, the |
| 124 | * connection is not really dequeued. It will be done during the |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 125 | * process_stream. It also decreases the pending count. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 126 | * |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 127 | * The caller must own the lock on the queue containing the pendconn. The |
| 128 | * pendconn must still be queued. |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 129 | */ |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 130 | static void __pendconn_unlink(struct pendconn *p) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 131 | { |
| 132 | if (p->srv) |
| 133 | p->srv->nbpend--; |
| 134 | else |
| 135 | p->px->nbpend--; |
| 136 | HA_ATOMIC_SUB(&p->px->totpend, 1); |
| 137 | LIST_DEL(&p->list); |
| 138 | LIST_INIT(&p->list); |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 141 | /* Locks the queue the pendconn element belongs to. This relies on both p->px |
| 142 | * and p->srv to be properly initialized (which is always the case once the |
| 143 | * element has been added). |
| 144 | */ |
| 145 | static inline void pendconn_queue_lock(struct pendconn *p) |
| 146 | { |
| 147 | if (p->srv) |
| 148 | HA_SPIN_LOCK(SERVER_LOCK, &p->srv->lock); |
| 149 | else |
| 150 | HA_SPIN_LOCK(PROXY_LOCK, &p->px->lock); |
| 151 | } |
| 152 | |
| 153 | /* Unlocks the queue the pendconn element belongs to. This relies on both p->px |
| 154 | * and p->srv to be properly initialized (which is always the case once the |
| 155 | * element has been added). |
| 156 | */ |
| 157 | static inline void pendconn_queue_unlock(struct pendconn *p) |
| 158 | { |
| 159 | if (p->srv) |
| 160 | HA_SPIN_UNLOCK(SERVER_LOCK, &p->srv->lock); |
| 161 | else |
| 162 | HA_SPIN_UNLOCK(PROXY_LOCK, &p->px->lock); |
| 163 | } |
| 164 | |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 165 | /* Removes the pendconn from the server/proxy queue. At this stage, the |
| 166 | * connection is not really dequeued. It will be done during process_stream(). |
| 167 | * This function takes all the required locks for the operation. The caller is |
| 168 | * responsible for ensuring that <p> is valid and still in the queue. Use |
| 169 | * pendconn_cond_unlink() if unsure. When the locks are already held, please |
| 170 | * use __pendconn_unlink() instead. |
| 171 | */ |
| 172 | void pendconn_unlink(struct pendconn *p) |
| 173 | { |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 174 | pendconn_queue_lock(p); |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 175 | |
| 176 | __pendconn_unlink(p); |
| 177 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 178 | pendconn_queue_unlock(p); |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 181 | /* Process the next pending connection from either a server or a proxy, and |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 182 | * returns a strictly positive value on success (see below). If no pending |
| 183 | * connection is found, 0 is returned. Note that neither <srv> nor <px> may be |
| 184 | * NULL. Priority is given to the oldest request in the queue if both <srv> and |
| 185 | * <px> have pending requests. This ensures that no request will be left |
| 186 | * unserved. The <px> queue is not considered if the server (or a tracked |
| 187 | * server) is not RUNNING, is disabled, or has a null weight (server going |
| 188 | * down). The <srv> queue is still considered in this case, because if some |
| 189 | * connections remain there, it means that some requests have been forced there |
| 190 | * after it was seen down (eg: due to option persist). The stream is |
| 191 | * immediately marked as "assigned", and both its <srv> and <srv_conn> are set |
| 192 | * to <srv>. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 193 | * |
| 194 | * This function must only be called if the server queue _AND_ the proxy queue |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 195 | * are locked. Today it is only called by process_srv_queue. When a pending |
| 196 | * connection is dequeued, this function returns 1 if the pending connection can |
| 197 | * be handled by the current thread, else it returns 2. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 198 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 199 | static int pendconn_process_next_strm(struct server *srv, struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 200 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 201 | struct pendconn *p = NULL; |
| 202 | struct server *rsrv; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 203 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 204 | rsrv = srv->track; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 205 | if (!rsrv) |
| 206 | rsrv = srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 207 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 208 | p = NULL; |
| 209 | if (srv->nbpend) |
| 210 | p = LIST_ELEM(srv->pendconns.n, struct pendconn *, list); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 211 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 212 | if (srv_currently_usable(rsrv) && px->nbpend) { |
| 213 | struct pendconn *pp; |
| 214 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 215 | pp = LIST_ELEM(px->pendconns.n, struct pendconn *, list); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 216 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 217 | /* If the server pendconn is older than the proxy one, |
| 218 | * we process the server one. |
| 219 | */ |
| 220 | if (p && !tv_islt(&pp->strm->logs.tv_request, &p->strm->logs.tv_request)) |
| 221 | goto pendconn_found; |
| 222 | |
| 223 | /* Let's switch from the server pendconn to the proxy pendconn */ |
| 224 | p = pp; |
| 225 | goto pendconn_found; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (!p) |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 229 | return 0; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 230 | |
| 231 | pendconn_found: |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 232 | __pendconn_unlink(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 233 | p->strm_flags |= SF_ASSIGNED; |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 234 | p->target = srv; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 235 | |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 236 | HA_ATOMIC_ADD(&srv->served, 1); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 237 | HA_ATOMIC_ADD(&srv->proxy->served, 1); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 238 | if (px->lbprm.server_take_conn) |
| 239 | px->lbprm.server_take_conn(srv); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 240 | __stream_add_srv_conn(p->strm, srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 241 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 242 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 243 | |
Olivier Houchard | ecfe673 | 2018-07-26 18:47:27 +0200 | [diff] [blame] | 244 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 247 | /* Manages a server's connection queue. This function will try to dequeue as |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 248 | * many pending streams as possible, and wake them up. |
| 249 | */ |
| 250 | void process_srv_queue(struct server *s) |
| 251 | { |
| 252 | struct proxy *p = s->proxy; |
Olivier Houchard | ecfe673 | 2018-07-26 18:47:27 +0200 | [diff] [blame] | 253 | int maxconn; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 254 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 255 | HA_SPIN_LOCK(PROXY_LOCK, &p->lock); |
| 256 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 257 | maxconn = srv_dynamic_maxconn(s); |
| 258 | while (s->served < maxconn) { |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 259 | int ret = pendconn_process_next_strm(s, p); |
| 260 | if (!ret) |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 261 | break; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 262 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 263 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 264 | HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 267 | /* Adds the stream <strm> to the pending connection list of server <strm>->srv |
| 268 | * 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] | 269 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 270 | * pendconn itself. If the stream was already marked as served, its flag is |
| 271 | * cleared. It is illegal to call this function with a non-NULL strm->srv_conn. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 272 | * |
| 273 | * This function must be called by the stream itself, so in the context of |
| 274 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 275 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 276 | struct pendconn *pendconn_add(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 277 | { |
| 278 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 279 | struct proxy *px; |
| 280 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 281 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 282 | p = pool_alloc(pool_head_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 283 | if (!p) |
| 284 | return NULL; |
| 285 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 286 | if (strm->flags & SF_ASSIGNED) |
| 287 | srv = objt_server(strm->target); |
| 288 | else |
| 289 | srv = NULL; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 290 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 291 | px = strm->be; |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 292 | p->target = NULL; |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 293 | p->srv = srv; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 294 | p->px = px; |
| 295 | p->strm = strm; |
| 296 | p->strm_flags = strm->flags; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 297 | |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 298 | pendconn_queue_lock(p); |
| 299 | |
| 300 | if (srv) { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 301 | srv->nbpend++; |
| 302 | strm->logs.srv_queue_size += srv->nbpend; |
| 303 | if (srv->nbpend > srv->counters.nbpend_max) |
| 304 | srv->counters.nbpend_max = srv->nbpend; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 305 | LIST_ADDQ(&srv->pendconns, &p->list); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 306 | } |
| 307 | else { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 308 | px->nbpend++; |
| 309 | strm->logs.prx_queue_size += px->nbpend; |
| 310 | if (px->nbpend > px->be_counters.nbpend_max) |
| 311 | px->be_counters.nbpend_max = px->nbpend; |
| 312 | LIST_ADDQ(&px->pendconns, &p->list); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 313 | } |
Willy Tarreau | 7c6f8a2 | 2018-07-26 08:03:14 +0200 | [diff] [blame] | 314 | strm->pend_pos = p; |
| 315 | |
| 316 | pendconn_queue_unlock(p); |
| 317 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 318 | HA_ATOMIC_ADD(&px->totpend, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 319 | return p; |
| 320 | } |
| 321 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 322 | /* Redistribute pending connections when a server goes down. The number of |
| 323 | * connections redistributed is returned. |
| 324 | */ |
| 325 | int pendconn_redistribute(struct server *s) |
| 326 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 327 | struct pendconn *p, *pback; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 328 | int xferred = 0; |
| 329 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 330 | /* The REDISP option was specified. We will ignore cookie and force to |
| 331 | * balance or use the dispatcher. */ |
| 332 | if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP) |
| 333 | return 0; |
| 334 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 335 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 336 | list_for_each_entry_safe(p, pback, &s->pendconns, list) { |
| 337 | if (p->strm_flags & SF_FORCE_PRST) |
| 338 | continue; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 339 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 340 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 341 | __pendconn_unlink(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 342 | p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 343 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 344 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 345 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 346 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 347 | return xferred; |
| 348 | } |
| 349 | |
| 350 | /* Check for pending connections at the backend, and assign some of them to |
| 351 | * the server coming up. The server's weight is checked before being assigned |
| 352 | * connections it may not be able to handle. The total number of transferred |
| 353 | * connections is returned. |
| 354 | */ |
| 355 | int pendconn_grab_from_px(struct server *s) |
| 356 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 357 | struct pendconn *p, *pback; |
| 358 | int maxconn, xferred = 0; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 359 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 360 | if (!srv_currently_usable(s)) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 361 | return 0; |
| 362 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 363 | HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 364 | maxconn = srv_dynamic_maxconn(s); |
| 365 | list_for_each_entry_safe(p, pback, &s->proxy->pendconns, list) { |
| 366 | if (s->maxconn && s->served + xferred >= maxconn) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 367 | break; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 368 | |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 369 | __pendconn_unlink(p); |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 370 | p->target = s; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 371 | |
| 372 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 373 | xferred++; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 374 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 375 | HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 376 | return xferred; |
| 377 | } |
| 378 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 379 | /* Try to dequeue pending connection attached to the stream <strm>. It must |
| 380 | * always exists here. If the pendconn is still linked to the server or the |
| 381 | * proxy queue, nothing is done and the function returns 1. Otherwise, |
| 382 | * <strm>->flags and <strm>->target are updated, the pendconn is released and 0 |
| 383 | * is returned. |
| 384 | * |
| 385 | * This function must be called by the stream itself, so in the context of |
| 386 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 387 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 388 | int pendconn_dequeue(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 390 | struct pendconn *p; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 391 | int is_unlinked; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 392 | |
| 393 | if (unlikely(!strm->pend_pos)) { |
| 394 | /* unexpected case because it is called by the stream itself and |
| 395 | * only the stream can release a pendconn. So it is only |
| 396 | * possible if a pendconn is released by someone else or if the |
| 397 | * stream is supposed to be queued but without its associated |
| 398 | * pendconn. In both cases it is a bug! */ |
| 399 | abort(); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 400 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 401 | p = strm->pend_pos; |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 402 | |
| 403 | /* note below : we need to grab the queue's lock to check for emptiness |
| 404 | * because we don't want a partial _grab_from_px() or _redistribute() |
| 405 | * to be called in parallel and show an empty list without having the |
| 406 | * time to finish. With this we know that if we see the element |
| 407 | * unlinked, these functions were completely done. |
| 408 | */ |
| 409 | pendconn_queue_lock(p); |
| 410 | is_unlinked = LIST_ISEMPTY(&p->list); |
| 411 | pendconn_queue_unlock(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 412 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 413 | if (!is_unlinked) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 414 | return 1; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 415 | |
Willy Tarreau | 3201e4e | 2018-07-26 08:23:24 +0200 | [diff] [blame] | 416 | /* the pendconn is not queued anymore and will not be so we're safe |
| 417 | * to proceed. |
| 418 | */ |
Willy Tarreau | 88930dd | 2018-07-26 07:38:54 +0200 | [diff] [blame] | 419 | if (p->target) |
| 420 | strm->target = &p->target->obj_type; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 421 | |
| 422 | strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 423 | strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 424 | strm->pend_pos = NULL; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 425 | pool_free(pool_head_pendconn, p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 426 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 429 | /* |
| 430 | * Local variables: |
| 431 | * c-indent-level: 8 |
| 432 | * c-basic-offset: 8 |
| 433 | * End: |
| 434 | */ |