Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Health-checks functions. |
| 3 | * |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 4 | * Copyright 2000-2008 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 | b881608 | 2008-01-18 12:18:15 +0100 | [diff] [blame] | 13 | #include <assert.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 18 | #include <string.h> |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 19 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <netinet/in.h> |
| 23 | #include <arpa/inet.h> |
| 24 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/compat.h> |
| 26 | #include <common/config.h> |
| 27 | #include <common/mini-clist.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 28 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 29 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
| 31 | #include <types/global.h> |
| 32 | #include <types/polling.h> |
| 33 | #include <types/proxy.h> |
| 34 | #include <types/session.h> |
| 35 | |
| 36 | #include <proto/backend.h> |
| 37 | #include <proto/fd.h> |
| 38 | #include <proto/log.h> |
| 39 | #include <proto/queue.h> |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 40 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 41 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 42 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <proto/server.h> |
| 44 | #include <proto/task.h> |
| 45 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 46 | /* sends a log message when a backend goes down, and also sets last |
| 47 | * change date. |
| 48 | */ |
| 49 | static void set_backend_down(struct proxy *be) |
| 50 | { |
| 51 | be->last_change = now.tv_sec; |
| 52 | be->down_trans++; |
| 53 | |
| 54 | Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id); |
| 55 | send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id); |
| 56 | } |
| 57 | |
| 58 | /* Redistribute pending connections when a server goes down. The number of |
| 59 | * connections redistributed is returned. |
| 60 | */ |
| 61 | static int redistribute_pending(struct server *s) |
| 62 | { |
| 63 | struct pendconn *pc, *pc_bck, *pc_end; |
| 64 | int xferred = 0; |
| 65 | |
| 66 | FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) { |
| 67 | struct session *sess = pc->sess; |
| 68 | if (sess->be->options & PR_O_REDISP) { |
| 69 | /* The REDISP option was specified. We will ignore |
| 70 | * cookie and force to balance or use the dispatcher. |
| 71 | */ |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 72 | |
| 73 | sess->srv->redispatches++; |
| 74 | sess->be->redispatches++; |
| 75 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 76 | sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 77 | sess->flags |= SN_REDISP; |
| 78 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 79 | sess->srv = NULL; /* it's left to the dispatcher to choose a server */ |
| 80 | http_flush_cookie_flags(&sess->txn); |
| 81 | pendconn_free(pc); |
| 82 | task_wakeup(sess->task); |
| 83 | xferred++; |
| 84 | } |
| 85 | } |
| 86 | return xferred; |
| 87 | } |
| 88 | |
| 89 | /* Check for pending connections at the backend, and assign some of them to |
| 90 | * the server coming up. The server's weight is checked before being assigned |
| 91 | * connections it may not be able to handle. The total number of transferred |
| 92 | * connections is returned. |
| 93 | */ |
| 94 | static int check_for_pending(struct server *s) |
| 95 | { |
| 96 | int xferred; |
| 97 | |
| 98 | if (!s->eweight) |
| 99 | return 0; |
| 100 | |
| 101 | for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) { |
| 102 | struct session *sess; |
| 103 | struct pendconn *p; |
| 104 | |
| 105 | p = pendconn_from_px(s->proxy); |
| 106 | if (!p) |
| 107 | break; |
| 108 | p->sess->srv = s; |
| 109 | sess = p->sess; |
| 110 | pendconn_free(p); |
| 111 | task_wakeup(sess->task); |
| 112 | } |
| 113 | return xferred; |
| 114 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | |
| 116 | /* Sets server <s> down, notifies by all available means, recounts the |
| 117 | * remaining servers on the proxy and transfers queued sessions whenever |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 118 | * possible to other servers. It automatically recomputes the number of |
| 119 | * servers, but not the map. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 120 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 121 | static void set_server_down(struct server *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 123 | int xferred; |
| 124 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 125 | if (s->health == s->rise) { |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 126 | int srv_was_paused = s->state & SRV_GOINGDOWN; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 127 | |
| 128 | s->last_change = now.tv_sec; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 129 | s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 130 | s->proxy->lbprm.set_server_status_down(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | |
| 132 | /* we might have sessions queued on this server and waiting for |
| 133 | * a connection. Those which are redispatchable will be queued |
| 134 | * to another server or to the proxy itself. |
| 135 | */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 136 | xferred = redistribute_pending(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 137 | sprintf(trash, "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s" |
| 138 | " %d sessions active, %d requeued, %d remaining in queue.\n", |
| 139 | s->state & SRV_BACKUP ? "Backup " : "", |
| 140 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 141 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 142 | s->cur_sess, xferred, s->nbpend); |
| 143 | |
| 144 | Warning("%s", trash); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 145 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 146 | /* we don't send an alert if the server was previously paused */ |
| 147 | if (srv_was_paused) |
| 148 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 149 | else |
| 150 | send_log(s->proxy, LOG_ALERT, "%s", trash); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 151 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 152 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 153 | set_backend_down(s->proxy); |
| 154 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 155 | s->down_trans++; |
| 156 | } |
| 157 | s->health = 0; /* failure */ |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /* |
| 162 | * This function is used only for server health-checks. It handles |
| 163 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 164 | * it sends the request. In other cases, it fills s->result with SRV_CHK_*. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 165 | * The function itself returns 0 if it needs some polling before being called |
| 166 | * again, otherwise 1. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 168 | static int event_srv_chk_w(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 170 | __label__ out_wakeup, out_nowake, out_poll, out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 171 | struct task *t = fdtab[fd].owner; |
| 172 | struct server *s = t->context; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 173 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 174 | //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state)); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 175 | if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) |
| 176 | goto out_error; |
| 177 | |
| 178 | /* here, we know that the connection is established */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 179 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 180 | if (!(s->result & SRV_CHK_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 181 | /* we don't want to mark 'UP' a server on which we detected an error earlier */ |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 182 | if ((s->proxy->options & PR_O_HTTP_CHK) || |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 183 | (s->proxy->options & PR_O_SSL3_CHK) || |
| 184 | (s->proxy->options & PR_O_SMTP_CHK)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 185 | int ret; |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 186 | /* we want to check if this host replies to HTTP or SSLv3 requests |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 187 | * so we'll send the request, and won't wake the checker up now. |
| 188 | */ |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 189 | |
| 190 | if (s->proxy->options & PR_O_SSL3_CHK) { |
| 191 | /* SSL requires that we put Unix time in the request */ |
| 192 | int gmt_time = htonl(now.tv_sec); |
| 193 | memcpy(s->proxy->check_req + 11, &gmt_time, 4); |
| 194 | } |
| 195 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 196 | #ifndef MSG_NOSIGNAL |
| 197 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT); |
| 198 | #else |
| 199 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 200 | #endif |
| 201 | if (ret == s->proxy->check_len) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 202 | /* we allow up to <timeout.check> if nonzero for a responce */ |
| 203 | //fprintf(stderr, "event_srv_chk_w, ms=%lu\n", __tv_to_ms(&s->proxy->timeout.check)); |
| 204 | tv_add_ifset(&t->expire, &now, &s->proxy->timeout.check); |
| 205 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 206 | EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 207 | goto out_nowake; |
| 208 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 209 | else if (ret == 0 || errno == EAGAIN) |
| 210 | goto out_poll; |
| 211 | else |
| 212 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 213 | } |
| 214 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 215 | /* We have no data to send to check the connection, and |
| 216 | * getsockopt() will not inform us whether the connection |
| 217 | * is still pending. So we'll reuse connect() to check the |
| 218 | * state of the socket. This has the advantage of givig us |
| 219 | * the following info : |
| 220 | * - error |
| 221 | * - connecting (EALREADY, EINPROGRESS) |
| 222 | * - connected (EISCONN, 0) |
| 223 | */ |
| 224 | |
| 225 | struct sockaddr_in sa; |
| 226 | |
| 227 | sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr; |
| 228 | sa.sin_port = htons(s->check_port); |
| 229 | |
| 230 | if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0) |
| 231 | errno = 0; |
| 232 | |
| 233 | if (errno == EALREADY || errno == EINPROGRESS) |
| 234 | goto out_poll; |
| 235 | |
| 236 | if (errno && errno != EISCONN) |
| 237 | goto out_error; |
| 238 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 239 | /* good TCP connection is enough */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 240 | s->result |= SRV_CHK_RUNNING; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 241 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 242 | } |
| 243 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 244 | out_wakeup: |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 245 | task_wakeup(t); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 246 | out_nowake: |
| 247 | EV_FD_CLR(fd, DIR_WR); /* nothing more to write */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 248 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 249 | return 1; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 250 | out_poll: |
| 251 | /* The connection is still pending. We'll have to poll it |
| 252 | * before attempting to go further. */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 253 | fdtab[fd].ev &= ~FD_POLL_OUT; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 254 | return 0; |
| 255 | out_error: |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 256 | s->result |= SRV_CHK_ERROR; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 257 | fdtab[fd].state = FD_STERROR; |
| 258 | goto out_wakeup; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
Willy Tarreau | f3c6920 | 2006-07-09 16:42:34 +0200 | [diff] [blame] | 263 | * This function is used only for server health-checks. It handles the server's |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 264 | * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING |
| 265 | * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP |
| 266 | * server returns 2xx, or if an SSL server returns at least 5 bytes in response |
| 267 | * to an SSL HELLO (the principle is that this is enough to distinguish between |
| 268 | * an SSL server and a pure TCP relay). All other cases will set s->result to |
| 269 | * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after |
| 270 | * some polling, otherwise non-zero.. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 271 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 272 | static int event_srv_chk_r(int fd) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 273 | { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 274 | __label__ out_wakeup; |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 275 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 276 | struct task *t = fdtab[fd].owner; |
| 277 | struct server *s = t->context; |
| 278 | int skerr; |
| 279 | socklen_t lskerr = sizeof(skerr); |
| 280 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 281 | len = -1; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 282 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 283 | if (unlikely((s->result & SRV_CHK_ERROR) || |
| 284 | (fdtab[fd].state == FD_STERROR) || |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 285 | (fdtab[fd].ev & FD_POLL_ERR) || |
| 286 | (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) || |
| 287 | (skerr != 0))) { |
| 288 | /* in case of TCP only, this tells us if the connection failed */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 289 | s->result |= SRV_CHK_ERROR; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 290 | goto out_wakeup; |
| 291 | } |
| 292 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 293 | #ifndef MSG_NOSIGNAL |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 294 | len = recv(fd, trash, sizeof(trash), 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 295 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 296 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 297 | * but the connection was closed on the remote end. Fortunately, recv still |
| 298 | * works correctly and we don't need to do the getsockopt() on linux. |
| 299 | */ |
Krzysztof Oledzki | 6b3f8b4 | 2007-10-11 18:41:08 +0200 | [diff] [blame] | 300 | len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 301 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 302 | if (unlikely(len < 0 && errno == EAGAIN)) { |
| 303 | /* we want some polling to happen first */ |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 304 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 305 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 308 | /* Note: the response will only be accepted if read at once */ |
| 309 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 310 | /* Check if the server speaks HTTP 1.X */ |
| 311 | if ((len < strlen("HTTP/1.0 000\r")) || |
| 312 | (memcmp(trash, "HTTP/1.", 7) != 0)) { |
| 313 | s->result |= SRV_CHK_ERROR; |
| 314 | goto out_wakeup; |
| 315 | } |
| 316 | |
| 317 | /* check the reply : HTTP/1.X 2xx and 3xx are OK */ |
| 318 | if (trash[9] == '2' || trash[9] == '3') |
| 319 | s->result |= SRV_CHK_RUNNING; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 320 | else if ((s->proxy->options & PR_O_DISABLE404) && |
| 321 | (s->state & SRV_RUNNING) && |
| 322 | (memcmp(&trash[9], "404", 3) == 0)) { |
| 323 | /* 404 may be accepted as "stopping" only if the server was up */ |
| 324 | s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE; |
| 325 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 326 | else |
| 327 | s->result |= SRV_CHK_ERROR; |
| 328 | } |
| 329 | else if (s->proxy->options & PR_O_SSL3_CHK) { |
| 330 | /* Check for SSLv3 alert or handshake */ |
| 331 | if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16)) |
| 332 | s->result |= SRV_CHK_RUNNING; |
| 333 | else |
| 334 | s->result |= SRV_CHK_ERROR; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 335 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 336 | else if (s->proxy->options & PR_O_SMTP_CHK) { |
| 337 | /* Check for SMTP code 2xx (should be 250) */ |
| 338 | if ((len >= 3) && (trash[0] == '2')) |
| 339 | s->result |= SRV_CHK_RUNNING; |
| 340 | else |
| 341 | s->result |= SRV_CHK_ERROR; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 342 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 343 | else { |
| 344 | /* other checks are valid if the connection succeeded anyway */ |
| 345 | s->result |= SRV_CHK_RUNNING; |
Willy Tarreau | 2367790 | 2007-05-08 23:50:35 +0200 | [diff] [blame] | 346 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 347 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 348 | out_wakeup: |
| 349 | if (s->result & SRV_CHK_ERROR) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 350 | fdtab[fd].state = FD_STERROR; |
| 351 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 352 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 353 | task_wakeup(t); |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 354 | fdtab[fd].ev &= ~FD_POLL_IN; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 355 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * manages a server health-check. Returns |
| 360 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
| 361 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 362 | void process_chk(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 363 | { |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 364 | __label__ new_chk, out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 365 | struct server *s = t->context; |
| 366 | struct sockaddr_in sa; |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 367 | int xferred; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | int fd; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 369 | int rv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 370 | |
| 371 | //fprintf(stderr, "process_chk: task=%p\n", t); |
| 372 | |
| 373 | new_chk: |
| 374 | fd = s->curfd; |
| 375 | if (fd < 0) { /* no check currently running */ |
| 376 | //fprintf(stderr, "process_chk: 2\n"); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 377 | if (!tv_isle(&t->expire, &now)) { /* not good time yet */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 378 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 379 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 380 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* we don't send any health-checks when the proxy is stopped or when |
| 384 | * the server should not be checked. |
| 385 | */ |
| 386 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 387 | while (tv_isle(&t->expire, &now)) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 388 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 390 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 391 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* we'll initiate a new check */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 395 | s->result = SRV_CHK_UNKNOWN; /* no result yet */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 396 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
| 397 | if ((fd < global.maxsock) && |
| 398 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 399 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 400 | //fprintf(stderr, "process_chk: 3\n"); |
| 401 | |
Willy Tarreau | 9edd161 | 2007-10-18 18:07:48 +0200 | [diff] [blame] | 402 | if (s->proxy->options & PR_O_TCP_NOLING) { |
| 403 | /* We don't want to useless data */ |
| 404 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 405 | } |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 406 | |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 407 | if (s->check_addr.sin_addr.s_addr) |
| 408 | /* we'll connect to the check addr specified on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 409 | sa = s->check_addr; |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 410 | else |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 411 | /* we'll connect to the addr on the server */ |
Willy Tarreau | 2ea3abb | 2007-03-25 16:45:16 +0200 | [diff] [blame] | 412 | sa = s->addr; |
Willy Tarreau | 0f03c6f | 2007-03-25 20:46:19 +0200 | [diff] [blame] | 413 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 414 | /* we'll connect to the check port on the server */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 415 | sa.sin_port = htons(s->check_port); |
| 416 | |
| 417 | /* allow specific binding : |
| 418 | * - server-specific at first |
| 419 | * - proxy-specific next |
| 420 | */ |
| 421 | if (s->state & SRV_BIND_SRC) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 422 | struct sockaddr_in *remote = NULL; |
| 423 | int ret, flags = 0; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 424 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame^] | 425 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 426 | if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) { |
| 427 | remote = (struct sockaddr_in *)&s->tproxy_addr; |
| 428 | flags = 3; |
| 429 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame^] | 430 | #endif |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 431 | ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote); |
| 432 | if (ret) { |
| 433 | s->result |= SRV_CHK_ERROR; |
| 434 | switch (ret) { |
| 435 | case 1: |
| 436 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 437 | s->proxy->id, s->id); |
| 438 | break; |
| 439 | case 2: |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 440 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
| 441 | s->proxy->id, s->id); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 442 | break; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 443 | } |
| 444 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 445 | } |
| 446 | else if (s->proxy->options & PR_O_BIND_SRC) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 447 | struct sockaddr_in *remote = NULL; |
| 448 | int ret, flags = 0; |
| 449 | |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame^] | 450 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 451 | if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 452 | remote = (struct sockaddr_in *)&s->proxy->tproxy_addr; |
| 453 | flags = 3; |
| 454 | } |
Willy Tarreau | cf1d572 | 2008-02-14 20:28:18 +0100 | [diff] [blame^] | 455 | #endif |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 456 | ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote); |
| 457 | if (ret) { |
| 458 | s->result |= SRV_CHK_ERROR; |
| 459 | switch (ret) { |
| 460 | case 1: |
| 461 | Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n", |
| 462 | proxy_type_str(s->proxy), s->proxy->id); |
| 463 | break; |
| 464 | case 2: |
Willy Tarreau | 2b5652f | 2006-12-31 17:46:05 +0100 | [diff] [blame] | 465 | Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n", |
| 466 | proxy_type_str(s->proxy), s->proxy->id); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 467 | break; |
Willy Tarreau | 163c532 | 2006-11-14 16:18:41 +0100 | [diff] [blame] | 468 | } |
| 469 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 472 | if (s->result == SRV_CHK_UNKNOWN) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 473 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 474 | struct timeval tv_con; |
| 475 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 476 | /* OK, connection in progress or established */ |
| 477 | |
| 478 | //fprintf(stderr, "process_chk: 4\n"); |
| 479 | |
| 480 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
Willy Tarreau | 7a96648 | 2007-04-15 10:58:02 +0200 | [diff] [blame] | 481 | fd_insert(fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 482 | fdtab[fd].owner = t; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 483 | fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r; |
| 484 | fdtab[fd].cb[DIR_RD].b = NULL; |
| 485 | fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w; |
| 486 | fdtab[fd].cb[DIR_WR].b = NULL; |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 487 | fdtab[fd].peeraddr = (struct sockaddr *)&sa; |
| 488 | fdtab[fd].peerlen = sizeof(sa); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 489 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 490 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 491 | #ifdef DEBUG_FULL |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 492 | assert (!EV_FD_ISSET(fd, DIR_RD)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 493 | #endif |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 494 | //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect)); |
| 495 | /* we allow up to min(inter, timeout.connect) for a connection |
| 496 | * to establish but only when timeout.check is set |
| 497 | * as it may be to short for a full check otherwise |
| 498 | */ |
| 499 | tv_add(&tv_con, &now, &s->proxy->timeout.connect); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 500 | tv_ms_add(&t->expire, &now, s->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 501 | |
| 502 | if (tv_isset(&s->proxy->timeout.check)) |
| 503 | tv_bound(&t->expire, &tv_con); |
| 504 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 505 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 506 | *next = t->expire; |
Willy Tarreau | 8eee9c8 | 2007-05-14 03:40:11 +0200 | [diff] [blame] | 507 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 508 | } |
| 509 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 510 | s->result |= SRV_CHK_ERROR; /* a real error */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | } |
| 514 | close(fd); /* socket creation error */ |
| 515 | } |
| 516 | |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 517 | if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 518 | //fprintf(stderr, "process_chk: 6\n"); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 519 | while (tv_isle(&t->expire, &now)) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 520 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 521 | goto new_chk; /* may be we should initialize a new check */ |
| 522 | } |
| 523 | |
| 524 | /* here, we have seen a failure */ |
| 525 | if (s->health > s->rise) { |
| 526 | s->health--; /* still good */ |
| 527 | s->failed_checks++; |
| 528 | } |
| 529 | else |
| 530 | set_server_down(s); |
| 531 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 532 | //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect)); |
| 533 | /* we allow up to min(inter, timeout.connect) for a connection |
| 534 | * to establish but only when timeout.check is set |
| 535 | * as it may be to short for a full check otherwise |
| 536 | */ |
| 537 | while (tv_isle(&t->expire, &now)) { |
| 538 | struct timeval tv_con; |
| 539 | |
| 540 | tv_add(&tv_con, &t->expire, &s->proxy->timeout.connect); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 541 | tv_ms_add(&t->expire, &t->expire, s->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 542 | |
| 543 | if (tv_isset(&s->proxy->timeout.check)) |
| 544 | tv_bound(&t->expire, &tv_con); |
| 545 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 546 | goto new_chk; |
| 547 | } |
| 548 | else { |
| 549 | //fprintf(stderr, "process_chk: 8\n"); |
| 550 | /* there was a test running */ |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 551 | if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 552 | //fprintf(stderr, "process_chk: 9\n"); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 553 | |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 554 | if (s->state & SRV_WARMINGUP) { |
| 555 | if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) { |
| 556 | s->state &= ~SRV_WARMINGUP; |
| 557 | if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) |
| 558 | s->eweight = s->uweight * BE_WEIGHT_SCALE; |
| 559 | if (s->proxy->lbprm.update_server_eweight) |
| 560 | s->proxy->lbprm.update_server_eweight(s); |
| 561 | } |
| 562 | else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) { |
| 563 | /* for dynamic algorithms, let's update the weight */ |
Willy Tarreau | 5542af6 | 2007-12-03 02:04:00 +0100 | [diff] [blame] | 564 | s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) + |
| 565 | s->slowstart - 1) / s->slowstart; |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 566 | s->eweight *= s->uweight; |
| 567 | if (s->proxy->lbprm.update_server_eweight) |
| 568 | s->proxy->lbprm.update_server_eweight(s); |
| 569 | } |
| 570 | /* probably that we can refill this server with a bit more connections */ |
| 571 | check_for_pending(s); |
| 572 | } |
| 573 | |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 574 | /* we may have to add/remove this server from the LB group */ |
| 575 | if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) { |
| 576 | if ((s->state & SRV_GOINGDOWN) && |
| 577 | ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING)) { |
| 578 | /* server enabled again */ |
| 579 | s->state &= ~SRV_GOINGDOWN; |
| 580 | s->proxy->lbprm.set_server_status_up(s); |
| 581 | |
| 582 | /* check if we can handle some connections queued at the proxy. We |
| 583 | * will take as many as we can handle. |
| 584 | */ |
| 585 | xferred = check_for_pending(s); |
| 586 | |
| 587 | sprintf(trash, |
| 588 | "Load-balancing on %sServer %s/%s is enabled again. %d active and %d backup servers online.%s" |
| 589 | " %d sessions requeued, %d total in queue.\n", |
| 590 | s->state & SRV_BACKUP ? "Backup " : "", |
| 591 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 592 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 593 | xferred, s->nbpend); |
| 594 | |
| 595 | Warning("%s", trash); |
| 596 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 597 | } |
| 598 | else if (!(s->state & SRV_GOINGDOWN) && |
| 599 | ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) == |
| 600 | (SRV_CHK_RUNNING | SRV_CHK_DISABLE))) { |
| 601 | /* server disabled */ |
| 602 | s->state |= SRV_GOINGDOWN; |
| 603 | s->proxy->lbprm.set_server_status_down(s); |
| 604 | |
| 605 | /* we might have sessions queued on this server and waiting for |
| 606 | * a connection. Those which are redispatchable will be queued |
| 607 | * to another server or to the proxy itself. |
| 608 | */ |
| 609 | xferred = redistribute_pending(s); |
| 610 | |
| 611 | sprintf(trash, |
| 612 | "Load-balancing on %sServer %s/%s is disabled. %d active and %d backup servers online.%s" |
| 613 | " %d sessions requeued, %d total in queue.\n", |
| 614 | s->state & SRV_BACKUP ? "Backup " : "", |
| 615 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 616 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 617 | xferred, s->nbpend); |
| 618 | |
| 619 | Warning("%s", trash); |
| 620 | |
| 621 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 622 | if (!s->proxy->srv_bck && !s->proxy->srv_act) |
| 623 | set_backend_down(s->proxy); |
| 624 | } |
| 625 | } |
| 626 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 627 | if (s->health < s->rise + s->fall - 1) { |
| 628 | s->health++; /* was bad, stays for a while */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 629 | |
| 630 | if (s->health == s->rise) { |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 631 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 632 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 633 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 634 | s->proxy->last_change = now.tv_sec; |
| 635 | } |
| 636 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 637 | if (s->last_change < now.tv_sec) // ignore negative times |
| 638 | s->down_time += now.tv_sec - s->last_change; |
| 639 | |
| 640 | s->last_change = now.tv_sec; |
| 641 | s->state |= SRV_RUNNING; |
Willy Tarreau | 9909fc1 | 2007-11-30 17:42:05 +0100 | [diff] [blame] | 642 | if (s->slowstart > 0) { |
| 643 | s->state |= SRV_WARMINGUP; |
| 644 | if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) { |
| 645 | /* For dynamic algorithms, start at the first step of the weight, |
| 646 | * without multiplying by BE_WEIGHT_SCALE. |
| 647 | */ |
| 648 | s->eweight = s->uweight; |
| 649 | if (s->proxy->lbprm.update_server_eweight) |
| 650 | s->proxy->lbprm.update_server_eweight(s); |
| 651 | } |
| 652 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 653 | s->proxy->lbprm.set_server_status_up(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 654 | |
| 655 | /* check if we can handle some connections queued at the proxy. We |
| 656 | * will take as many as we can handle. |
| 657 | */ |
Willy Tarreau | 48494c0 | 2007-11-30 10:41:39 +0100 | [diff] [blame] | 658 | xferred = check_for_pending(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 659 | |
| 660 | sprintf(trash, |
| 661 | "%sServer %s/%s is UP. %d active and %d backup servers online.%s" |
| 662 | " %d sessions requeued, %d total in queue.\n", |
| 663 | s->state & SRV_BACKUP ? "Backup " : "", |
| 664 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 665 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
| 666 | xferred, s->nbpend); |
| 667 | |
| 668 | Warning("%s", trash); |
| 669 | send_log(s->proxy, LOG_NOTICE, "%s", trash); |
| 670 | } |
| 671 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 672 | if (s->health >= s->rise) |
| 673 | s->health = s->rise + s->fall - 1; /* OK now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 674 | } |
| 675 | s->curfd = -1; /* no check running anymore */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 676 | fd_delete(fd); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 677 | |
| 678 | rv = 0; |
| 679 | if (global.spread_checks > 0) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 680 | rv = srv_getinter(s) * global.spread_checks / 100; |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 681 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 682 | //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv); |
Willy Tarreau | 44ec0f0 | 2007-10-14 23:47:04 +0200 | [diff] [blame] | 683 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 684 | tv_ms_add(&t->expire, &now, srv_getinter(s) + rv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 685 | goto new_chk; |
| 686 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 687 | else if ((s->result & SRV_CHK_ERROR) || tv_isle(&t->expire, &now)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 688 | //fprintf(stderr, "process_chk: 10\n"); |
| 689 | /* failure or timeout detected */ |
| 690 | if (s->health > s->rise) { |
| 691 | s->health--; /* still good */ |
| 692 | s->failed_checks++; |
| 693 | } |
| 694 | else |
| 695 | set_server_down(s); |
| 696 | s->curfd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 697 | fd_delete(fd); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 698 | |
| 699 | rv = 0; |
| 700 | if (global.spread_checks > 0) { |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 701 | rv = srv_getinter(s) * global.spread_checks / 100; |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 702 | rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0))); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 703 | //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 704 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 705 | tv_ms_add(&t->expire, &now, srv_getinter(s) + rv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 706 | goto new_chk; |
| 707 | } |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 708 | /* if result is unknown and there's no timeout, we have to wait again */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 709 | } |
| 710 | //fprintf(stderr, "process_chk: 11\n"); |
Willy Tarreau | c7dd71a | 2007-11-30 08:33:21 +0100 | [diff] [blame] | 711 | s->result = SRV_CHK_UNKNOWN; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 712 | task_queue(t); /* restore t to its place in the task list */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 713 | *next = t->expire; |
Willy Tarreau | 7317eb5 | 2007-05-09 00:54:10 +0200 | [diff] [blame] | 714 | out: |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 715 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 718 | /* |
| 719 | * Start health-check. |
| 720 | * Returns 0 if OK, -1 if error, and prints the error in this case. |
| 721 | */ |
| 722 | int start_checks() { |
| 723 | |
| 724 | struct proxy *px; |
| 725 | struct server *s; |
| 726 | struct task *t; |
| 727 | int nbchk=0, mininter=0, srvpos=0; |
| 728 | |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 729 | /* 1- count the checkers to run simultaneously. |
| 730 | * We also determine the minimum interval among all of those which |
| 731 | * have an interval larger than SRV_CHK_INTER_THRES. This interval |
| 732 | * will be used to spread their start-up date. Those which have |
| 733 | * a shorter interval will start independantly and will not dictate |
| 734 | * too short an interval for all others. |
| 735 | */ |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 736 | for (px = proxy; px; px = px->next) { |
| 737 | for (s = px->srv; s; s = s->next) { |
| 738 | if (!(s->state & SRV_CHECKED)) |
| 739 | continue; |
| 740 | |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 741 | if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) && |
| 742 | (!mininter || mininter > srv_getinter(s))) |
| 743 | mininter = srv_getinter(s); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 744 | |
| 745 | nbchk++; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if (!nbchk) |
| 750 | return 0; |
| 751 | |
| 752 | srand((unsigned)time(NULL)); |
| 753 | |
| 754 | /* |
| 755 | * 2- start them as far as possible from each others. For this, we will |
| 756 | * start them after their interval set to the min interval divided by |
| 757 | * the number of servers, weighted by the server's position in the list. |
| 758 | */ |
| 759 | for (px = proxy; px; px = px->next) { |
| 760 | for (s = px->srv; s; s = s->next) { |
| 761 | if (!(s->state & SRV_CHECKED)) |
| 762 | continue; |
| 763 | |
| 764 | if ((t = pool_alloc2(pool2_task)) == NULL) { |
| 765 | Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id); |
| 766 | return -1; |
| 767 | } |
| 768 | |
| 769 | t->wq = NULL; |
| 770 | t->qlist.p = NULL; |
| 771 | t->state = TASK_IDLE; |
| 772 | t->process = process_chk; |
| 773 | t->context = s; |
| 774 | |
| 775 | /* check this every ms */ |
Willy Tarreau | 2c43a1e | 2007-10-14 23:05:39 +0200 | [diff] [blame] | 776 | tv_ms_add(&t->expire, &now, |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 777 | ((mininter && mininter >= srv_getinter(s)) ? mininter : srv_getinter(s)) * srvpos / nbchk); |
Krzysztof Oledzki | b304dc7 | 2007-10-14 23:40:01 +0200 | [diff] [blame] | 778 | task_queue(t); |
| 779 | |
| 780 | srvpos++; |
| 781 | } |
| 782 | } |
| 783 | return 0; |
| 784 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 785 | |
| 786 | /* |
| 787 | * Local variables: |
| 788 | * c-indent-level: 8 |
| 789 | * c-basic-offset: 8 |
| 790 | * End: |
| 791 | */ |