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> |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 16 | #include <common/hathreads.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 17 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | #include <proto/queue.h> |
| 19 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 20 | #include <proto/stream.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 21 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #include <proto/task.h> |
| 23 | |
| 24 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 25 | struct pool_head *pool_head_pendconn; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 26 | |
| 27 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 28 | int init_pendconn() |
| 29 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 30 | pool_head_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED); |
| 31 | return pool_head_pendconn != NULL; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 32 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | |
| 34 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 35 | * 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] | 36 | * expected that 0 < s->minconn <= s->maxconn when this is called. If the |
| 37 | * server is currently warming up, the slowstart is also applied to the |
| 38 | * resulting value, which can be lower than minconn in this case, but never |
| 39 | * less than 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 41 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 43 | unsigned int max; |
| 44 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 45 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 46 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 47 | max = s->maxconn; |
| 48 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 49 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 50 | max = s->maxconn; |
| 51 | else max = MAX(s->minconn, |
| 52 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 53 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 54 | if ((s->cur_state == SRV_ST_STARTING) && |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 55 | now.tv_sec < s->last_change + s->slowstart && |
| 56 | now.tv_sec >= s->last_change) { |
| 57 | unsigned int ratio; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 58 | ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; |
| 59 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 60 | } |
| 61 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 64 | /* Remove the pendconn from the server/proxy queue. At this stage, the |
| 65 | * connection is not really dequeued. It will be done during the |
| 66 | * process_stream. This function must be called by function owning the locks on |
| 67 | * the pendconn _AND_ the server/proxy. It also decreases the pending count. |
| 68 | * |
| 69 | * The caller must own the lock on the pendconn _AND_ the queue containing the |
| 70 | * pendconn. The pendconn must still be queued. |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 71 | */ |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 72 | static void __pendconn_unlink(struct pendconn *p) |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 73 | { |
| 74 | if (p->srv) |
| 75 | p->srv->nbpend--; |
| 76 | else |
| 77 | p->px->nbpend--; |
| 78 | HA_ATOMIC_SUB(&p->px->totpend, 1); |
| 79 | LIST_DEL(&p->list); |
| 80 | LIST_INIT(&p->list); |
Christopher Faulet | f3a55db | 2017-06-09 14:26:38 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 83 | /* Removes the pendconn from the server/proxy queue. At this stage, the |
| 84 | * connection is not really dequeued. It will be done during process_stream(). |
| 85 | * This function takes all the required locks for the operation. The caller is |
| 86 | * responsible for ensuring that <p> is valid and still in the queue. Use |
| 87 | * pendconn_cond_unlink() if unsure. When the locks are already held, please |
| 88 | * use __pendconn_unlink() instead. |
| 89 | */ |
| 90 | void pendconn_unlink(struct pendconn *p) |
| 91 | { |
| 92 | struct server __maybe_unused *sv; |
| 93 | struct proxy __maybe_unused *px; |
| 94 | |
| 95 | HA_SPIN_LOCK(PENDCONN_LOCK, &p->lock); |
| 96 | |
| 97 | px = p->px; |
| 98 | sv = p->srv; |
| 99 | |
| 100 | if (sv) |
| 101 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 102 | else |
| 103 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 104 | |
| 105 | __pendconn_unlink(p); |
| 106 | |
| 107 | if (sv) |
| 108 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 109 | else |
| 110 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
| 111 | |
| 112 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
| 113 | } |
| 114 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 115 | /* 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] | 116 | * returns a strictly positive value on success (see below). If no pending |
| 117 | * connection is found, 0 is returned. Note that neither <srv> nor <px> may be |
| 118 | * NULL. Priority is given to the oldest request in the queue if both <srv> and |
| 119 | * <px> have pending requests. This ensures that no request will be left |
| 120 | * unserved. The <px> queue is not considered if the server (or a tracked |
| 121 | * server) is not RUNNING, is disabled, or has a null weight (server going |
| 122 | * down). The <srv> queue is still considered in this case, because if some |
| 123 | * connections remain there, it means that some requests have been forced there |
| 124 | * after it was seen down (eg: due to option persist). The stream is |
| 125 | * immediately marked as "assigned", and both its <srv> and <srv_conn> are set |
| 126 | * to <srv>. |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 127 | * |
| 128 | * 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] | 129 | * are locked. Today it is only called by process_srv_queue. When a pending |
| 130 | * connection is dequeued, this function returns 1 if the pending connection can |
| 131 | * be handled by the current thread, else it returns 2. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 132 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 133 | static int pendconn_process_next_strm(struct server *srv, struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 134 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 135 | struct pendconn *p = NULL; |
| 136 | struct server *rsrv; |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 137 | int remote; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 138 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 139 | rsrv = srv->track; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 140 | if (!rsrv) |
| 141 | rsrv = srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 142 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 143 | if (srv->nbpend) { |
| 144 | list_for_each_entry(p, &srv->pendconns, list) { |
| 145 | if (!HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock)) |
| 146 | goto ps_found; |
| 147 | } |
| 148 | p = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 149 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 150 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 151 | ps_found: |
| 152 | if (srv_currently_usable(rsrv) && px->nbpend) { |
| 153 | struct pendconn *pp; |
| 154 | |
| 155 | list_for_each_entry(pp, &px->pendconns, list) { |
| 156 | /* If the server pendconn is older than the proxy one, |
| 157 | * we process the server one. */ |
| 158 | if (p && !tv_islt(&pp->strm->logs.tv_request, &p->strm->logs.tv_request)) |
| 159 | goto pendconn_found; |
| 160 | |
| 161 | if (!HA_SPIN_TRYLOCK(PENDCONN_LOCK, &pp->lock)) { |
| 162 | /* Let's switch from the server pendconn to the |
| 163 | * proxy pendconn. Don't forget to unlock the |
| 164 | * server pendconn, if any. */ |
| 165 | if (p) |
| 166 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
| 167 | p = pp; |
| 168 | goto pendconn_found; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (!p) |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 174 | return 0; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 175 | |
| 176 | pendconn_found: |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 177 | __pendconn_unlink(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 178 | p->strm_flags |= SF_ASSIGNED; |
| 179 | p->srv = srv; |
| 180 | |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 181 | HA_ATOMIC_ADD(&srv->served, 1); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 182 | HA_ATOMIC_ADD(&srv->proxy->served, 1); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 183 | if (px->lbprm.server_take_conn) |
| 184 | px->lbprm.server_take_conn(srv); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 185 | __stream_add_srv_conn(p->strm, srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 186 | |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 187 | remote = !(p->strm->task->thread_mask & tid_bit); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 188 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
| 189 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 190 | |
| 191 | /* Returns 1 if the current thread can process the stream, otherwise returns 2. */ |
| 192 | return remote ? 2 : 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 195 | /* 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] | 196 | * many pending streams as possible, and wake them up. |
| 197 | */ |
| 198 | void process_srv_queue(struct server *s) |
| 199 | { |
| 200 | struct proxy *p = s->proxy; |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 201 | int maxconn, remote = 0; |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 202 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 203 | HA_SPIN_LOCK(PROXY_LOCK, &p->lock); |
| 204 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 205 | maxconn = srv_dynamic_maxconn(s); |
| 206 | while (s->served < maxconn) { |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 207 | int ret = pendconn_process_next_strm(s, p); |
| 208 | if (!ret) |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 209 | break; |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 210 | remote |= (ret == 2); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 211 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 212 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 213 | HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock); |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 214 | |
| 215 | if (remote) |
Ilya Shipitsin | 7741c85 | 2018-03-24 17:17:32 +0500 | [diff] [blame] | 216 | THREAD_WANT_SYNC(); |
Christopher Faulet | 87566c9 | 2017-06-06 10:34:51 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 219 | /* Adds the stream <strm> to the pending connection list of server <strm>->srv |
| 220 | * 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] | 221 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 222 | * pendconn itself. If the stream was already marked as served, its flag is |
| 223 | * 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] | 224 | * |
| 225 | * This function must be called by the stream itself, so in the context of |
| 226 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 227 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 228 | struct pendconn *pendconn_add(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 229 | { |
| 230 | struct pendconn *p; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 231 | struct proxy *px; |
| 232 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 233 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 234 | p = pool_alloc(pool_head_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 235 | if (!p) |
| 236 | return NULL; |
| 237 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 238 | srv = objt_server(strm->target); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 239 | px = strm->be; |
| 240 | |
| 241 | p->srv = NULL; |
| 242 | p->px = px; |
| 243 | p->strm = strm; |
| 244 | p->strm_flags = strm->flags; |
| 245 | HA_SPIN_INIT(&p->lock); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 246 | |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 247 | if ((strm->flags & SF_ASSIGNED) && srv) { |
| 248 | p->srv = srv; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 249 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 250 | srv->nbpend++; |
| 251 | strm->logs.srv_queue_size += srv->nbpend; |
| 252 | if (srv->nbpend > srv->counters.nbpend_max) |
| 253 | srv->counters.nbpend_max = srv->nbpend; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 254 | LIST_ADDQ(&srv->pendconns, &p->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 255 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 256 | } |
| 257 | else { |
| 258 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 259 | px->nbpend++; |
| 260 | strm->logs.prx_queue_size += px->nbpend; |
| 261 | if (px->nbpend > px->be_counters.nbpend_max) |
| 262 | px->be_counters.nbpend_max = px->nbpend; |
| 263 | LIST_ADDQ(&px->pendconns, &p->list); |
| 264 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 265 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 266 | HA_ATOMIC_ADD(&px->totpend, 1); |
| 267 | strm->pend_pos = p; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 268 | return p; |
| 269 | } |
| 270 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 271 | /* Redistribute pending connections when a server goes down. The number of |
| 272 | * connections redistributed is returned. |
| 273 | */ |
| 274 | int pendconn_redistribute(struct server *s) |
| 275 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 276 | struct pendconn *p, *pback; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 277 | int xferred = 0; |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 278 | int remote = 0; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 279 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 280 | /* The REDISP option was specified. We will ignore cookie and force to |
| 281 | * balance or use the dispatcher. */ |
| 282 | if ((s->proxy->options & (PR_O_REDISP|PR_O_PERSIST)) != PR_O_REDISP) |
| 283 | return 0; |
| 284 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 285 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 286 | list_for_each_entry_safe(p, pback, &s->pendconns, list) { |
| 287 | if (p->strm_flags & SF_FORCE_PRST) |
| 288 | continue; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 289 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 290 | if (HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock)) |
| 291 | continue; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 292 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 293 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 294 | __pendconn_unlink(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 295 | p->strm_flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 296 | |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 297 | remote |= !(p->strm->task->thread_mask & tid_bit); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 298 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
| 299 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 300 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 301 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 302 | |
| 303 | if (remote) |
Ilya Shipitsin | 7741c85 | 2018-03-24 17:17:32 +0500 | [diff] [blame] | 304 | THREAD_WANT_SYNC(); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 305 | return xferred; |
| 306 | } |
| 307 | |
| 308 | /* Check for pending connections at the backend, and assign some of them to |
| 309 | * the server coming up. The server's weight is checked before being assigned |
| 310 | * connections it may not be able to handle. The total number of transferred |
| 311 | * connections is returned. |
| 312 | */ |
| 313 | int pendconn_grab_from_px(struct server *s) |
| 314 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 315 | struct pendconn *p, *pback; |
| 316 | int maxconn, xferred = 0; |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 317 | int remote = 0; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 318 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 319 | if (!srv_currently_usable(s)) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 320 | return 0; |
| 321 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 322 | HA_SPIN_LOCK(PROXY_LOCK, &s->proxy->lock); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 323 | maxconn = srv_dynamic_maxconn(s); |
| 324 | list_for_each_entry_safe(p, pback, &s->proxy->pendconns, list) { |
| 325 | if (s->maxconn && s->served + xferred >= maxconn) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 326 | break; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 327 | |
| 328 | if (HA_SPIN_TRYLOCK(PENDCONN_LOCK, &p->lock)) |
| 329 | continue; |
| 330 | |
Willy Tarreau | 9624fae | 2018-07-25 08:04:20 +0200 | [diff] [blame] | 331 | __pendconn_unlink(p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 332 | p->srv = s; |
| 333 | |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 334 | remote |= !(p->strm->task->thread_mask & tid_bit); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 335 | task_wakeup(p->strm->task, TASK_WOKEN_RES); |
| 336 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
| 337 | xferred++; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 338 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 339 | HA_SPIN_UNLOCK(PROXY_LOCK, &s->proxy->lock); |
Christopher Faulet | fd83f0b | 2018-03-19 15:22:09 +0100 | [diff] [blame] | 340 | |
| 341 | if (remote) |
Ilya Shipitsin | 7741c85 | 2018-03-24 17:17:32 +0500 | [diff] [blame] | 342 | THREAD_WANT_SYNC(); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 343 | return xferred; |
| 344 | } |
| 345 | |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 346 | /* Try to dequeue pending connection attached to the stream <strm>. It must |
| 347 | * always exists here. If the pendconn is still linked to the server or the |
| 348 | * proxy queue, nothing is done and the function returns 1. Otherwise, |
| 349 | * <strm>->flags and <strm>->target are updated, the pendconn is released and 0 |
| 350 | * is returned. |
| 351 | * |
| 352 | * This function must be called by the stream itself, so in the context of |
| 353 | * process_stream. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 354 | */ |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 355 | int pendconn_dequeue(struct stream *strm) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 356 | { |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 357 | struct pendconn *p; |
| 358 | |
| 359 | if (unlikely(!strm->pend_pos)) { |
| 360 | /* unexpected case because it is called by the stream itself and |
| 361 | * only the stream can release a pendconn. So it is only |
| 362 | * possible if a pendconn is released by someone else or if the |
| 363 | * stream is supposed to be queued but without its associated |
| 364 | * pendconn. In both cases it is a bug! */ |
| 365 | abort(); |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 366 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 367 | p = strm->pend_pos; |
| 368 | HA_SPIN_LOCK(PENDCONN_LOCK, &p->lock); |
| 369 | |
| 370 | /* the pendconn is still linked to the server/proxy queue, so unlock it |
| 371 | * and go away. */ |
| 372 | if (!LIST_ISEMPTY(&p->list)) { |
| 373 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
| 374 | return 1; |
Christopher Faulet | 8ba5914 | 2017-06-27 15:43:53 +0200 | [diff] [blame] | 375 | } |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 376 | |
| 377 | /* the pendconn must be dequeued now */ |
| 378 | if (p->srv) |
| 379 | strm->target = &p->srv->obj_type; |
| 380 | |
| 381 | strm->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 382 | strm->flags |= p->strm_flags & (SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET); |
| 383 | strm->pend_pos = NULL; |
| 384 | HA_SPIN_UNLOCK(PENDCONN_LOCK, &p->lock); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 385 | pool_free(pool_head_pendconn, p); |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 386 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | /* |
| 390 | * Local variables: |
| 391 | * c-indent-level: 8 |
| 392 | * c-basic-offset: 8 |
| 393 | * End: |
| 394 | */ |