1 /* Copyright 1999-2006 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* Load balancer module for Apache proxy */
21 #include "mod_proxy.h"
23 #include "apr_version.h"
24 #include "apr_hooks.h"
27 #include <unistd.h> /* for getpid() */
30 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
32 static int proxy_balancer_canon(request_rec *r, char *url)
34 char *host, *path, *search;
38 if (strncasecmp(url, "balancer:", 9) == 0) {
45 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
46 "proxy: BALANCER: canonicalising URL %s", url);
49 * We break the URL into host, port, path, search
51 err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
53 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
54 "error parsing URL %s: %s",
56 return HTTP_BAD_REQUEST;
58 /* now parse path/search args, according to rfc1738 */
59 /* N.B. if this isn't a true proxy request, then the URL _path_
60 * has already been decoded. True proxy requests have r->uri
61 * == r->unparsed_uri, and no others have that property.
63 if (r->uri == r->unparsed_uri) {
64 search = strchr(url, '?');
72 path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0, r->proxyreq);
74 return HTTP_BAD_REQUEST;
76 r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
77 "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
79 r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
84 static int init_balancer_members(proxy_server_conf *conf, server_rec *s,
85 proxy_balancer *balancer)
88 proxy_worker *workers;
90 workers = (proxy_worker *)balancer->workers->elts;
92 for (i = 0; i < balancer->workers->nelts; i++) {
93 ap_proxy_initialize_worker_share(conf, workers, s);
94 ap_proxy_initialize_worker(workers, s);
98 workers = (proxy_worker *)balancer->workers->elts;
99 for (i = 0; i < balancer->workers->nelts; i++) {
100 /* Set to the original configuration */
101 workers[i].s->lbstatus = workers[i].s->lbfactor =
102 (workers[i].lbfactor ? workers[i].lbfactor : 1);
104 /* Set default number of attempts to the number of
107 if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
108 balancer->max_attempts = balancer->workers->nelts - 1;
109 balancer->max_attempts_set = 1;
114 /* Retrieve the parameter with the given name
115 * Something like 'JSESSIONID=12345...N'
117 static char *get_path_param(apr_pool_t *pool, char *url,
122 for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
123 path += strlen(name);
126 * Session path was found, get it's value
131 path = apr_pstrdup(pool, path);
132 if ((q = strchr(path, '?')))
141 static char *get_cookie_param(request_rec *r, const char *name)
144 const char *start_cookie;
146 if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
147 for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
148 start_cookie = ap_strstr_c(start_cookie + 1, name)) {
149 if (start_cookie == cookies ||
150 start_cookie[-1] == ';' ||
151 start_cookie[-1] == ',' ||
152 isspace(start_cookie[-1])) {
154 start_cookie += strlen(name);
155 while(*start_cookie && isspace(*start_cookie))
157 if (*start_cookie == '=' && start_cookie[1]) {
159 * Session cookie was found, get it's value
161 char *end_cookie, *cookie;
163 cookie = apr_pstrdup(r->pool, start_cookie);
164 if ((end_cookie = strchr(cookie, ';')) != NULL)
166 if((end_cookie = strchr(cookie, ',')) != NULL)
176 /* Find the worker that has the 'route' defined
178 static proxy_worker *find_route_worker(proxy_balancer *balancer,
182 proxy_worker *worker = (proxy_worker *)balancer->workers->elts;
183 for (i = 0; i < balancer->workers->nelts; i++) {
184 if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
192 static proxy_worker *find_session_route(proxy_balancer *balancer,
197 proxy_worker *worker = NULL;
199 if (!balancer->sticky)
201 /* Try to find the sticky route inside url */
202 *route = get_path_param(r->pool, *url, balancer->sticky);
204 *route = get_cookie_param(r, balancer->sticky);
205 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
206 "proxy: BALANCER: Found value %s for "
207 "stickysession %s", *route, balancer->sticky);
209 * If we found a value for sticksession, find the first '.' within.
210 * Everything after '.' (if present) is our route.
212 if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
214 if ((*route) && (**route)) {
215 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
216 "proxy: BALANCER: Found route %s", *route);
217 /* We have a route in path or in cookie
218 * Find the worker that has this route defined.
220 worker = find_route_worker(balancer, *route);
221 if (worker && !PROXY_WORKER_IS_USABLE(worker)) {
222 /* We have a worker that is unusable.
223 * It can be in error or disabled, but in case
224 * it has a redirection set use that redirection worker.
225 * This enables to safely remove the member from the
226 * balancer. Of course you will need a some kind of
227 * session replication between those two remote.
229 if (*worker->s->redirect)
230 worker = find_route_worker(balancer, worker->s->redirect);
231 /* Check if the redirect worker is usable */
232 if (worker && !PROXY_WORKER_IS_USABLE(worker))
241 static proxy_worker *find_best_worker(proxy_balancer *balancer,
244 proxy_worker *candidate = NULL;
246 if (PROXY_THREAD_LOCK(balancer) != APR_SUCCESS)
249 candidate = (*balancer->lbmethod->finder)(balancer, r);
252 PROXY_THREAD_UNLOCK(balancer);
256 PROXY_THREAD_UNLOCK(balancer);
258 if (candidate == NULL) {
259 /* All the workers are in error state or disabled.
260 * If the balancer has a timeout sleep for a while
261 * and try again to find the worker. The chances are
262 * that some other thread will release a connection.
263 * By default the timeout is not set, and the server
264 * returns SERVER_BUSY.
267 if (balancer->timeout) {
268 /* XXX: This can perhaps be build using some
269 * smarter mechanism, like tread_cond.
270 * But since the statuses can came from
271 * different childs, use the provided algo.
273 apr_interval_time_t timeout = balancer->timeout;
274 apr_interval_time_t step, tval = 0;
275 /* Set the timeout to 0 so that we don't
276 * end in infinite loop
278 balancer->timeout = 0;
279 step = timeout / 100;
280 while (tval < timeout) {
283 if ((candidate = find_best_worker(balancer, r)))
287 /* restore the timeout */
288 balancer->timeout = timeout;
295 static int rewrite_url(request_rec *r, proxy_worker *worker,
298 const char *scheme = strstr(*url, "://");
299 const char *path = NULL;
302 path = ap_strchr_c(scheme + 3, '/');
304 /* we break the URL into host, port, uri */
306 return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
307 "missing worker. URI cannot be parsed: ", *url,
311 *url = apr_pstrcat(r->pool, worker->name, path, NULL);
316 static int proxy_balancer_pre_request(proxy_worker **worker,
317 proxy_balancer **balancer,
319 proxy_server_conf *conf, char **url)
322 proxy_worker *runtime;
327 /* Step 1: check if the url is for us
328 * The url we can handle starts with 'balancer://'
329 * If balancer is already provided skip the search
330 * for balancer, because this is failover attempt.
333 !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
336 /* Step 2: find the session route */
338 runtime = find_session_route(*balancer, r, &route, url);
339 /* Lock the LoadBalancer
340 * XXX: perhaps we need the process lock here
342 if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
343 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
344 "proxy: BALANCER: lock");
348 int i, total_factor = 0;
349 proxy_worker *workers;
350 /* We have a sticky load balancer
351 * Update the workers status
352 * so that even session routes get
355 workers = (proxy_worker *)(*balancer)->workers->elts;
356 for (i = 0; i < (*balancer)->workers->nelts; i++) {
357 /* Take into calculation only the workers that are
358 * not in error state or not disabled.
360 if (PROXY_WORKER_IS_USABLE(workers)) {
361 workers->s->lbstatus += workers->s->lbfactor;
362 total_factor += workers->s->lbfactor;
366 runtime->s->lbstatus -= total_factor;
367 runtime->s->elected++;
371 else if (route && (*balancer)->sticky_force) {
372 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
373 "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
374 (*balancer)->name, route);
375 PROXY_THREAD_UNLOCK(*balancer);
376 return HTTP_SERVICE_UNAVAILABLE;
379 PROXY_THREAD_UNLOCK(*balancer);
381 runtime = find_best_worker(*balancer, r);
383 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
384 "proxy: BALANCER: (%s). All workers are in error state",
387 return HTTP_SERVICE_UNAVAILABLE;
392 /* Rewrite the url from 'balancer://url'
393 * to the 'worker_scheme://worker_hostname[:worker_port]/url'
394 * This replaces the balancers fictional name with the
395 * real hostname of the elected worker.
397 access_status = rewrite_url(r, *worker, url);
398 /* Add the session route to request notes if present */
400 apr_table_setn(r->notes, "session-sticky", (*balancer)->sticky);
401 apr_table_setn(r->notes, "session-route", route);
403 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
404 "proxy: BALANCER (%s) worker (%s) rewritten to %s",
405 (*balancer)->name, (*worker)->name, *url);
407 return access_status;
410 static int proxy_balancer_post_request(proxy_worker *worker,
411 proxy_balancer *balancer,
413 proxy_server_conf *conf)
417 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
418 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
419 "proxy: BALANCER: lock");
420 return HTTP_INTERNAL_SERVER_ERROR;
422 /* TODO: calculate the bytes transferred
423 * This will enable to elect the worker that has
425 * The bytes transferred depends on the protocol
426 * used, so each protocol handler should keep the
430 PROXY_THREAD_UNLOCK(balancer);
431 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
432 "proxy_balancer_post_request for (%s)", balancer->name);
437 static void recalc_factors(proxy_balancer *balancer)
440 proxy_worker *workers;
443 /* Recalculate lbfactors */
444 workers = (proxy_worker *)balancer->workers->elts;
445 /* Special case if there is only one worker it's
446 * load factor will always be 1
448 if (balancer->workers->nelts == 1) {
449 workers->s->lbstatus = workers->s->lbfactor = 1;
452 for (i = 0; i < balancer->workers->nelts; i++) {
453 /* Update the status entries */
454 workers[i].s->lbstatus = workers[i].s->lbfactor;
458 /* Manages the loadfactors and member status
460 static int balancer_handler(request_rec *r)
462 void *sconf = r->server->module_config;
463 proxy_server_conf *conf = (proxy_server_conf *)
464 ap_get_module_config(sconf, &proxy_module);
465 proxy_balancer *balancer, *bsel = NULL;
466 proxy_worker *worker, *wsel = NULL;
467 apr_table_t *params = apr_table_make(r->pool, 10);
472 /* is this for us? */
473 if (strcmp(r->handler, "balancer-manager"))
475 r->allowed = (AP_METHOD_BIT << M_GET);
476 if (r->method_number != M_GET)
480 char *args = apr_pstrdup(r->pool, r->args);
482 while (args && *args) {
483 if ((val = ap_strchr(args, '='))) {
485 if ((tok = ap_strchr(val, '&')))
488 * Special case: workers are allowed path information
490 if ((access_status = ap_unescape_url(val)) != OK)
491 if (strcmp(args, "w") || (access_status != HTTP_NOT_FOUND))
492 return access_status;
493 apr_table_setn(params, args, val);
497 return HTTP_BAD_REQUEST;
500 if ((name = apr_table_get(params, "b")))
501 bsel = ap_proxy_get_balancer(r->pool, conf,
502 apr_pstrcat(r->pool, "balancer://", name, NULL));
503 if ((name = apr_table_get(params, "w"))) {
506 ws = ap_proxy_get_worker(r->pool, conf, name);
508 worker = (proxy_worker *)bsel->workers->elts;
509 for (n = 0; n < bsel->workers->nelts; n++) {
510 if (strcasecmp(worker->name, ws->name) == 0) {
518 /* First set the params */
521 if ((val = apr_table_get(params, "ss"))) {
523 bsel->sticky = apr_pstrdup(conf->pool, val);
527 if ((val = apr_table_get(params, "tm"))) {
528 int ival = atoi(val);
530 bsel->timeout = apr_time_from_sec(ival);
532 if ((val = apr_table_get(params, "fa"))) {
533 int ival = atoi(val);
535 bsel->max_attempts = ival;
536 bsel->max_attempts_set = 1;
538 if ((val = apr_table_get(params, "lm"))) {
539 proxy_balancer_method *provider;
540 provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
542 bsel->lbmethod = provider;
548 if ((val = apr_table_get(params, "lf"))) {
549 int ival = atoi(val);
550 if (ival >= 1 && ival <= 100) {
551 wsel->s->lbfactor = ival;
553 recalc_factors(bsel);
556 if ((val = apr_table_get(params, "wr"))) {
557 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
558 strcpy(wsel->s->route, val);
560 *wsel->s->route = '\0';
562 if ((val = apr_table_get(params, "rr"))) {
563 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
564 strcpy(wsel->s->redirect, val);
566 *wsel->s->redirect = '\0';
568 if ((val = apr_table_get(params, "dw"))) {
569 if (!strcasecmp(val, "Disable"))
570 wsel->s->status |= PROXY_WORKER_DISABLED;
571 else if (!strcasecmp(val, "Enable"))
572 wsel->s->status &= ~PROXY_WORKER_DISABLED;
576 if (apr_table_get(params, "xml")) {
577 ap_set_content_type(r, "text/xml");
578 ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
579 ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
580 ap_rputs(" <httpd:balancers>\n", r);
581 balancer = (proxy_balancer *)conf->balancers->elts;
582 for (i = 0; i < conf->balancers->nelts; i++) {
583 ap_rputs(" <httpd:balancer>\n", r);
584 ap_rvputs(r, " <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
585 ap_rputs(" <httpd:workers>\n", r);
586 worker = (proxy_worker *)balancer->workers->elts;
587 for (n = 0; n < balancer->workers->nelts; n++) {
588 ap_rputs(" <httpd:worker>\n", r);
589 ap_rvputs(r, " <httpd:scheme>", worker->scheme,
590 "</httpd:scheme>\n", NULL);
591 ap_rvputs(r, " <httpd:hostname>", worker->hostname,
592 "</httpd:hostname>\n", NULL);
593 ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
594 worker->s->lbfactor);
595 ap_rputs(" </httpd:worker>\n", r);
598 ap_rputs(" </httpd:workers>\n", r);
599 ap_rputs(" </httpd:balancer>\n", r);
602 ap_rputs(" </httpd:balancers>\n", r);
603 ap_rputs("</httpd:manager>", r);
606 ap_set_content_type(r, "text/html");
607 ap_rputs(DOCTYPE_HTML_3_2
608 "<html><head><title>Balancer Manager</title></head>\n", r);
609 ap_rputs("<body><h1>Load Balancer Manager for ", r);
610 ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
611 ap_rvputs(r, "<dl><dt>Server Version: ",
612 ap_get_server_version(), "</dt>\n", NULL);
613 ap_rvputs(r, "<dt>Server Built: ",
614 ap_get_server_built(), "\n</dt></dl>\n", NULL);
615 balancer = (proxy_balancer *)conf->balancers->elts;
616 for (i = 0; i < conf->balancers->nelts; i++) {
618 ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
619 ap_rvputs(r, "<a href=\"", r->uri, "?b=",
620 balancer->name + sizeof("balancer://") - 1,
622 ap_rvputs(r, balancer->name, "</a></h3>\n\n", NULL);
623 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
624 "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
626 ap_rvputs(r, "<td>", balancer->sticky, NULL);
627 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
628 apr_time_sec(balancer->timeout));
629 ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
630 ap_rprintf(r, "<td>%s</td>\n",
631 balancer->lbmethod->name);
632 ap_rputs("</table>\n<br />", r);
633 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
634 "<th>Worker URL</th>"
635 "<th>Route</th><th>RouteRedir</th>"
636 "<th>Factor</th><th>Status</th>"
639 worker = (proxy_worker *)balancer->workers->elts;
640 for (n = 0; n < balancer->workers->nelts; n++) {
642 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
643 balancer->name + sizeof("balancer://") - 1, "&w=",
644 ap_escape_uri(r->pool, worker->name),
646 ap_rvputs(r, worker->name, "</a></td>", NULL);
647 ap_rvputs(r, "<td>", worker->s->route, NULL);
648 ap_rvputs(r, "</td><td>", worker->s->redirect, NULL);
649 ap_rprintf(r, "</td><td>%d</td><td>", worker->s->lbfactor);
650 if (worker->s->status & PROXY_WORKER_DISABLED)
652 else if (worker->s->status & PROXY_WORKER_IN_ERROR)
654 else if (worker->s->status & PROXY_WORKER_INITIALIZED)
658 ap_rputs("</td></tr>\n", r);
662 ap_rputs("</table>\n", r);
665 ap_rputs("<hr />\n", r);
667 ap_rputs("<h3>Edit worker settings for ", r);
668 ap_rvputs(r, wsel->name, "</h3>\n", NULL);
669 ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
670 ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
671 ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
672 ap_rprintf(r, "value=\"%d\"></td><tr>\n", wsel->s->lbfactor);
673 ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
674 ap_rvputs(r, "value=\"", wsel->route, NULL);
675 ap_rputs("\"></td><tr>\n", r);
676 ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
677 ap_rvputs(r, "value=\"", wsel->redirect, NULL);
678 ap_rputs("\"></td><tr>\n", r);
679 ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
680 if (wsel->s->status & PROXY_WORKER_DISABLED)
681 ap_rputs(" checked", r);
682 ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
683 if (!(wsel->s->status & PROXY_WORKER_DISABLED))
684 ap_rputs(" checked", r);
685 ap_rputs("></td><tr>\n", r);
686 ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
687 ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ", NULL);
688 ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
689 ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
690 ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
691 "\">\n</form>\n", NULL);
692 ap_rputs("<hr />\n", r);
695 ap_rputs("<h3>Edit balancer settings for ", r);
696 ap_rvputs(r, bsel->name, "</h3>\n", NULL);
697 ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
698 ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
699 ap_rputs("<table><tr><td>StickySession Identifier:</td><td><input name=\"ss\" type=text ", r);
701 ap_rvputs(r, "value=\"", bsel->sticky, "\"", NULL);
702 ap_rputs("></td><tr>\n<tr><td>Timeout:</td><td><input name=\"tm\" type=text ", r);
703 ap_rprintf(r, "value=\"%" APR_TIME_T_FMT "\"></td></tr>\n",
704 apr_time_sec(bsel->timeout));
705 ap_rputs("<tr><td>Failover Attempts:</td><td><input name=\"fa\" type=text ", r);
706 ap_rprintf(r, "value=\"%d\"></td></tr>\n",
708 ap_rputs("<tr><td>LB Method:</td><td><select name=\"lm\">", r);
710 apr_array_header_t *methods;
711 ap_list_provider_names_t *method;
713 methods = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
714 method = (ap_list_provider_names_t *)methods->elts;
715 for (i = 0; i < methods->nelts; i++) {
716 ap_rprintf(r, "<option value=\"%s\" %s>%s</option>", method->provider_name,
717 (!strcasecmp(bsel->lbmethod->name, method->provider_name)) ? "selected" : "",
718 method->provider_name);
722 ap_rputs("</select></td></tr>\n", r);
723 ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
724 ap_rvputs(r, "</table>\n<input type=hidden name=\"b\" ", NULL);
725 ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
726 "\">\n</form>\n", NULL);
727 ap_rputs("<hr />\n", r);
729 ap_rputs(ap_psignature("",r), r);
730 ap_rputs("</body></html>\n", r);
735 static void child_init(apr_pool_t *p, server_rec *s)
738 void *sconf = s->module_config;
739 proxy_server_conf *conf;
740 proxy_balancer *balancer;
742 conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
744 /* Initialize shared scoreboard data */
745 balancer = (proxy_balancer *)conf->balancers->elts;
746 for (i = 0; i < conf->balancers->nelts; i++) {
747 init_balancer_members(conf, s, balancer);
756 * The idea behind the find_best_byrequests scheduler is the following:
758 * lbfactor is "how much we expect this worker to work", or "the worker's
759 * normalized work quota".
761 * lbstatus is "how urgent this worker has to work to fulfill its quota
764 * We distribute each worker's work quota to the worker, and then look
765 * which of them needs to work most urgently (biggest lbstatus). This
766 * worker is then selected for work, and its lbstatus reduced by the
767 * total work quota we distributed to all workers. Thus the sum of all
768 * lbstatus does not change.(*)
770 * If some workers are disabled, the others will
771 * still be scheduled correctly.
773 * If a balancer is configured as follows:
776 * lbfactor 25 25 25 25
778 * And b gets disabled, the following schedule is produced:
780 * a c d a c d a c d ...
782 * Note that the above lbfactor setting is the *exact* same as:
787 * Asymmetric configurations work as one would expect. For
793 * would have a, b and c all handling about the same
794 * amount of load with d handling twice what a or b
795 * or c handles individually. So we could see:
797 * b a d c d a c d b d ...
801 static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
805 int total_factor = 0;
806 proxy_worker *worker = (proxy_worker *)balancer->workers->elts;
807 proxy_worker *mycandidate = NULL;
810 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
811 "proxy: Entering byrequests for BALANCER (%s)",
814 /* First try to see if we have available candidate */
815 for (i = 0; i < balancer->workers->nelts; i++) {
816 /* If the worker is in error state run
817 * retry on that worker. It will be marked as
818 * operational if the retry timeout is elapsed.
819 * The worker might still be unusable, but we try
822 if (!PROXY_WORKER_IS_USABLE(worker))
823 ap_proxy_retry_worker("BALANCER", worker, r->server);
824 /* Take into calculation only the workers that are
825 * not in error state or not disabled.
827 if (PROXY_WORKER_IS_USABLE(worker)) {
828 worker->s->lbstatus += worker->s->lbfactor;
829 total_factor += worker->s->lbfactor;
830 if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
831 mycandidate = worker;
837 mycandidate->s->lbstatus -= total_factor;
838 mycandidate->s->elected++;
845 * The idea behind the find_best_bytraffic scheduler is the following:
847 * We know the amount of traffic (bytes in and out) handled by each
848 * worker. We normalize that traffic by each workers' weight. So assuming
854 * the scheduler will allow worker c to handle 3 times the
855 * traffic of a and b. If each request/response results in the
856 * same amount of traffic, then c would be accessed 3 times as
857 * often as a or b. If, for example, a handled a request that
858 * resulted in a large i/o bytecount, then b and c would be
859 * chosen more often, to even things out.
861 static proxy_worker *find_best_bytraffic(proxy_balancer *balancer,
865 apr_off_t mytraffic = 0;
866 apr_off_t curmin = 0;
867 proxy_worker *worker = (proxy_worker *)balancer->workers->elts;
868 proxy_worker *mycandidate = NULL;
870 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
871 "proxy: Entering bytraffic for BALANCER (%s)",
874 /* First try to see if we have available candidate */
875 for (i = 0; i < balancer->workers->nelts; i++) {
876 /* If the worker is in error state run
877 * retry on that worker. It will be marked as
878 * operational if the retry timeout is elapsed.
879 * The worker might still be unusable, but we try
882 if (!PROXY_WORKER_IS_USABLE(worker))
883 ap_proxy_retry_worker("BALANCER", worker, r->server);
884 /* Take into calculation only the workers that are
885 * not in error state or not disabled.
887 if (PROXY_WORKER_IS_USABLE(worker)) {
888 mytraffic = (worker->s->transferred/worker->s->lbfactor) +
889 (worker->s->read/worker->s->lbfactor);
890 if (!mycandidate || mytraffic < curmin) {
891 mycandidate = worker;
899 mycandidate->s->elected++;
906 * How to add additional lbmethods:
907 * 1. Create func which determines "best" candidate worker
908 * (eg: find_best_bytraffic, above)
909 * 2. Register it as a provider.
911 static const proxy_balancer_method byrequests =
914 &find_best_byrequests,
918 static const proxy_balancer_method bytraffic =
921 &find_best_bytraffic,
925 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
927 /* Only the mpm_winnt has child init hook handler.
928 * make sure that we are called after the mpm
929 * initializes and after the mod_proxy
931 static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy.c", NULL};
932 /* manager handler */
933 ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
934 ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
935 proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
936 proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
937 proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
938 ap_register_provider(p, PROXY_LBMETHOD, "bytraffic", "0", &bytraffic);
939 ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
942 module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
943 STANDARD20_MODULE_STUFF,
944 NULL, /* create per-directory config structure */
945 NULL, /* merge per-directory config structures */
946 NULL, /* create per-server config structure */
947 NULL, /* merge per-server config structures */
948 NULL, /* command apr_table_t */
949 ap_proxy_balancer_register_hook /* register hooks */