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 *worker;
202 checking_standby = checked_standby = 0;
203 while (!checked_standby) {
204 worker = (proxy_worker *)balancer->workers->elts;
205 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
206 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
208 if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
209 if (worker && PROXY_WORKER_IS_USABLE(worker)) {
213 * If the worker is in error state run
214 * retry on that worker. It will be marked as
215 * operational if the retry timeout is elapsed.
216 * The worker might still be unusable, but we try
219 ap_proxy_retry_worker("BALANCER", worker, r->server);
220 if (PROXY_WORKER_IS_USABLE(worker)) {
224 * We have a worker that is unusable.
225 * It can be in error or disabled, but in case
226 * it has a redirection set use that redirection worker.
227 * This enables to safely remove the member from the
228 * balancer. Of course you will need some kind of
229 * session replication between those two remote.
231 if (*worker->s->redirect) {
232 proxy_worker *rworker = NULL;
233 rworker = find_route_worker(balancer, worker->s->redirect, r);
234 /* Check if the redirect worker is usable */
235 if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
237 * If the worker is in error state run
238 * retry on that worker. It will be marked as
239 * operational if the retry timeout is elapsed.
240 * The worker might still be unusable, but we try
243 ap_proxy_retry_worker("BALANCER", rworker, r->server);
245 if (rworker && PROXY_WORKER_IS_USABLE(rworker))
252 checked_standby = checking_standby++;
257 static proxy_worker *find_session_route(proxy_balancer *balancer,
260 const char **sticky_used,
263 proxy_worker *worker = NULL;
265 if (!balancer->sticky)
267 /* Try to find the sticky route inside url */
268 *route = get_path_param(r->pool, *url, balancer->sticky_path, balancer->scolonsep);
270 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
271 "proxy: BALANCER: Found value %s for "
272 "stickysession %s", *route, balancer->sticky_path);
273 *sticky_used = balancer->sticky_path;
276 *route = get_cookie_param(r, balancer->sticky);
278 *sticky_used = balancer->sticky;
279 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
280 "proxy: BALANCER: Found value %s for "
281 "stickysession %s", *route, balancer->sticky);
285 * If we found a value for sticksession, find the first '.' within.
286 * Everything after '.' (if present) is our route.
288 if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
290 if ((*route) && (**route)) {
291 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
292 "proxy: BALANCER: Found route %s", *route);
293 /* We have a route in path or in cookie
294 * Find the worker that has this route defined.
296 worker = find_route_worker(balancer, *route, r);
297 if (worker && strcmp(*route, worker->s->route)) {
299 * Notice that the route of the worker chosen is different from
300 * the route supplied by the client.
302 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
303 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
304 "proxy: BALANCER: Route changed from %s to %s",
305 *route, worker->s->route);
313 static proxy_worker *find_best_worker(proxy_balancer *balancer,
316 proxy_worker *candidate = NULL;
319 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
320 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
321 "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
325 candidate = (*balancer->lbmethod->finder)(balancer, r);
328 candidate->s->elected++;
331 PROXY_THREAD_UNLOCK(balancer);
335 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
336 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
337 "proxy: BALANCER: (%s). Unlock failed for find_best_worker()", balancer->name);
340 if (candidate == NULL) {
341 /* All the workers are in error state or disabled.
342 * If the balancer has a timeout sleep for a while
343 * and try again to find the worker. The chances are
344 * that some other thread will release a connection.
345 * By default the timeout is not set, and the server
346 * returns SERVER_BUSY.
349 if (balancer->timeout) {
350 /* XXX: This can perhaps be build using some
351 * smarter mechanism, like tread_cond.
352 * But since the statuses can came from
353 * different childs, use the provided algo.
355 apr_interval_time_t timeout = balancer->timeout;
356 apr_interval_time_t step, tval = 0;
357 /* Set the timeout to 0 so that we don't
358 * end in infinite loop
360 balancer->timeout = 0;
361 step = timeout / 100;
362 while (tval < timeout) {
365 if ((candidate = find_best_worker(balancer, r)))
369 /* restore the timeout */
370 balancer->timeout = timeout;
379 static int rewrite_url(request_rec *r, proxy_worker *worker,
382 const char *scheme = strstr(*url, "://");
383 const char *path = NULL;
386 path = ap_strchr_c(scheme + 3, '/');
388 /* we break the URL into host, port, uri */
390 return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
391 "missing worker. URI cannot be parsed: ", *url,
395 *url = apr_pstrcat(r->pool, worker->name, path, NULL);
400 static void force_recovery(proxy_balancer *balancer, server_rec *s)
404 proxy_worker **worker;
406 worker = (proxy_worker **)balancer->workers->elts;
407 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
408 if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
413 /* Try if we can recover */
414 ap_proxy_retry_worker("BALANCER", *worker, s);
415 if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
422 /* If all workers are in error state force the recovery.
424 worker = (proxy_worker **)balancer->workers->elts;
425 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
426 ++(*worker)->s->retries;
427 (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
428 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
429 "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
430 balancer->name, (*worker)->hostname);
435 static int proxy_balancer_pre_request(proxy_worker **worker,
436 proxy_balancer **balancer,
438 proxy_server_conf *conf, char **url)
441 proxy_worker *runtime;
443 const char *sticky = NULL;
447 /* Step 1: check if the url is for us
448 * The url we can handle starts with 'balancer://'
449 * If balancer is already provided skip the search
450 * for balancer, because this is failover attempt.
453 !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
456 /* Step 2: Lock the LoadBalancer
457 * XXX: perhaps we need the process lock here
459 if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
460 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
461 "proxy: BALANCER: (%s). Lock failed for pre_request",
466 /* Step 3: force recovery */
467 force_recovery(*balancer, r->server);
469 /* Step 4: find the session route */
470 runtime = find_session_route(*balancer, r, &route, &sticky, url);
472 int i, total_factor = 0;
473 proxy_worker *workers;
474 /* We have a sticky load balancer
475 * Update the workers status
476 * so that even session routes get
479 workers = (proxy_worker *)(*balancer)->workers->elts;
480 for (i = 0; i < (*balancer)->workers->nelts; i++) {
481 /* Take into calculation only the workers that are
482 * not in error state or not disabled.
484 * TODO: Abstract the below, since this is dependent
485 * on the LB implementation
487 if (PROXY_WORKER_IS_USABLE(workers)) {
488 workers->s->lbstatus += workers->s->lbfactor;
489 total_factor += workers->s->lbfactor;
493 runtime->s->lbstatus -= total_factor;
494 runtime->s->elected++;
498 else if (route && (*balancer)->sticky_force) {
499 int i, member_of = 0;
500 proxy_worker *workers;
502 * We have a route provided that doesn't match the
503 * balancer name. See if the provider route is the
504 * member of the same balancer in which case return 503
506 workers = (proxy_worker *)(*balancer)->workers->elts;
507 for (i = 0; i < (*balancer)->workers->nelts; i++) {
508 if (*(workers->s->route) && strcmp(workers->s->route, route) == 0) {
515 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
516 "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
517 (*balancer)->name, route);
518 if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
519 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
520 "proxy: BALANCER: (%s). Unlock failed for pre_request",
523 return HTTP_SERVICE_UNAVAILABLE;
527 if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
528 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
529 "proxy: BALANCER: (%s). Unlock failed for pre_request",
533 runtime = find_best_worker(*balancer, r);
535 if ((*balancer)->workers->nelts) {
536 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
537 "proxy: BALANCER: (%s). All workers are in error state",
540 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
541 "proxy: BALANCER: (%s). No workers in balancer",
545 return HTTP_SERVICE_UNAVAILABLE;
547 if ((*balancer)->sticky && runtime) {
549 * This balancer has sticky sessions and the client either has not
550 * supplied any routing information or all workers for this route
551 * including possible redirect and hotstandby workers are in error
552 * state, but we have found another working worker for this
553 * balancer where we can send the request. Thus notice that we have
554 * changed the route to the backend.
556 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
561 (*worker)->s->busy++;
563 /* Add balancer/worker info to env. */
564 apr_table_setn(r->subprocess_env,
565 "BALANCER_NAME", (*balancer)->name);
566 apr_table_setn(r->subprocess_env,
567 "BALANCER_WORKER_NAME", (*worker)->name);
568 apr_table_setn(r->subprocess_env,
569 "BALANCER_WORKER_ROUTE", (*worker)->s->route);
571 /* Rewrite the url from 'balancer://url'
572 * to the 'worker_scheme://worker_hostname[:worker_port]/url'
573 * This replaces the balancers fictional name with the
574 * real hostname of the elected worker.
576 access_status = rewrite_url(r, *worker, url);
577 /* Add the session route to request notes if present */
579 apr_table_setn(r->notes, "session-sticky", sticky);
580 apr_table_setn(r->notes, "session-route", route);
582 /* Add session info to env. */
583 apr_table_setn(r->subprocess_env,
584 "BALANCER_SESSION_STICKY", sticky);
585 apr_table_setn(r->subprocess_env,
586 "BALANCER_SESSION_ROUTE", route);
588 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
589 "proxy: BALANCER (%s) worker (%s) rewritten to %s",
590 (*balancer)->name, (*worker)->name, *url);
592 return access_status;
595 static int proxy_balancer_post_request(proxy_worker *worker,
596 proxy_balancer *balancer,
598 proxy_server_conf *conf)
604 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
605 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
606 "proxy: BALANCER: (%s). Lock failed for post_request",
608 return HTTP_INTERNAL_SERVER_ERROR;
610 /* TODO: placeholder for post_request actions
613 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
614 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
615 "proxy: BALANCER: (%s). Unlock failed for post_request",
618 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
619 "proxy_balancer_post_request for (%s)", balancer->name);
623 if (worker && worker->s->busy)
630 static void recalc_factors(proxy_balancer *balancer)
633 proxy_worker *workers;
636 /* Recalculate lbfactors */
637 workers = (proxy_worker *)balancer->workers->elts;
638 /* Special case if there is only one worker it's
639 * load factor will always be 1
641 if (balancer->workers->nelts == 1) {
642 workers->s->lbstatus = workers->s->lbfactor = 1;
645 for (i = 0; i < balancer->workers->nelts; i++) {
646 /* Update the status entries */
647 workers[i].s->lbstatus = workers[i].s->lbfactor;
651 /* post_config hook: */
652 static int balancer_init(apr_pool_t *p, apr_pool_t *plog,
653 apr_pool_t *ptemp, server_rec *s)
656 const char *userdata_key = "mod_proxy_balancer_init";
659 /* balancer_init() will be called twice during startup. So, only
660 * set up the static data the second time through. */
661 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
663 apr_pool_userdata_set((const void *)1, userdata_key,
664 apr_pool_cleanup_null, s->process->pool);
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 apr_table_t *params = apr_table_make(r->pool, 10);
690 /* is this for us? */
691 if (strcmp(r->handler, "balancer-manager"))
693 r->allowed = (AP_METHOD_BIT << M_GET);
694 if (r->method_number != M_GET)
698 char *args = apr_pstrdup(r->pool, r->args);
700 while (args && *args) {
701 if ((val = ap_strchr(args, '='))) {
703 if ((tok = ap_strchr(val, '&')))
706 * Special case: workers are allowed path information
708 if ((access_status = ap_unescape_url(val)) != OK)
709 if (strcmp(args, "w") || (access_status != HTTP_NOT_FOUND))
710 return access_status;
711 apr_table_setn(params, args, val);
715 return HTTP_BAD_REQUEST;
719 /* Check that the supplied nonce matches this server's nonce;
720 * otherwise ignore all parameters, to prevent a CSRF attack. */
721 if ((name = apr_table_get(params, "nonce")) == NULL
722 || strcmp(balancer_nonce, name) != 0) {
723 apr_table_clear(params);
726 if ((name = apr_table_get(params, "b")))
727 bsel = ap_proxy_get_balancer(r->pool, conf,
728 apr_pstrcat(r->pool, "balancer://", name, NULL));
729 if ((name = apr_table_get(params, "w"))) {
732 ws = ap_proxy_get_worker(r->pool, conf, name);
734 worker = (proxy_worker *)bsel->workers->elts;
735 for (n = 0; n < bsel->workers->nelts; n++) {
736 if (strcasecmp(worker->name, ws->name) == 0) {
744 /* First set the params */
746 * Note that it is not possible set the proxy_balancer because it is not
751 if ((val = apr_table_get(params, "lf"))) {
752 int ival = atoi(val);
753 if (ival >= 1 && ival <= 100) {
754 wsel->s->lbfactor = ival;
756 recalc_factors(bsel);
759 if ((val = apr_table_get(params, "wr"))) {
760 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
761 strcpy(wsel->s->route, val);
763 *wsel->s->route = '\0';
765 if ((val = apr_table_get(params, "rr"))) {
766 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
767 strcpy(wsel->s->redirect, val);
769 *wsel->s->redirect = '\0';
771 if ((val = apr_table_get(params, "dw"))) {
772 if (!strcasecmp(val, "Disable"))
773 wsel->s->status |= PROXY_WORKER_DISABLED;
774 else if (!strcasecmp(val, "Enable"))
775 wsel->s->status &= ~PROXY_WORKER_DISABLED;
777 if ((val = apr_table_get(params, "ls"))) {
778 int ival = atoi(val);
779 if (ival >= 0 && ival <= 99) {
780 wsel->s->lbset = ival;
785 if (apr_table_get(params, "xml")) {
786 ap_set_content_type(r, "text/xml");
787 ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
788 ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
789 ap_rputs(" <httpd:balancers>\n", r);
790 balancer = (proxy_balancer *)conf->balancers->elts;
791 for (i = 0; i < conf->balancers->nelts; i++) {
792 ap_rputs(" <httpd:balancer>\n", r);
793 ap_rvputs(r, " <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
794 ap_rputs(" <httpd:workers>\n", r);
795 worker = (proxy_worker *)balancer->workers->elts;
796 for (n = 0; n < balancer->workers->nelts; n++) {
797 ap_rputs(" <httpd:worker>\n", r);
798 ap_rvputs(r, " <httpd:scheme>", worker->scheme,
799 "</httpd:scheme>\n", NULL);
800 ap_rvputs(r, " <httpd:hostname>", worker->hostname,
801 "</httpd:hostname>\n", NULL);
802 ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
803 worker->s->lbfactor);
804 ap_rputs(" </httpd:worker>\n", r);
807 ap_rputs(" </httpd:workers>\n", r);
808 ap_rputs(" </httpd:balancer>\n", r);
811 ap_rputs(" </httpd:balancers>\n", r);
812 ap_rputs("</httpd:manager>", r);
815 ap_set_content_type(r, "text/html; charset=ISO-8859-1");
816 ap_rputs(DOCTYPE_HTML_3_2
817 "<html><head><title>Balancer Manager</title></head>\n", r);
818 ap_rputs("<body><h1>Load Balancer Manager for ", r);
819 ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
820 ap_rvputs(r, "<dl><dt>Server Version: ",
821 ap_get_server_description(), "</dt>\n", NULL);
822 ap_rvputs(r, "<dt>Server Built: ",
823 ap_get_server_built(), "\n</dt></dl>\n", NULL);
824 balancer = (proxy_balancer *)conf->balancers->elts;
825 for (i = 0; i < conf->balancers->nelts; i++) {
827 ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
828 ap_rvputs(r, balancer->name, "</h3>\n\n", NULL);
829 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
830 "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
832 if (balancer->sticky) {
833 if (strcmp(balancer->sticky, balancer->sticky_path)) {
834 ap_rvputs(r, "<td>", balancer->sticky, " | ",
835 balancer->sticky_path, NULL);
838 ap_rvputs(r, "<td>", balancer->sticky, NULL);
842 ap_rputs("<td> - ", r);
844 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
845 apr_time_sec(balancer->timeout));
846 ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
847 ap_rprintf(r, "<td>%s</td>\n",
848 balancer->lbmethod->name);
849 ap_rputs("</table>\n<br />", r);
850 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
851 "<th>Worker URL</th>"
852 "<th>Route</th><th>RouteRedir</th>"
853 "<th>Factor</th><th>Set</th><th>Status</th>"
854 "<th>Elected</th><th>To</th><th>From</th>"
857 worker = (proxy_worker *)balancer->workers->elts;
858 for (n = 0; n < balancer->workers->nelts; n++) {
860 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
861 balancer->name + sizeof("balancer://") - 1, "&w=",
862 ap_escape_uri(r->pool, worker->name),
863 "&nonce=", balancer_nonce,
865 ap_rvputs(r, worker->name, "</a></td>", NULL);
866 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
868 ap_rvputs(r, "</td><td>",
869 ap_escape_html(r->pool, worker->s->redirect), NULL);
870 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
871 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
872 if (worker->s->status & PROXY_WORKER_DISABLED)
874 if (worker->s->status & PROXY_WORKER_IN_ERROR)
876 if (worker->s->status & PROXY_WORKER_STOPPED)
877 ap_rputs("Stop ", r);
878 if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
879 ap_rputs("Stby ", r);
880 if (PROXY_WORKER_IS_USABLE(worker))
882 if (!PROXY_WORKER_IS_INITIALIZED(worker))
884 ap_rputs("</td>", r);
885 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
886 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
887 ap_rputs("</td><td>", r);
888 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
889 ap_rputs("</td></tr>\n", r);
893 ap_rputs("</table>\n", r);
896 ap_rputs("<hr />\n", r);
898 ap_rputs("<h3>Edit worker settings for ", r);
899 ap_rvputs(r, wsel->name, "</h3>\n", NULL);
900 ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
901 ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
902 ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
903 ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbfactor);
904 ap_rputs("<tr><td>LB Set:</td><td><input name=\"ls\" type=text ", r);
905 ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbset);
906 ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
907 ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->route),
909 ap_rputs("\"></td></tr>\n", r);
910 ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
911 ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->redirect),
913 ap_rputs("\"></td></tr>\n", r);
914 ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
915 if (wsel->s->status & PROXY_WORKER_DISABLED)
916 ap_rputs(" checked", r);
917 ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
918 if (!(wsel->s->status & PROXY_WORKER_DISABLED))
919 ap_rputs(" checked", r);
920 ap_rputs("></td></tr>\n", r);
921 ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
922 ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ", NULL);
923 ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
924 ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
925 ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
927 ap_rvputs(r, "<input type=hidden name=\"nonce\" value=\"",
928 balancer_nonce, "\">\n", NULL);
929 ap_rvputs(r, "</form>\n", NULL);
930 ap_rputs("<hr />\n", r);
932 ap_rputs(ap_psignature("",r), r);
933 ap_rputs("</body></html>\n", r);
938 static void child_init(apr_pool_t *p, server_rec *s)
941 void *sconf = s->module_config;
942 proxy_server_conf *conf;
943 proxy_balancer *balancer;
945 conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
947 /* Initialize shared scoreboard data */
948 balancer = (proxy_balancer *)conf->balancers->elts;
949 for (i = 0; i < conf->balancers->nelts; i++) {
950 if (balancer->lbmethod && balancer->lbmethod->reset)
951 balancer->lbmethod->reset(balancer, s);
952 init_balancer_members(conf, s, balancer);
960 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
962 /* Only the mpm_winnt has child init hook handler.
963 * make sure that we are called after the mpm
966 static const char *const aszPred[] = { "mpm_winnt.c", NULL};
967 /* manager handler */
968 ap_hook_post_config(balancer_init, NULL, NULL, APR_HOOK_MIDDLE);
969 ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
970 ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
971 proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
972 proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
973 proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
976 module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
977 STANDARD20_MODULE_STUFF,
978 NULL, /* create per-directory config structure */
979 NULL, /* merge per-directory config structures */
980 NULL, /* create per-server config structure */
981 NULL, /* merge per-server config structures */
982 NULL, /* command apr_table_t */
983 ap_proxy_balancer_register_hook /* register hooks */