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> |
| 19 | #include <proto/task.h> |
| 20 | |
| 21 | |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 22 | struct pool_head *pool2_pendconn; |
| 23 | |
| 24 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 25 | int init_pendconn() |
| 26 | { |
| 27 | pool2_pendconn = create_pool("pendconn", sizeof(struct pendconn), MEM_F_SHARED); |
| 28 | return pool2_pendconn != NULL; |
| 29 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
| 31 | /* returns the effective dynamic maxconn for a server, considering the minconn |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 32 | * 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] | 33 | * expected that 0 < s->minconn <= s->maxconn when this is called. If the |
| 34 | * server is currently warming up, the slowstart is also applied to the |
| 35 | * resulting value, which can be lower than minconn in this case, but never |
| 36 | * less than 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 38 | unsigned int srv_dynamic_maxconn(const struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | { |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 40 | unsigned int max; |
| 41 | |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 42 | if (s->proxy->beconn >= s->proxy->fullconn) |
| 43 | /* no fullconn or proxy is full */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 44 | max = s->maxconn; |
| 45 | else if (s->minconn == s->maxconn) |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 46 | /* static limit */ |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 47 | max = s->maxconn; |
| 48 | else max = MAX(s->minconn, |
| 49 | s->proxy->beconn * s->maxconn / s->proxy->fullconn); |
Willy Tarreau | 8603431 | 2006-12-29 00:10:33 +0100 | [diff] [blame] | 50 | |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 51 | if ((s->state & SRV_WARMINGUP) && |
| 52 | now.tv_sec < s->last_change + s->slowstart && |
| 53 | now.tv_sec >= s->last_change) { |
| 54 | unsigned int ratio; |
Willy Tarreau | 28a9e52 | 2008-09-14 17:43:27 +0200 | [diff] [blame] | 55 | ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; |
| 56 | max = MAX(1, max * ratio / 100); |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 57 | } |
| 58 | return max; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| 62 | /* |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 63 | * Manages a server's connection queue. This function will try to dequeue as |
| 64 | * many pending sessions as possible, and wake them up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 66 | void process_srv_queue(struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | struct proxy *p = s->proxy; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 69 | int maxconn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | |
| 71 | /* First, check if we can handle some connections queued at the proxy. We |
| 72 | * will take as many as we can handle. |
| 73 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 75 | maxconn = srv_dynamic_maxconn(s); |
| 76 | while (s->served < maxconn) { |
| 77 | struct session *sess = pendconn_get_next_sess(s, p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | if (sess == NULL) |
| 79 | break; |
Willy Tarreau | fdccded | 2008-08-29 18:19:04 +0200 | [diff] [blame] | 80 | task_wakeup(sess->task, TASK_WOKEN_RES); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* Detaches the next pending connection from either a server or a proxy, and |
| 85 | * returns its associated session. If no pending connection is found, NULL is |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 86 | * returned. Note that neither <srv> nor <px> may be NULL. |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 87 | * Priority is given to the oldest request in the queue if both <srv> and <px> |
| 88 | * have pending requests. This ensures that no request will be left unserved. |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 89 | * The <px> queue is not considered if the server is not RUNNING. The <srv> |
| 90 | * queue is still considered in this case, because if some connections remain |
| 91 | * there, it means that some requests have been forced there after it was seen |
| 92 | * down (eg: due to option persist). |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 93 | * The session is immediately marked as "assigned", and both its <srv> and |
| 94 | * <srv_conn> are set to <srv>, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | */ |
| 96 | struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px) |
| 97 | { |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 98 | struct pendconn *ps, *pp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | struct session *sess; |
| 100 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 101 | ps = pendconn_from_srv(srv); |
| 102 | pp = pendconn_from_px(px); |
| 103 | /* we want to get the definitive pendconn in <ps> */ |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 104 | if (!pp || !(srv->state & SRV_RUNNING)) { |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 105 | if (!ps) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | return NULL; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 107 | } else { |
| 108 | /* pendconn exists in the proxy queue */ |
| 109 | if (!ps || tv_islt(&pp->sess->logs.tv_request, &ps->sess->logs.tv_request)) { |
| 110 | ps = pp; |
| 111 | ps->sess->srv = srv; |
| 112 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | } |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 114 | sess = ps->sess; |
| 115 | pendconn_free(ps); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 116 | |
| 117 | /* we want to note that the session has now been assigned a server */ |
| 118 | sess->flags |= SN_ASSIGNED; |
| 119 | sess->srv = srv; |
| 120 | sess->srv_conn = srv; |
| 121 | srv->served++; |
| 122 | if (px->lbprm.server_take_conn) |
| 123 | px->lbprm.server_take_conn(srv); |
| 124 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 125 | return sess; |
| 126 | } |
| 127 | |
| 128 | /* Adds the session <sess> to the pending connection list of server <sess>->srv |
| 129 | * or to the one of <sess>->proxy if srv is NULL. All counters and back pointers |
| 130 | * are updated accordingly. Returns NULL if no memory is available, otherwise the |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 131 | * pendconn itself. If the session was already marked as served, its flag is |
| 132 | * 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] | 133 | */ |
| 134 | struct pendconn *pendconn_add(struct session *sess) |
| 135 | { |
| 136 | struct pendconn *p; |
| 137 | |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 138 | p = pool_alloc2(pool2_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 139 | if (!p) |
| 140 | return NULL; |
| 141 | |
| 142 | sess->pend_pos = p; |
| 143 | p->sess = sess; |
| 144 | p->srv = sess->srv; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 145 | |
| 146 | if (sess->flags & SN_ASSIGNED && sess->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 147 | LIST_ADDQ(&sess->srv->pendconns, &p->list); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 148 | sess->srv->nbpend++; |
Willy Tarreau | 7a63abd | 2008-06-13 21:48:18 +0200 | [diff] [blame] | 149 | sess->logs.srv_queue_size += sess->srv->nbpend; |
Willy Tarreau | ac68c5d | 2009-10-04 23:12:44 +0200 | [diff] [blame] | 150 | if (sess->srv->nbpend > sess->srv->counters.nbpend_max) |
| 151 | sess->srv->counters.nbpend_max = sess->srv->nbpend; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 152 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 153 | LIST_ADDQ(&sess->be->pendconns, &p->list); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 154 | sess->be->nbpend++; |
Willy Tarreau | 7a63abd | 2008-06-13 21:48:18 +0200 | [diff] [blame] | 155 | sess->logs.prx_queue_size += sess->be->nbpend; |
Willy Tarreau | ac68c5d | 2009-10-04 23:12:44 +0200 | [diff] [blame] | 156 | if (sess->be->nbpend > sess->be->counters.nbpend_max) |
| 157 | sess->be->counters.nbpend_max = sess->be->nbpend; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 158 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 159 | sess->be->totpend++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 160 | return p; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Detaches pending connection <p>, decreases the pending count, and frees |
| 165 | * the pending connection. The connection might have been queued to a specific |
| 166 | * server as well as to the proxy. The session also gets marked unqueued. |
| 167 | */ |
| 168 | void pendconn_free(struct pendconn *p) |
| 169 | { |
| 170 | LIST_DEL(&p->list); |
| 171 | p->sess->pend_pos = NULL; |
| 172 | if (p->srv) |
| 173 | p->srv->nbpend--; |
| 174 | else |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 175 | p->sess->be->nbpend--; |
| 176 | p->sess->be->totpend--; |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 177 | pool_free2(pool2_pendconn, p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | * Local variables: |
| 183 | * c-indent-level: 8 |
| 184 | * c-basic-offset: 8 |
| 185 | * End: |
| 186 | */ |