1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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);
103 workers[i].s->lbset = workers[i].lbset;
105 /* Set default number of attempts to the number of
108 if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
109 balancer->max_attempts = balancer->workers->nelts - 1;
110 balancer->max_attempts_set = 1;
115 /* Retrieve the parameter with the given name
116 * Something like 'JSESSIONID=12345...N'
118 static char *get_path_param(apr_pool_t *pool, char *url,
123 for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
124 path += strlen(name);
127 * Session path was found, get it's value
132 path = apr_strtok(apr_pstrdup(pool, path), "?&", &q);
140 static char *get_cookie_param(request_rec *r, const char *name)
143 const char *start_cookie;
145 if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
146 for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
147 start_cookie = ap_strstr_c(start_cookie + 1, name)) {
148 if (start_cookie == cookies ||
149 start_cookie[-1] == ';' ||
150 start_cookie[-1] == ',' ||
151 isspace(start_cookie[-1])) {
153 start_cookie += strlen(name);
154 while(*start_cookie && isspace(*start_cookie))
156 if (*start_cookie == '=' && start_cookie[1]) {
158 * Session cookie was found, get it's value
160 char *end_cookie, *cookie;
162 cookie = apr_pstrdup(r->pool, start_cookie);
163 if ((end_cookie = strchr(cookie, ';')) != NULL)
165 if((end_cookie = strchr(cookie, ',')) != NULL)
175 /* Find the worker that has the 'route' defined
177 static proxy_worker *find_route_worker(proxy_balancer *balancer,
178 const char *route, request_rec *r)
181 int checking_standby;
184 proxy_worker *worker;
186 checking_standby = checked_standby = 0;
187 while (!checked_standby) {
188 worker = (proxy_worker *)balancer->workers->elts;
189 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
190 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
192 if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
193 if (worker && PROXY_WORKER_IS_USABLE(worker)) {
197 * If the worker is in error state run
198 * retry on that worker. It will be marked as
199 * operational if the retry timeout is elapsed.
200 * The worker might still be unusable, but we try
203 ap_proxy_retry_worker("BALANCER", worker, r->server);
204 if (PROXY_WORKER_IS_USABLE(worker)) {
208 * We have a worker that is unusable.
209 * It can be in error or disabled, but in case
210 * it has a redirection set use that redirection worker.
211 * This enables to safely remove the member from the
212 * balancer. Of course you will need some kind of
213 * session replication between those two remote.
215 if (*worker->s->redirect) {
216 proxy_worker *rworker = NULL;
217 rworker = find_route_worker(balancer, worker->s->redirect, r);
218 /* Check if the redirect worker is usable */
219 if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
221 * If the worker is in error state run
222 * retry on that worker. It will be marked as
223 * operational if the retry timeout is elapsed.
224 * The worker might still be unusable, but we try
227 ap_proxy_retry_worker("BALANCER", rworker, r->server);
229 if (rworker && PROXY_WORKER_IS_USABLE(rworker))
236 checked_standby = checking_standby++;
241 static proxy_worker *find_session_route(proxy_balancer *balancer,
246 proxy_worker *worker = NULL;
248 if (!balancer->sticky)
250 /* Try to find the sticky route inside url */
251 *route = get_path_param(r->pool, *url, balancer->sticky);
253 *route = get_cookie_param(r, balancer->sticky);
254 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
255 "proxy: BALANCER: Found value %s for "
256 "stickysession %s", *route, balancer->sticky);
258 * If we found a value for sticksession, find the first '.' within.
259 * Everything after '.' (if present) is our route.
261 if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
263 if ((*route) && (**route)) {
264 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
265 "proxy: BALANCER: Found route %s", *route);
266 /* We have a route in path or in cookie
267 * Find the worker that has this route defined.
269 worker = find_route_worker(balancer, *route, r);
270 if (worker && strcmp(*route, worker->s->route)) {
272 * Notice that the route of the worker chosen is different from
273 * the route supplied by the client.
275 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
276 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
277 "proxy: BALANCER: Route changed from %s to %s",
278 *route, worker->s->route);
286 static proxy_worker *find_best_worker(proxy_balancer *balancer,
289 proxy_worker *candidate = NULL;
292 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
293 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
294 "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
298 candidate = (*balancer->lbmethod->finder)(balancer, r);
301 candidate->s->elected++;
304 PROXY_THREAD_UNLOCK(balancer);
308 PROXY_THREAD_UNLOCK(balancer);
310 if (candidate == NULL) {
311 /* All the workers are in error state or disabled.
312 * If the balancer has a timeout sleep for a while
313 * and try again to find the worker. The chances are
314 * that some other thread will release a connection.
315 * By default the timeout is not set, and the server
316 * returns SERVER_BUSY.
319 if (balancer->timeout) {
320 /* XXX: This can perhaps be build using some
321 * smarter mechanism, like tread_cond.
322 * But since the statuses can came from
323 * different childs, use the provided algo.
325 apr_interval_time_t timeout = balancer->timeout;
326 apr_interval_time_t step, tval = 0;
327 /* Set the timeout to 0 so that we don't
328 * end in infinite loop
330 balancer->timeout = 0;
331 step = timeout / 100;
332 while (tval < timeout) {
335 if ((candidate = find_best_worker(balancer, r)))
339 /* restore the timeout */
340 balancer->timeout = timeout;
347 static int rewrite_url(request_rec *r, proxy_worker *worker,
350 const char *scheme = strstr(*url, "://");
351 const char *path = NULL;
354 path = ap_strchr_c(scheme + 3, '/');
356 /* we break the URL into host, port, uri */
358 return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
359 "missing worker. URI cannot be parsed: ", *url,
363 *url = apr_pstrcat(r->pool, worker->name, path, NULL);
368 static void force_recovery(proxy_balancer *balancer, server_rec *s)
372 proxy_worker *worker;
374 worker = (proxy_worker *)balancer->workers->elts;
375 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
376 if (!(worker->s->status & PROXY_WORKER_IN_ERROR)) {
382 /* If all workers are in error state force the recovery.
384 worker = (proxy_worker *)balancer->workers->elts;
385 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
386 ++worker->s->retries;
387 worker->s->status &= ~PROXY_WORKER_IN_ERROR;
388 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
389 "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
390 balancer->name, worker->hostname);
395 static int proxy_balancer_pre_request(proxy_worker **worker,
396 proxy_balancer **balancer,
398 proxy_server_conf *conf, char **url)
401 proxy_worker *runtime;
406 /* Step 1: check if the url is for us
407 * The url we can handle starts with 'balancer://'
408 * If balancer is already provided skip the search
409 * for balancer, because this is failover attempt.
412 !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
415 /* Step 2: Lock the LoadBalancer
416 * XXX: perhaps we need the process lock here
418 if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
419 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
420 "proxy: BALANCER: (%s). Lock failed for pre_request",
425 /* Step 3: force recovery */
426 force_recovery(*balancer, r->server);
428 /* Step 4: find the session route */
429 runtime = find_session_route(*balancer, r, &route, url);
431 int i, total_factor = 0;
432 proxy_worker *workers;
433 /* We have a sticky load balancer
434 * Update the workers status
435 * so that even session routes get
438 workers = (proxy_worker *)(*balancer)->workers->elts;
439 for (i = 0; i < (*balancer)->workers->nelts; i++) {
440 /* Take into calculation only the workers that are
441 * not in error state or not disabled.
443 * TODO: Abstract the below, since this is dependent
444 * on the LB implementation
446 if (PROXY_WORKER_IS_USABLE(workers)) {
447 workers->s->lbstatus += workers->s->lbfactor;
448 total_factor += workers->s->lbfactor;
452 runtime->s->lbstatus -= total_factor;
453 runtime->s->elected++;
457 else if (route && (*balancer)->sticky_force) {
458 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
459 "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
460 (*balancer)->name, route);
461 PROXY_THREAD_UNLOCK(*balancer);
462 return HTTP_SERVICE_UNAVAILABLE;
465 PROXY_THREAD_UNLOCK(*balancer);
467 runtime = find_best_worker(*balancer, r);
469 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
470 "proxy: BALANCER: (%s). All workers are in error state",
473 return HTTP_SERVICE_UNAVAILABLE;
475 if ((*balancer)->sticky && runtime) {
477 * This balancer has sticky sessions and the client either has not
478 * supplied any routing information or all workers for this route
479 * including possible redirect and hotstandby workers are in error
480 * state, but we have found another working worker for this
481 * balancer where we can send the request. Thus notice that we have
482 * changed the route to the backend.
484 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
489 /* Add balancer/worker info to env. */
490 apr_table_setn(r->subprocess_env,
491 "BALANCER_NAME", (*balancer)->name);
492 apr_table_setn(r->subprocess_env,
493 "BALANCER_WORKER_NAME", (*worker)->name);
494 apr_table_setn(r->subprocess_env,
495 "BALANCER_WORKER_ROUTE", (*worker)->s->route);
497 /* Rewrite the url from 'balancer://url'
498 * to the 'worker_scheme://worker_hostname[:worker_port]/url'
499 * This replaces the balancers fictional name with the
500 * real hostname of the elected worker.
502 access_status = rewrite_url(r, *worker, url);
503 /* Add the session route to request notes if present */
505 apr_table_setn(r->notes, "session-sticky", (*balancer)->sticky);
506 apr_table_setn(r->notes, "session-route", route);
508 /* Add session info to env. */
509 apr_table_setn(r->subprocess_env,
510 "BALANCER_SESSION_STICKY", (*balancer)->sticky);
511 apr_table_setn(r->subprocess_env,
512 "BALANCER_SESSION_ROUTE", route);
514 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
515 "proxy: BALANCER (%s) worker (%s) rewritten to %s",
516 (*balancer)->name, (*worker)->name, *url);
518 return access_status;
521 static int proxy_balancer_post_request(proxy_worker *worker,
522 proxy_balancer *balancer,
524 proxy_server_conf *conf)
528 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
529 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
530 "proxy: BALANCER: (%s). Lock failed for post_request",
532 return HTTP_INTERNAL_SERVER_ERROR;
534 /* TODO: calculate the bytes transferred
535 * This will enable to elect the worker that has
537 * The bytes transferred depends on the protocol
538 * used, so each protocol handler should keep the
542 PROXY_THREAD_UNLOCK(balancer);
543 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
544 "proxy_balancer_post_request for (%s)", balancer->name);
549 static void recalc_factors(proxy_balancer *balancer)
552 proxy_worker *workers;
555 /* Recalculate lbfactors */
556 workers = (proxy_worker *)balancer->workers->elts;
557 /* Special case if there is only one worker it's
558 * load factor will always be 1
560 if (balancer->workers->nelts == 1) {
561 workers->s->lbstatus = workers->s->lbfactor = 1;
564 for (i = 0; i < balancer->workers->nelts; i++) {
565 /* Update the status entries */
566 workers[i].s->lbstatus = workers[i].s->lbfactor;
570 /* Manages the loadfactors and member status
572 static int balancer_handler(request_rec *r)
574 void *sconf = r->server->module_config;
575 proxy_server_conf *conf = (proxy_server_conf *)
576 ap_get_module_config(sconf, &proxy_module);
577 proxy_balancer *balancer, *bsel = NULL;
578 proxy_worker *worker, *wsel = NULL;
579 apr_table_t *params = apr_table_make(r->pool, 10);
584 /* is this for us? */
585 if (strcmp(r->handler, "balancer-manager"))
587 r->allowed = (AP_METHOD_BIT << M_GET);
588 if (r->method_number != M_GET)
592 char *args = apr_pstrdup(r->pool, r->args);
594 while (args && *args) {
595 if ((val = ap_strchr(args, '='))) {
597 if ((tok = ap_strchr(val, '&')))
600 * Special case: workers are allowed path information
602 if ((access_status = ap_unescape_url(val)) != OK)
603 if (strcmp(args, "w") || (access_status != HTTP_NOT_FOUND))
604 return access_status;
605 apr_table_setn(params, args, val);
609 return HTTP_BAD_REQUEST;
612 if ((name = apr_table_get(params, "b")))
613 bsel = ap_proxy_get_balancer(r->pool, conf,
614 apr_pstrcat(r->pool, "balancer://", name, NULL));
615 if ((name = apr_table_get(params, "w"))) {
618 ws = ap_proxy_get_worker(r->pool, conf, name);
620 worker = (proxy_worker *)bsel->workers->elts;
621 for (n = 0; n < bsel->workers->nelts; n++) {
622 if (strcasecmp(worker->name, ws->name) == 0) {
630 /* First set the params */
633 if ((val = apr_table_get(params, "ss"))) {
635 bsel->sticky = apr_pstrdup(conf->pool, val);
639 if ((val = apr_table_get(params, "tm"))) {
640 int ival = atoi(val);
642 bsel->timeout = apr_time_from_sec(ival);
644 if ((val = apr_table_get(params, "fa"))) {
645 int ival = atoi(val);
647 bsel->max_attempts = ival;
648 bsel->max_attempts_set = 1;
650 if ((val = apr_table_get(params, "lm"))) {
651 proxy_balancer_method *provider;
652 provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
654 bsel->lbmethod = provider;
660 if ((val = apr_table_get(params, "lf"))) {
661 int ival = atoi(val);
662 if (ival >= 1 && ival <= 100) {
663 wsel->s->lbfactor = ival;
665 recalc_factors(bsel);
668 if ((val = apr_table_get(params, "wr"))) {
669 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
670 strcpy(wsel->s->route, val);
672 *wsel->s->route = '\0';
674 if ((val = apr_table_get(params, "rr"))) {
675 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
676 strcpy(wsel->s->redirect, val);
678 *wsel->s->redirect = '\0';
680 if ((val = apr_table_get(params, "dw"))) {
681 if (!strcasecmp(val, "Disable"))
682 wsel->s->status |= PROXY_WORKER_DISABLED;
683 else if (!strcasecmp(val, "Enable"))
684 wsel->s->status &= ~PROXY_WORKER_DISABLED;
686 if ((val = apr_table_get(params, "ls"))) {
687 int ival = atoi(val);
688 if (ival >= 0 && ival <= 99) {
689 wsel->s->lbset = ival;
694 if (apr_table_get(params, "xml")) {
695 ap_set_content_type(r, "text/xml");
696 ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
697 ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
698 ap_rputs(" <httpd:balancers>\n", r);
699 balancer = (proxy_balancer *)conf->balancers->elts;
700 for (i = 0; i < conf->balancers->nelts; i++) {
701 ap_rputs(" <httpd:balancer>\n", r);
702 ap_rvputs(r, " <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
703 ap_rputs(" <httpd:workers>\n", r);
704 worker = (proxy_worker *)balancer->workers->elts;
705 for (n = 0; n < balancer->workers->nelts; n++) {
706 ap_rputs(" <httpd:worker>\n", r);
707 ap_rvputs(r, " <httpd:scheme>", worker->scheme,
708 "</httpd:scheme>\n", NULL);
709 ap_rvputs(r, " <httpd:hostname>", worker->hostname,
710 "</httpd:hostname>\n", NULL);
711 ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
712 worker->s->lbfactor);
713 ap_rputs(" </httpd:worker>\n", r);
716 ap_rputs(" </httpd:workers>\n", r);
717 ap_rputs(" </httpd:balancer>\n", r);
720 ap_rputs(" </httpd:balancers>\n", r);
721 ap_rputs("</httpd:manager>", r);
724 ap_set_content_type(r, "text/html");
725 ap_rputs(DOCTYPE_HTML_3_2
726 "<html><head><title>Balancer Manager</title></head>\n", r);
727 ap_rputs("<body><h1>Load Balancer Manager for ", r);
728 ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
729 ap_rvputs(r, "<dl><dt>Server Version: ",
730 ap_get_server_description(), "</dt>\n", NULL);
731 ap_rvputs(r, "<dt>Server Built: ",
732 ap_get_server_built(), "\n</dt></dl>\n", NULL);
733 balancer = (proxy_balancer *)conf->balancers->elts;
734 for (i = 0; i < conf->balancers->nelts; i++) {
736 ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
737 ap_rvputs(r, "<a href=\"", r->uri, "?b=",
738 balancer->name + sizeof("balancer://") - 1,
740 ap_rvputs(r, balancer->name, "</a></h3>\n\n", NULL);
741 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
742 "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
744 ap_rvputs(r, "<td>", balancer->sticky, NULL);
745 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
746 apr_time_sec(balancer->timeout));
747 ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
748 ap_rprintf(r, "<td>%s</td>\n",
749 balancer->lbmethod->name);
750 ap_rputs("</table>\n<br />", r);
751 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
752 "<th>Worker URL</th>"
753 "<th>Route</th><th>RouteRedir</th>"
754 "<th>Factor</th><th>Set</th><th>Status</th>"
755 "<th>Elected</th><th>To</th><th>From</th>"
758 worker = (proxy_worker *)balancer->workers->elts;
759 for (n = 0; n < balancer->workers->nelts; n++) {
761 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
762 balancer->name + sizeof("balancer://") - 1, "&w=",
763 ap_escape_uri(r->pool, worker->name),
765 ap_rvputs(r, worker->name, "</a></td>", NULL);
766 ap_rvputs(r, "<td>", worker->s->route, NULL);
767 ap_rvputs(r, "</td><td>", worker->s->redirect, NULL);
768 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
769 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
770 if (worker->s->status & PROXY_WORKER_DISABLED)
772 if (worker->s->status & PROXY_WORKER_IN_ERROR)
774 if (worker->s->status & PROXY_WORKER_STOPPED)
775 ap_rputs("Stop ", r);
776 if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
777 ap_rputs("Stby ", r);
778 if (PROXY_WORKER_IS_USABLE(worker))
780 if (!PROXY_WORKER_IS_INITIALIZED(worker))
782 ap_rputs("</td>", r);
783 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
784 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
785 ap_rputs("</td><td>", r);
786 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
787 ap_rputs("</td></tr>\n", r);
791 ap_rputs("</table>\n", r);
794 ap_rputs("<hr />\n", r);
796 ap_rputs("<h3>Edit worker settings for ", r);
797 ap_rvputs(r, wsel->name, "</h3>\n", NULL);
798 ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
799 ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
800 ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
801 ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbfactor);
802 ap_rputs("<tr><td>LB Set:</td><td><input name=\"ls\" type=text ", r);
803 ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbset);
804 ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
805 ap_rvputs(r, "value=\"", wsel->route, NULL);
806 ap_rputs("\"></td></tr>\n", r);
807 ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
808 ap_rvputs(r, "value=\"", wsel->redirect, NULL);
809 ap_rputs("\"></td></tr>\n", r);
810 ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
811 if (wsel->s->status & PROXY_WORKER_DISABLED)
812 ap_rputs(" checked", r);
813 ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
814 if (!(wsel->s->status & PROXY_WORKER_DISABLED))
815 ap_rputs(" checked", r);
816 ap_rputs("></td></tr>\n", r);
817 ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
818 ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ", NULL);
819 ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
820 ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
821 ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
822 "\">\n</form>\n", NULL);
823 ap_rputs("<hr />\n", r);
826 ap_rputs("<h3>Edit balancer settings for ", r);
827 ap_rvputs(r, bsel->name, "</h3>\n", NULL);
828 ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
829 ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
830 ap_rputs("<table><tr><td>StickySession Identifier:</td><td><input name=\"ss\" type=text ", r);
832 ap_rvputs(r, "value=\"", bsel->sticky, "\"", NULL);
833 ap_rputs("></td><tr>\n<tr><td>Timeout:</td><td><input name=\"tm\" type=text ", r);
834 ap_rprintf(r, "value=\"%" APR_TIME_T_FMT "\"></td></tr>\n",
835 apr_time_sec(bsel->timeout));
836 ap_rputs("<tr><td>Failover Attempts:</td><td><input name=\"fa\" type=text ", r);
837 ap_rprintf(r, "value=\"%d\"></td></tr>\n",
839 ap_rputs("<tr><td>LB Method:</td><td><select name=\"lm\">", r);
841 apr_array_header_t *methods;
842 ap_list_provider_names_t *method;
844 methods = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
845 method = (ap_list_provider_names_t *)methods->elts;
846 for (i = 0; i < methods->nelts; i++) {
847 ap_rprintf(r, "<option value=\"%s\" %s>%s</option>", method->provider_name,
848 (!strcasecmp(bsel->lbmethod->name, method->provider_name)) ? "selected" : "",
849 method->provider_name);
853 ap_rputs("</select></td></tr>\n", r);
854 ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
855 ap_rvputs(r, "</table>\n<input type=hidden name=\"b\" ", NULL);
856 ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
857 "\">\n</form>\n", NULL);
858 ap_rputs("<hr />\n", r);
860 ap_rputs(ap_psignature("",r), r);
861 ap_rputs("</body></html>\n", r);
866 static void child_init(apr_pool_t *p, server_rec *s)
869 void *sconf = s->module_config;
870 proxy_server_conf *conf;
871 proxy_balancer *balancer;
873 conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
875 /* Initialize shared scoreboard data */
876 balancer = (proxy_balancer *)conf->balancers->elts;
877 for (i = 0; i < conf->balancers->nelts; i++) {
878 init_balancer_members(conf, s, balancer);
887 * The idea behind the find_best_byrequests scheduler is the following:
889 * lbfactor is "how much we expect this worker to work", or "the worker's
890 * normalized work quota".
892 * lbstatus is "how urgent this worker has to work to fulfill its quota
895 * We distribute each worker's work quota to the worker, and then look
896 * which of them needs to work most urgently (biggest lbstatus). This
897 * worker is then selected for work, and its lbstatus reduced by the
898 * total work quota we distributed to all workers. Thus the sum of all
899 * lbstatus does not change.(*)
901 * If some workers are disabled, the others will
902 * still be scheduled correctly.
904 * If a balancer is configured as follows:
907 * lbfactor 25 25 25 25
909 * And b gets disabled, the following schedule is produced:
911 * a c d a c d a c d ...
913 * Note that the above lbfactor setting is the *exact* same as:
918 * Asymmetric configurations work as one would expect. For
924 * would have a, b and c all handling about the same
925 * amount of load with d handling twice what a or b
926 * or c handles individually. So we could see:
928 * b a d c d a c d b d ...
932 static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
936 int total_factor = 0;
937 proxy_worker *worker;
938 proxy_worker *mycandidate = NULL;
941 int checking_standby;
944 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
945 "proxy: Entering byrequests for BALANCER (%s)",
948 /* First try to see if we have available candidate */
950 checking_standby = checked_standby = 0;
951 while (!mycandidate && !checked_standby) {
952 worker = (proxy_worker *)balancer->workers->elts;
953 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
954 if (!checking_standby) { /* first time through */
955 if (worker->s->lbset > max_lbset)
956 max_lbset = worker->s->lbset;
958 if (worker->s->lbset > cur_lbset)
960 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
962 /* If the worker is in error state run
963 * retry on that worker. It will be marked as
964 * operational if the retry timeout is elapsed.
965 * The worker might still be unusable, but we try
968 if (!PROXY_WORKER_IS_USABLE(worker))
969 ap_proxy_retry_worker("BALANCER", worker, r->server);
970 /* Take into calculation only the workers that are
971 * not in error state or not disabled.
973 if (PROXY_WORKER_IS_USABLE(worker)) {
974 worker->s->lbstatus += worker->s->lbfactor;
975 total_factor += worker->s->lbfactor;
976 if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
977 mycandidate = worker;
980 checked_standby = checking_standby++;
983 } while (cur_lbset <= max_lbset && !mycandidate);
986 mycandidate->s->lbstatus -= total_factor;
993 * The idea behind the find_best_bytraffic scheduler is the following:
995 * We know the amount of traffic (bytes in and out) handled by each
996 * worker. We normalize that traffic by each workers' weight. So assuming
1002 * the scheduler will allow worker c to handle 3 times the
1003 * traffic of a and b. If each request/response results in the
1004 * same amount of traffic, then c would be accessed 3 times as
1005 * often as a or b. If, for example, a handled a request that
1006 * resulted in a large i/o bytecount, then b and c would be
1007 * chosen more often, to even things out.
1009 static proxy_worker *find_best_bytraffic(proxy_balancer *balancer,
1013 apr_off_t mytraffic = 0;
1014 apr_off_t curmin = 0;
1015 proxy_worker *worker;
1016 proxy_worker *mycandidate = NULL;
1019 int checking_standby;
1020 int checked_standby;
1022 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1023 "proxy: Entering bytraffic for BALANCER (%s)",
1026 /* First try to see if we have available candidate */
1028 checking_standby = checked_standby = 0;
1029 while (!mycandidate && !checked_standby) {
1030 worker = (proxy_worker *)balancer->workers->elts;
1031 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
1032 if (!checking_standby) { /* first time through */
1033 if (worker->s->lbset > max_lbset)
1034 max_lbset = worker->s->lbset;
1036 if (worker->s->lbset > cur_lbset)
1038 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
1040 /* If the worker is in error state run
1041 * retry on that worker. It will be marked as
1042 * operational if the retry timeout is elapsed.
1043 * The worker might still be unusable, but we try
1046 if (!PROXY_WORKER_IS_USABLE(worker))
1047 ap_proxy_retry_worker("BALANCER", worker, r->server);
1048 /* Take into calculation only the workers that are
1049 * not in error state or not disabled.
1051 if (PROXY_WORKER_IS_USABLE(worker)) {
1052 mytraffic = (worker->s->transferred/worker->s->lbfactor) +
1053 (worker->s->read/worker->s->lbfactor);
1054 if (!mycandidate || mytraffic < curmin) {
1055 mycandidate = worker;
1060 checked_standby = checking_standby++;
1063 } while (cur_lbset <= max_lbset && !mycandidate);
1069 * How to add additional lbmethods:
1070 * 1. Create func which determines "best" candidate worker
1071 * (eg: find_best_bytraffic, above)
1072 * 2. Register it as a provider.
1074 static const proxy_balancer_method byrequests =
1077 &find_best_byrequests,
1081 static const proxy_balancer_method bytraffic =
1084 &find_best_bytraffic,
1088 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
1090 /* Only the mpm_winnt has child init hook handler.
1091 * make sure that we are called after the mpm
1092 * initializes and after the mod_proxy
1094 static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy.c", NULL};
1095 /* manager handler */
1096 ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1097 ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1098 proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1099 proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1100 proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1101 ap_register_provider(p, PROXY_LBMETHOD, "bytraffic", "0", &bytraffic);
1102 ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
1105 module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
1106 STANDARD20_MODULE_STUFF,
1107 NULL, /* create per-directory config structure */
1108 NULL, /* merge per-directory config structures */
1109 NULL, /* create per-server config structure */
1110 NULL, /* merge per-server config structures */
1111 NULL, /* command apr_table_t */
1112 ap_proxy_balancer_register_hook /* register hooks */