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 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
536 "proxy: BALANCER: (%s). All workers are in error state",
539 return HTTP_SERVICE_UNAVAILABLE;
541 if ((*balancer)->sticky && runtime) {
543 * This balancer has sticky sessions and the client either has not
544 * supplied any routing information or all workers for this route
545 * including possible redirect and hotstandby workers are in error
546 * state, but we have found another working worker for this
547 * balancer where we can send the request. Thus notice that we have
548 * changed the route to the backend.
550 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
555 (*worker)->s->busy++;
557 /* Add balancer/worker info to env. */
558 apr_table_setn(r->subprocess_env,
559 "BALANCER_NAME", (*balancer)->name);
560 apr_table_setn(r->subprocess_env,
561 "BALANCER_WORKER_NAME", (*worker)->name);
562 apr_table_setn(r->subprocess_env,
563 "BALANCER_WORKER_ROUTE", (*worker)->s->route);
565 /* Rewrite the url from 'balancer://url'
566 * to the 'worker_scheme://worker_hostname[:worker_port]/url'
567 * This replaces the balancers fictional name with the
568 * real hostname of the elected worker.
570 access_status = rewrite_url(r, *worker, url);
571 /* Add the session route to request notes if present */
573 apr_table_setn(r->notes, "session-sticky", sticky);
574 apr_table_setn(r->notes, "session-route", route);
576 /* Add session info to env. */
577 apr_table_setn(r->subprocess_env,
578 "BALANCER_SESSION_STICKY", sticky);
579 apr_table_setn(r->subprocess_env,
580 "BALANCER_SESSION_ROUTE", route);
582 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
583 "proxy: BALANCER (%s) worker (%s) rewritten to %s",
584 (*balancer)->name, (*worker)->name, *url);
586 return access_status;
589 static int proxy_balancer_post_request(proxy_worker *worker,
590 proxy_balancer *balancer,
592 proxy_server_conf *conf)
598 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
599 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
600 "proxy: BALANCER: (%s). Lock failed for post_request",
602 return HTTP_INTERNAL_SERVER_ERROR;
604 /* TODO: placeholder for post_request actions
607 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
608 ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
609 "proxy: BALANCER: (%s). Unlock failed for post_request",
612 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
613 "proxy_balancer_post_request for (%s)", balancer->name);
617 if (worker && worker->s->busy)
624 static void recalc_factors(proxy_balancer *balancer)
627 proxy_worker *workers;
630 /* Recalculate lbfactors */
631 workers = (proxy_worker *)balancer->workers->elts;
632 /* Special case if there is only one worker it's
633 * load factor will always be 1
635 if (balancer->workers->nelts == 1) {
636 workers->s->lbstatus = workers->s->lbfactor = 1;
639 for (i = 0; i < balancer->workers->nelts; i++) {
640 /* Update the status entries */
641 workers[i].s->lbstatus = workers[i].s->lbfactor;
645 /* post_config hook: */
646 static int balancer_init(apr_pool_t *p, apr_pool_t *plog,
647 apr_pool_t *ptemp, server_rec *s)
650 const char *userdata_key = "mod_proxy_balancer_init";
653 /* balancer_init() will be called twice during startup. So, only
654 * set up the static data the second time through. */
655 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
657 apr_pool_userdata_set((const void *)1, userdata_key,
658 apr_pool_cleanup_null, s->process->pool);
662 /* Retrieve a UUID and store the nonce for the lifetime of
665 apr_uuid_format(balancer_nonce, &uuid);
670 /* Manages the loadfactors and member status
672 static int balancer_handler(request_rec *r)
674 void *sconf = r->server->module_config;
675 proxy_server_conf *conf = (proxy_server_conf *)
676 ap_get_module_config(sconf, &proxy_module);
677 proxy_balancer *balancer, *bsel = NULL;
678 proxy_worker *worker, *wsel = NULL;
679 apr_table_t *params = apr_table_make(r->pool, 10);
684 /* is this for us? */
685 if (strcmp(r->handler, "balancer-manager"))
687 r->allowed = (AP_METHOD_BIT << M_GET);
688 if (r->method_number != M_GET)
692 char *args = apr_pstrdup(r->pool, r->args);
694 while (args && *args) {
695 if ((val = ap_strchr(args, '='))) {
697 if ((tok = ap_strchr(val, '&')))
700 * Special case: workers are allowed path information
702 if ((access_status = ap_unescape_url(val)) != OK)
703 if (strcmp(args, "w") || (access_status != HTTP_NOT_FOUND))
704 return access_status;
705 apr_table_setn(params, args, val);
709 return HTTP_BAD_REQUEST;
713 /* Check that the supplied nonce matches this server's nonce;
714 * otherwise ignore all parameters, to prevent a CSRF attack. */
715 if ((name = apr_table_get(params, "nonce")) == NULL
716 || strcmp(balancer_nonce, name) != 0) {
717 apr_table_clear(params);
720 if ((name = apr_table_get(params, "b")))
721 bsel = ap_proxy_get_balancer(r->pool, conf,
722 apr_pstrcat(r->pool, "balancer://", name, NULL));
723 if ((name = apr_table_get(params, "w"))) {
726 ws = ap_proxy_get_worker(r->pool, conf, name);
728 worker = (proxy_worker *)bsel->workers->elts;
729 for (n = 0; n < bsel->workers->nelts; n++) {
730 if (strcasecmp(worker->name, ws->name) == 0) {
738 /* First set the params */
740 * Note that it is not possible set the proxy_balancer because it is not
745 if ((val = apr_table_get(params, "lf"))) {
746 int ival = atoi(val);
747 if (ival >= 1 && ival <= 100) {
748 wsel->s->lbfactor = ival;
750 recalc_factors(bsel);
753 if ((val = apr_table_get(params, "wr"))) {
754 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
755 strcpy(wsel->s->route, val);
757 *wsel->s->route = '\0';
759 if ((val = apr_table_get(params, "rr"))) {
760 if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
761 strcpy(wsel->s->redirect, val);
763 *wsel->s->redirect = '\0';
765 if ((val = apr_table_get(params, "dw"))) {
766 if (!strcasecmp(val, "Disable"))
767 wsel->s->status |= PROXY_WORKER_DISABLED;
768 else if (!strcasecmp(val, "Enable"))
769 wsel->s->status &= ~PROXY_WORKER_DISABLED;
771 if ((val = apr_table_get(params, "ls"))) {
772 int ival = atoi(val);
773 if (ival >= 0 && ival <= 99) {
774 wsel->s->lbset = ival;
779 if (apr_table_get(params, "xml")) {
780 ap_set_content_type(r, "text/xml");
781 ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
782 ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
783 ap_rputs(" <httpd:balancers>\n", r);
784 balancer = (proxy_balancer *)conf->balancers->elts;
785 for (i = 0; i < conf->balancers->nelts; i++) {
786 ap_rputs(" <httpd:balancer>\n", r);
787 ap_rvputs(r, " <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
788 ap_rputs(" <httpd:workers>\n", r);
789 worker = (proxy_worker *)balancer->workers->elts;
790 for (n = 0; n < balancer->workers->nelts; n++) {
791 ap_rputs(" <httpd:worker>\n", r);
792 ap_rvputs(r, " <httpd:scheme>", worker->scheme,
793 "</httpd:scheme>\n", NULL);
794 ap_rvputs(r, " <httpd:hostname>", worker->hostname,
795 "</httpd:hostname>\n", NULL);
796 ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
797 worker->s->lbfactor);
798 ap_rputs(" </httpd:worker>\n", r);
801 ap_rputs(" </httpd:workers>\n", r);
802 ap_rputs(" </httpd:balancer>\n", r);
805 ap_rputs(" </httpd:balancers>\n", r);
806 ap_rputs("</httpd:manager>", r);
809 ap_set_content_type(r, "text/html; charset=ISO-8859-1");
810 ap_rputs(DOCTYPE_HTML_3_2
811 "<html><head><title>Balancer Manager</title></head>\n", r);
812 ap_rputs("<body><h1>Load Balancer Manager for ", r);
813 ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
814 ap_rvputs(r, "<dl><dt>Server Version: ",
815 ap_get_server_description(), "</dt>\n", NULL);
816 ap_rvputs(r, "<dt>Server Built: ",
817 ap_get_server_built(), "\n</dt></dl>\n", NULL);
818 balancer = (proxy_balancer *)conf->balancers->elts;
819 for (i = 0; i < conf->balancers->nelts; i++) {
821 ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
822 ap_rvputs(r, balancer->name, "</h3>\n\n", NULL);
823 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
824 "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
826 if (balancer->sticky) {
827 if (strcmp(balancer->sticky, balancer->sticky_path)) {
828 ap_rvputs(r, "<td>", balancer->sticky, " | ",
829 balancer->sticky_path, NULL);
832 ap_rvputs(r, "<td>", balancer->sticky, NULL);
836 ap_rputs("<td> - ", r);
838 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
839 apr_time_sec(balancer->timeout));
840 ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
841 ap_rprintf(r, "<td>%s</td>\n",
842 balancer->lbmethod->name);
843 ap_rputs("</table>\n<br />", r);
844 ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
845 "<th>Worker URL</th>"
846 "<th>Route</th><th>RouteRedir</th>"
847 "<th>Factor</th><th>Set</th><th>Status</th>"
848 "<th>Elected</th><th>To</th><th>From</th>"
851 worker = (proxy_worker *)balancer->workers->elts;
852 for (n = 0; n < balancer->workers->nelts; n++) {
854 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
855 balancer->name + sizeof("balancer://") - 1, "&w=",
856 ap_escape_uri(r->pool, worker->name),
857 "&nonce=", balancer_nonce,
859 ap_rvputs(r, worker->name, "</a></td>", NULL);
860 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
862 ap_rvputs(r, "</td><td>",
863 ap_escape_html(r->pool, worker->s->redirect), NULL);
864 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
865 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
866 if (worker->s->status & PROXY_WORKER_DISABLED)
868 if (worker->s->status & PROXY_WORKER_IN_ERROR)
870 if (worker->s->status & PROXY_WORKER_STOPPED)
871 ap_rputs("Stop ", r);
872 if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
873 ap_rputs("Stby ", r);
874 if (PROXY_WORKER_IS_USABLE(worker))
876 if (!PROXY_WORKER_IS_INITIALIZED(worker))
878 ap_rputs("</td>", r);
879 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
880 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
881 ap_rputs("</td><td>", r);
882 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
883 ap_rputs("</td></tr>\n", r);
887 ap_rputs("</table>\n", r);
890 ap_rputs("<hr />\n", r);
892 ap_rputs("<h3>Edit worker settings for ", r);
893 ap_rvputs(r, wsel->name, "</h3>\n", NULL);
894 ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
895 ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
896 ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
897 ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbfactor);
898 ap_rputs("<tr><td>LB Set:</td><td><input name=\"ls\" type=text ", r);
899 ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbset);
900 ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
901 ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->route),
903 ap_rputs("\"></td></tr>\n", r);
904 ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
905 ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->redirect),
907 ap_rputs("\"></td></tr>\n", r);
908 ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
909 if (wsel->s->status & PROXY_WORKER_DISABLED)
910 ap_rputs(" checked", r);
911 ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
912 if (!(wsel->s->status & PROXY_WORKER_DISABLED))
913 ap_rputs(" checked", r);
914 ap_rputs("></td></tr>\n", r);
915 ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
916 ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ", NULL);
917 ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
918 ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
919 ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
921 ap_rvputs(r, "<input type=hidden name=\"nonce\" value=\"",
922 balancer_nonce, "\">\n", NULL);
923 ap_rvputs(r, "</form>\n", NULL);
924 ap_rputs("<hr />\n", r);
926 ap_rputs(ap_psignature("",r), r);
927 ap_rputs("</body></html>\n", r);
932 static void child_init(apr_pool_t *p, server_rec *s)
935 void *sconf = s->module_config;
936 proxy_server_conf *conf;
937 proxy_balancer *balancer;
939 conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
941 /* Initialize shared scoreboard data */
942 balancer = (proxy_balancer *)conf->balancers->elts;
943 for (i = 0; i < conf->balancers->nelts; i++) {
944 proxy_balancer_method *lbmethod = balancer->lbmethod;
945 if (balancer->lbmethod!=NULL && balancer->lbmethod->reset != NULL)
946 balancer->lbmethod->reset(balancer, s);
947 init_balancer_members(conf, s, balancer);
955 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
957 /* Only the mpm_winnt has child init hook handler.
958 * make sure that we are called after the mpm
961 static const char *const aszPred[] = { "mpm_winnt.c", NULL};
962 /* manager handler */
963 ap_hook_post_config(balancer_init, NULL, NULL, APR_HOOK_MIDDLE);
964 ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
965 ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
966 proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
967 proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
968 proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
971 module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
972 STANDARD20_MODULE_STUFF,
973 NULL, /* create per-directory config structure */
974 NULL, /* merge per-directory config structures */
975 NULL, /* create per-server config structure */
976 NULL, /* merge per-server config structures */
977 NULL, /* command apr_table_t */
978 ap_proxy_balancer_register_hook /* register hooks */