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
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 "scoreboard.h"
23#include "ap_mpm.h"
24#include "apr_version.h"
25#include "apr_hooks.h"
26#include "apr_uuid.h"
27
28module AP_MODULE_DECLARE_DATA proxy_balancer_module;
29
30static char balancer_nonce[APR_UUID_FORMATTED_LENGTH + 1];
31
32static int proxy_balancer_canon(request_rec *r, char *url)
33{
34    char *host, *path;
35    char *search = NULL;
36    const char *err;
37    apr_port_t port = 0;
38
39    if (strncasecmp(url, "balancer:", 9) == 0) {
40        url += 9;
41    }
42    else {
43        return DECLINED;
44    }
45
46    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
47             "proxy: BALANCER: canonicalising URL %s", url);
48
49    /* do syntatic check.
50     * We break the URL into host, port, path, search
51     */
52    err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
53    if (err) {
54        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
55                      "error parsing URL %s: %s",
56                      url, err);
57        return HTTP_BAD_REQUEST;
58    }
59    /*
60     * now parse path/search args, according to rfc1738:
61     * process the path. With proxy-noncanon set (by
62     * mod_proxy) we use the raw, unparsed uri
63     */
64    if (apr_table_get(r->notes, "proxy-nocanon")) {
65        path = url;   /* this is the raw path */
66    }
67    else {
68        path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
69                                 r->proxyreq);
70        search = r->args;
71    }
72    if (path == NULL)
73        return HTTP_BAD_REQUEST;
74
75    r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
76            "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
77    return OK;
78}
79
80static int init_balancer_members(proxy_server_conf *conf, server_rec *s,
81                                 proxy_balancer *balancer)
82{
83    int i;
84    proxy_worker *workers;
85    int worker_is_initialized;
86    proxy_worker_stat *slot;
87
88    workers = (proxy_worker *)balancer->workers->elts;
89
90    for (i = 0; i < balancer->workers->nelts; i++) {
91        worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(workers);
92        if (!worker_is_initialized) {
93            /*
94             * If the worker is not initialized check whether its scoreboard
95             * slot is already initialized.
96             */
97            slot = (proxy_worker_stat *) ap_get_scoreboard_lb(workers->id);
98            if (slot) {
99                worker_is_initialized = slot->status & PROXY_WORKER_INITIALIZED;
100            }
101            else {
102                worker_is_initialized = 0;
103            }
104        }
105        ap_proxy_initialize_worker_share(conf, workers, s);
106        ap_proxy_initialize_worker(workers, s);
107        if (!worker_is_initialized) {
108            /* Set to the original configuration */
109            workers->s->lbstatus = workers->s->lbfactor =
110            (workers->lbfactor ? workers->lbfactor : 1);
111            workers->s->lbset = workers->lbset;
112        }
113        ++workers;
114    }
115
116    /* Set default number of attempts to the number of
117     * workers.
118     */
119    if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
120        balancer->max_attempts = balancer->workers->nelts - 1;
121        balancer->max_attempts_set = 1;
122    }
123    return 0;
124}
125
126/* Retrieve the parameter with the given name
127 * Something like 'JSESSIONID=12345...N'
128 */
129static char *get_path_param(apr_pool_t *pool, char *url,
130                            const char *name, int scolon_sep)
131{
132    char *path = NULL;
133    char *pathdelims = "?&";
134
135    if (scolon_sep) {
136        pathdelims = ";?&";
137    }
138    for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
139        path += strlen(name);
140        if (*path == '=') {
141            /*
142             * Session path was found, get it's value
143             */
144            ++path;
145            if (strlen(path)) {
146                char *q;
147                path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
148                return path;
149            }
150        }
151    }
152    return NULL;
153}
154
155static char *get_cookie_param(request_rec *r, const char *name)
156{
157    const char *cookies;
158    const char *start_cookie;
159
160    if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
161        for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
162             start_cookie = ap_strstr_c(start_cookie + 1, name)) {
163            if (start_cookie == cookies ||
164                start_cookie[-1] == ';' ||
165                start_cookie[-1] == ',' ||
166                isspace(start_cookie[-1])) {
167
168                start_cookie += strlen(name);
169                while(*start_cookie && isspace(*start_cookie))
170                    ++start_cookie;
171                if (*start_cookie == '=' && start_cookie[1]) {
172                    /*
173                     * Session cookie was found, get it's value
174                     */
175                    char *end_cookie, *cookie;
176                    ++start_cookie;
177                    cookie = apr_pstrdup(r->pool, start_cookie);
178                    if ((end_cookie = strchr(cookie, ';')) != NULL)
179                        *end_cookie = '\0';
180                    if((end_cookie = strchr(cookie, ',')) != NULL)
181                        *end_cookie = '\0';
182                    return cookie;
183                }
184            }
185        }
186    }
187    return NULL;
188}
189
190/* Find the worker that has the 'route' defined
191 */
192static proxy_worker *find_route_worker(proxy_balancer *balancer,
193                                       const char *route, request_rec *r)
194{
195    int i;
196    int checking_standby;
197    int checked_standby;
198
199    proxy_worker *worker;
200
201    checking_standby = checked_standby = 0;
202    while (!checked_standby) {
203        worker = (proxy_worker *)balancer->workers->elts;
204        for (i = 0; i < balancer->workers->nelts; i++, worker++) {
205            if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
206                continue;
207            if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
208                if (worker && PROXY_WORKER_IS_USABLE(worker)) {
209                    return worker;
210                } else {
211                    /*
212                     * If the worker is in error state run
213                     * retry on that worker. It will be marked as
214                     * operational if the retry timeout is elapsed.
215                     * The worker might still be unusable, but we try
216                     * anyway.
217                     */
218                    ap_proxy_retry_worker("BALANCER", worker, r->server);
219                    if (PROXY_WORKER_IS_USABLE(worker)) {
220                            return worker;
221                    } else {
222                        /*
223                         * We have a worker that is unusable.
224                         * It can be in error or disabled, but in case
225                         * it has a redirection set use that redirection worker.
226                         * This enables to safely remove the member from the
227                         * balancer. Of course you will need some kind of
228                         * session replication between those two remote.
229                         */
230                        if (*worker->s->redirect) {
231                            proxy_worker *rworker = NULL;
232                            rworker = find_route_worker(balancer, worker->s->redirect, r);
233                            /* Check if the redirect worker is usable */
234                            if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
235                                /*
236                                 * If the worker is in error state run
237                                 * retry on that worker. It will be marked as
238                                 * operational if the retry timeout is elapsed.
239                                 * The worker might still be unusable, but we try
240                                 * anyway.
241                                 */
242                                ap_proxy_retry_worker("BALANCER", rworker, r->server);
243                            }
244                            if (rworker && PROXY_WORKER_IS_USABLE(rworker))
245                                return rworker;
246                        }
247                    }
248                }
249            }
250        }
251        checked_standby = checking_standby++;
252    }
253    return NULL;
254}
255
256static proxy_worker *find_session_route(proxy_balancer *balancer,
257                                        request_rec *r,
258                                        char **route,
259                                        char **sticky_used,
260                                        char **url)
261{
262    proxy_worker *worker = NULL;
263    char *sticky, *sticky_path, *path;
264
265    if (!balancer->sticky)
266        return NULL;
267    sticky = sticky_path = apr_pstrdup(r->pool, balancer->sticky);
268    if ((path = strchr(sticky, '|'))) {
269        *path++ = '\0';
270         sticky_path = path;
271    }
272
273    /* Try to find the sticky route inside url */
274    *sticky_used = sticky_path;
275    *route = get_path_param(r->pool, *url, sticky_path, balancer->scolonsep);
276    if (!*route) {
277        *route = get_cookie_param(r, sticky);
278        *sticky_used = sticky;
279    }
280    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
281                            "proxy: BALANCER: Found value %s for "
282                            "stickysession %s", *route, balancer->sticky);
283    /*
284     * If we found a value for sticksession, find the first '.' within.
285     * Everything after '.' (if present) is our route.
286     */
287    if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
288        (*route)++;
289    if ((*route) && (**route)) {
290        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
291                                  "proxy: BALANCER: Found route %s", *route);
292        /* We have a route in path or in cookie
293         * Find the worker that has this route defined.
294         */
295        worker = find_route_worker(balancer, *route, r);
296        if (worker && strcmp(*route, worker->s->route)) {
297            /*
298             * Notice that the route of the worker chosen is different from
299             * the route supplied by the client.
300             */
301            apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
302            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
303                         "proxy: BALANCER: Route changed from %s to %s",
304                         *route, worker->s->route);
305        }
306        return worker;
307    }
308    else
309        return NULL;
310}
311
312static proxy_worker *find_best_worker(proxy_balancer *balancer,
313                                      request_rec *r)
314{
315    proxy_worker *candidate = NULL;
316    apr_status_t rv;
317
318    if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
319        ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
320        "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
321        return NULL;
322    }
323
324    candidate = (*balancer->lbmethod->finder)(balancer, r);
325
326    if (candidate)
327        candidate->s->elected++;
328
329/*
330        PROXY_THREAD_UNLOCK(balancer);
331        return NULL;
332*/
333
334    if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
335        ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
336        "proxy: BALANCER: (%s). Unlock failed for find_best_worker()", balancer->name);
337    }
338
339    if (candidate == NULL) {
340        /* All the workers are in error state or disabled.
341         * If the balancer has a timeout sleep for a while
342         * and try again to find the worker. The chances are
343         * that some other thread will release a connection.
344         * By default the timeout is not set, and the server
345         * returns SERVER_BUSY.
346         */
347#if APR_HAS_THREADS
348        if (balancer->timeout) {
349            /* XXX: This can perhaps be build using some
350             * smarter mechanism, like tread_cond.
351             * But since the statuses can came from
352             * different childs, use the provided algo.
353             */
354            apr_interval_time_t timeout = balancer->timeout;
355            apr_interval_time_t step, tval = 0;
356            /* Set the timeout to 0 so that we don't
357             * end in infinite loop
358             */
359            balancer->timeout = 0;
360            step = timeout / 100;
361            while (tval < timeout) {
362                apr_sleep(step);
363                /* Try again */
364                if ((candidate = find_best_worker(balancer, r)))
365                    break;
366                tval += step;
367            }
368            /* restore the timeout */
369            balancer->timeout = timeout;
370        }
371#endif
372    }
373
374    return candidate;
375
376}
377
378static int rewrite_url(request_rec *r, proxy_worker *worker,
379                        char **url)
380{
381    const char *scheme = strstr(*url, "://");
382    const char *path = NULL;
383
384    if (scheme)
385        path = ap_strchr_c(scheme + 3, '/');
386
387    /* we break the URL into host, port, uri */
388    if (!worker) {
389        return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
390                             "missing worker. URI cannot be parsed: ", *url,
391                             NULL));
392    }
393
394    *url = apr_pstrcat(r->pool, worker->name, path, NULL);
395
396    return OK;
397}
398
399static void force_recovery(proxy_balancer *balancer, server_rec *s)
400{
401    int i;
402    int ok = 0;
403    proxy_worker *worker;
404
405    worker = (proxy_worker *)balancer->workers->elts;
406    for (i = 0; i < balancer->workers->nelts; i++, worker++) {
407        if (!(worker->s->status & PROXY_WORKER_IN_ERROR)) {
408            ok = 1;
409            break;
410        }
411        else {
412            /* Try if we can recover */
413            ap_proxy_retry_worker("BALANCER", worker, s);
414            if (!(worker->s->status & PROXY_WORKER_IN_ERROR)) {
415                ok = 1;
416                break;
417            }
418        }
419    }
420    if (!ok && balancer->forcerecovery) {
421        /* If all workers are in error state force the recovery.
422         */
423        worker = (proxy_worker *)balancer->workers->elts;
424        for (i = 0; i < balancer->workers->nelts; i++, worker++) {
425            ++worker->s->retries;
426            worker->s->status &= ~PROXY_WORKER_IN_ERROR;
427            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
428                         "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
429                         balancer->name, worker->hostname);
430        }
431    }
432}
433
434static apr_status_t decrement_busy_count(void *worker_)
435{
436    proxy_worker *worker = worker_;
437
438    if (worker->s->busy) {
439        worker->s->busy--;
440    }
441
442    return APR_SUCCESS;
443}
444
445static int proxy_balancer_pre_request(proxy_worker **worker,
446                                      proxy_balancer **balancer,
447                                      request_rec *r,
448                                      proxy_server_conf *conf, char **url)
449{
450    int access_status;
451    proxy_worker *runtime;
452    char *route = NULL;
453    char *sticky = NULL;
454    apr_status_t rv;
455
456    *worker = NULL;
457    /* Step 1: check if the url is for us
458     * The url we can handle starts with 'balancer://'
459     * If balancer is already provided skip the search
460     * for balancer, because this is failover attempt.
461     */
462    if (!*balancer &&
463        !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
464        return DECLINED;
465
466    /* Step 2: Lock the LoadBalancer
467     * XXX: perhaps we need the process lock here
468     */
469    if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
470        ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
471                     "proxy: BALANCER: (%s). Lock failed for pre_request",
472                     (*balancer)->name);
473        return DECLINED;
474    }
475
476    /* Step 3: force recovery */
477    force_recovery(*balancer, r->server);
478
479    /* Step 4: find the session route */
480    runtime = find_session_route(*balancer, r, &route, &sticky, url);
481    if (runtime) {
482        int i, total_factor = 0;
483        proxy_worker *workers;
484        /* We have a sticky load balancer
485         * Update the workers status
486         * so that even session routes get
487         * into account.
488         */
489        workers = (proxy_worker *)(*balancer)->workers->elts;
490        for (i = 0; i < (*balancer)->workers->nelts; i++) {
491            /* Take into calculation only the workers that are
492             * not in error state or not disabled.
493             *
494             * TODO: Abstract the below, since this is dependent
495             *       on the LB implementation
496             */
497            if (PROXY_WORKER_IS_USABLE(workers)) {
498                workers->s->lbstatus += workers->s->lbfactor;
499                total_factor += workers->s->lbfactor;
500            }
501            workers++;
502        }
503        runtime->s->lbstatus -= total_factor;
504        runtime->s->elected++;
505
506        *worker = runtime;
507    }
508    else if (route && (*balancer)->sticky_force) {
509        int i, member_of = 0;
510        proxy_worker *workers;
511        /*
512         * We have a route provided that doesn't match the
513         * balancer name. See if the provider route is the
514         * member of the same balancer in which case return 503
515         */
516        workers = (proxy_worker *)(*balancer)->workers->elts;
517        for (i = 0; i < (*balancer)->workers->nelts; i++) {
518            if (*(workers->s->route) && strcmp(workers->s->route, route) == 0) {
519                member_of = 1;
520                break;
521            }
522            workers++;
523        }
524        if (member_of) {
525            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
526                         "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
527                         (*balancer)->name, route);
528            if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
529                ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
530                             "proxy: BALANCER: (%s). Unlock failed for pre_request",
531                             (*balancer)->name);
532            }
533            return HTTP_SERVICE_UNAVAILABLE;
534        }
535    }
536
537    if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
538        ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
539                     "proxy: BALANCER: (%s). Unlock failed for pre_request",
540                     (*balancer)->name);
541    }
542    if (!*worker) {
543        runtime = find_best_worker(*balancer, r);
544        if (!runtime) {
545            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
546                         "proxy: BALANCER: (%s). All workers are in error state",
547                         (*balancer)->name);
548
549            return HTTP_SERVICE_UNAVAILABLE;
550        }
551        if ((*balancer)->sticky && runtime) {
552            /*
553             * This balancer has sticky sessions and the client either has not
554             * supplied any routing information or all workers for this route
555             * including possible redirect and hotstandby workers are in error
556             * state, but we have found another working worker for this
557             * balancer where we can send the request. Thus notice that we have
558             * changed the route to the backend.
559             */
560            apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
561        }
562        *worker = runtime;
563    }
564
565    (*worker)->s->busy++;
566    apr_pool_cleanup_register(r->pool, *worker, decrement_busy_count,
567                              apr_pool_cleanup_null);
568
569    /* Add balancer/worker info to env. */
570    apr_table_setn(r->subprocess_env,
571                   "BALANCER_NAME", (*balancer)->name);
572    apr_table_setn(r->subprocess_env,
573                   "BALANCER_WORKER_NAME", (*worker)->name);
574    apr_table_setn(r->subprocess_env,
575                   "BALANCER_WORKER_ROUTE", (*worker)->s->route);
576
577    /* Rewrite the url from 'balancer://url'
578     * to the 'worker_scheme://worker_hostname[:worker_port]/url'
579     * This replaces the balancers fictional name with the
580     * real hostname of the elected worker.
581     */
582    access_status = rewrite_url(r, *worker, url);
583    /* Add the session route to request notes if present */
584    if (route) {
585        apr_table_setn(r->notes, "session-sticky", sticky);
586        apr_table_setn(r->notes, "session-route", route);
587
588        /* Add session info to env. */
589        apr_table_setn(r->subprocess_env,
590                       "BALANCER_SESSION_STICKY", sticky);
591        apr_table_setn(r->subprocess_env,
592                       "BALANCER_SESSION_ROUTE", route);
593    }
594    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
595                 "proxy: BALANCER (%s) worker (%s) rewritten to %s",
596                 (*balancer)->name, (*worker)->name, *url);
597
598    return access_status;
599}
600
601static int proxy_balancer_post_request(proxy_worker *worker,
602                                       proxy_balancer *balancer,
603                                       request_rec *r,
604                                       proxy_server_conf *conf)
605{
606
607    apr_status_t rv;
608
609    if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
610        ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
611            "proxy: BALANCER: (%s). Lock failed for post_request",
612            balancer->name);
613        return HTTP_INTERNAL_SERVER_ERROR;
614    }
615    if (!apr_is_empty_array(balancer->errstatuses)) {
616        int i;
617        for (i = 0; i < balancer->errstatuses->nelts; i++) {
618            int val = ((int *)balancer->errstatuses->elts)[i];
619            if (r->status == val) {
620                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
621                             "proxy: BALANCER: (%s).  Forcing worker (%s) into error state "
622                             "due to status code %d matching 'failonstatus' "
623                             "balancer parameter",
624                             balancer->name, worker->name, val);
625                worker->s->status |= PROXY_WORKER_IN_ERROR;
626                worker->s->error_time = apr_time_now();
627                break;
628            }
629        }
630    }
631
632    if (balancer->failontimeout
633        && (apr_table_get(r->notes, "proxy_timedout")) != NULL) {
634        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
635                      "%s: Forcing worker (%s) into error state "
636                      "due to timeout and 'failonstatus' parameter being set",
637                       balancer->name, worker->name);
638        worker->s->status |= PROXY_WORKER_IN_ERROR;
639        worker->s->error_time = apr_time_now();
640
641    }
642
643    if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
644        ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
645            "proxy: BALANCER: (%s). Unlock failed for post_request",
646            balancer->name);
647    }
648    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
649                 "proxy_balancer_post_request for (%s)", balancer->name);
650
651    return OK;
652}
653
654static void recalc_factors(proxy_balancer *balancer)
655{
656    int i;
657    proxy_worker *workers;
658
659
660    /* Recalculate lbfactors */
661    workers = (proxy_worker *)balancer->workers->elts;
662    /* Special case if there is only one worker it's
663     * load factor will always be 1
664     */
665    if (balancer->workers->nelts == 1) {
666        workers->s->lbstatus = workers->s->lbfactor = 1;
667        return;
668    }
669    for (i = 0; i < balancer->workers->nelts; i++) {
670        /* Update the status entries */
671        workers[i].s->lbstatus = workers[i].s->lbfactor;
672    }
673}
674
675/* post_config hook: */
676static int balancer_init(apr_pool_t *p, apr_pool_t *plog,
677                         apr_pool_t *ptemp, server_rec *s)
678{
679    void *data;
680    const char *userdata_key = "mod_proxy_balancer_init";
681    apr_uuid_t uuid;
682
683    /* balancer_init() will be called twice during startup.  So, only
684     * set up the static data the second time through. */
685    apr_pool_userdata_get(&data, userdata_key, s->process->pool);
686    if (!data) {
687        apr_pool_userdata_set((const void *)1, userdata_key,
688                               apr_pool_cleanup_null, s->process->pool);
689        return OK;
690    }
691
692    /* Retrieve a UUID and store the nonce for the lifetime of
693     * the process. */
694    apr_uuid_get(&uuid);
695    apr_uuid_format(balancer_nonce, &uuid);
696
697    return OK;
698}
699
700/* Manages the loadfactors and member status
701 */
702static int balancer_handler(request_rec *r)
703{
704    void *sconf = r->server->module_config;
705    proxy_server_conf *conf = (proxy_server_conf *)
706        ap_get_module_config(sconf, &proxy_module);
707    proxy_balancer *balancer, *bsel = NULL;
708    proxy_worker *worker, *wsel = NULL;
709    apr_table_t *params = apr_table_make(r->pool, 10);
710    int access_status;
711    int i, n;
712    const char *name;
713
714    /* is this for us? */
715    if (strcmp(r->handler, "balancer-manager"))
716        return DECLINED;
717    r->allowed = (AP_METHOD_BIT << M_GET);
718    if (r->method_number != M_GET)
719        return DECLINED;
720
721    if (r->args) {
722        char *args = apr_pstrdup(r->pool, r->args);
723        char *tok, *val;
724        while (args && *args) {
725            if ((val = ap_strchr(args, '='))) {
726                *val++ = '\0';
727                if ((tok = ap_strchr(val, '&')))
728                    *tok++ = '\0';
729                /*
730                 * Special case: workers are allowed path information
731                 */
732                if ((access_status = ap_unescape_url(val)) != OK)
733                    if (strcmp(args, "w") || (access_status !=  HTTP_NOT_FOUND))
734                        return access_status;
735                apr_table_setn(params, args, val);
736                args = tok;
737            }
738            else
739                return HTTP_BAD_REQUEST;
740        }
741    }
742
743    /* Check that the supplied nonce matches this server's nonce;
744     * otherwise ignore all parameters, to prevent a CSRF attack. */
745    if ((name = apr_table_get(params, "nonce")) == NULL
746        || strcmp(balancer_nonce, name) != 0) {
747        apr_table_clear(params);
748    }
749
750    if ((name = apr_table_get(params, "b")))
751        bsel = ap_proxy_get_balancer(r->pool, conf,
752            apr_pstrcat(r->pool, "balancer://", name, NULL));
753    if ((name = apr_table_get(params, "w"))) {
754        proxy_worker *ws;
755
756        ws = ap_proxy_get_worker(r->pool, conf, name);
757        if (bsel && ws) {
758            worker = (proxy_worker *)bsel->workers->elts;
759            for (n = 0; n < bsel->workers->nelts; n++) {
760                if (strcasecmp(worker->name, ws->name) == 0) {
761                    wsel = worker;
762                    break;
763                }
764                ++worker;
765            }
766        }
767    }
768    /* First set the params */
769    /*
770     * Note that it is not possible set the proxy_balancer because it is not
771     * in shared memory.
772     */
773    if (wsel) {
774        const char *val;
775        if ((val = apr_table_get(params, "lf"))) {
776            int ival = atoi(val);
777            if (ival >= 1 && ival <= 100) {
778                wsel->s->lbfactor = ival;
779                if (bsel)
780                    recalc_factors(bsel);
781            }
782        }
783        if ((val = apr_table_get(params, "wr"))) {
784            if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
785                strcpy(wsel->s->route, val);
786            else
787                *wsel->s->route = '\0';
788        }
789        if ((val = apr_table_get(params, "rr"))) {
790            if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
791                strcpy(wsel->s->redirect, val);
792            else
793                *wsel->s->redirect = '\0';
794        }
795        if ((val = apr_table_get(params, "dw"))) {
796            if (!strcasecmp(val, "Disable"))
797                wsel->s->status |= PROXY_WORKER_DISABLED;
798            else if (!strcasecmp(val, "Enable"))
799                wsel->s->status &= ~PROXY_WORKER_DISABLED;
800        }
801        if ((val = apr_table_get(params, "ls"))) {
802            int ival = atoi(val);
803            if (ival >= 0 && ival <= 99) {
804                wsel->s->lbset = ival;
805             }
806        }
807
808    }
809    if (apr_table_get(params, "xml")) {
810        ap_set_content_type(r, "text/xml");
811        ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
812        ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
813        ap_rputs("  <httpd:balancers>\n", r);
814        balancer = (proxy_balancer *)conf->balancers->elts;
815        for (i = 0; i < conf->balancers->nelts; i++) {
816            ap_rputs("    <httpd:balancer>\n", r);
817            ap_rvputs(r, "      <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
818            ap_rputs("      <httpd:workers>\n", r);
819            worker = (proxy_worker *)balancer->workers->elts;
820            for (n = 0; n < balancer->workers->nelts; n++) {
821                ap_rputs("        <httpd:worker>\n", r);
822                ap_rvputs(r, "          <httpd:scheme>", worker->scheme,
823                          "</httpd:scheme>\n", NULL);
824                ap_rvputs(r, "          <httpd:hostname>", worker->hostname,
825                          "</httpd:hostname>\n", NULL);
826               ap_rprintf(r, "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
827                          worker->s->lbfactor);
828                ap_rputs("        </httpd:worker>\n", r);
829                ++worker;
830            }
831            ap_rputs("      </httpd:workers>\n", r);
832            ap_rputs("    </httpd:balancer>\n", r);
833            ++balancer;
834        }
835        ap_rputs("  </httpd:balancers>\n", r);
836        ap_rputs("</httpd:manager>", r);
837    }
838    else {
839        ap_set_content_type(r, "text/html; charset=ISO-8859-1");
840        ap_rputs(DOCTYPE_HTML_3_2
841                 "<html><head><title>Balancer Manager</title></head>\n", r);
842        ap_rputs("<body><h1>Load Balancer Manager for ", r);
843        ap_rvputs(r, ap_escape_html(r->pool, ap_get_server_name(r)),
844                  "</h1>\n\n", NULL);
845        ap_rvputs(r, "<dl><dt>Server Version: ",
846                  ap_get_server_description(), "</dt>\n", NULL);
847        ap_rvputs(r, "<dt>Server Built: ",
848                  ap_get_server_built(), "\n</dt></dl>\n", NULL);
849        balancer = (proxy_balancer *)conf->balancers->elts;
850        for (i = 0; i < conf->balancers->nelts; i++) {
851
852            ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
853            ap_rvputs(r, balancer->name, "</h3>\n\n", NULL);
854            ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
855                "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
856                "</tr>\n<tr>", r);
857            if (balancer->sticky) {
858                ap_rvputs(r, "<td>", balancer->sticky, NULL);
859            }
860            else {
861                ap_rputs("<td> - ", r);
862            }
863            ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
864                apr_time_sec(balancer->timeout));
865            ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
866            ap_rprintf(r, "<td>%s</td>\n",
867                       balancer->lbmethod->name);
868            ap_rputs("</table>\n<br />", r);
869            ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
870                "<th>Worker URL</th>"
871                "<th>Route</th><th>RouteRedir</th>"
872                "<th>Factor</th><th>Set</th><th>Status</th>"
873                "<th>Elected</th><th>To</th><th>From</th>"
874                "</tr>\n", r);
875
876            worker = (proxy_worker *)balancer->workers->elts;
877            for (n = 0; n < balancer->workers->nelts; n++) {
878                char fbuf[50];
879                ap_rvputs(r, "<tr>\n<td><a href=\"",
880                          ap_escape_uri(r->pool, r->uri), "?b=",
881                          balancer->name + sizeof("balancer://") - 1, "&w=",
882                          ap_escape_uri(r->pool, worker->name),
883                          "&nonce=", balancer_nonce,
884                          "\">", NULL);
885                ap_rvputs(r, worker->name, "</a></td>", NULL);
886                ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
887                          NULL);
888                ap_rvputs(r, "</td><td>",
889                          ap_escape_html(r->pool, worker->s->redirect), NULL);
890                ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
891                ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
892                if (worker->s->status & PROXY_WORKER_DISABLED)
893                   ap_rputs("Dis ", r);
894                if (worker->s->status & PROXY_WORKER_IN_ERROR)
895                   ap_rputs("Err ", r);
896                if (worker->s->status & PROXY_WORKER_STOPPED)
897                   ap_rputs("Stop ", r);
898                if (worker->s->status & PROXY_WORKER_HOT_STANDBY)
899                   ap_rputs("Stby ", r);
900                if (PROXY_WORKER_IS_USABLE(worker))
901                    ap_rputs("Ok", r);
902                if (!PROXY_WORKER_IS_INITIALIZED(worker))
903                    ap_rputs("-", r);
904                ap_rputs("</td>", r);
905                ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
906                ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
907                ap_rputs("</td><td>", r);
908                ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
909                ap_rputs("</td></tr>\n", r);
910
911                ++worker;
912            }
913            ap_rputs("</table>\n", r);
914            ++balancer;
915        }
916        ap_rputs("<hr />\n", r);
917        if (wsel && bsel) {
918            ap_rputs("<h3>Edit worker settings for ", r);
919            ap_rvputs(r, wsel->name, "</h3>\n", NULL);
920            ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
921            ap_rvputs(r, ap_escape_uri(r->pool, r->uri), "\">\n<dl>", NULL);
922            ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
923            ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbfactor);
924            ap_rputs("<tr><td>LB Set:</td><td><input name=\"ls\" type=text ", r);
925            ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbset);
926            ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
927            ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->route),
928                      NULL);
929            ap_rputs("\"></td></tr>\n", r);
930            ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
931            ap_rvputs(r, "value=\"", ap_escape_html(r->pool, wsel->s->redirect),
932                      NULL);
933            ap_rputs("\"></td></tr>\n", r);
934            ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
935            if (wsel->s->status & PROXY_WORKER_DISABLED)
936                ap_rputs(" checked", r);
937            ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
938            if (!(wsel->s->status & PROXY_WORKER_DISABLED))
939                ap_rputs(" checked", r);
940            ap_rputs("></td></tr>\n", r);
941            ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
942            ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ",  NULL);
943            ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
944            ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
945            ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
946                      "\">\n", NULL);
947            ap_rvputs(r, "<input type=hidden name=\"nonce\" value=\"",
948                      balancer_nonce, "\">\n", NULL);
949            ap_rvputs(r, "</form>\n", NULL);
950            ap_rputs("<hr />\n", r);
951        }
952        ap_rputs(ap_psignature("",r), r);
953        ap_rputs("</body></html>\n", r);
954    }
955    return OK;
956}
957
958static void child_init(apr_pool_t *p, server_rec *s)
959{
960    while (s) {
961        void *sconf = s->module_config;
962        proxy_server_conf *conf;
963        proxy_balancer *balancer;
964        int i;
965        conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
966
967        /* Initialize shared scoreboard data */
968        balancer = (proxy_balancer *)conf->balancers->elts;
969        for (i = 0; i < conf->balancers->nelts; i++) {
970            init_balancer_members(conf, s, balancer);
971            balancer++;
972        }
973        s = s->next;
974    }
975
976}
977
978/*
979 * The idea behind the find_best_byrequests scheduler is the following:
980 *
981 * lbfactor is "how much we expect this worker to work", or "the worker's
982 * normalized work quota".
983 *
984 * lbstatus is "how urgent this worker has to work to fulfill its quota
985 * of work".
986 *
987 * We distribute each worker's work quota to the worker, and then look
988 * which of them needs to work most urgently (biggest lbstatus).  This
989 * worker is then selected for work, and its lbstatus reduced by the
990 * total work quota we distributed to all workers.  Thus the sum of all
991 * lbstatus does not change.(*)
992 *
993 * If some workers are disabled, the others will
994 * still be scheduled correctly.
995 *
996 * If a balancer is configured as follows:
997 *
998 * worker     a    b    c    d
999 * lbfactor  25   25   25   25
1000 *
1001 * And b gets disabled, the following schedule is produced:
1002 *
1003 *    a c d a c d a c d ...
1004 *
1005 * Note that the above lbfactor setting is the *exact* same as:
1006 *
1007 * worker     a    b    c    d
1008 * lbfactor   1    1    1    1
1009 *
1010 * Asymmetric configurations work as one would expect. For
1011 * example:
1012 *
1013 * worker     a    b    c    d
1014 * lbfactor   1    1    1    2
1015 *
1016 * would have a, b and c all handling about the same
1017 * amount of load with d handling twice what a or b
1018 * or c handles individually. So we could see:
1019 *
1020 *   b a d c d a c d b d ...
1021 *
1022 */
1023
1024static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
1025                                request_rec *r)
1026{
1027    int i;
1028    int total_factor = 0;
1029    proxy_worker *worker;
1030    proxy_worker *mycandidate = NULL;
1031    int cur_lbset = 0;
1032    int max_lbset = 0;
1033    int checking_standby;
1034    int checked_standby;
1035
1036    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1037                 "proxy: Entering byrequests for BALANCER (%s)",
1038                 balancer->name);
1039
1040    /* First try to see if we have available candidate */
1041    do {
1042        checking_standby = checked_standby = 0;
1043        while (!mycandidate && !checked_standby) {
1044            worker = (proxy_worker *)balancer->workers->elts;
1045            for (i = 0; i < balancer->workers->nelts; i++, worker++) {
1046                if (!checking_standby) {    /* first time through */
1047                    if (worker->s->lbset > max_lbset)
1048                        max_lbset = worker->s->lbset;
1049                }
1050                if (worker->s->lbset != cur_lbset)
1051                    continue;
1052                if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
1053                    continue;
1054                /* If the worker is in error state run
1055                 * retry on that worker. It will be marked as
1056                 * operational if the retry timeout is elapsed.
1057                 * The worker might still be unusable, but we try
1058                 * anyway.
1059                 */
1060                if (!PROXY_WORKER_IS_USABLE(worker))
1061                    ap_proxy_retry_worker("BALANCER", worker, r->server);
1062                /* Take into calculation only the workers that are
1063                 * not in error state or not disabled.
1064                 */
1065                if (PROXY_WORKER_IS_USABLE(worker)) {
1066                    worker->s->lbstatus += worker->s->lbfactor;
1067                    total_factor += worker->s->lbfactor;
1068                    if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
1069                        mycandidate = worker;
1070                }
1071            }
1072            checked_standby = checking_standby++;
1073        }
1074        cur_lbset++;
1075    } while (cur_lbset <= max_lbset && !mycandidate);
1076
1077    if (mycandidate) {
1078        mycandidate->s->lbstatus -= total_factor;
1079        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1080                     "proxy: byrequests selected worker \"%s\" : busy %" APR_SIZE_T_FMT " : lbstatus %d",
1081                     mycandidate->name, mycandidate->s->busy, mycandidate->s->lbstatus);
1082
1083    }
1084
1085    return mycandidate;
1086}
1087
1088/*
1089 * The idea behind the find_best_bytraffic scheduler is the following:
1090 *
1091 * We know the amount of traffic (bytes in and out) handled by each
1092 * worker. We normalize that traffic by each workers' weight. So assuming
1093 * a setup as below:
1094 *
1095 * worker     a    b    c
1096 * lbfactor   1    1    3
1097 *
1098 * the scheduler will allow worker c to handle 3 times the
1099 * traffic of a and b. If each request/response results in the
1100 * same amount of traffic, then c would be accessed 3 times as
1101 * often as a or b. If, for example, a handled a request that
1102 * resulted in a large i/o bytecount, then b and c would be
1103 * chosen more often, to even things out.
1104 */
1105static proxy_worker *find_best_bytraffic(proxy_balancer *balancer,
1106                                         request_rec *r)
1107{
1108    int i;
1109    apr_off_t mytraffic = 0;
1110    apr_off_t curmin = 0;
1111    proxy_worker *worker;
1112    proxy_worker *mycandidate = NULL;
1113    int cur_lbset = 0;
1114    int max_lbset = 0;
1115    int checking_standby;
1116    int checked_standby;
1117
1118    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1119                 "proxy: Entering bytraffic for BALANCER (%s)",
1120                 balancer->name);
1121
1122    /* First try to see if we have available candidate */
1123    do {
1124        checking_standby = checked_standby = 0;
1125        while (!mycandidate && !checked_standby) {
1126            worker = (proxy_worker *)balancer->workers->elts;
1127            for (i = 0; i < balancer->workers->nelts; i++, worker++) {
1128                if (!checking_standby) {    /* first time through */
1129                    if (worker->s->lbset > max_lbset)
1130                        max_lbset = worker->s->lbset;
1131                }
1132                if (worker->s->lbset != cur_lbset)
1133                    continue;
1134                if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
1135                    continue;
1136                /* If the worker is in error state run
1137                 * retry on that worker. It will be marked as
1138                 * operational if the retry timeout is elapsed.
1139                 * The worker might still be unusable, but we try
1140                 * anyway.
1141                 */
1142                if (!PROXY_WORKER_IS_USABLE(worker))
1143                    ap_proxy_retry_worker("BALANCER", worker, r->server);
1144                /* Take into calculation only the workers that are
1145                 * not in error state or not disabled.
1146                 */
1147                if (PROXY_WORKER_IS_USABLE(worker)) {
1148                    mytraffic = (worker->s->transferred/worker->s->lbfactor) +
1149                                (worker->s->read/worker->s->lbfactor);
1150                    if (!mycandidate || mytraffic < curmin) {
1151                        mycandidate = worker;
1152                        curmin = mytraffic;
1153                    }
1154                }
1155            }
1156            checked_standby = checking_standby++;
1157        }
1158        cur_lbset++;
1159    } while (cur_lbset <= max_lbset && !mycandidate);
1160
1161    if (mycandidate) {
1162        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1163                     "proxy: bytraffic selected worker \"%s\" : busy %" APR_SIZE_T_FMT,
1164                     mycandidate->name, mycandidate->s->busy);
1165
1166    }
1167
1168    return mycandidate;
1169}
1170
1171static proxy_worker *find_best_bybusyness(proxy_balancer *balancer,
1172                                request_rec *r)
1173{
1174
1175    int i;
1176    proxy_worker *worker;
1177    proxy_worker *mycandidate = NULL;
1178    int cur_lbset = 0;
1179    int max_lbset = 0;
1180    int checking_standby;
1181    int checked_standby;
1182
1183    int total_factor = 0;
1184
1185    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1186                 "proxy: Entering bybusyness for BALANCER (%s)",
1187                 balancer->name);
1188
1189    /* First try to see if we have available candidate */
1190    do {
1191
1192        checking_standby = checked_standby = 0;
1193        while (!mycandidate && !checked_standby) {
1194
1195            worker = (proxy_worker *)balancer->workers->elts;
1196            for (i = 0; i < balancer->workers->nelts; i++, worker++) {
1197                if  (!checking_standby) {    /* first time through */
1198                    if (worker->s->lbset > max_lbset)
1199                        max_lbset = worker->s->lbset;
1200                }
1201
1202                if (worker->s->lbset != cur_lbset)
1203                    continue;
1204
1205                if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
1206                    continue;
1207
1208                /* If the worker is in error state run
1209                 * retry on that worker. It will be marked as
1210                 * operational if the retry timeout is elapsed.
1211                 * The worker might still be unusable, but we try
1212                 * anyway.
1213                 */
1214                if (!PROXY_WORKER_IS_USABLE(worker))
1215                    ap_proxy_retry_worker("BALANCER", worker, r->server);
1216
1217                /* Take into calculation only the workers that are
1218                 * not in error state or not disabled.
1219                 */
1220                if (PROXY_WORKER_IS_USABLE(worker)) {
1221
1222                    worker->s->lbstatus += worker->s->lbfactor;
1223                    total_factor += worker->s->lbfactor;
1224
1225                    if (!mycandidate
1226                        || worker->s->busy < mycandidate->s->busy
1227                        || (worker->s->busy == mycandidate->s->busy && worker->s->lbstatus > mycandidate->s->lbstatus))
1228                        mycandidate = worker;
1229
1230                }
1231
1232            }
1233
1234            checked_standby = checking_standby++;
1235
1236        }
1237
1238        cur_lbset++;
1239
1240    } while (cur_lbset <= max_lbset && !mycandidate);
1241
1242    if (mycandidate) {
1243        mycandidate->s->lbstatus -= total_factor;
1244        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
1245                     "proxy: bybusyness selected worker \"%s\" : busy %" APR_SIZE_T_FMT " : lbstatus %d",
1246                     mycandidate->name, mycandidate->s->busy, mycandidate->s->lbstatus);
1247
1248    }
1249
1250    return mycandidate;
1251
1252}
1253
1254/*
1255 * How to add additional lbmethods:
1256 *   1. Create func which determines "best" candidate worker
1257 *      (eg: find_best_bytraffic, above)
1258 *   2. Register it as a provider.
1259 */
1260static const proxy_balancer_method byrequests =
1261{
1262    "byrequests",
1263    &find_best_byrequests,
1264    NULL
1265};
1266
1267static const proxy_balancer_method bytraffic =
1268{
1269    "bytraffic",
1270    &find_best_bytraffic,
1271    NULL
1272};
1273
1274static const proxy_balancer_method bybusyness =
1275{
1276    "bybusyness",
1277    &find_best_bybusyness,
1278    NULL
1279};
1280
1281
1282static void ap_proxy_balancer_register_hook(apr_pool_t *p)
1283{
1284    /* Only the mpm_winnt has child init hook handler.
1285     * make sure that we are called after the mpm
1286     * initializes and after the mod_proxy
1287     */
1288    static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy.c", NULL};
1289     /* manager handler */
1290    ap_hook_post_config(balancer_init, NULL, NULL, APR_HOOK_MIDDLE);
1291    ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1292    ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1293    proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1294    proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1295    proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1296    ap_register_provider(p, PROXY_LBMETHOD, "bytraffic", "0", &bytraffic);
1297    ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
1298    ap_register_provider(p, PROXY_LBMETHOD, "bybusyness", "0", &bybusyness);
1299}
1300
1301module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
1302    STANDARD20_MODULE_STUFF,
1303    NULL,       /* create per-directory config structure */
1304    NULL,       /* merge per-directory config structures */
1305    NULL,       /* create per-server config structure */
1306    NULL,       /* merge per-server config structures */
1307    NULL,       /* command apr_table_t */
1308    ap_proxy_balancer_register_hook /* register hooks */
1309};
1310