]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy_balancer.c
Merge the fcgi-proxy-dev branch to trunk, adding a FastCGI back end for
[apache] / modules / proxy / mod_proxy_balancer.c
1 /* Copyright 1999-2006 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /* Load balancer module for Apache proxy */
18
19 #define CORE_PRIVATE
20
21 #include "mod_proxy.h"
22 #include "ap_mpm.h"
23 #include "apr_version.h"
24 #include "apr_hooks.h"
25
26 #if APR_HAVE_UNISTD_H
27 #include <unistd.h> /* for getpid() */
28 #endif
29
30 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
31
32 static int proxy_balancer_canon(request_rec *r, char *url)
33 {
34     char *host, *path, *search;
35     const char *err;
36     apr_port_t port = 0;
37
38     if (strncasecmp(url, "balancer:", 9) == 0) {
39         url += 9;
40     }
41     else {
42         return DECLINED;
43     }
44
45     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
46              "proxy: BALANCER: canonicalising URL %s", url);
47
48     /* do syntatic check.
49      * We break the URL into host, port, path, search
50      */
51     err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
52     if (err) {
53         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
54                       "error parsing URL %s: %s",
55                       url, err);
56         return HTTP_BAD_REQUEST;
57     }
58     /* now parse path/search args, according to rfc1738 */
59     /* N.B. if this isn't a true proxy request, then the URL _path_
60      * has already been decoded.  True proxy requests have r->uri
61      * == r->unparsed_uri, and no others have that property.
62      */
63     if (r->uri == r->unparsed_uri) {
64         search = strchr(url, '?');
65         if (search != NULL)
66             *(search++) = '\0';
67     }
68     else
69         search = r->args;
70
71     /* process path */
72     path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0, r->proxyreq);
73     if (path == NULL)
74         return HTTP_BAD_REQUEST;
75
76     r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
77             "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
78
79     r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
80
81     return OK;
82 }
83
84 static int init_balancer_members(proxy_server_conf *conf, server_rec *s,
85                                  proxy_balancer *balancer)
86 {
87     int i;
88     proxy_worker *workers;
89
90     workers = (proxy_worker *)balancer->workers->elts;
91
92     for (i = 0; i < balancer->workers->nelts; i++) {
93         ap_proxy_initialize_worker_share(conf, workers, s);
94         ap_proxy_initialize_worker(workers, s);
95         ++workers;
96     }
97
98     workers = (proxy_worker *)balancer->workers->elts;
99     for (i = 0; i < balancer->workers->nelts; i++) {
100         /* Set to the original configuration */
101         workers[i].s->lbstatus = workers[i].s->lbfactor =
102           (workers[i].lbfactor ? workers[i].lbfactor : 1);
103     }
104     /* Set default number of attempts to the number of
105      * workers.
106      */
107     if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
108         balancer->max_attempts = balancer->workers->nelts - 1;
109         balancer->max_attempts_set = 1;
110     }
111     return 0;
112 }
113
114 /* Retrieve the parameter with the given name
115  * Something like 'JSESSIONID=12345...N'
116  */
117 static char *get_path_param(apr_pool_t *pool, char *url,
118                             const char *name)
119 {
120     char *path = NULL;
121
122     for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
123         path += strlen(name);
124         if (*path == '=') {
125             /*
126              * Session path was found, get it's value
127              */
128             ++path;
129             if (strlen(path)) {
130                 char *q;
131                 path = apr_pstrdup(pool, path);
132                 if ((q = strchr(path, '?')))
133                     *q = '\0';
134                 return path;
135             }
136         }
137     }
138     return NULL;
139 }
140
141 static char *get_cookie_param(request_rec *r, const char *name)
142 {
143     const char *cookies;
144     const char *start_cookie;
145
146     if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
147         for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
148              start_cookie = ap_strstr_c(start_cookie + 1, name)) {
149             if (start_cookie == cookies ||
150                 start_cookie[-1] == ';' ||
151                 start_cookie[-1] == ',' ||
152                 isspace(start_cookie[-1])) {
153
154                 start_cookie += strlen(name);
155                 while(*start_cookie && isspace(*start_cookie))
156                     ++start_cookie;
157                 if (*start_cookie == '=' && start_cookie[1]) {
158                     /*
159                      * Session cookie was found, get it's value
160                      */
161                     char *end_cookie, *cookie;
162                     ++start_cookie;
163                     cookie = apr_pstrdup(r->pool, start_cookie);
164                     if ((end_cookie = strchr(cookie, ';')) != NULL)
165                         *end_cookie = '\0';
166                     if((end_cookie = strchr(cookie, ',')) != NULL)
167                         *end_cookie = '\0';
168                     return cookie;
169                 }
170             }
171         }
172     }
173     return NULL;
174 }
175
176 /* Find the worker that has the 'route' defined
177  */
178 static proxy_worker *find_route_worker(proxy_balancer *balancer,
179                                        const char *route)
180 {
181     int i;
182     proxy_worker *worker = (proxy_worker *)balancer->workers->elts;
183     for (i = 0; i < balancer->workers->nelts; i++) {
184         if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
185             return worker;
186         }
187         worker++;
188     }
189     return NULL;
190 }
191
192 static proxy_worker *find_session_route(proxy_balancer *balancer,
193                                         request_rec *r,
194                                         char **route,
195                                         char **url)
196 {
197     proxy_worker *worker = NULL;
198
199     if (!balancer->sticky)
200         return NULL;
201     /* Try to find the sticky route inside url */
202     *route = get_path_param(r->pool, *url, balancer->sticky);
203     if (!*route)
204         *route = get_cookie_param(r, balancer->sticky);
205     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
206                             "proxy: BALANCER: Found value %s for "
207                             "stickysession %s", *route, balancer->sticky);
208     /*
209      * If we found a value for sticksession, find the first '.' within.
210      * Everything after '.' (if present) is our route.
211      */
212     if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
213         (*route)++;
214     if ((*route) && (**route)) {
215         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
216                                   "proxy: BALANCER: Found route %s", *route);
217         /* We have a route in path or in cookie
218          * Find the worker that has this route defined.
219          */
220         worker = find_route_worker(balancer, *route);
221         if (worker && !PROXY_WORKER_IS_USABLE(worker)) {
222             /* We have a worker that is unusable.
223              * It can be in error or disabled, but in case
224              * it has a redirection set use that redirection worker.
225              * This enables to safely remove the member from the
226              * balancer. Of course you will need a some kind of
227              * session replication between those two remote.
228              */
229             if (*worker->s->redirect)
230                 worker = find_route_worker(balancer, worker->s->redirect);
231             /* Check if the redirect worker is usable */
232             if (worker && !PROXY_WORKER_IS_USABLE(worker))
233                 worker = NULL;
234         }
235         return worker;
236     }
237     else
238         return NULL;
239 }
240
241 static proxy_worker *find_best_worker(proxy_balancer *balancer,
242                                       request_rec *r)
243 {
244     proxy_worker *candidate = NULL;
245
246     if (PROXY_THREAD_LOCK(balancer) != APR_SUCCESS)
247         return NULL;
248
249     candidate = (*balancer->lbmethod->finder)(balancer, r);
250
251 /*
252         PROXY_THREAD_UNLOCK(balancer);
253         return NULL;
254 */
255
256     PROXY_THREAD_UNLOCK(balancer);
257
258     if (candidate == NULL) {
259         /* All the workers are in error state or disabled.
260          * If the balancer has a timeout sleep for a while
261          * and try again to find the worker. The chances are
262          * that some other thread will release a connection.
263          * By default the timeout is not set, and the server
264          * returns SERVER_BUSY.
265          */
266 #if APR_HAS_THREADS
267         if (balancer->timeout) {
268             /* XXX: This can perhaps be build using some
269              * smarter mechanism, like tread_cond.
270              * But since the statuses can came from
271              * different childs, use the provided algo.
272              */
273             apr_interval_time_t timeout = balancer->timeout;
274             apr_interval_time_t step, tval = 0;
275             /* Set the timeout to 0 so that we don't
276              * end in infinite loop
277              */
278             balancer->timeout = 0;
279             step = timeout / 100;
280             while (tval < timeout) {
281                 apr_sleep(step);
282                 /* Try again */
283                 if ((candidate = find_best_worker(balancer, r)))
284                     break;
285                 tval += step;
286             }
287             /* restore the timeout */
288             balancer->timeout = timeout;
289         }
290 #endif
291     }
292     return candidate;
293 }
294
295 static int rewrite_url(request_rec *r, proxy_worker *worker,
296                         char **url)
297 {
298     const char *scheme = strstr(*url, "://");
299     const char *path = NULL;
300
301     if (scheme)
302         path = ap_strchr_c(scheme + 3, '/');
303
304     /* we break the URL into host, port, uri */
305     if (!worker) {
306         return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
307                              "missing worker. URI cannot be parsed: ", *url,
308                              NULL));
309     }
310
311     *url = apr_pstrcat(r->pool, worker->name, path, NULL);
312
313     return OK;
314 }
315
316 static int proxy_balancer_pre_request(proxy_worker **worker,
317                                       proxy_balancer **balancer,
318                                       request_rec *r,
319                                       proxy_server_conf *conf, char **url)
320 {
321     int access_status;
322     proxy_worker *runtime;
323     char *route = NULL;
324     apr_status_t rv;
325
326     *worker = NULL;
327     /* Step 1: check if the url is for us
328      * The url we can handle starts with 'balancer://'
329      * If balancer is already provided skip the search
330      * for balancer, because this is failover attempt.
331      */
332     if (!*balancer &&
333         !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
334         return DECLINED;
335
336     /* Step 2: find the session route */
337
338     runtime = find_session_route(*balancer, r, &route, url);
339     /* Lock the LoadBalancer
340      * XXX: perhaps we need the process lock here
341      */
342     if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
343         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
344                      "proxy: BALANCER: lock");
345         return DECLINED;
346     }
347     if (runtime) {
348         int i, total_factor = 0;
349         proxy_worker *workers;
350         /* We have a sticky load balancer
351          * Update the workers status
352          * so that even session routes get
353          * into account.
354          */
355         workers = (proxy_worker *)(*balancer)->workers->elts;
356         for (i = 0; i < (*balancer)->workers->nelts; i++) {
357             /* Take into calculation only the workers that are
358              * not in error state or not disabled.
359              */
360             if (PROXY_WORKER_IS_USABLE(workers)) {
361                 workers->s->lbstatus += workers->s->lbfactor;
362                 total_factor += workers->s->lbfactor;
363             }
364             workers++;
365         }
366         runtime->s->lbstatus -= total_factor;
367         runtime->s->elected++;
368
369         *worker = runtime;
370     }
371     else if (route && (*balancer)->sticky_force) {
372         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
373                      "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
374                      (*balancer)->name, route);
375         PROXY_THREAD_UNLOCK(*balancer);
376         return HTTP_SERVICE_UNAVAILABLE;
377     }
378
379     PROXY_THREAD_UNLOCK(*balancer);
380     if (!*worker) {
381         runtime = find_best_worker(*balancer, r);
382         if (!runtime) {
383             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
384                          "proxy: BALANCER: (%s). All workers are in error state",
385                          (*balancer)->name);
386
387             return HTTP_SERVICE_UNAVAILABLE;
388         }
389         *worker = runtime;
390     }
391
392     /* Rewrite the url from 'balancer://url'
393      * to the 'worker_scheme://worker_hostname[:worker_port]/url'
394      * This replaces the balancers fictional name with the
395      * real hostname of the elected worker.
396      */
397     access_status = rewrite_url(r, *worker, url);
398     /* Add the session route to request notes if present */
399     if (route) {
400         apr_table_setn(r->notes, "session-sticky", (*balancer)->sticky);
401         apr_table_setn(r->notes, "session-route", route);
402     }
403     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
404                  "proxy: BALANCER (%s) worker (%s) rewritten to %s",
405                  (*balancer)->name, (*worker)->name, *url);
406
407     return access_status;
408 }
409
410 static int proxy_balancer_post_request(proxy_worker *worker,
411                                        proxy_balancer *balancer,
412                                        request_rec *r,
413                                        proxy_server_conf *conf)
414 {
415     apr_status_t rv;
416
417     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
418         ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
419             "proxy: BALANCER: lock");
420         return HTTP_INTERNAL_SERVER_ERROR;
421     }
422     /* TODO: calculate the bytes transferred
423      * This will enable to elect the worker that has
424      * the lowest load.
425      * The bytes transferred depends on the protocol
426      * used, so each protocol handler should keep the
427      * track on that.
428      */
429
430     PROXY_THREAD_UNLOCK(balancer);
431     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
432                  "proxy_balancer_post_request for (%s)", balancer->name);
433
434     return OK;
435 }
436
437 static void recalc_factors(proxy_balancer *balancer)
438 {
439     int i;
440     proxy_worker *workers;
441
442
443     /* Recalculate lbfactors */
444     workers = (proxy_worker *)balancer->workers->elts;
445     /* Special case if there is only one worker it's
446      * load factor will always be 1
447      */
448     if (balancer->workers->nelts == 1) {
449         workers->s->lbstatus = workers->s->lbfactor = 1;
450         return;
451     }
452     for (i = 0; i < balancer->workers->nelts; i++) {
453         /* Update the status entries */
454         workers[i].s->lbstatus = workers[i].s->lbfactor;
455     }
456 }
457
458 /* Manages the loadfactors and member status
459  */
460 static int balancer_handler(request_rec *r)
461 {
462     void *sconf = r->server->module_config;
463     proxy_server_conf *conf = (proxy_server_conf *)
464         ap_get_module_config(sconf, &proxy_module);
465     proxy_balancer *balancer, *bsel = NULL;
466     proxy_worker *worker, *wsel = NULL;
467     apr_table_t *params = apr_table_make(r->pool, 10);
468     int access_status;
469     int i, n;
470     const char *name;
471
472     /* is this for us? */
473     if (strcmp(r->handler, "balancer-manager"))
474         return DECLINED;
475     r->allowed = (AP_METHOD_BIT << M_GET);
476     if (r->method_number != M_GET)
477         return DECLINED;
478
479     if (r->args) {
480         char *args = apr_pstrdup(r->pool, r->args);
481         char *tok, *val;
482         while (args && *args) {
483             if ((val = ap_strchr(args, '='))) {
484                 *val++ = '\0';
485                 if ((tok = ap_strchr(val, '&')))
486                     *tok++ = '\0';
487                 /*
488                  * Special case: workers are allowed path information
489                  */
490                 if ((access_status = ap_unescape_url(val)) != OK)
491                     if (strcmp(args, "w") || (access_status !=  HTTP_NOT_FOUND))
492                         return access_status;
493                 apr_table_setn(params, args, val);
494                 args = tok;
495             }
496             else
497                 return HTTP_BAD_REQUEST;
498         }
499     }
500     if ((name = apr_table_get(params, "b")))
501         bsel = ap_proxy_get_balancer(r->pool, conf,
502             apr_pstrcat(r->pool, "balancer://", name, NULL));
503     if ((name = apr_table_get(params, "w"))) {
504         proxy_worker *ws;
505
506         ws = ap_proxy_get_worker(r->pool, conf, name);
507         if (ws) {
508             worker = (proxy_worker *)bsel->workers->elts;
509             for (n = 0; n < bsel->workers->nelts; n++) {
510                 if (strcasecmp(worker->name, ws->name) == 0) {
511                     wsel = worker;
512                     break;
513                 }
514                 ++worker;
515             }
516         }
517     }
518     /* First set the params */
519     if (bsel) {
520         const char *val;
521         if ((val = apr_table_get(params, "ss"))) {
522             if (strlen(val))
523                 bsel->sticky = apr_pstrdup(conf->pool, val);
524             else
525                 bsel->sticky = NULL;
526         }
527         if ((val = apr_table_get(params, "tm"))) {
528             int ival = atoi(val);
529             if (ival >= 0)
530                 bsel->timeout = apr_time_from_sec(ival);
531         }
532         if ((val = apr_table_get(params, "fa"))) {
533             int ival = atoi(val);
534             if (ival >= 0)
535                 bsel->max_attempts = ival;
536             bsel->max_attempts_set = 1;
537         }
538         if ((val = apr_table_get(params, "lm"))) {
539             proxy_balancer_method *provider;
540             provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
541             if (provider) {
542                 bsel->lbmethod = provider;
543             }
544         }
545     }
546     if (wsel) {
547         const char *val;
548         if ((val = apr_table_get(params, "lf"))) {
549             int ival = atoi(val);
550             if (ival >= 1 && ival <= 100) {
551                 wsel->s->lbfactor = ival;
552                 if (bsel)
553                     recalc_factors(bsel);
554             }
555         }
556         if ((val = apr_table_get(params, "wr"))) {
557             if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
558                 strcpy(wsel->s->route, val);
559             else
560                 *wsel->s->route = '\0';
561         }
562         if ((val = apr_table_get(params, "rr"))) {
563             if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
564                 strcpy(wsel->s->redirect, val);
565             else
566                 *wsel->s->redirect = '\0';
567         }
568         if ((val = apr_table_get(params, "dw"))) {
569             if (!strcasecmp(val, "Disable"))
570                 wsel->s->status |= PROXY_WORKER_DISABLED;
571             else if (!strcasecmp(val, "Enable"))
572                 wsel->s->status &= ~PROXY_WORKER_DISABLED;
573         }
574
575     }
576     if (apr_table_get(params, "xml")) {
577         ap_set_content_type(r, "text/xml");
578         ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
579         ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
580         ap_rputs("  <httpd:balancers>\n", r);
581         balancer = (proxy_balancer *)conf->balancers->elts;
582         for (i = 0; i < conf->balancers->nelts; i++) {
583             ap_rputs("    <httpd:balancer>\n", r);
584             ap_rvputs(r, "      <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
585             ap_rputs("      <httpd:workers>\n", r);
586             worker = (proxy_worker *)balancer->workers->elts;
587             for (n = 0; n < balancer->workers->nelts; n++) {
588                 ap_rputs("        <httpd:worker>\n", r);
589                 ap_rvputs(r, "          <httpd:scheme>", worker->scheme,
590                           "</httpd:scheme>\n", NULL);
591                 ap_rvputs(r, "          <httpd:hostname>", worker->hostname,
592                           "</httpd:hostname>\n", NULL);
593                ap_rprintf(r, "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
594                           worker->s->lbfactor);
595                 ap_rputs("        </httpd:worker>\n", r);
596                 ++worker;
597             }
598             ap_rputs("      </httpd:workers>\n", r);
599             ap_rputs("    </httpd:balancer>\n", r);
600             ++balancer;
601         }
602         ap_rputs("  </httpd:balancers>\n", r);
603         ap_rputs("</httpd:manager>", r);
604     }
605     else {
606         ap_set_content_type(r, "text/html");
607         ap_rputs(DOCTYPE_HTML_3_2
608                  "<html><head><title>Balancer Manager</title></head>\n", r);
609         ap_rputs("<body><h1>Load Balancer Manager for ", r);
610         ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
611         ap_rvputs(r, "<dl><dt>Server Version: ",
612                   ap_get_server_version(), "</dt>\n", NULL);
613         ap_rvputs(r, "<dt>Server Built: ",
614                   ap_get_server_built(), "\n</dt></dl>\n", NULL);
615         balancer = (proxy_balancer *)conf->balancers->elts;
616         for (i = 0; i < conf->balancers->nelts; i++) {
617
618             ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
619             ap_rvputs(r, "<a href=\"", r->uri, "?b=",
620                       balancer->name + sizeof("balancer://") - 1,
621                       "\">", NULL);
622             ap_rvputs(r, balancer->name, "</a></h3>\n\n", NULL);
623             ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
624                 "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
625                 "</tr>\n<tr>", r);
626             ap_rvputs(r, "<td>", balancer->sticky, NULL);
627             ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
628                 apr_time_sec(balancer->timeout));
629             ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
630             ap_rprintf(r, "<td>%s</td>\n",
631                        balancer->lbmethod->name);
632             ap_rputs("</table>\n<br />", r);
633             ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
634                 "<th>Worker URL</th>"
635                 "<th>Route</th><th>RouteRedir</th>"
636                 "<th>Factor</th><th>Status</th>"
637                 "</tr>\n", r);
638
639             worker = (proxy_worker *)balancer->workers->elts;
640             for (n = 0; n < balancer->workers->nelts; n++) {
641
642                 ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
643                           balancer->name + sizeof("balancer://") - 1, "&w=",
644                           ap_escape_uri(r->pool, worker->name),
645                           "\">", NULL);
646                 ap_rvputs(r, worker->name, "</a></td>", NULL);
647                 ap_rvputs(r, "<td>", worker->s->route, NULL);
648                 ap_rvputs(r, "</td><td>", worker->s->redirect, NULL);
649                 ap_rprintf(r, "</td><td>%d</td><td>", worker->s->lbfactor);
650                 if (worker->s->status & PROXY_WORKER_DISABLED)
651                     ap_rputs("Dis", r);
652                 else if (worker->s->status & PROXY_WORKER_IN_ERROR)
653                     ap_rputs("Err", r);
654                 else if (worker->s->status & PROXY_WORKER_INITIALIZED)
655                     ap_rputs("Ok", r);
656                 else
657                     ap_rputs("-", r);
658                 ap_rputs("</td></tr>\n", r);
659
660                 ++worker;
661             }
662             ap_rputs("</table>\n", r);
663             ++balancer;
664         }
665         ap_rputs("<hr />\n", r);
666         if (wsel && bsel) {
667             ap_rputs("<h3>Edit worker settings for ", r);
668             ap_rvputs(r, wsel->name, "</h3>\n", NULL);
669             ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
670             ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
671             ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
672             ap_rprintf(r, "value=\"%d\"></td><tr>\n", wsel->s->lbfactor);
673             ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
674             ap_rvputs(r, "value=\"", wsel->route, NULL);
675             ap_rputs("\"></td><tr>\n", r);
676             ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
677             ap_rvputs(r, "value=\"", wsel->redirect, NULL);
678             ap_rputs("\"></td><tr>\n", r);
679             ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
680             if (wsel->s->status & PROXY_WORKER_DISABLED)
681                 ap_rputs(" checked", r);
682             ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
683             if (!(wsel->s->status & PROXY_WORKER_DISABLED))
684                 ap_rputs(" checked", r);
685             ap_rputs("></td><tr>\n", r);
686             ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
687             ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ",  NULL);
688             ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
689             ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
690             ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
691                       "\">\n</form>\n", NULL);
692             ap_rputs("<hr />\n", r);
693         }
694         else if (bsel) {
695             ap_rputs("<h3>Edit balancer settings for ", r);
696             ap_rvputs(r, bsel->name, "</h3>\n", NULL);
697             ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
698             ap_rvputs(r, r->uri, "\">\n<dl>", NULL);
699             ap_rputs("<table><tr><td>StickySession Identifier:</td><td><input name=\"ss\" type=text ", r);
700             if (bsel->sticky)
701                 ap_rvputs(r, "value=\"", bsel->sticky, "\"", NULL);
702             ap_rputs("></td><tr>\n<tr><td>Timeout:</td><td><input name=\"tm\" type=text ", r);
703             ap_rprintf(r, "value=\"%" APR_TIME_T_FMT "\"></td></tr>\n",
704                        apr_time_sec(bsel->timeout));
705             ap_rputs("<tr><td>Failover Attempts:</td><td><input name=\"fa\" type=text ", r);
706             ap_rprintf(r, "value=\"%d\"></td></tr>\n",
707                        bsel->max_attempts);
708             ap_rputs("<tr><td>LB Method:</td><td><select name=\"lm\">", r);
709             {
710                 apr_array_header_t *methods;
711                 ap_list_provider_names_t *method;
712                 int i;
713                 methods = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
714                 method = (ap_list_provider_names_t *)methods->elts;
715                 for (i = 0; i < methods->nelts; i++) {
716                     ap_rprintf(r, "<option value=\"%s\" %s>%s</option>", method->provider_name,
717                        (!strcasecmp(bsel->lbmethod->name, method->provider_name)) ? "selected" : "",
718                        method->provider_name);
719                     method++;
720                 }
721             }
722             ap_rputs("</select></td></tr>\n", r);
723             ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
724             ap_rvputs(r, "</table>\n<input type=hidden name=\"b\" ", NULL);
725             ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
726                       "\">\n</form>\n", NULL);
727             ap_rputs("<hr />\n", r);
728         }
729         ap_rputs(ap_psignature("",r), r);
730         ap_rputs("</body></html>\n", r);
731     }
732     return OK;
733 }
734
735 static void child_init(apr_pool_t *p, server_rec *s)
736 {
737     while (s) {
738         void *sconf = s->module_config;
739         proxy_server_conf *conf;
740         proxy_balancer *balancer;
741         int i;
742         conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
743
744         /* Initialize shared scoreboard data */
745         balancer = (proxy_balancer *)conf->balancers->elts;
746         for (i = 0; i < conf->balancers->nelts; i++) {
747             init_balancer_members(conf, s, balancer);
748             balancer++;
749         }
750         s = s->next;
751     }
752
753 }
754
755 /*
756  * The idea behind the find_best_byrequests scheduler is the following:
757  *
758  * lbfactor is "how much we expect this worker to work", or "the worker's
759  * normalized work quota".
760  *
761  * lbstatus is "how urgent this worker has to work to fulfill its quota
762  * of work".
763  *
764  * We distribute each worker's work quota to the worker, and then look
765  * which of them needs to work most urgently (biggest lbstatus).  This
766  * worker is then selected for work, and its lbstatus reduced by the
767  * total work quota we distributed to all workers.  Thus the sum of all
768  * lbstatus does not change.(*)
769  *
770  * If some workers are disabled, the others will
771  * still be scheduled correctly.
772  *
773  * If a balancer is configured as follows:
774  *
775  * worker     a    b    c    d
776  * lbfactor  25   25   25   25
777  *
778  * And b gets disabled, the following schedule is produced:
779  *
780  *    a c d a c d a c d ...
781  *
782  * Note that the above lbfactor setting is the *exact* same as:
783  *
784  * worker     a    b    c    d
785  * lbfactor   1    1    1    1
786  *
787  * Asymmetric configurations work as one would expect. For
788  * example:
789  *
790  * worker     a    b    c    d
791  * lbfactor   1    1    1    2
792  *
793  * would have a, b and c all handling about the same
794  * amount of load with d handling twice what a or b
795  * or c handles individually. So we could see:
796  *
797  *   b a d c d a c d b d ...
798  *
799  */
800
801 static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
802                                 request_rec *r)
803 {
804     int i;
805     int total_factor = 0;
806     proxy_worker *worker = (proxy_worker *)balancer->workers->elts;
807     proxy_worker *mycandidate = NULL;
808
809
810     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
811                  "proxy: Entering byrequests for BALANCER (%s)",
812                  balancer->name);
813
814     /* First try to see if we have available candidate */
815     for (i = 0; i < balancer->workers->nelts; i++) {
816         /* If the worker is in error state run
817          * retry on that worker. It will be marked as
818          * operational if the retry timeout is elapsed.
819          * The worker might still be unusable, but we try
820          * anyway.
821          */
822         if (!PROXY_WORKER_IS_USABLE(worker))
823             ap_proxy_retry_worker("BALANCER", worker, r->server);
824         /* Take into calculation only the workers that are
825          * not in error state or not disabled.
826          */
827         if (PROXY_WORKER_IS_USABLE(worker)) {
828             worker->s->lbstatus += worker->s->lbfactor;
829             total_factor += worker->s->lbfactor;
830             if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
831                 mycandidate = worker;
832         }
833         worker++;
834     }
835
836     if (mycandidate) {
837         mycandidate->s->lbstatus -= total_factor;
838         mycandidate->s->elected++;
839     }
840
841     return mycandidate;
842 }
843
844 /*
845  * The idea behind the find_best_bytraffic scheduler is the following:
846  *
847  * We know the amount of traffic (bytes in and out) handled by each
848  * worker. We normalize that traffic by each workers' weight. So assuming
849  * a setup as below:
850  *
851  * worker     a    b    c
852  * lbfactor   1    1    3
853  *
854  * the scheduler will allow worker c to handle 3 times the
855  * traffic of a and b. If each request/response results in the
856  * same amount of traffic, then c would be accessed 3 times as
857  * often as a or b. If, for example, a handled a request that
858  * resulted in a large i/o bytecount, then b and c would be
859  * chosen more often, to even things out.
860  */
861 static proxy_worker *find_best_bytraffic(proxy_balancer *balancer,
862                                          request_rec *r)
863 {
864     int i;
865     apr_off_t mytraffic = 0;
866     apr_off_t curmin = 0;
867     proxy_worker *worker = (proxy_worker *)balancer->workers->elts;
868     proxy_worker *mycandidate = NULL;
869
870     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
871                  "proxy: Entering bytraffic for BALANCER (%s)",
872                  balancer->name);
873
874     /* First try to see if we have available candidate */
875     for (i = 0; i < balancer->workers->nelts; i++) {
876         /* If the worker is in error state run
877          * retry on that worker. It will be marked as
878          * operational if the retry timeout is elapsed.
879          * The worker might still be unusable, but we try
880          * anyway.
881          */
882         if (!PROXY_WORKER_IS_USABLE(worker))
883             ap_proxy_retry_worker("BALANCER", worker, r->server);
884         /* Take into calculation only the workers that are
885          * not in error state or not disabled.
886          */
887         if (PROXY_WORKER_IS_USABLE(worker)) {
888             mytraffic = (worker->s->transferred/worker->s->lbfactor) +
889                         (worker->s->read/worker->s->lbfactor);
890             if (!mycandidate || mytraffic < curmin) {
891                 mycandidate = worker;
892                 curmin = mytraffic;
893             }
894         }
895         worker++;
896     }
897
898     if (mycandidate) {
899         mycandidate->s->elected++;
900     }
901
902     return mycandidate;
903 }
904
905 /*
906  * How to add additional lbmethods:
907  *   1. Create func which determines "best" candidate worker
908  *      (eg: find_best_bytraffic, above)
909  *   2. Register it as a provider.
910  */
911 static const proxy_balancer_method byrequests =
912 {
913     "byrequests",
914     &find_best_byrequests,
915     NULL
916 };
917
918 static const proxy_balancer_method bytraffic =
919 {
920     "bytraffic",
921     &find_best_bytraffic,
922     NULL
923 };
924
925 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
926 {
927     /* Only the mpm_winnt has child init hook handler.
928      * make sure that we are called after the mpm
929      * initializes and after the mod_proxy
930      */
931     static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy.c", NULL};
932      /* manager handler */
933     ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
934     ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
935     proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
936     proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
937     proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
938     ap_register_provider(p, PROXY_LBMETHOD, "bytraffic", "0", &bytraffic);
939     ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
940 }
941
942 module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
943     STANDARD20_MODULE_STUFF,
944     NULL,       /* create per-directory config structure */
945     NULL,       /* merge per-directory config structures */
946     NULL,       /* create per-server config structure */
947     NULL,       /* merge per-server config structures */
948     NULL,       /* command apr_table_t */
949     ap_proxy_balancer_register_hook /* register hooks */
950 };