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 */
19 #include "mod_proxy.h"
20 #include "scoreboard.h"
22 #include "apr_version.h"
23 #include "apr_hooks.h"
26 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
28 static char balancer_nonce[APR_UUID_FORMATTED_LENGTH + 1];
30 static int proxy_balancer_canon(request_rec *r, char *url)
37 if (strncasecmp(url, "balancer:", 9) == 0) {
44 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
45 "proxy: BALANCER: canonicalising URL %s", url);
48 * We break the URL into host, port, path, search
50 err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
52 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
53 "error parsing URL %s: %s",
55 return HTTP_BAD_REQUEST;
58 * now parse path/search args, according to rfc1738:
59 * process the path. With proxy-noncanon set (by
60 * mod_proxy) we use the raw, unparsed uri
62 if (apr_table_get(r->notes, "proxy-nocanon")) {
63 path = url; /* this is the raw path */
66 path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
71 return HTTP_BAD_REQUEST;
73 r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
74 "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
76 r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
81 static int init_balancer_members(proxy_server_conf *conf, server_rec *s,
82 proxy_balancer *balancer)
85 proxy_worker **workers;
87 workers = (proxy_worker **)balancer->workers->elts;
89 for (i = 0; i < balancer->workers->nelts; i++) {
90 int worker_is_initialized;
91 worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(*workers);
92 if (!worker_is_initialized) {
93 proxy_worker_stat *slot;
95 * If the worker is not initialized check whether its scoreboard
96 * slot is already initialized.
98 slot = (proxy_worker_stat *) ap_get_scoreboard_lb((*workers)->id);
100 worker_is_initialized = slot->status & PROXY_WORKER_INITIALIZED;
103 worker_is_initialized = 0;
106 ap_proxy_initialize_worker_share(conf, *workers, s);
107 ap_proxy_initialize_worker(*workers, s, conf->pool);
108 if (!worker_is_initialized) {
109 /* Set to the original configuration */
110 (*workers)->s->lbstatus = (*workers)->s->lbfactor =
111 ((*workers)->lbfactor ? (*workers)->lbfactor : 1);
112 (*workers)->s->lbset = (*workers)->lbset;
117 /* Set default number of attempts to the number of
120 if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
121 balancer->max_attempts = balancer->workers->nelts - 1;
122 balancer->max_attempts_set = 1;
127 /* Retrieve the parameter with the given name
128 * Something like 'JSESSIONID=12345...N'
130 static char *get_path_param(apr_pool_t *pool, char *url,
131 const char *name, int scolon_sep)
134 char *pathdelims = "?&";
139 for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
140 path += strlen(name);
143 * Session path was found, get it's value
148 path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
156 static char *get_cookie_param(request_rec *r, const char *name)
159 const char *start_cookie;
161 if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
162 for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
163 start_cookie = ap_strstr_c(start_cookie + 1, name)) {
164 if (start_cookie == cookies ||
165 start_cookie[-1] == ';' ||
166 start_cookie[-1] == ',' ||
167 isspace(start_cookie[-1])) {
169 start_cookie += strlen(name);
170 while(*start_cookie && isspace(*start_cookie))
172 if (*start_cookie == '=' && start_cookie[1]) {
174 * Session cookie was found, get it's value
176 char *end_cookie, *cookie;
178 cookie = apr_pstrdup(r->pool, start_cookie);
179 if ((end_cookie = strchr(cookie, ';')) != NULL)
181 if((end_cookie = strchr(cookie, ',')) != NULL)
191 /* Find the worker that has the 'route' defined
193 static proxy_worker *find_route_worker(proxy_balancer *balancer,
194 const char *route, request_rec *r)
197 int checking_standby;
200 proxy_worker **workers;
201 proxy_worker *worker;
203 checking_standby = checked_standby = 0;
204 while (!checked_standby) {
205 workers = (proxy_worker **)balancer->workers->elts;
206 for (i = 0; i < balancer->workers->nelts; i++, workers++) {
208 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
210 if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
211 if (worker && PROXY_WORKER_IS_USABLE(worker)) {
215 * If the worker is in error state run
216 * retry on that worker. It will be marked as
217 * operational if the retry timeout is elapsed.
218 * The worker might still be unusable, but we try
221 ap_proxy_retry_worker("BALANCER", worker, r->server);
222 if (PROXY_WORKER_IS_USABLE(worker)) {
226 * We have a worker that is unusable.
227 * It can be in error or disabled, but in case
228 * it has a redirection set use that redirection worker.
229 * This enables to safely remove the member from the
230 * balancer. Of course you will need some kind of
231 * session replication between those two remote.
233 if (*worker->s->redirect) {
234 proxy_worker *rworker = NULL;
235 rworker = find_route_worker(balancer, worker->s->redirect, r);
236 /* Check if the redirect worker is usable */
237 if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
239 * If the worker is in error state run
240 * retry on that worker. It will be marked as
241 * operational if the retry timeout is elapsed.
242 * The worker might still be unusable, but we try
245 ap_proxy_retry_worker("BALANCER", rworker, r->server);
247 if (rworker && PROXY_WORKER_IS_USABLE(rworker))
254 checked_standby = checking_standby++;
259 static proxy_worker *find_session_route(proxy_balancer *balancer,
262 const char **sticky_used,
265 proxy_worker *worker = NULL;
267 if (!balancer->sticky)
269 /* Try to find the sticky route inside url */
270 *route = get_path_param(r->pool, *url, balancer->sticky_path, balancer->scolonsep);
272 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
273 "proxy: BALANCER: Found value %s for "
274 "stickysession %s", *route, balancer->sticky_path);
275 *sticky_used = balancer->sticky_path;
278 *route = get_cookie_param(r, balancer->sticky);
280 *sticky_used = balancer->sticky;
281 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
282 "proxy: BALANCER: Found value %s for "
283 "stickysession %s", *route, balancer->sticky);
287 * If we found a value for sticksession, find the first '.' within.
288 * Everything after '.' (if present) is our route.
290 if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
292 if ((*route) && (**route)) {
293 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
294 "proxy: BALANCER: Found route %s", *route);
295 /* We have a route in path or in cookie
296 * Find the worker that has this route defined.
298 worker = find_route_worker(balancer, *route, r);
299 if (worker && strcmp(*route, worker->s->route)) {
301 * Notice that the route of the worker chosen is different from
302 * the route supplied by the client.
304 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
305 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
306 "proxy: BALANCER: Route changed from %s to %s",
307 *route, worker->s->route);
315 static proxy_worker *find_best_worker(proxy_balancer *balancer,
318 proxy_worker *candidate = NULL;
321 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
322 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
323 "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
327 candidate = (*balancer->lbmethod->finder)(balancer, r);
330 candidate->s->elected++;
333 PROXY_THREAD_UNLOCK(balancer);
337 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
338 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
339 "proxy: BALANCER: (%s). Unlock failed for find_best_worker()", balancer->name);
342 if (candidate == NULL) {
343 /* All the workers are in error state or disabled.
344 * If the balancer has a timeout sleep for a while
345 * and try again to find the worker. The chances are
346 * that some other thread will release a connection.
347 * By default the timeout is not set, and the server
348 * returns SERVER_BUSY.
351 if (balancer->timeout) {
352 /* XXX: This can perhaps be build using some
353 * smarter mechanism, like tread_cond.
354 * But since the statuses can came from
355 * different childs, use the provided algo.
357 apr_interval_time_t timeout = balancer->timeout;
358 apr_interval_time_t step, tval = 0;
359 /* Set the timeout to 0 so that we don't
360 * end in infinite loop
362 balancer->timeout = 0;
363 step = timeout / 100;
364 while (tval < timeout) {
367 if ((candidate = find_best_worker(balancer, r)))
371 /* restore the timeout */
372 balancer->timeout = timeout;
381 static int rewrite_url(request_rec *r, proxy_worker *worker,
384 const char *scheme = strstr(*url, "://");
385 const char *path = NULL;
388 path = ap_strchr_c(scheme + 3, '/');
390 /* we break the URL into host, port, uri */
392 return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
393 "missing worker. URI cannot be parsed: ", *url,
397 *url = apr_pstrcat(r->pool, worker->name, path, NULL);
402 static void force_recovery(proxy_balancer *balancer, server_rec *s)
406 proxy_worker **worker;
408 worker = (proxy_worker **)balancer->workers->elts;
409 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
410 if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
415 /* Try if we can recover */
416 ap_proxy_retry_worker("BALANCER", *worker, s);
417 if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
424 /* If all workers are in error state force the recovery.
426 worker = (proxy_worker **)balancer->workers->elts;
427 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
428 ++(*worker)->s->retries;
429 (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
430 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
431 "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
432 balancer->name, (*worker)->hostname);
437 static int proxy_balancer_pre_request(proxy_worker **worker,
438 proxy_balancer **balancer,
440 proxy_server_conf *conf, char **url)
443 proxy_worker *runtime;
445 const char *sticky = NULL;
449 /* Step 1: check if the url is for us
450 * The url we can handle starts with 'balancer://'
451 * If balancer is already provided skip the search
452 * for balancer, because this is failover attempt.
455 !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
458 /* Step 2: Lock the LoadBalancer
459 * XXX: perhaps we need the process lock here
461 if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
462 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
463 "proxy: BALANCER: (%s). Lock failed for pre_request",
468 /* Step 3: force recovery */
469 force_recovery(*balancer, r->server);
471 /* Step 4: find the session route */
472 runtime = find_session_route(*balancer, r, &route, &sticky, url);
474 int i, total_factor = 0;
475 proxy_worker **workers;
476 /* We have a sticky load balancer
477 * Update the workers status
478 * so that even session routes get
481 workers = (proxy_worker **)(*balancer)->workers->elts;
482 for (i = 0; i < (*balancer)->workers->nelts; i++) {
483 /* Take into calculation only the workers that are
484 * not in error state or not disabled.
486 * TODO: Abstract the below, since this is dependent
487 * on the LB implementation
489 if (PROXY_WORKER_IS_USABLE(*workers)) {
490 (*workers)->s->lbstatus += (*workers)->s->lbfactor;
491 total_factor += (*workers)->s->lbfactor;
495 runtime->s->lbstatus -= total_factor;
496 runtime->s->elected++;
500 else if (route && (*balancer)->sticky_force) {
501 int i, member_of = 0;
502 proxy_worker **workers;
504 * We have a route provided that doesn't match the
505 * balancer name. See if the provider route is the
506 * member of the same balancer in which case return 503
508 workers = (proxy_worker **)(*balancer)->workers->elts;
509 for (i = 0; i < (*balancer)->workers->nelts; i++) {
510 if (*((*workers)->s->route) && strcmp((*workers)->s->route, route) == 0) {
517 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
518 "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
519 (*balancer)->name, route);
520 if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
521 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
522 "proxy: BALANCER: (%s). Unlock failed for pre_request",
525 return HTTP_SERVICE_UNAVAILABLE;
529 if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
530 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
531 "proxy: BALANCER: (%s). Unlock failed for pre_request",
535 runtime = find_best_worker(*balancer, r);
537 if ((*balancer)->workers->nelts) {
538 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
539 "proxy: BALANCER: (%s). All workers are in error state",
542 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
543 "proxy: BALANCER: (%s). No workers in balancer",
547 return HTTP_SERVICE_UNAVAILABLE;
549 if ((*balancer)->sticky && runtime) {
551 * This balancer has sticky sessions and the client either has not
552 * supplied any routing information or all workers for this route
553 * including possible redirect and hotstandby workers are in error
554 * state, but we have found another working worker for this
555 * balancer where we can send the request. Thus notice that we have
556 * changed the route to the backend.
558 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
563 (*worker)->s->busy++;
565 /* Add balancer/worker info to env. */
566 apr_table_setn(r->subprocess_env,
567 "BALANCER_NAME", (*balancer)->name);
568 apr_table_setn(r->subprocess_env,
569 "BALANCER_WORKER_NAME", (*worker)->name);
570 apr_table_setn(r->subprocess_env,
571 "BALANCER_WORKER_ROUTE", (*worker)->s->route);
573 /* Rewrite the url from 'balancer://url'
574 * to the 'worker_scheme://worker_hostname[:worker_port]/url'
575 * This replaces the balancers fictional name with the
576 * real hostname of the elected worker.
578 access_status = rewrite_url(r, *worker, url);
579 /* Add the session route to request notes if present */
581 apr_table_setn(r->notes, "session-sticky", sticky);
582 apr_table_setn(r->notes, "session-route", route);
584 /* Add session info to env. */
585 apr_table_setn(r->subprocess_env,
586 "BALANCER_SESSION_STICKY", sticky);
587 apr_table_setn(r->subprocess_env,
588 "BALANCER_SESSION_ROUTE", route);
590 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
591 "proxy: BALANCER (%s) worker (%s) rewritten to %s",
592 (*balancer)->name, (*worker)->name, *url);
594 return access_status;
597 static int proxy_balancer_post_request(proxy_worker *worker,
598 proxy_balancer *balancer,
600 proxy_server_conf *conf)
605 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
606 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
607 "proxy: BALANCER: (%s). Lock failed for post_request",
609 return HTTP_INTERNAL_SERVER_ERROR;
612 if (!apr_is_empty_array(balancer->errstatuses)) {
614 for (i = 0; i < balancer->errstatuses->nelts; i++) {
615 int val = ((int *)balancer->errstatuses->elts)[i];
616 if (r->status == val) {
617 ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, r->server,
618 "Detected ErrorOnState (%d) for member (%s). Forcing worker into error state.", val, worker->name);
619 worker->s->status |= PROXY_WORKER_IN_ERROR;
620 worker->s->error_time = apr_time_now();
626 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
627 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
628 "proxy: BALANCER: (%s). Unlock failed for post_request",
631 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
632 "proxy_balancer_post_request for (%s)", balancer->name);
634 if (worker && worker->s->busy)
641 static void recalc_factors(proxy_balancer *balancer)
644 proxy_worker **workers;
647 /* Recalculate lbfactors */
648 workers = (proxy_worker **)balancer->workers->elts;
649 /* Special case if there is only one worker it's
650 * load factor will always be 1
652 if (balancer->workers->nelts == 1) {
653 (*workers)->s->lbstatus = (*workers)->s->lbfactor = 1;
656 for (i = 0; i < balancer->workers->nelts; i++) {
657 /* Update the status entries */
658 workers[i]->s->lbstatus = workers[i]->s->lbfactor;
662 /* pre_config hook: */
663 static int balancer_init(apr_pool_t *pconf, apr_pool_t *plog,
668 /* Retrieve a UUID and store the nonce for the lifetime of
671 apr_uuid_format(balancer_nonce, &uuid);
676 /* Manages the loadfactors and member status
678 static int balancer_handler(request_rec *r)
680 void *sconf = r->server->module_config;
681 proxy_server_conf *conf = (proxy_server_conf *)
682 ap_get_module_config(sconf, &proxy_module);
683 proxy_balancer *balancer, *bsel = NULL;
684 proxy_worker *worker, *wsel = NULL;
685 proxy_worker **workers = NULL;
686 apr_table_t *params = apr_table_make(r->pool, 10);
691 /* is this for us? */
692 if (strcmp(r->handler, "balancer-manager"))
694 r->allowed = (AP_METHOD_BIT << M_GET);
695 if (r->method_number != M_GET)
699 char *args = apr_pstrdup(r->pool, r->args);
701 while (args && *args) {
702 if ((val = ap_strchr(args, '='))) {
704 if ((tok = ap_strchr(val, '&')))
707 * Special case: workers are allowed path information
709 if ((access_status = ap_unescape_url(val)) != OK)
710 if (strcmp(args, "w") || (access_status != HTTP_NOT_FOUND))
711 return access_status;
712 apr_table_setn(params, args, val);
716 return HTTP_BAD_REQUEST;
720 /* Check that the supplied nonce matches this server's nonce;
721 * otherwise ignore all parameters, to prevent a CSRF attack. */
722 if (*balancer_nonce &&
723 ((name = apr_table_get(params, "nonce")) == NULL
724 || strcmp(balancer_nonce, name) != 0)) {
725 apr_table_clear(params);
728 if ((name = apr_table_get(params, "b")))
729 bsel = ap_proxy_get_balancer(r->pool, conf,
730 apr_pstrcat(r->pool, "balancer://", name, NULL));
731 if ((name = apr_table_get(params, "w"))) {
734 ws = ap_proxy_get_worker(r->pool, conf, name);
736 workers = (proxy_worker **)bsel->workers->elts;
737 for (n = 0; n < bsel->workers->nelts; n++) {
739 if (strcasecmp(worker->name, ws->name) == 0) {
747 /* First set the params */
749 * Note that it is not possible set the proxy_balancer because it is not
754 if ((val = apr_table_get(params, "lf"))) {
755 int ival = atoi(val);
756 if (ival >= 1 && ival <= 100) {
757 wsel->s->lbfactor = ival;
759 recalc_factors(bsel);
762 if ((val = apr_table_get(params, "wr"))) {
763 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
764 strcpy(wsel->s->route, val);
766 *wsel->s->route = '\0';
768 if ((val = apr_table_get(params, "rr"))) {
769 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
770 strcpy(wsel->s->redirect, val);
772 *wsel->s->redirect = '\0';
774 if ((val = apr_table_get(params, "dw"))) {
775 if (!strcasecmp(val, "Disable"))
776 wsel->s->status |= PROXY_WORKER_DISABLED;
777 else if (!strcasecmp(val, "Enable"))
778 wsel->s->status &= ~PROXY_WORKER_DISABLED;
780 if ((val = apr_table_get(params, "ls"))) {
781 int ival = atoi(val);
782 if (ival >= 0 && ival <= 99) {
783 wsel->s->lbset = ival;
788 if (apr_table_get(params, "xml")) {
789 ap_set_content_type(r, "text/xml");
790 ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
791 ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
792 ap_rputs(" <httpd:balancers>\n", r);
793 balancer = (proxy_balancer *)conf->balancers->elts;
794 for (i = 0; i < conf->balancers->nelts; i++) {
795 ap_rputs(" <httpd:balancer>\n", r);
796 ap_rvputs(r, " <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
797 ap_rputs(" <httpd:workers>\n", r);
798 workers = (proxy_worker **)balancer->workers->elts;
799 for (n = 0; n < balancer->workers->nelts; n++) {
801 ap_rputs(" <httpd:worker>\n", r);
802 ap_rvputs(r, " <httpd:scheme>", worker->scheme,
803 "</httpd:scheme>\n", NULL);
804 ap_rvputs(r, " <httpd:hostname>", worker->hostname,
805 "</httpd:hostname>\n", NULL);
806 ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
807 worker->s->lbfactor);
808 ap_rputs(" </httpd:worker>\n", r);
811 ap_rputs(" </httpd:workers>\n", r);
812 ap_rputs(" </httpd:balancer>\n", r);
815 ap_rputs(" </httpd:balancers>\n", r);
816 ap_rputs("</httpd:manager>", r);
819 ap_set_content_type(r, "text/html; charset=ISO-8859-1");
820 ap_rputs(DOCTYPE_HTML_3_2
821 "<html><head><title>Balancer Manager</title></head>\n", r);
822 ap_rputs("<body><h1>Load Balancer Manager for ", r);
823 ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
824 ap_rvputs(r, "<dl><dt>Server Version: ",
825 ap_get_server_description(), "</dt>\n", NULL);
826 ap_rvputs(r, "<dt>Server Built: ",
827 ap_get_server_built(), "\n</dt></dl>\n", NULL);
828 balancer = (proxy_balancer *)conf->balancers->elts;
829 for (i = 0; i < conf->balancers->nelts; i++) {
831 ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
832 ap_rvputs(r, balancer->name, "</h3>\n\n", NULL);
833 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
834 "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
836 if (balancer->sticky) {
837 if (strcmp(balancer->sticky, balancer->sticky_path)) {
838 ap_rvputs(r, "<td>", balancer->sticky, " | ",
839 balancer->sticky_path, NULL);
842 ap_rvputs(r, "<td>", balancer->sticky, NULL);
846 ap_rputs("<td> - ", r);
848 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
849 apr_time_sec(balancer->timeout));
850 ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
851 ap_rprintf(r, "<td>%s</td>\n",
852 balancer->lbmethod->name);
853 ap_rputs("</table>\n<br />", r);
854 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
855 "<th>Worker URL</th>"
856 "<th>Route</th><th>RouteRedir</th>"
857 "<th>Factor</th><th>Set</th><th>Status</th>"
858 "<th>Elected</th><th>To</th><th>From</th>"
861 workers = (proxy_worker **)balancer->workers->elts;
862 for (n = 0; n < balancer->workers->nelts; n++) {
865 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
866 balancer->name + sizeof("balancer://") - 1, "&w=",
867 ap_escape_uri(r->pool, worker->name),
868 "&nonce=", balancer_nonce,
870 ap_rvputs(r, worker->name, "</a></td>", NULL);
871 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
873 ap_rvputs(r, "</td><td>",
874 ap_escape_html(r->pool, worker->s->redirect), NULL);
875 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
876 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
877 if (worker->s->status & PROXY_WORKER_DISABLED)
879 if (worker->s->status & PROXY_WORKER_IN_ERROR)
881 if (worker->s->status & PROXY_WORKER_STOPPED)
882 ap_rputs("Stop ", r);
883 if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
884 ap_rputs("Stby ", r);
885 if (PROXY_WORKER_IS_USABLE(worker))
887 if (!PROXY_WORKER_IS_INITIALIZED(worker))
889 ap_rputs("</td>", r);
890 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
891 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
892 ap_rputs("</td><td>", r);
893 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
894 ap_rputs("</td></tr>\n", r);
898 ap_rputs("</table>\n", r);
901 ap_rputs("<hr />\n", r);
903 ap_rputs("<h3>Edit worker settings for ", r);
904 ap_rvputs(r, wsel->name, "</h3>\n", NULL);
905 ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
906 ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
907 ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
908 ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbfactor);
909 ap_rputs("<tr><td>LB Set:</td><td><input name=\"ls\" type=text ", r);
910 ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbset);
911 ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
912 ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->route),
914 ap_rputs("\"></td></tr>\n", r);
915 ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
916 ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->redirect),
918 ap_rputs("\"></td></tr>\n", r);
919 ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
920 if (wsel->s->status & PROXY_WORKER_DISABLED)
921 ap_rputs(" checked", r);
922 ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
923 if (!(wsel->s->status & PROXY_WORKER_DISABLED))
924 ap_rputs(" checked", r);
925 ap_rputs("></td></tr>\n", r);
926 ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
927 ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ", NULL);
928 ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
929 ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
930 ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
932 ap_rvputs(r, "<input type=hidden name=\"nonce\" value=\"",
933 balancer_nonce, "\">\n", NULL);
934 ap_rvputs(r, "</form>\n", NULL);
935 ap_rputs("<hr />\n", r);
937 ap_rputs(ap_psignature("",r), r);
938 ap_rputs("</body></html>\n", r);
943 static void child_init(apr_pool_t *p, server_rec *s)
946 void *sconf = s->module_config;
947 proxy_server_conf *conf;
948 proxy_balancer *balancer;
950 conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
952 /* Initialize shared scoreboard data */
953 balancer = (proxy_balancer *)conf->balancers->elts;
954 for (i = 0; i < conf->balancers->nelts; i++) {
955 if (balancer->lbmethod && balancer->lbmethod->reset)
956 balancer->lbmethod->reset(balancer, s);
957 init_balancer_members(conf, s, balancer);
965 static const char *set_balancer_nonce (cmd_parms *cmd, void *dummy, const char *arg,
968 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
973 if (!strcasecmp(arg, "None")) {
974 *balancer_nonce = '\0';
975 } else if (!strcasecmp(arg, "Set")) {
977 apr_cpystrn(balancer_nonce, val, sizeof(balancer_nonce));
979 return "BalancerNonce Set requires an argument";
981 } else if (strcasecmp(arg, "Default")) {
982 return "Bad argument for BalancerNonce: Must be 'Set', 'None' or 'Default'";
987 static const command_rec balancer_cmds[] =
989 AP_INIT_TAKE12("BalancerNonce", set_balancer_nonce, NULL,
990 RSRC_CONF, "Set value for balancer-manager nonce"),
994 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
996 /* Only the mpm_winnt has child init hook handler.
997 * make sure that we are called after the mpm
1000 static const char *const aszPred[] = { "mpm_winnt.c", NULL};
1001 /* manager handler */
1002 ap_hook_pre_config(balancer_init, NULL, NULL, APR_HOOK_MIDDLE);
1003 ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1004 ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1005 proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1006 proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1007 proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1010 module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
1011 STANDARD20_MODULE_STUFF,
1012 NULL, /* create per-directory config structure */
1013 NULL, /* merge per-directory config structures */
1014 NULL, /* create per-server config structure */
1015 NULL, /* merge per-server config structures */
1016 balancer_cmds, /* command apr_table_t */
1017 ap_proxy_balancer_register_hook /* register hooks */