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