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"
26 static const char *balancer_mutex_type = "proxy-balancer-shm";
27 ap_slotmem_provider_t *storage = NULL;
29 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
31 static int (*ap_proxy_retry_worker_fn)(const char *proxy_function,
32 proxy_worker *worker, server_rec *s) = NULL;
35 * Register our mutex type before the config is read so we
36 * can adjust the mutex settings using the Mutex directive.
38 static int balancer_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
44 rv = ap_mutex_register(pconf, balancer_mutex_type, NULL,
46 if (rv != APR_SUCCESS) {
54 extern void proxy_update_members(proxy_balancer **balancer, request_rec *r,
55 proxy_server_conf *conf);
58 static int proxy_balancer_canon(request_rec *r, char *url)
65 /* TODO: offset of BALANCER_PREFIX ?? */
66 if (strncasecmp(url, "balancer:", 9) == 0) {
73 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, "canonicalising URL %s", url);
76 * We break the URL into host, port, path, search
78 err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
80 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01157)
81 "error parsing URL %s: %s",
83 return HTTP_BAD_REQUEST;
86 * now parse path/search args, according to rfc1738:
87 * process the path. With proxy-noncanon set (by
88 * mod_proxy) we use the raw, unparsed uri
90 if (apr_table_get(r->notes, "proxy-nocanon")) {
91 path = url; /* this is the raw path */
94 path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
99 return HTTP_BAD_REQUEST;
101 r->filename = apr_pstrcat(r->pool, "proxy:", BALANCER_PREFIX, host,
102 "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
104 r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
109 static void init_balancer_members(apr_pool_t *p, server_rec *s,
110 proxy_balancer *balancer)
113 proxy_worker **workers;
115 workers = (proxy_worker **)balancer->workers->elts;
117 for (i = 0; i < balancer->workers->nelts; i++) {
118 int worker_is_initialized;
119 proxy_worker *worker = *workers;
120 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01158)
121 "Looking at %s -> %s initialized?", balancer->s->name, worker->s->name);
122 worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(worker);
123 if (!worker_is_initialized) {
124 ap_proxy_initialize_worker(worker, s, p);
129 /* Set default number of attempts to the number of
132 if (!balancer->s->max_attempts_set && balancer->workers->nelts > 1) {
133 balancer->s->max_attempts = balancer->workers->nelts - 1;
134 balancer->s->max_attempts_set = 1;
138 /* Retrieve the parameter with the given name
139 * Something like 'JSESSIONID=12345...N'
141 static char *get_path_param(apr_pool_t *pool, char *url,
142 const char *name, int scolon_sep)
145 char *pathdelims = "?&";
150 for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
151 path += strlen(name);
154 * Session path was found, get its value
159 path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
167 static char *get_cookie_param(request_rec *r, const char *name)
170 const char *start_cookie;
172 if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
173 for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
174 start_cookie = ap_strstr_c(start_cookie + 1, name)) {
175 if (start_cookie == cookies ||
176 start_cookie[-1] == ';' ||
177 start_cookie[-1] == ',' ||
178 isspace(start_cookie[-1])) {
180 start_cookie += strlen(name);
181 while(*start_cookie && isspace(*start_cookie))
183 if (*start_cookie++ == '=' && *start_cookie) {
185 * Session cookie was found, get its value
187 char *end_cookie, *cookie;
188 cookie = apr_pstrdup(r->pool, start_cookie);
189 if ((end_cookie = strchr(cookie, ';')) != NULL)
191 if((end_cookie = strchr(cookie, ',')) != NULL)
201 /* Find the worker that has the 'route' defined
203 static proxy_worker *find_route_worker(proxy_balancer *balancer,
204 const char *route, request_rec *r)
207 int checking_standby;
210 proxy_worker **workers;
212 checking_standby = checked_standby = 0;
213 while (!checked_standby) {
214 workers = (proxy_worker **)balancer->workers->elts;
215 for (i = 0; i < balancer->workers->nelts; i++, workers++) {
216 proxy_worker *worker = *workers;
217 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
219 if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
220 if (worker && PROXY_WORKER_IS_USABLE(worker)) {
224 * If the worker is in error state run
225 * retry on that worker. It will be marked as
226 * operational if the retry timeout is elapsed.
227 * The worker might still be unusable, but we try
230 ap_proxy_retry_worker_fn("BALANCER", worker, r->server);
231 if (PROXY_WORKER_IS_USABLE(worker)) {
235 * We have a worker that is unusable.
236 * It can be in error or disabled, but in case
237 * it has a redirection set use that redirection worker.
238 * This enables to safely remove the member from the
239 * balancer. Of course you will need some kind of
240 * session replication between those two remote.
242 if (*worker->s->redirect) {
243 proxy_worker *rworker = NULL;
244 rworker = find_route_worker(balancer, worker->s->redirect, r);
245 /* Check if the redirect worker is usable */
246 if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
248 * If the worker is in error state run
249 * retry on that worker. It will be marked as
250 * operational if the retry timeout is elapsed.
251 * The worker might still be unusable, but we try
254 ap_proxy_retry_worker_fn("BALANCER", rworker, r->server);
256 if (rworker && PROXY_WORKER_IS_USABLE(rworker))
263 checked_standby = checking_standby++;
268 static proxy_worker *find_session_route(proxy_balancer *balancer,
271 const char **sticky_used,
274 proxy_worker *worker = NULL;
276 if (!*balancer->s->sticky)
278 /* Try to find the sticky route inside url */
279 *route = get_path_param(r->pool, *url, balancer->s->sticky_path, balancer->s->scolonsep);
281 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01159)
282 "Found value %s for stickysession %s",
283 *route, balancer->s->sticky_path);
284 *sticky_used = balancer->s->sticky_path;
287 *route = get_cookie_param(r, balancer->s->sticky);
289 *sticky_used = balancer->s->sticky;
290 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01160)
291 "Found value %s for stickysession %s",
292 *route, balancer->s->sticky);
296 * If we found a value for sticksession, find the first '.' within.
297 * Everything after '.' (if present) is our route.
299 if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
301 if ((*route) && (**route)) {
302 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01161) "Found route %s", *route);
303 /* We have a route in path or in cookie
304 * Find the worker that has this route defined.
306 worker = find_route_worker(balancer, *route, r);
307 if (worker && strcmp(*route, worker->s->route)) {
309 * Notice that the route of the worker chosen is different from
310 * the route supplied by the client.
312 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
313 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01162)
314 "Route changed from %s to %s",
315 *route, worker->s->route);
323 static proxy_worker *find_best_worker(proxy_balancer *balancer,
326 proxy_worker *candidate = NULL;
329 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
330 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01163)
331 "%s: Lock failed for find_best_worker()",
336 candidate = (*balancer->lbmethod->finder)(balancer, r);
339 candidate->s->elected++;
341 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
342 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01164)
343 "%s: Unlock failed for find_best_worker()",
347 if (candidate == NULL) {
348 /* All the workers are in error state or disabled.
349 * If the balancer has a timeout sleep for a while
350 * and try again to find the worker. The chances are
351 * that some other thread will release a connection.
352 * By default the timeout is not set, and the server
353 * returns SERVER_BUSY.
355 if (balancer->s->timeout) {
356 /* XXX: This can perhaps be build using some
357 * smarter mechanism, like tread_cond.
358 * But since the statuses can came from
359 * different childs, use the provided algo.
361 apr_interval_time_t timeout = balancer->s->timeout;
362 apr_interval_time_t step, tval = 0;
363 /* Set the timeout to 0 so that we don't
364 * end in infinite loop
366 balancer->s->timeout = 0;
367 step = timeout / 100;
368 while (tval < timeout) {
371 if ((candidate = find_best_worker(balancer, r)))
375 /* restore the timeout */
376 balancer->s->timeout = timeout;
384 static int rewrite_url(request_rec *r, proxy_worker *worker,
387 const char *scheme = strstr(*url, "://");
388 const char *path = NULL;
391 path = ap_strchr_c(scheme + 3, '/');
393 /* we break the URL into host, port, uri */
395 return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
396 "missing worker. URI cannot be parsed: ", *url,
400 *url = apr_pstrcat(r->pool, worker->s->name, path, NULL);
405 static void force_recovery(proxy_balancer *balancer, server_rec *s)
409 proxy_worker **worker;
411 worker = (proxy_worker **)balancer->workers->elts;
412 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
413 if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
418 /* Try if we can recover */
419 ap_proxy_retry_worker_fn("BALANCER", *worker, s);
420 if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
427 /* If all workers are in error state force the recovery.
429 worker = (proxy_worker **)balancer->workers->elts;
430 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
431 ++(*worker)->s->retries;
432 (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
433 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01165)
434 "%s: Forcing recovery for worker (%s)",
435 balancer->s->name, (*worker)->s->hostname);
440 static int proxy_balancer_pre_request(proxy_worker **worker,
441 proxy_balancer **balancer,
443 proxy_server_conf *conf, char **url)
446 proxy_worker *runtime;
448 const char *sticky = NULL;
452 /* Step 1: check if the url is for us
453 * The url we can handle starts with 'balancer://'
454 * If balancer is already provided skip the search
455 * for balancer, because this is failover attempt.
458 !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url, 1)))
461 /* Step 2: Lock the LoadBalancer
462 * XXX: perhaps we need the process lock here
464 if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
465 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01166)
466 "%s: Lock failed for pre_request", (*balancer)->s->name);
470 /* Step 3: force recovery */
471 force_recovery(*balancer, r->server);
473 /* Step 3.5: Update member list for the balancer */
474 /* TODO: Implement as provider! */
475 ap_proxy_sync_balancer(*balancer, r->server, conf);
477 /* Step 4: find the session route */
478 runtime = find_session_route(*balancer, r, &route, &sticky, url);
480 if ((*balancer)->lbmethod && (*balancer)->lbmethod->updatelbstatus) {
481 /* Call the LB implementation */
482 (*balancer)->lbmethod->updatelbstatus(*balancer, runtime, r->server);
484 else { /* Use the default one */
485 int i, total_factor = 0;
486 proxy_worker **workers;
487 /* We have a sticky load balancer
488 * Update the workers status
489 * so that even session routes get
492 workers = (proxy_worker **)(*balancer)->workers->elts;
493 for (i = 0; i < (*balancer)->workers->nelts; i++) {
494 /* Take into calculation only the workers that are
495 * not in error state or not disabled.
497 if (PROXY_WORKER_IS_USABLE(*workers)) {
498 (*workers)->s->lbstatus += (*workers)->s->lbfactor;
499 total_factor += (*workers)->s->lbfactor;
503 runtime->s->lbstatus -= total_factor;
505 runtime->s->elected++;
509 else if (route && (*balancer)->s->sticky_force) {
510 int i, member_of = 0;
511 proxy_worker **workers;
513 * We have a route provided that doesn't match the
514 * balancer name. See if the provider route is the
515 * member of the same balancer in which case return 503
517 workers = (proxy_worker **)(*balancer)->workers->elts;
518 for (i = 0; i < (*balancer)->workers->nelts; i++) {
519 if (*((*workers)->s->route) && strcmp((*workers)->s->route, route) == 0) {
526 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01167)
527 "%s: All workers are in error state for route (%s)",
528 (*balancer)->s->name, route);
529 if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
530 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01168)
531 "%s: Unlock failed for pre_request",
532 (*balancer)->s->name);
534 return HTTP_SERVICE_UNAVAILABLE;
538 if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
539 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01169)
540 "%s: Unlock failed for pre_request",
541 (*balancer)->s->name);
544 runtime = find_best_worker(*balancer, r);
546 if ((*balancer)->workers->nelts) {
547 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01170)
548 "%s: All workers are in error state",
549 (*balancer)->s->name);
551 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01171)
552 "%s: No workers in balancer",
553 (*balancer)->s->name);
556 return HTTP_SERVICE_UNAVAILABLE;
558 if (*(*balancer)->s->sticky && runtime) {
560 * This balancer has sticky sessions and the client either has not
561 * supplied any routing information or all workers for this route
562 * including possible redirect and hotstandby workers are in error
563 * state, but we have found another working worker for this
564 * balancer where we can send the request. Thus notice that we have
565 * changed the route to the backend.
567 apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
572 (*worker)->s->busy++;
574 /* Add balancer/worker info to env. */
575 apr_table_setn(r->subprocess_env,
576 "BALANCER_NAME", (*balancer)->s->name);
577 apr_table_setn(r->subprocess_env,
578 "BALANCER_WORKER_NAME", (*worker)->s->name);
579 apr_table_setn(r->subprocess_env,
580 "BALANCER_WORKER_ROUTE", (*worker)->s->route);
582 /* Rewrite the url from 'balancer://url'
583 * to the 'worker_scheme://worker_hostname[:worker_port]/url'
584 * This replaces the balancers fictional name with the
585 * real hostname of the elected worker.
587 access_status = rewrite_url(r, *worker, url);
588 /* Add the session route to request notes if present */
590 apr_table_setn(r->notes, "session-sticky", sticky);
591 apr_table_setn(r->notes, "session-route", route);
593 /* Add session info to env. */
594 apr_table_setn(r->subprocess_env,
595 "BALANCER_SESSION_STICKY", sticky);
596 apr_table_setn(r->subprocess_env,
597 "BALANCER_SESSION_ROUTE", route);
599 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01172)
600 "%s: worker (%s) rewritten to %s",
601 (*balancer)->s->name, (*worker)->s->name, *url);
603 return access_status;
606 static int proxy_balancer_post_request(proxy_worker *worker,
607 proxy_balancer *balancer,
609 proxy_server_conf *conf)
614 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
615 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01173)
616 "%s: Lock failed for post_request",
618 return HTTP_INTERNAL_SERVER_ERROR;
621 if (!apr_is_empty_array(balancer->errstatuses)) {
623 for (i = 0; i < balancer->errstatuses->nelts; i++) {
624 int val = ((int *)balancer->errstatuses->elts)[i];
625 if (r->status == val) {
626 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01174)
627 "%s: Forcing recovery for worker (%s), "
629 balancer->s->name, worker->s->name, val);
630 worker->s->status |= PROXY_WORKER_IN_ERROR;
631 worker->s->error_time = apr_time_now();
637 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
638 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01175)
639 "%s: Unlock failed for post_request", balancer->s->name);
641 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01176)
642 "proxy_balancer_post_request for (%s)", balancer->s->name);
644 if (worker && worker->s->busy)
651 static void recalc_factors(proxy_balancer *balancer)
654 proxy_worker **workers;
657 /* Recalculate lbfactors */
658 workers = (proxy_worker **)balancer->workers->elts;
659 /* Special case if there is only one worker its
660 * load factor will always be 1
662 if (balancer->workers->nelts == 1) {
663 (*workers)->s->lbstatus = (*workers)->s->lbfactor = 1;
666 for (i = 0; i < balancer->workers->nelts; i++) {
667 /* Update the status entries */
668 workers[i]->s->lbstatus = workers[i]->s->lbfactor;
672 static apr_status_t lock_remove(void *data)
675 proxy_balancer *balancer;
676 server_rec *s = data;
677 void *sconf = s->module_config;
678 proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
680 balancer = (proxy_balancer *)conf->balancers->elts;
681 for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
682 if (balancer->gmutex) {
683 apr_global_mutex_destroy(balancer->gmutex);
684 balancer->gmutex = NULL;
690 /* post_config hook: */
691 static int balancer_post_config(apr_pool_t *pconf, apr_pool_t *plog,
692 apr_pool_t *ptemp, server_rec *s)
695 void *sconf = s->module_config;
696 proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
697 ap_slotmem_instance_t *new = NULL;
700 /* balancer_post_config() will be called twice during startup. So, don't
701 * set up the static data the 1st time through. */
702 if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) {
706 if (!ap_proxy_retry_worker_fn) {
707 ap_proxy_retry_worker_fn =
708 APR_RETRIEVE_OPTIONAL_FN(ap_proxy_retry_worker);
709 if (!ap_proxy_retry_worker_fn) {
710 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02230)
711 "mod_proxy must be loaded for mod_proxy_balancer");
719 storage = ap_lookup_provider(AP_SLOTMEM_PROVIDER_GROUP, "shm",
720 AP_SLOTMEM_PROVIDER_VERSION);
722 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01177)
723 "Failed to lookup provider 'shm' for '%s': is "
724 "mod_slotmem_shm loaded??",
725 AP_SLOTMEM_PROVIDER_GROUP);
729 tstamp = apr_time_now();
731 * Go thru each Vhost and create the shared mem slotmem for
732 * each balancer's workers
736 proxy_balancer *balancer;
737 sconf = s->module_config;
738 conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
740 if (conf->balancers->nelts) {
741 conf->max_balancers = conf->balancers->nelts + conf->bgrowth;
742 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01178) "Doing balancers create: %d, %d (%d)",
743 (int)ALIGNED_PROXY_BALANCER_SHARED_SIZE,
744 (int)conf->balancers->nelts, conf->max_balancers);
746 rv = storage->create(&new, conf->id,
747 ALIGNED_PROXY_BALANCER_SHARED_SIZE,
748 conf->max_balancers, AP_SLOTMEM_TYPE_PREGRAB, pconf);
749 if (rv != APR_SUCCESS) {
750 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01179) "balancer slotmem_create failed");
755 conf->storage = storage;
757 /* Initialize shared scoreboard data */
758 balancer = (proxy_balancer *)conf->balancers->elts;
759 for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
760 proxy_worker **workers;
761 proxy_worker *worker;
762 proxy_balancer_shared *bshm;
765 balancer->max_workers = balancer->workers->nelts + balancer->growth;
767 /* Create global mutex */
768 rv = ap_global_mutex_create(&(balancer->gmutex), NULL, balancer_mutex_type,
769 balancer->s->sname, s, pconf, 0);
770 if (rv != APR_SUCCESS || !balancer->gmutex) {
771 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01180)
772 "mutex creation of %s : %s failed", balancer_mutex_type,
774 return HTTP_INTERNAL_SERVER_ERROR;
777 apr_pool_cleanup_register(pconf, (void *)s, lock_remove,
778 apr_pool_cleanup_null);
780 /* setup shm for balancers */
781 if ((rv = storage->grab(conf->bslot, &index)) != APR_SUCCESS) {
782 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01181) "balancer slotmem_grab failed");
786 if ((rv = storage->dptr(conf->bslot, index, (void *)&bshm)) != APR_SUCCESS) {
787 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01182) "balancer slotmem_dptr failed");
790 if ((rv = ap_proxy_share_balancer(balancer, bshm, index)) != APR_SUCCESS) {
791 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01183) "Cannot share balancer");
795 /* create slotmem slots for workers */
796 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01184) "Doing workers create: %s (%s), %d, %d",
797 balancer->s->name, balancer->s->sname,
798 (int)ALIGNED_PROXY_WORKER_SHARED_SIZE,
799 (int)balancer->max_workers);
801 rv = storage->create(&new, balancer->s->sname,
802 ALIGNED_PROXY_WORKER_SHARED_SIZE,
803 balancer->max_workers, AP_SLOTMEM_TYPE_PREGRAB, pconf);
804 if (rv != APR_SUCCESS) {
805 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01185) "worker slotmem_create failed");
808 balancer->wslot = new;
809 balancer->storage = storage;
811 /* sync all timestamps */
812 balancer->wupdated = balancer->s->wupdated = tstamp;
814 /* now go thru each worker */
815 workers = (proxy_worker **)balancer->workers->elts;
816 for (j = 0; j < balancer->workers->nelts; j++, workers++) {
817 proxy_worker_shared *shm;
820 if ((rv = storage->grab(balancer->wslot, &index)) != APR_SUCCESS) {
821 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01186) "worker slotmem_grab failed");
825 if ((rv = storage->dptr(balancer->wslot, index, (void *)&shm)) != APR_SUCCESS) {
826 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01187) "worker slotmem_dptr failed");
829 if ((rv = ap_proxy_share_worker(worker, shm, index)) != APR_SUCCESS) {
830 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01188) "Cannot share worker");
833 worker->s->updated = tstamp;
842 static void create_radio(const char *name, unsigned int flag, request_rec *r)
844 ap_rvputs(r, "<td>On <input name='", name, "' id='", name, "' value='1' type=radio", NULL);
846 ap_rputs(" checked", r);
847 ap_rvputs(r, "> <br/> Off <input name='", name, "' id='", name, "' value='0' type=radio", NULL);
849 ap_rputs(" checked", r);
850 ap_rputs("></td>\n", r);
853 static void push2table(const char *input, apr_table_t *params,
854 const char *allowed[], apr_pool_t *p)
863 args = apr_pstrdup(p, input);
865 key = apr_strtok(args, "&", &tok);
867 val = strchr(key, '=');
874 ap_unescape_url(key);
875 ap_unescape_url(val);
876 if (allowed == NULL) { /* allow all */
877 apr_table_set(params, key, val);
880 const char *ok = *allowed;
882 if (strcmp(ok, key) == 0) {
883 apr_table_set(params, key, val);
889 key = apr_strtok(NULL, "&", &tok);
893 /* Manages the loadfactors and member status
894 * The balancer, worker and nonce are obtained from
895 * the request args (?b=...&w=...&nonce=....).
896 * All other params are pulled from any POST
899 * /.../<whatever>/balancer/worker/nonce
901 static int balancer_handler(request_rec *r)
904 proxy_server_conf *conf;
905 proxy_balancer *balancer, *bsel = NULL;
906 proxy_worker *worker, *wsel = NULL;
907 proxy_worker **workers = NULL;
915 /* is this for us? */
916 if (strcmp(r->handler, "balancer-manager")) {
921 | (AP_METHOD_BIT << M_GET)
922 | (AP_METHOD_BIT << M_POST);
923 if ((r->method_number != M_GET) && (r->method_number != M_POST)) {
927 sconf = r->server->module_config;
928 conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
929 params = apr_table_make(r->pool, 10);
931 balancer = (proxy_balancer *)conf->balancers->elts;
932 for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
933 if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
934 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01189)
935 "%s: Lock failed for balancer_handler",
938 ap_proxy_sync_balancer(balancer, r->server, conf);
939 if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
940 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01190)
941 "%s: Unlock failed for balancer_handler",
946 if (r->args && (r->method_number == M_GET)) {
947 const char *allowed[] = { "w", "b", "nonce", NULL };
948 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01191) "parsing r->args");
950 push2table(r->args, params, allowed, r->pool);
952 if (r->method_number == M_POST) {
953 apr_bucket_brigade *ib;
954 apr_size_t len = 1024;
955 char *buf = apr_pcalloc(r->pool, len+1);
957 ib = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
958 rv = ap_get_brigade(r->input_filters, ib, AP_MODE_READBYTES,
959 APR_BLOCK_READ, len);
960 if (rv != APR_SUCCESS) {
961 return HTTP_INTERNAL_SERVER_ERROR;
963 apr_brigade_flatten(ib, buf, &len);
965 push2table(buf, params, NULL, r->pool);
967 if ((name = apr_table_get(params, "b")))
968 bsel = ap_proxy_get_balancer(r->pool, conf,
969 apr_pstrcat(r->pool, BALANCER_PREFIX, name, NULL), 0);
971 if ((name = apr_table_get(params, "w"))) {
972 wsel = ap_proxy_get_worker(r->pool, bsel, conf, name);
976 /* Check that the supplied nonce matches this server's nonce;
977 * otherwise ignore all parameters, to prevent a CSRF attack. */
981 (name = apr_table_get(params, "nonce")) == NULL ||
982 strcmp(bsel->s->nonce, name) != 0
986 apr_table_clear(params);
990 /* First set the params */
991 if (wsel && ok2change) {
993 int was_usable = PROXY_WORKER_IS_USABLE(wsel);
995 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01192) "settings worker params");
997 if ((val = apr_table_get(params, "w_lf"))) {
998 int ival = atoi(val);
999 if (ival >= 1 && ival <= 100) {
1000 wsel->s->lbfactor = ival;
1002 recalc_factors(bsel);
1005 if ((val = apr_table_get(params, "w_wr"))) {
1006 if (strlen(val) && strlen(val) < sizeof(wsel->s->route))
1007 strcpy(wsel->s->route, val);
1009 *wsel->s->route = '\0';
1011 if ((val = apr_table_get(params, "w_rr"))) {
1012 if (strlen(val) && strlen(val) < sizeof(wsel->s->redirect))
1013 strcpy(wsel->s->redirect, val);
1015 *wsel->s->redirect = '\0';
1017 if ((val = apr_table_get(params, "w_status_I"))) {
1018 ap_proxy_set_wstatus('I', atoi(val), wsel);
1020 if ((val = apr_table_get(params, "w_status_N"))) {
1021 ap_proxy_set_wstatus('N', atoi(val), wsel);
1023 if ((val = apr_table_get(params, "w_status_D"))) {
1024 ap_proxy_set_wstatus('D', atoi(val), wsel);
1026 if ((val = apr_table_get(params, "w_status_H"))) {
1027 ap_proxy_set_wstatus('H', atoi(val), wsel);
1029 if ((val = apr_table_get(params, "w_ls"))) {
1030 int ival = atoi(val);
1031 if (ival >= 0 && ival <= 99) {
1032 wsel->s->lbset = ival;
1035 /* if enabling, we need to reset all lb params */
1036 if (bsel && !was_usable && PROXY_WORKER_IS_USABLE(wsel)) {
1037 bsel->s->need_reset = 1;
1042 if (bsel && ok2change) {
1045 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01193)
1046 "settings balancer params");
1047 if ((val = apr_table_get(params, "b_lbm"))) {
1048 if ((strlen(val) < (sizeof(bsel->s->lbpname)-1)) &&
1049 strcmp(val, bsel->s->lbpname)) {
1050 proxy_balancer_method *lbmethod;
1051 lbmethod = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
1053 PROXY_STRNCPY(bsel->s->lbpname, val);
1054 bsel->lbmethod = lbmethod;
1055 bsel->s->wupdated = apr_time_now();
1056 bsel->s->need_reset = 1;
1060 if ((val = apr_table_get(params, "b_tmo"))) {
1062 if (ival >= 0 && ival <= 7200) { /* 2 hrs enuff? */
1063 bsel->s->timeout = apr_time_from_sec(ival);
1066 if ((val = apr_table_get(params, "b_max"))) {
1068 if (ival >= 0 && ival <= 99) {
1069 bsel->s->max_attempts = ival;
1072 if ((val = apr_table_get(params, "b_sforce"))) {
1074 bsel->s->sticky_force = (ival != 0);
1076 if ((val = apr_table_get(params, "b_ss")) && *val) {
1077 if (strlen(val) < (sizeof(bsel->s->sticky_path)-1)) {
1078 if (*val == '-' && *(val+1) == '\0')
1079 *bsel->s->sticky_path = *bsel->s->sticky = '\0';
1082 PROXY_STRNCPY(bsel->s->sticky_path, val);
1083 PROXY_STRNCPY(bsel->s->sticky, val);
1085 if ((path = strchr((char *)bsel->s->sticky, '|'))) {
1087 PROXY_STRNCPY(bsel->s->sticky_path, path);
1092 if ((val = apr_table_get(params, "b_wyes")) &&
1093 (*val == '1' && *(val+1) == '\0') &&
1094 (val = apr_table_get(params, "b_nwrkr"))) {
1096 proxy_worker *nworker;
1097 nworker = ap_proxy_get_worker(conf->pool, bsel, conf, val);
1098 if (!nworker && storage->num_free_slots(bsel->wslot)) {
1099 if ((rv = PROXY_GLOBAL_LOCK(bsel)) != APR_SUCCESS) {
1100 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01194)
1101 "%s: Lock failed for adding worker",
1104 ret = ap_proxy_define_worker(conf->pool, &nworker, bsel, conf, val, 0);
1107 proxy_worker_shared *shm;
1108 PROXY_COPY_CONF_PARAMS(nworker, conf);
1109 if ((rv = storage->grab(bsel->wslot, &index)) != APR_SUCCESS) {
1110 ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01195)
1111 "worker slotmem_grab failed");
1112 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1113 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01196)
1114 "%s: Unlock failed for adding worker",
1117 return HTTP_BAD_REQUEST;
1119 if ((rv = storage->dptr(bsel->wslot, index, (void *)&shm)) != APR_SUCCESS) {
1120 ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01197)
1121 "worker slotmem_dptr failed");
1122 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1123 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01198)
1124 "%s: Unlock failed for adding worker",
1127 return HTTP_BAD_REQUEST;
1129 if ((rv = ap_proxy_share_worker(nworker, shm, index)) != APR_SUCCESS) {
1130 ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01199)
1131 "Cannot share worker");
1132 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1133 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01200)
1134 "%s: Unlock failed for adding worker",
1137 return HTTP_BAD_REQUEST;
1139 if ((rv = ap_proxy_initialize_worker(nworker, r->server, conf->pool)) != APR_SUCCESS) {
1140 ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01201)
1141 "Cannot init worker");
1142 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1143 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01202)
1144 "%s: Unlock failed for adding worker",
1147 return HTTP_BAD_REQUEST;
1149 /* sync all timestamps */
1150 bsel->wupdated = bsel->s->wupdated = nworker->s->updated = apr_time_now();
1151 /* by default, all new workers are disabled */
1152 ap_proxy_set_wstatus('D', 1, nworker);
1154 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1155 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01203)
1156 "%s: Unlock failed for adding worker",
1165 action = ap_construct_url(r->pool, r->uri, r);
1166 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01204) "genning page");
1168 if (apr_table_get(params, "xml")) {
1169 ap_set_content_type(r, "text/xml");
1170 ap_rputs("<?xml version='1.0' encoding='UTF-8' ?>\n", r);
1171 ap_rputs("<httpd:manager xmlns:httpd='http://httpd.apache.org'>\n", r);
1172 ap_rputs(" <httpd:balancers>\n", r);
1173 balancer = (proxy_balancer *)conf->balancers->elts;
1174 for (i = 0; i < conf->balancers->nelts; i++) {
1175 ap_rputs(" <httpd:balancer>\n", r);
1176 ap_rvputs(r, " <httpd:name>", balancer->s->name, "</httpd:name>\n", NULL);
1177 ap_rputs(" <httpd:workers>\n", r);
1178 workers = (proxy_worker **)balancer->workers->elts;
1179 for (n = 0; n < balancer->workers->nelts; n++) {
1181 ap_rputs(" <httpd:worker>\n", r);
1182 ap_rvputs(r, " <httpd:scheme>", worker->s->scheme,
1183 "</httpd:scheme>\n", NULL);
1184 ap_rvputs(r, " <httpd:hostname>", worker->s->hostname,
1185 "</httpd:hostname>\n", NULL);
1186 ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
1187 worker->s->lbfactor);
1188 ap_rputs(" </httpd:worker>\n", r);
1191 ap_rputs(" </httpd:workers>\n", r);
1192 ap_rputs(" </httpd:balancer>\n", r);
1195 ap_rputs(" </httpd:balancers>\n", r);
1196 ap_rputs("</httpd:manager>", r);
1199 ap_set_content_type(r, "text/html; charset=ISO-8859-1");
1200 ap_rputs(DOCTYPE_HTML_3_2
1201 "<html><head><title>Balancer Manager</title>\n", r);
1202 ap_rputs("<style type='text/css'>\n"
1204 " border-width: 1px;\n"
1205 " border-spacing: 3px;\n"
1206 " border-style: solid;\n"
1207 " border-color: gray;\n"
1208 " border-collapse: collapse;\n"
1209 " background-color: white;\n"
1210 " text-align: center;\n"
1213 " border-width: 1px;\n"
1215 " border-style: dotted;\n"
1216 " border-color: gray;\n"
1217 " background-color: white;\n"
1218 " text-align: center;\n"
1221 " border-width: 1px;\n"
1223 " border-style: dotted;\n"
1224 " border-color: gray;\n"
1225 " background-color: white;\n"
1226 " text-align: center;\n"
1228 "</style>\n</head>\n", r);
1229 ap_rputs("<body><h1>Load Balancer Manager for ", r);
1230 ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
1231 ap_rvputs(r, "<dl><dt>Server Version: ",
1232 ap_get_server_description(), "</dt>\n", NULL);
1233 ap_rvputs(r, "<dt>Server Built: ",
1234 ap_get_server_built(), "\n</dt></dl>\n", NULL);
1235 balancer = (proxy_balancer *)conf->balancers->elts;
1236 for (i = 0; i < conf->balancers->nelts; i++) {
1238 ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
1239 ap_rvputs(r, "<a href='", r->uri, "?b=",
1240 balancer->s->name + sizeof(BALANCER_PREFIX) - 1,
1241 "&nonce=", balancer->s->nonce,
1243 ap_rvputs(r, balancer->s->name, "</a></h3>\n", NULL);
1244 ap_rputs("\n\n<table><tr>"
1245 "<th>MaxMembers</th><th>StickySession</th><th>DisableFailover</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
1246 "<th>Path</th><th>Active</th></tr>\n<tr>", r);
1247 /* the below is a safe cast, since the number of slots total will
1248 * never be more than max_workers, which is restricted to int */
1249 ap_rprintf(r, "<td>%d [%d Used]</td>\n", balancer->max_workers,
1250 balancer->max_workers - (int)storage->num_free_slots(balancer->wslot));
1251 if (*balancer->s->sticky) {
1252 if (strcmp(balancer->s->sticky, balancer->s->sticky_path)) {
1253 ap_rvputs(r, "<td>", balancer->s->sticky, " | ",
1254 balancer->s->sticky_path, NULL);
1257 ap_rvputs(r, "<td>", balancer->s->sticky, NULL);
1261 ap_rputs("<td> (None) ", r);
1263 ap_rprintf(r, "<td>%s</td>\n",
1264 balancer->s->sticky_force ? "On" : "Off");
1265 ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
1266 apr_time_sec(balancer->s->timeout));
1267 ap_rprintf(r, "<td>%d</td>\n", balancer->s->max_attempts);
1268 ap_rprintf(r, "<td>%s</td>\n",
1269 balancer->s->lbpname);
1270 ap_rputs("<td>", r);
1271 if (balancer->s->vhost && *(balancer->s->vhost)) {
1272 ap_rvputs(r, balancer->s->vhost, " -> ", NULL);
1274 ap_rvputs(r, balancer->s->vpath, "</td>\n", NULL);
1275 ap_rprintf(r, "<td>%s</td>\n",
1276 !balancer->s->inactive ? "Yes" : "No");
1277 ap_rputs("</table>\n<br />", r);
1278 ap_rputs("\n\n<table><tr>"
1279 "<th>Worker URL</th>"
1280 "<th>Route</th><th>RouteRedir</th>"
1281 "<th>Factor</th><th>Set</th><th>Status</th>"
1282 "<th>Elected</th><th>Busy</th><th>Load</th><th>To</th><th>From</th>"
1285 workers = (proxy_worker **)balancer->workers->elts;
1286 for (n = 0; n < balancer->workers->nelts; n++) {
1289 ap_rvputs(r, "<tr>\n<td><a href='", r->uri, "?b=",
1290 balancer->s->name + sizeof(BALANCER_PREFIX) - 1, "&w=",
1291 ap_escape_uri(r->pool, worker->s->name),
1292 "&nonce=", balancer->s->nonce,
1294 ap_rvputs(r, worker->s->name, "</a></td>", NULL);
1295 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
1297 ap_rvputs(r, "</td><td>",
1298 ap_escape_html(r->pool, worker->s->redirect), NULL);
1299 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
1300 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
1301 ap_rvputs(r, ap_proxy_parse_wstatus(r->pool, worker), NULL);
1302 ap_rputs("</td>", r);
1303 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td>", worker->s->elected);
1304 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td>", worker->s->busy);
1305 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbstatus);
1306 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
1307 ap_rputs("</td><td>", r);
1308 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
1309 ap_rputs("</td></tr>\n", r);
1313 ap_rputs("</table>\n", r);
1316 ap_rputs("<hr />\n", r);
1318 ap_rputs("<h3>Edit worker settings for ", r);
1319 ap_rvputs(r, wsel->s->name, "</h3>\n", NULL);
1320 ap_rputs("<form method='POST' enctype='application/x-www-form-urlencoded' action='", r);
1321 ap_rvputs(r, action, "'>\n", NULL);
1322 ap_rputs("<dl>\n<table><tr><td>Load factor:</td><td><input name='w_lf' id='w_lf' type=text ", r);
1323 ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbfactor);
1324 ap_rputs("<tr><td>LB Set:</td><td><input name='w_ls' id='w_ls' type=text ", r);
1325 ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbset);
1326 ap_rputs("<tr><td>Route:</td><td><input name='w_wr' id='w_wr' type=text ", r);
1327 ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->route),
1329 ap_rputs("'></td></tr>\n", r);
1330 ap_rputs("<tr><td>Route Redirect:</td><td><input name='w_rr' id='w_rr' type=text ", r);
1331 ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->redirect),
1333 ap_rputs("'></td></tr>\n", r);
1334 ap_rputs("<tr><td>Status:</td>", r);
1335 ap_rputs("<td><table><tr><th>Ign</th><th>Drn</th><th>Dis</th><th>Stby</th></tr>\n<tr>", r);
1336 create_radio("w_status_I", (PROXY_WORKER_IGNORE_ERRORS & wsel->s->status), r);
1337 create_radio("w_status_N", (PROXY_WORKER_DRAIN & wsel->s->status), r);
1338 create_radio("w_status_D", (PROXY_WORKER_DISABLED & wsel->s->status), r);
1339 create_radio("w_status_H", (PROXY_WORKER_HOT_STANDBY & wsel->s->status), r);
1340 ap_rputs("</tr></table>\n", r);
1341 ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
1342 ap_rvputs(r, "</table>\n<input type=hidden name='w' id='w' ", NULL);
1343 ap_rvputs(r, "value='", ap_escape_uri(r->pool, wsel->s->name), "'>\n", NULL);
1344 ap_rvputs(r, "<input type=hidden name='b' id='b' ", NULL);
1345 ap_rvputs(r, "value='", bsel->s->name + sizeof(BALANCER_PREFIX) - 1,
1347 ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
1348 bsel->s->nonce, "'>\n", NULL);
1349 ap_rvputs(r, "</form>\n", NULL);
1350 ap_rputs("<hr />\n", r);
1352 const apr_array_header_t *provs;
1353 const ap_list_provider_names_t *pname;
1355 ap_rputs("<h3>Edit balancer settings for ", r);
1356 ap_rvputs(r, bsel->s->name, "</h3>\n", NULL);
1357 ap_rputs("<form method='POST' enctype='application/x-www-form-urlencoded' action='", r);
1358 ap_rvputs(r, action, "'>\n", NULL);
1359 ap_rputs("<dl>\n<table>\n", r);
1360 provs = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
1362 ap_rputs("<tr><td>LBmethod:</td>", r);
1363 ap_rputs("<td>\n<select name='b_lbm' id='b_lbm'>", r);
1364 pname = (ap_list_provider_names_t *)provs->elts;
1365 for (i = 0; i < provs->nelts; i++, pname++) {
1366 ap_rvputs(r,"<option value='", pname->provider_name, "'", NULL);
1367 if (strcmp(pname->provider_name, bsel->s->lbpname) == 0)
1368 ap_rputs(" selected ", r);
1369 ap_rvputs(r, ">", pname->provider_name, "\n", NULL);
1371 ap_rputs("</select>\n</td></tr>\n", r);
1373 ap_rputs("<tr><td>Timeout:</td><td><input name='b_tmo' id='b_tmo' type=text ", r);
1374 ap_rprintf(r, "value='%" APR_TIME_T_FMT "'></td></tr>\n", apr_time_sec(bsel->s->timeout));
1375 ap_rputs("<tr><td>Failover Attempts:</td><td><input name='b_max' id='b_max' type=text ", r);
1376 ap_rprintf(r, "value='%d'></td></tr>\n", bsel->s->max_attempts);
1377 ap_rputs("<tr><td>Disable Failover:</td>", r);
1378 create_radio("b_sforce", bsel->s->sticky_force, r);
1379 ap_rputs("<tr><td>Sticky Session:</td><td><input name='b_ss' id='b_ss' size=64 type=text ", r);
1380 if (strcmp(bsel->s->sticky, bsel->s->sticky_path)) {
1381 ap_rvputs(r, "value ='", bsel->s->sticky, " | ",
1382 bsel->s->sticky_path, NULL);
1385 ap_rvputs(r, "value ='", bsel->s->sticky, NULL);
1387 ap_rputs("'> (Use '-' to delete)</td></tr>\n", r);
1388 if (storage->num_free_slots(bsel->wslot) != 0) {
1389 ap_rputs("<tr><td>Add New Worker:</td><td><input name='b_nwrkr' id='b_nwrkr' size=32 type=text>"
1390 " Are you sure? <input name='b_wyes' id='b_wyes' type=checkbox value='1'>"
1393 ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
1394 ap_rvputs(r, "</table>\n<input type=hidden name='b' id='b' ", NULL);
1395 ap_rvputs(r, "value='", bsel->s->name + sizeof(BALANCER_PREFIX) - 1,
1397 ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
1398 bsel->s->nonce, "'>\n", NULL);
1399 ap_rvputs(r, "</form>\n", NULL);
1400 ap_rputs("<hr />\n", r);
1402 ap_rputs(ap_psignature("",r), r);
1403 ap_rputs("</body></html>\n", r);
1409 static void balancer_child_init(apr_pool_t *p, server_rec *s)
1412 proxy_balancer *balancer;
1414 void *sconf = s->module_config;
1415 proxy_server_conf *conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
1418 if (conf->balancers->nelts) {
1421 storage->attach(&(conf->bslot), conf->id, &size, &num, p);
1423 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01205) "slotmem_attach failed");
1424 exit(1); /* Ugly, but what else? */
1428 balancer = (proxy_balancer *)conf->balancers->elts;
1429 for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
1430 rv = ap_proxy_initialize_balancer(balancer, s, p);
1432 if (rv != APR_SUCCESS) {
1433 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(01206)
1434 "Failed to init balancer %s in child",
1436 exit(1); /* Ugly, but what else? */
1438 init_balancer_members(conf->pool, s, balancer);
1445 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
1447 /* Only the mpm_winnt has child init hook handler.
1448 * make sure that we are called after the mpm
1451 static const char *const aszPred[] = { "mpm_winnt.c", "mod_slotmem_shm.c", NULL};
1452 /* manager handler */
1453 ap_hook_post_config(balancer_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1454 ap_hook_pre_config(balancer_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1455 ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1456 ap_hook_child_init(balancer_child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1457 proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1458 proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1459 proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1462 AP_DECLARE_MODULE(proxy_balancer) = {
1463 STANDARD20_MODULE_STUFF,
1464 NULL, /* create per-directory config structure */
1465 NULL, /* merge per-directory config structures */
1466 NULL, /* create per-server config structure */
1467 NULL, /* merge per-server config structures */
1468 NULL, /* command apr_table_t */
1469 ap_proxy_balancer_register_hook /* register hooks */