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> |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 19 | #include <proto/session.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 | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 53 | if ((s->state & SRV_WARMINGUP) && |
| 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 |
| 66 | * many pending sessions 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) { |
| 79 | struct session *sess = pendconn_get_next_sess(s, p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | if (sess == NULL) |
| 81 | break; |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 82 | task_wakeup(sess->task, TASK_WOKEN_RES); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Detaches the next pending connection from either a server or a proxy, and |
| 87 | * returns its associated session. If no pending connection is found, NULL is |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 88 | * returned. Note that neither <srv> nor <px> may be NULL. |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 89 | * Priority is given to the oldest request in the queue if both <srv> and <px> |
| 90 | * have pending requests. This ensures that no request will be left unserved. |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 91 | * The <px> queue is not considered if the server (or a tracked server) is not |
| 92 | * 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] | 93 | * queue is still considered in this case, because if some connections remain |
| 94 | * there, it means that some requests have been forced there after it was seen |
| 95 | * down (eg: due to option persist). |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 96 | * The session is immediately marked as "assigned", and both its <srv> and |
| 97 | * <srv_conn> are set to <srv>, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | */ |
| 99 | struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px) |
| 100 | { |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 101 | struct pendconn *ps, *pp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | struct session *sess; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 103 | struct server *rsrv; |
| 104 | |
Willy Tarreau | 4426770 | 2011-10-28 15:35:33 +0200 | [diff] [blame] | 105 | rsrv = srv->track; |
Willy Tarreau | d132f74 | 2010-08-06 10:08:23 +0200 | [diff] [blame] | 106 | if (!rsrv) |
| 107 | rsrv = srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 109 | ps = pendconn_from_srv(srv); |
| 110 | pp = pendconn_from_px(px); |
| 111 | /* we want to get the definitive pendconn in <ps> */ |
Willy Tarreau | f8e8b76 | 2012-01-20 15:57:05 +0100 | [diff] [blame] | 112 | if (!pp || !srv_is_usable(rsrv->state, rsrv->eweight)) { |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 113 | if (!ps) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | return NULL; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 115 | } else { |
| 116 | /* pendconn exists in the proxy queue */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 117 | if (!ps || tv_islt(&pp->sess->logs.tv_request, &ps->sess->logs.tv_request)) |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 118 | ps = pp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | } |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 120 | sess = ps->sess; |
| 121 | pendconn_free(ps); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 122 | |
| 123 | /* we want to note that the session has now been assigned a server */ |
| 124 | sess->flags |= SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 125 | set_target_server(&sess->target, srv); |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 126 | session_add_srv_conn(sess, srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 127 | srv->served++; |
| 128 | if (px->lbprm.server_take_conn) |
| 129 | px->lbprm.server_take_conn(srv); |
| 130 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | return sess; |
| 132 | } |
| 133 | |
| 134 | /* Adds the session <sess> to the pending connection list of server <sess>->srv |
| 135 | * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers |
| 136 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 137 | * pendconn itself. If the session was already marked as served, its flag is |
| 138 | * cleared. It is illegal to call this function with a non-NULL sess->srv_conn. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 139 | */ |
| 140 | struct pendconn *pendconn_add(struct session *sess) |
| 141 | { |
| 142 | struct pendconn *p; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 143 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 144 | |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 145 | p = pool_alloc2(pool2_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 146 | if (!p) |
| 147 | return NULL; |
| 148 | |
| 149 | sess->pend_pos = p; |
| 150 | p->sess = sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 151 | p->srv = srv = target_srv(&sess->target); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 153 | if (sess->flags & SN_ASSIGNED && srv) { |
| 154 | LIST_ADDQ(&srv->pendconns, &p->list); |
| 155 | srv->nbpend++; |
| 156 | sess->logs.srv_queue_size += srv->nbpend; |
| 157 | if (srv->nbpend > srv->counters.nbpend_max) |
| 158 | srv->counters.nbpend_max = srv->nbpend; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 159 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 160 | LIST_ADDQ(&sess->be->pendconns, &p->list); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 161 | sess->be->nbpend++; |
Willy Tarreau | 7a63abd | 2008-06-13 21:48:18 +0200 | [diff] [blame] | 162 | sess->logs.prx_queue_size += sess->be->nbpend; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 163 | if (sess->be->nbpend > sess->be->be_counters.nbpend_max) |
| 164 | sess->be->be_counters.nbpend_max = sess->be->nbpend; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 165 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 166 | sess->be->totpend++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | return p; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Detaches pending connection <p>, decreases the pending count, and frees |
| 172 | * the pending connection. The connection might have been queued to a specific |
| 173 | * server as well as to the proxy. The session also gets marked unqueued. |
| 174 | */ |
| 175 | void pendconn_free(struct pendconn *p) |
| 176 | { |
| 177 | LIST_DEL(&p->list); |
| 178 | p->sess->pend_pos = NULL; |
| 179 | if (p->srv) |
| 180 | p->srv->nbpend--; |
| 181 | else |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 182 | p->sess->be->nbpend--; |
| 183 | p->sess->be->totpend--; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 184 | pool_free2(pool2_pendconn, p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | /* |
| 189 | * Local variables: |
| 190 | * c-indent-level: 8 |
| 191 | * c-basic-offset: 8 |
| 192 | * End: |
| 193 | */ |