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