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