blob: 2dc1d601992c3dc9f7d1318eb24bf488868a5d19 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Health-checks functions.
3 *
Willy Tarreaue8c66af2008-01-13 18:40:14 +01004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaub8816082008-01-18 12:18:15 +010014#include <assert.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020018#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <string.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <unistd.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/compat.h>
27#include <common/config.h>
28#include <common/mini-clist.h>
Willy Tarreau83749182007-04-15 20:56:27 +020029#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
32#include <types/global.h>
33#include <types/polling.h>
34#include <types/proxy.h>
35#include <types/session.h>
36
37#include <proto/backend.h>
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +010038#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/fd.h>
40#include <proto/log.h>
41#include <proto/queue.h>
Willy Tarreau3d300592007-03-18 18:34:41 +010042#include <proto/proto_http.h>
Willy Tarreaue8c66af2008-01-13 18:40:14 +010043#include <proto/proto_tcp.h>
Willy Tarreau2b5652f2006-12-31 17:46:05 +010044#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/server.h>
46#include <proto/task.h>
47
Willy Tarreau48494c02007-11-30 10:41:39 +010048/* sends a log message when a backend goes down, and also sets last
49 * change date.
50 */
51static void set_backend_down(struct proxy *be)
52{
53 be->last_change = now.tv_sec;
54 be->down_trans++;
55
56 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
57 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
58}
59
60/* Redistribute pending connections when a server goes down. The number of
61 * connections redistributed is returned.
62 */
63static int redistribute_pending(struct server *s)
64{
65 struct pendconn *pc, *pc_bck, *pc_end;
66 int xferred = 0;
67
68 FOREACH_ITEM_SAFE(pc, pc_bck, &s->pendconns, pc_end, struct pendconn *, list) {
69 struct session *sess = pc->sess;
70 if (sess->be->options & PR_O_REDISP) {
71 /* The REDISP option was specified. We will ignore
72 * cookie and force to balance or use the dispatcher.
73 */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010074
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +010075 /* it's left to the dispatcher to choose a server */
Willy Tarreau48494c02007-11-30 10:41:39 +010076 sess->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +010077
Willy Tarreau48494c02007-11-30 10:41:39 +010078 pendconn_free(pc);
79 task_wakeup(sess->task);
80 xferred++;
81 }
82 }
83 return xferred;
84}
85
86/* Check for pending connections at the backend, and assign some of them to
87 * the server coming up. The server's weight is checked before being assigned
88 * connections it may not be able to handle. The total number of transferred
89 * connections is returned.
90 */
91static int check_for_pending(struct server *s)
92{
93 int xferred;
94
95 if (!s->eweight)
96 return 0;
97
98 for (xferred = 0; !s->maxconn || xferred < srv_dynamic_maxconn(s); xferred++) {
99 struct session *sess;
100 struct pendconn *p;
101
102 p = pendconn_from_px(s->proxy);
103 if (!p)
104 break;
105 p->sess->srv = s;
106 sess = p->sess;
107 pendconn_free(p);
108 task_wakeup(sess->task);
109 }
110 return xferred;
111}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112
113/* Sets server <s> down, notifies by all available means, recounts the
114 * remaining servers on the proxy and transfers queued sessions whenever
Willy Tarreau5af3a692007-07-24 23:32:33 +0200115 * possible to other servers. It automatically recomputes the number of
116 * servers, but not the map.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 */
Willy Tarreau83749182007-04-15 20:56:27 +0200118static void set_server_down(struct server *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119{
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100120 struct server *srv;
121 struct chunk msg;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 int xferred;
123
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100124 if (s->health == s->rise || s->tracked) {
Willy Tarreau48494c02007-11-30 10:41:39 +0100125 int srv_was_paused = s->state & SRV_GOINGDOWN;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200126
127 s->last_change = now.tv_sec;
Willy Tarreau48494c02007-11-30 10:41:39 +0100128 s->state &= ~(SRV_RUNNING | SRV_GOINGDOWN);
Willy Tarreaub625a082007-11-26 01:15:43 +0100129 s->proxy->lbprm.set_server_status_down(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200130
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 Tarreau48494c02007-11-30 10:41:39 +0100135 xferred = redistribute_pending(s);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100136
137 msg.len = 0;
138 msg.str = trash;
139
140 chunk_printf(&msg, sizeof(trash),
141 "%sServer %s/%s is DOWN", s->state & SRV_BACKUP ? "Backup " : "",
142 s->proxy->id, s->id);
143
144 if (s->tracked)
145 chunk_printf(&msg, sizeof(trash), " via %s/%s",
146 s->tracked->proxy->id, s->tracked->id);
147
148 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers left.%s"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 " %d sessions active, %d requeued, %d remaining in queue.\n",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100150 s->proxy->srv_act, s->proxy->srv_bck,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
152 s->cur_sess, xferred, s->nbpend);
153
154 Warning("%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200155
Willy Tarreau48494c02007-11-30 10:41:39 +0100156 /* we don't send an alert if the server was previously paused */
157 if (srv_was_paused)
158 send_log(s->proxy, LOG_NOTICE, "%s", trash);
159 else
160 send_log(s->proxy, LOG_ALERT, "%s", trash);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200161
Willy Tarreau48494c02007-11-30 10:41:39 +0100162 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
163 set_backend_down(s->proxy);
164
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165 s->down_trans++;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100166
167 if (s->state && SRV_CHECKED)
168 for(srv = s->tracknext; srv; srv = srv->tracknext)
169 set_server_down(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 }
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100171
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172 s->health = 0; /* failure */
173}
174
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100175static void set_server_up(struct server *s) {
176
177 struct server *srv;
178 struct chunk msg;
179 int xferred;
180
181 if (s->health == s->rise || s->tracked) {
182 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
183 if (s->proxy->last_change < now.tv_sec) // ignore negative times
184 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
185 s->proxy->last_change = now.tv_sec;
186 }
187
188 if (s->last_change < now.tv_sec) // ignore negative times
189 s->down_time += now.tv_sec - s->last_change;
190
191 s->last_change = now.tv_sec;
192 s->state |= SRV_RUNNING;
193
194 if (s->slowstart > 0) {
195 s->state |= SRV_WARMINGUP;
196 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
197 /* For dynamic algorithms, start at the first step of the weight,
198 * without multiplying by BE_WEIGHT_SCALE.
199 */
200 s->eweight = s->uweight;
201 if (s->proxy->lbprm.update_server_eweight)
202 s->proxy->lbprm.update_server_eweight(s);
203 }
204 }
205 s->proxy->lbprm.set_server_status_up(s);
206
207 /* check if we can handle some connections queued at the proxy. We
208 * will take as many as we can handle.
209 */
210 xferred = check_for_pending(s);
211
212 msg.len = 0;
213 msg.str = trash;
214
215 chunk_printf(&msg, sizeof(trash),
216 "%sServer %s/%s is UP", s->state & SRV_BACKUP ? "Backup " : "",
217 s->proxy->id, s->id);
218
219 if (s->tracked)
220 chunk_printf(&msg, sizeof(trash), " via %s/%s",
221 s->tracked->proxy->id, s->tracked->id);
222
223 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
224 " %d sessions requeued, %d total in queue.\n",
225 s->proxy->srv_act, s->proxy->srv_bck,
226 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
227 s->cur_sess, xferred, s->nbpend);
228
229 Warning("%s", trash);
230 send_log(s->proxy, LOG_NOTICE, "%s", trash);
231
232 if (s->state && SRV_CHECKED)
233 for(srv = s->tracknext; srv; srv = srv->tracknext)
234 set_server_up(srv);
235 }
236
237 if (s->health >= s->rise)
238 s->health = s->rise + s->fall - 1; /* OK now */
239
240}
241
242static void set_server_disabled(struct server *s) {
243
244 struct server *srv;
245 struct chunk msg;
246 int xferred;
247
248 s->state |= SRV_GOINGDOWN;
249 s->proxy->lbprm.set_server_status_down(s);
250
251 /* we might have sessions queued on this server and waiting for
252 * a connection. Those which are redispatchable will be queued
253 * to another server or to the proxy itself.
254 */
255 xferred = redistribute_pending(s);
256
257 msg.len = 0;
258 msg.str = trash;
259
260 chunk_printf(&msg, sizeof(trash),
261 "Load-balancing on %sServer %s/%s is disabled",
262 s->state & SRV_BACKUP ? "Backup " : "",
263 s->proxy->id, s->id);
264
265 if (s->tracked)
266 chunk_printf(&msg, sizeof(trash), " via %s/%s",
267 s->tracked->proxy->id, s->tracked->id);
268
269
270 chunk_printf(&msg, sizeof(trash),". %d active and %d backup servers online.%s"
271 " %d sessions requeued, %d total in queue.\n",
272 s->proxy->srv_act, s->proxy->srv_bck,
273 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
274 xferred, s->nbpend);
275
276 Warning("%s", trash);
277
278 send_log(s->proxy, LOG_NOTICE, "%s", trash);
279
280 if (!s->proxy->srv_bck && !s->proxy->srv_act)
281 set_backend_down(s->proxy);
282
283 if (s->state && SRV_CHECKED)
284 for(srv = s->tracknext; srv; srv = srv->tracknext)
285 set_server_disabled(srv);
286}
287
288static void set_server_enabled(struct server *s) {
289
290 struct server *srv;
291 struct chunk msg;
292 int xferred;
293
294 s->state &= ~SRV_GOINGDOWN;
295 s->proxy->lbprm.set_server_status_up(s);
296
297 /* check if we can handle some connections queued at the proxy. We
298 * will take as many as we can handle.
299 */
300 xferred = check_for_pending(s);
301
302 msg.len = 0;
303 msg.str = trash;
304
305 chunk_printf(&msg, sizeof(trash),
306 "Load-balancing on %sServer %s/%s is enabled again",
307 s->state & SRV_BACKUP ? "Backup " : "",
308 s->proxy->id, s->id);
309
310 if (s->tracked)
311 chunk_printf(&msg, sizeof(trash), " via %s/%s",
312 s->tracked->proxy->id, s->tracked->id);
313
314 chunk_printf(&msg, sizeof(trash), ". %d active and %d backup servers online.%s"
315 " %d sessions requeued, %d total in queue.\n",
316 s->proxy->srv_act, s->proxy->srv_bck,
317 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
318 xferred, s->nbpend);
319
320 Warning("%s", trash);
321 send_log(s->proxy, LOG_NOTICE, "%s", trash);
322
323 if (s->state && SRV_CHECKED)
324 for(srv = s->tracknext; srv; srv = srv->tracknext)
325 set_server_enabled(srv);
326}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327
328/*
329 * This function is used only for server health-checks. It handles
330 * the connection acknowledgement. If the proxy requires HTTP health-checks,
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100331 * it sends the request. In other cases, it fills s->result with SRV_CHK_*.
Willy Tarreau83749182007-04-15 20:56:27 +0200332 * The function itself returns 0 if it needs some polling before being called
333 * again, otherwise 1.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 */
Willy Tarreau83749182007-04-15 20:56:27 +0200335static int event_srv_chk_w(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336{
Willy Tarreau6996e152007-04-30 14:37:43 +0200337 __label__ out_wakeup, out_nowake, out_poll, out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200338 struct task *t = fdtab[fd].owner;
339 struct server *s = t->context;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100341 //fprintf(stderr, "event_srv_chk_w, state=%ld\n", unlikely(fdtab[fd].state));
Willy Tarreau6996e152007-04-30 14:37:43 +0200342 if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR)))
343 goto out_error;
344
345 /* here, we know that the connection is established */
Willy Tarreau83749182007-04-15 20:56:27 +0200346
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100347 if (!(s->result & SRV_CHK_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348 /* we don't want to mark 'UP' a server on which we detected an error earlier */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200349 if ((s->proxy->options & PR_O_HTTP_CHK) ||
Willy Tarreau23677902007-05-08 23:50:35 +0200350 (s->proxy->options & PR_O_SSL3_CHK) ||
351 (s->proxy->options & PR_O_SMTP_CHK)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352 int ret;
Willy Tarreauf3c69202006-07-09 16:42:34 +0200353 /* we want to check if this host replies to HTTP or SSLv3 requests
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354 * so we'll send the request, and won't wake the checker up now.
355 */
Willy Tarreauf3c69202006-07-09 16:42:34 +0200356
357 if (s->proxy->options & PR_O_SSL3_CHK) {
358 /* SSL requires that we put Unix time in the request */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200359 int gmt_time = htonl(date.tv_sec);
Willy Tarreauf3c69202006-07-09 16:42:34 +0200360 memcpy(s->proxy->check_req + 11, &gmt_time, 4);
361 }
362
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363#ifndef MSG_NOSIGNAL
364 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT);
365#else
366 ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL);
367#endif
368 if (ret == s->proxy->check_len) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100369 /* we allow up to <timeout.check> if nonzero for a responce */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200370 t->expire = tick_add_ifset(now_ms, s->proxy->timeout.check);
Willy Tarreauf161a342007-04-08 16:59:42 +0200371 EV_FD_SET(fd, DIR_RD); /* prepare for reading reply */
Willy Tarreau83749182007-04-15 20:56:27 +0200372 goto out_nowake;
373 }
Willy Tarreau6996e152007-04-30 14:37:43 +0200374 else if (ret == 0 || errno == EAGAIN)
375 goto out_poll;
376 else
377 goto out_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378 }
379 else {
Willy Tarreau6996e152007-04-30 14:37:43 +0200380 /* We have no data to send to check the connection, and
381 * getsockopt() will not inform us whether the connection
382 * is still pending. So we'll reuse connect() to check the
383 * state of the socket. This has the advantage of givig us
384 * the following info :
385 * - error
386 * - connecting (EALREADY, EINPROGRESS)
387 * - connected (EISCONN, 0)
388 */
389
390 struct sockaddr_in sa;
391
392 sa = (s->check_addr.sin_addr.s_addr) ? s->check_addr : s->addr;
393 sa.sin_port = htons(s->check_port);
394
395 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0)
396 errno = 0;
397
398 if (errno == EALREADY || errno == EINPROGRESS)
399 goto out_poll;
400
401 if (errno && errno != EISCONN)
402 goto out_error;
403
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404 /* good TCP connection is enough */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100405 s->result |= SRV_CHK_RUNNING;
Willy Tarreau6996e152007-04-30 14:37:43 +0200406 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407 }
408 }
Willy Tarreau83749182007-04-15 20:56:27 +0200409 out_wakeup:
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200410 task_wakeup(t);
Willy Tarreau83749182007-04-15 20:56:27 +0200411 out_nowake:
412 EV_FD_CLR(fd, DIR_WR); /* nothing more to write */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100413 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau83749182007-04-15 20:56:27 +0200414 return 1;
Willy Tarreau6996e152007-04-30 14:37:43 +0200415 out_poll:
416 /* The connection is still pending. We'll have to poll it
417 * before attempting to go further. */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100418 fdtab[fd].ev &= ~FD_POLL_OUT;
Willy Tarreau6996e152007-04-30 14:37:43 +0200419 return 0;
420 out_error:
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100421 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200422 fdtab[fd].state = FD_STERROR;
423 goto out_wakeup;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424}
425
426
427/*
Willy Tarreauf3c69202006-07-09 16:42:34 +0200428 * This function is used only for server health-checks. It handles the server's
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100429 * reply to an HTTP request or SSL HELLO. It sets s->result to SRV_CHK_RUNNING
430 * if an HTTP server replies HTTP 2xx or 3xx (valid responses), if an SMTP
431 * server returns 2xx, or if an SSL server returns at least 5 bytes in response
432 * to an SSL HELLO (the principle is that this is enough to distinguish between
433 * an SSL server and a pure TCP relay). All other cases will set s->result to
434 * SRV_CHK_ERROR. The function returns 0 if it needs to be called again after
435 * some polling, otherwise non-zero..
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 */
Willy Tarreau83749182007-04-15 20:56:27 +0200437static int event_srv_chk_r(int fd)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438{
Willy Tarreau83749182007-04-15 20:56:27 +0200439 __label__ out_wakeup;
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100440 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441 struct task *t = fdtab[fd].owner;
442 struct server *s = t->context;
443 int skerr;
444 socklen_t lskerr = sizeof(skerr);
445
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100446 len = -1;
Willy Tarreau83749182007-04-15 20:56:27 +0200447
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100448 if (unlikely((s->result & SRV_CHK_ERROR) ||
449 (fdtab[fd].state == FD_STERROR) ||
Willy Tarreau83749182007-04-15 20:56:27 +0200450 (fdtab[fd].ev & FD_POLL_ERR) ||
451 (getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) ||
452 (skerr != 0))) {
453 /* in case of TCP only, this tells us if the connection failed */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100454 s->result |= SRV_CHK_ERROR;
Willy Tarreau83749182007-04-15 20:56:27 +0200455 goto out_wakeup;
456 }
457
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458#ifndef MSG_NOSIGNAL
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200459 len = recv(fd, trash, sizeof(trash), 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460#else
Willy Tarreau83749182007-04-15 20:56:27 +0200461 /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available
462 * but the connection was closed on the remote end. Fortunately, recv still
463 * works correctly and we don't need to do the getsockopt() on linux.
464 */
Krzysztof Oledzki6b3f8b42007-10-11 18:41:08 +0200465 len = recv(fd, trash, sizeof(trash), MSG_NOSIGNAL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466#endif
Willy Tarreau83749182007-04-15 20:56:27 +0200467 if (unlikely(len < 0 && errno == EAGAIN)) {
468 /* we want some polling to happen first */
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100469 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200470 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200471 }
472
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100473 /* Note: the response will only be accepted if read at once */
474 if (s->proxy->options & PR_O_HTTP_CHK) {
475 /* Check if the server speaks HTTP 1.X */
476 if ((len < strlen("HTTP/1.0 000\r")) ||
477 (memcmp(trash, "HTTP/1.", 7) != 0)) {
478 s->result |= SRV_CHK_ERROR;
479 goto out_wakeup;
480 }
481
482 /* check the reply : HTTP/1.X 2xx and 3xx are OK */
483 if (trash[9] == '2' || trash[9] == '3')
484 s->result |= SRV_CHK_RUNNING;
Willy Tarreau48494c02007-11-30 10:41:39 +0100485 else if ((s->proxy->options & PR_O_DISABLE404) &&
486 (s->state & SRV_RUNNING) &&
487 (memcmp(&trash[9], "404", 3) == 0)) {
488 /* 404 may be accepted as "stopping" only if the server was up */
489 s->result |= SRV_CHK_RUNNING | SRV_CHK_DISABLE;
490 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100491 else
492 s->result |= SRV_CHK_ERROR;
493 }
494 else if (s->proxy->options & PR_O_SSL3_CHK) {
495 /* Check for SSLv3 alert or handshake */
496 if ((len >= 5) && (trash[0] == 0x15 || trash[0] == 0x16))
497 s->result |= SRV_CHK_RUNNING;
498 else
499 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200500 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100501 else if (s->proxy->options & PR_O_SMTP_CHK) {
502 /* Check for SMTP code 2xx (should be 250) */
503 if ((len >= 3) && (trash[0] == '2'))
504 s->result |= SRV_CHK_RUNNING;
505 else
506 s->result |= SRV_CHK_ERROR;
Willy Tarreau6996e152007-04-30 14:37:43 +0200507 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100508 else {
509 /* other checks are valid if the connection succeeded anyway */
510 s->result |= SRV_CHK_RUNNING;
Willy Tarreau23677902007-05-08 23:50:35 +0200511 }
Willy Tarreau83749182007-04-15 20:56:27 +0200512
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100513 out_wakeup:
514 if (s->result & SRV_CHK_ERROR)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515 fdtab[fd].state = FD_STERROR;
516
Willy Tarreauf161a342007-04-08 16:59:42 +0200517 EV_FD_CLR(fd, DIR_RD);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200518 task_wakeup(t);
Willy Tarreaud6f087e2008-01-18 17:20:13 +0100519 fdtab[fd].ev &= ~FD_POLL_IN;
Willy Tarreau83749182007-04-15 20:56:27 +0200520 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521}
522
523/*
524 * manages a server health-check. Returns
525 * the time the task accepts to wait, or TIME_ETERNITY for infinity.
526 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200527void process_chk(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528{
Willy Tarreau7317eb52007-05-09 00:54:10 +0200529 __label__ new_chk, out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530 struct server *s = t->context;
531 struct sockaddr_in sa;
532 int fd;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200533 int rv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200534
535 //fprintf(stderr, "process_chk: task=%p\n", t);
536
537 new_chk:
538 fd = s->curfd;
539 if (fd < 0) { /* no check currently running */
540 //fprintf(stderr, "process_chk: 2\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200541 if (!tick_is_expired(t->expire, now_ms)) { /* not good time yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200543 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200544 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545 }
546
547 /* we don't send any health-checks when the proxy is stopped or when
548 * the server should not be checked.
549 */
550 if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200551 while (tick_is_expired(t->expire, now_ms))
552 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200554 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200555 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556 }
557
558 /* we'll initiate a new check */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100559 s->result = SRV_CHK_UNKNOWN; /* no result yet */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200560 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) {
561 if ((fd < global.maxsock) &&
562 (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) &&
563 (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
564 //fprintf(stderr, "process_chk: 3\n");
565
Willy Tarreau9edd1612007-10-18 18:07:48 +0200566 if (s->proxy->options & PR_O_TCP_NOLING) {
567 /* We don't want to useless data */
568 setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
569 }
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200570
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200571 if (s->check_addr.sin_addr.s_addr)
572 /* we'll connect to the check addr specified on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200573 sa = s->check_addr;
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200574 else
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200575 /* we'll connect to the addr on the server */
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200576 sa = s->addr;
Willy Tarreau0f03c6f2007-03-25 20:46:19 +0200577
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 /* we'll connect to the check port on the server */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 sa.sin_port = htons(s->check_port);
580
581 /* allow specific binding :
582 * - server-specific at first
583 * - proxy-specific next
584 */
585 if (s->state & SRV_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100586 struct sockaddr_in *remote = NULL;
587 int ret, flags = 0;
Willy Tarreau163c5322006-11-14 16:18:41 +0100588
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100589#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100590 if ((s->state & SRV_TPROXY_MASK) == SRV_TPROXY_ADDR) {
591 remote = (struct sockaddr_in *)&s->tproxy_addr;
592 flags = 3;
593 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100594#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100595 ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote);
596 if (ret) {
597 s->result |= SRV_CHK_ERROR;
598 switch (ret) {
599 case 1:
600 Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n",
601 s->proxy->id, s->id);
602 break;
603 case 2:
Willy Tarreau163c5322006-11-14 16:18:41 +0100604 Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n",
605 s->proxy->id, s->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100606 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100607 }
608 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200609 }
610 else if (s->proxy->options & PR_O_BIND_SRC) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100611 struct sockaddr_in *remote = NULL;
612 int ret, flags = 0;
613
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100614#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau163c5322006-11-14 16:18:41 +0100615 if ((s->proxy->options & PR_O_TPXY_MASK) == PR_O_TPXY_ADDR) {
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100616 remote = (struct sockaddr_in *)&s->proxy->tproxy_addr;
617 flags = 3;
618 }
Willy Tarreaucf1d5722008-02-14 20:28:18 +0100619#endif
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100620 ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote);
621 if (ret) {
622 s->result |= SRV_CHK_ERROR;
623 switch (ret) {
624 case 1:
625 Alert("Cannot bind to source address before connect() for %s '%s'. Aborting.\n",
626 proxy_type_str(s->proxy), s->proxy->id);
627 break;
628 case 2:
Willy Tarreau2b5652f2006-12-31 17:46:05 +0100629 Alert("Cannot bind to tproxy source address before connect() for %s '%s'. Aborting.\n",
630 proxy_type_str(s->proxy), s->proxy->id);
Willy Tarreaue8c66af2008-01-13 18:40:14 +0100631 break;
Willy Tarreau163c5322006-11-14 16:18:41 +0100632 }
633 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 }
635
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100636 if (s->result == SRV_CHK_UNKNOWN) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
638 /* OK, connection in progress or established */
639
640 //fprintf(stderr, "process_chk: 4\n");
641
642 s->curfd = fd; /* that's how we know a test is in progress ;-) */
Willy Tarreau7a966482007-04-15 10:58:02 +0200643 fd_insert(fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200644 fdtab[fd].owner = t;
Willy Tarreau54469402006-07-29 16:59:06 +0200645 fdtab[fd].cb[DIR_RD].f = &event_srv_chk_r;
646 fdtab[fd].cb[DIR_RD].b = NULL;
647 fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
648 fdtab[fd].cb[DIR_WR].b = NULL;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200649 fdtab[fd].peeraddr = (struct sockaddr *)&sa;
650 fdtab[fd].peerlen = sizeof(sa);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651 fdtab[fd].state = FD_STCONN; /* connection in progress */
Willy Tarreauf161a342007-04-08 16:59:42 +0200652 EV_FD_SET(fd, DIR_WR); /* for connect status */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200653#ifdef DEBUG_FULL
Willy Tarreauf161a342007-04-08 16:59:42 +0200654 assert (!EV_FD_ISSET(fd, DIR_RD));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655#endif
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100656 //fprintf(stderr, "process_chk: 4+, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
657 /* we allow up to min(inter, timeout.connect) for a connection
658 * to establish but only when timeout.check is set
659 * as it may be to short for a full check otherwise
660 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200661 t->expire = tick_add(now_ms, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100662
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200663 if (s->proxy->timeout.check && s->proxy->timeout.connect) {
664 int t_con = tick_add(now_ms, s->proxy->timeout.connect);
665 t->expire = tick_first(t->expire, t_con);
Willy Tarreau60548192008-02-17 11:34:10 +0100666 }
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100667
Willy Tarreaubaaee002006-06-26 02:48:02 +0200668 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200669 *next = t->expire;
Willy Tarreau8eee9c82007-05-14 03:40:11 +0200670 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200671 }
672 else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) {
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100673 s->result |= SRV_CHK_ERROR; /* a real error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 }
675 }
676 }
677 close(fd); /* socket creation error */
678 }
679
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100680 if (s->result == SRV_CHK_UNKNOWN) { /* nothing done */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681 //fprintf(stderr, "process_chk: 6\n");
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200682 while (tick_is_expired(t->expire, now_ms))
683 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684 goto new_chk; /* may be we should initialize a new check */
685 }
686
687 /* here, we have seen a failure */
688 if (s->health > s->rise) {
689 s->health--; /* still good */
690 s->failed_checks++;
691 }
692 else
693 set_server_down(s);
694
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100695 //fprintf(stderr, "process_chk: 7, %lu\n", __tv_to_ms(&s->proxy->timeout.connect));
696 /* we allow up to min(inter, timeout.connect) for a connection
697 * to establish but only when timeout.check is set
698 * as it may be to short for a full check otherwise
699 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200700 while (tick_is_expired(t->expire, now_ms)) {
701 int t_con;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100702
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200703 t_con = tick_add(t->expire, s->proxy->timeout.connect);
704 t->expire = tick_add(t->expire, MS_TO_TICKS(s->inter));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100705
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200706 if (s->proxy->timeout.check)
707 t->expire = tick_first(t->expire, t_con);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100708 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 goto new_chk;
710 }
711 else {
712 //fprintf(stderr, "process_chk: 8\n");
713 /* there was a test running */
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100714 if ((s->result & (SRV_CHK_ERROR|SRV_CHK_RUNNING)) == SRV_CHK_RUNNING) { /* good server detected */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715 //fprintf(stderr, "process_chk: 9\n");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200716
Willy Tarreau9909fc12007-11-30 17:42:05 +0100717 if (s->state & SRV_WARMINGUP) {
718 if (now.tv_sec < s->last_change || now.tv_sec >= s->last_change + s->slowstart) {
719 s->state &= ~SRV_WARMINGUP;
720 if (s->proxy->lbprm.algo & BE_LB_PROP_DYN)
721 s->eweight = s->uweight * BE_WEIGHT_SCALE;
722 if (s->proxy->lbprm.update_server_eweight)
723 s->proxy->lbprm.update_server_eweight(s);
724 }
725 else if (s->proxy->lbprm.algo & BE_LB_PROP_DYN) {
726 /* for dynamic algorithms, let's update the weight */
Willy Tarreau5542af62007-12-03 02:04:00 +0100727 s->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - s->last_change) +
728 s->slowstart - 1) / s->slowstart;
Willy Tarreau9909fc12007-11-30 17:42:05 +0100729 s->eweight *= s->uweight;
730 if (s->proxy->lbprm.update_server_eweight)
731 s->proxy->lbprm.update_server_eweight(s);
732 }
733 /* probably that we can refill this server with a bit more connections */
734 check_for_pending(s);
735 }
736
Willy Tarreau48494c02007-11-30 10:41:39 +0100737 /* we may have to add/remove this server from the LB group */
738 if ((s->state & SRV_RUNNING) && (s->proxy->options & PR_O_DISABLE404)) {
739 if ((s->state & SRV_GOINGDOWN) &&
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100740 ((s->result & (SRV_CHK_RUNNING|SRV_CHK_DISABLE)) == SRV_CHK_RUNNING))
741 set_server_enabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100742 else if (!(s->state & SRV_GOINGDOWN) &&
743 ((s->result & (SRV_CHK_RUNNING | SRV_CHK_DISABLE)) ==
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100744 (SRV_CHK_RUNNING | SRV_CHK_DISABLE)))
745 set_server_disabled(s);
Willy Tarreau48494c02007-11-30 10:41:39 +0100746 }
747
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200748 if (s->health < s->rise + s->fall - 1) {
749 s->health++; /* was bad, stays for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100751 set_server_up(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752 }
753 s->curfd = -1; /* no check running anymore */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 fd_delete(fd);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200755
756 rv = 0;
757 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100758 rv = srv_getinter(s) * global.spread_checks / 100;
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200759 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100760 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv);
Willy Tarreau44ec0f02007-10-14 23:47:04 +0200761 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200762 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 goto new_chk;
764 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200765 else if ((s->result & SRV_CHK_ERROR) || tick_is_expired(t->expire, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766 //fprintf(stderr, "process_chk: 10\n");
767 /* failure or timeout detected */
768 if (s->health > s->rise) {
769 s->health--; /* still good */
770 s->failed_checks++;
771 }
772 else
773 set_server_down(s);
774 s->curfd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775 fd_delete(fd);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200776
777 rv = 0;
778 if (global.spread_checks > 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100779 rv = srv_getinter(s) * global.spread_checks / 100;
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200780 rv -= (int) (2 * rv * (rand() / (RAND_MAX + 1.0)));
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100781 //fprintf(stderr, "process_chk(%p): (%d+/-%d%%) random=%d\n", s, srv_getinter(s), global.spread_checks, rv);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200782 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200783 t->expire = tick_add(now_ms, MS_TO_TICKS(srv_getinter(s) + rv));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784 goto new_chk;
785 }
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100786 /* if result is unknown and there's no timeout, we have to wait again */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 }
788 //fprintf(stderr, "process_chk: 11\n");
Willy Tarreauc7dd71a2007-11-30 08:33:21 +0100789 s->result = SRV_CHK_UNKNOWN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200790 task_queue(t); /* restore t to its place in the task list */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200791 *next = t->expire;
Willy Tarreau7317eb52007-05-09 00:54:10 +0200792 out:
Willy Tarreaud825eef2007-05-12 22:35:00 +0200793 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794}
795
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200796/*
797 * Start health-check.
798 * Returns 0 if OK, -1 if error, and prints the error in this case.
799 */
800int start_checks() {
801
802 struct proxy *px;
803 struct server *s;
804 struct task *t;
805 int nbchk=0, mininter=0, srvpos=0;
806
Willy Tarreau2c43a1e2007-10-14 23:05:39 +0200807 /* 1- count the checkers to run simultaneously.
808 * We also determine the minimum interval among all of those which
809 * have an interval larger than SRV_CHK_INTER_THRES. This interval
810 * will be used to spread their start-up date. Those which have
811 * a shorter interval will start independantly and will not dictate
812 * too short an interval for all others.
813 */
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200814 for (px = proxy; px; px = px->next) {
815 for (s = px->srv; s; s = s->next) {
816 if (!(s->state & SRV_CHECKED))
817 continue;
818
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100819 if ((srv_getinter(s) >= SRV_CHK_INTER_THRES) &&
820 (!mininter || mininter > srv_getinter(s)))
821 mininter = srv_getinter(s);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200822
823 nbchk++;
824 }
825 }
826
827 if (!nbchk)
828 return 0;
829
830 srand((unsigned)time(NULL));
831
832 /*
833 * 2- start them as far as possible from each others. For this, we will
834 * start them after their interval set to the min interval divided by
835 * the number of servers, weighted by the server's position in the list.
836 */
837 for (px = proxy; px; px = px->next) {
838 for (s = px->srv; s; s = s->next) {
839 if (!(s->state & SRV_CHECKED))
840 continue;
841
842 if ((t = pool_alloc2(pool2_task)) == NULL) {
843 Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
844 return -1;
845 }
846
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200847 s->check = t;
848
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200849 task_init(t);
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200850 t->process = process_chk;
851 t->context = s;
852
853 /* check this every ms */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200854 t->expire = tick_add(now_ms,
855 MS_TO_TICKS(((mininter && mininter >= srv_getinter(s)) ?
856 mininter : srv_getinter(s)) * srvpos / nbchk));
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200857 task_queue(t);
858
859 srvpos++;
860 }
861 }
862 return 0;
863}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864
865/*
866 * Local variables:
867 * c-indent-level: 8
868 * c-basic-offset: 8
869 * End:
870 */