mod_proxy_balancer.c revision 9056fdeb30584edd8495a254901a50f62bc81979
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz/* Licensed to the Apache Software Foundation (ASF) under one or more
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz * contributor license agreements. See the NOTICE file distributed with
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * this work for additional information regarding copyright ownership.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The ASF licenses this file to You under the Apache License, Version 2.0
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * (the "License"); you may not use this file except in compliance with
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the License. You may obtain a copy of the License at
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Unless required by applicable law or agreed to in writing, software
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distributed under the License is distributed on an "AS IS" BASIS,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * See the License for the specific language governing permissions and
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * limitations under the License.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Load balancer module for Apache proxy */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesmodule AP_MODULE_DECLARE_DATA proxy_balancer_module;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_balancer_canon(request_rec *r, char *url)
5c0419d51818eb02045cf923a9fe456127a44c60wrowe const char *err;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* do syntatic check.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We break the URL into host, port, path, search
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "error parsing URL %s: %s",
b6c405caec8a41b770da7466bf8c8737ef1472d7bnicholes /* now parse path/search args, according to rfc1738 */
b6c405caec8a41b770da7466bf8c8737ef1472d7bnicholes /* N.B. if this isn't a true proxy request, then the URL _path_
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * has already been decoded. True proxy requests have r->uri
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * == r->unparsed_uri, and no others have that property.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* process path */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0, r->proxyreq);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int init_balancer_members(proxy_server_conf *conf, server_rec *s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes workers = (proxy_worker *)balancer->workers->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_proxy_initialize_worker_share(conf, workers, s);
0568280364eb026393be492ebc732795c4934643jorton /* Set to the original configuration */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Set default number of attempts to the number of
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes balancer->max_attempts = balancer->workers->nelts - 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Retrieve the parameter with the given name
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Something like 'JSESSIONID=12345...N'
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic char *get_path_param(apr_pool_t *pool, char *url,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *name)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Session path was found, get it's value
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic char *get_cookie_param(request_rec *r, const char *name)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *cookies;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes start_cookie = ap_strstr_c(start_cookie + 1, name)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Session cookie was found, get it's value
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Find the worker that has the 'route' defined
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic proxy_worker *find_route_worker(proxy_balancer *balancer,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; i < balancer->workers->nelts; i++, worker++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If the worker is in error state run
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * retry on that worker. It will be marked as
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * operational if the retry timeout is elapsed.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The worker might still be unusable, but we try
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_proxy_retry_worker("BALANCER", worker, r->server);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We have a worker that is unusable.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * It can be in error or disabled, but in case
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * it has a redirection set use that redirection worker.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * This enables to safely remove the member from the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * balancer. Of course you will need some kind of
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe * session replication between those two remote.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rworker = find_route_worker(balancer, worker->s->redirect, r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Check if the redirect worker is usable */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If the worker is in error state run
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * retry on that worker. It will be marked as
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * operational if the retry timeout is elapsed.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The worker might still be unusable, but we try
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_proxy_retry_worker("BALANCER", rworker, r->server);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic proxy_worker *find_session_route(proxy_balancer *balancer,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Try to find the sticky route inside url */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin *route = get_path_param(r->pool, *url, balancer->sticky);
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes "proxy: BALANCER: Found value %s for "
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * If we found a value for sticksession, find the first '.' within.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Everything after '.' (if present) is our route.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* We have a route in path or in cookie
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Find the worker that has this route defined.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Notice that the route of the worker chosen is different from
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the route supplied by the client.
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin "proxy: BALANCER: Route changed from %s to %s",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic proxy_worker *find_best_worker(proxy_balancer *balancer,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin candidate = (*balancer->lbmethod->finder)(balancer, r);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin PROXY_THREAD_UNLOCK(balancer);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return NULL;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy: BALANCER: (%s). Unlock failed for find_best_worker()", balancer->name);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* All the workers are in error state or disabled.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * If the balancer has a timeout sleep for a while
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * and try again to find the worker. The chances are
81965264d92dd8c9ca21d058420f6f6da34b3032minfrin * that some other thread will release a connection.
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * By default the timeout is not set, and the server
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * returns SERVER_BUSY.
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin /* XXX: This can perhaps be build using some
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * smarter mechanism, like tread_cond.
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * But since the statuses can came from
e1cc1ac970d0aa2910027f1f20445a16207a6deeminfrin * different childs, use the provided algo.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Set the timeout to 0 so that we don't
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * end in infinite loop
141e1368614dc7564e1627671361b01b4869b491bnicholes /* Try again */
7add8f7fb048534390571801b7794f71cd9e127abnicholes /* restore the timeout */
7add8f7fb048534390571801b7794f71cd9e127abnicholesstatic int rewrite_url(request_rec *r, proxy_worker *worker,
7add8f7fb048534390571801b7794f71cd9e127abnicholes /* we break the URL into host, port, uri */
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes *url = apr_pstrcat(r->pool, worker->name, path, NULL);
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholesstatic void force_recovery(proxy_balancer *balancer, server_rec *s)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; i < balancer->workers->nelts; i++, worker++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!(worker->s->status & PROXY_WORKER_IN_ERROR)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* If all workers are in error state force the recovery.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; i < balancer->workers->nelts; i++, worker++) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_balancer_pre_request(proxy_worker **worker,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Step 1: check if the url is for us
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The url we can handle starts with 'balancer://'
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If balancer is already provided skip the search
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * for balancer, because this is failover attempt.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* Step 2: Lock the LoadBalancer
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * XXX: perhaps we need the process lock here
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
8a03cd420b800a2428f49f4617293de9b2387b20jorton "proxy: BALANCER: (%s). Lock failed for pre_request",
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* Step 3: force recovery */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* Step 4: find the session route */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin runtime = find_session_route(*balancer, r, &route, url);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* We have a sticky load balancer
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Update the workers status
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * so that even session routes get
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * into account.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* Take into calculation only the workers that are
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * not in error state or not disabled.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * TODO: Abstract the below, since this is dependent
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * on the LB implementation
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe "proxy: BALANCER: (%s). Unlock failed for pre_request",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: BALANCER: (%s). Unlock failed for pre_request",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: BALANCER: (%s). All workers are in error state",
560fd0658902ab57754616c172d8953e69fc4722bnicholes * This balancer has sticky sessions and the client either has not
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * supplied any routing information or all workers for this route
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * including possible redirect and hotstandby workers are in error
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * state, but we have found another working worker for this
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * balancer where we can send the request. Thus notice that we have
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * changed the route to the backend.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* Add balancer/worker info to env. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Rewrite the url from 'balancer://url'
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * to the 'worker_scheme://worker_hostname[:worker_port]/url'
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * This replaces the balancers fictional name with the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * real hostname of the elected worker.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Add the session route to request notes if present */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_setn(r->notes, "session-sticky", (*balancer)->sticky);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Add session info to env. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
560fd0658902ab57754616c172d8953e69fc4722bnicholes "proxy: BALANCER (%s) worker (%s) rewritten to %s",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_balancer_post_request(proxy_worker *worker,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: BALANCER: (%s). Lock failed for post_request",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* TODO: placeholder for post_request actions
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: BALANCER: (%s). Unlock failed for post_request",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy_balancer_post_request for (%s)", balancer->name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic void recalc_factors(proxy_balancer *balancer)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Recalculate lbfactors */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes workers = (proxy_worker *)balancer->workers->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Special case if there is only one worker it's
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * load factor will always be 1
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* Update the status entries */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Manages the loadfactors and member status
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_t *params = apr_table_make(r->pool, 10);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *name;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* is this for us? */
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe * Special case: workers are allowed path information
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strcmp(args, "w") || (access_status != HTTP_NOT_FOUND))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* First set the params */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *val;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *val;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (strlen(val) && strlen(val) < PROXY_WORKER_MAX_ROUTE_SIZ)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<httpd:manager xmlns:httpd=\"http://httpd.apache.org\">\n", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes balancer = (proxy_balancer *)conf->balancers->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, " <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, " <httpd:hostname>", worker->hostname,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "<html><head><title>Balancer Manager</title></head>\n", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<body><h1>Load Balancer Manager for ", r);
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes balancer = (proxy_balancer *)conf->balancers->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "<th>StickySession</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "</tr>\n<tr>", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("\n\n<table border=\"0\" style=\"text-align: left;\"><tr>"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "<th>Worker URL</th>"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "<th>Route</th><th>RouteRedir</th>"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "<th>Factor</th><th>Set</th><th>Status</th>"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "<th>Elected</th><th>To</th><th>From</th>"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "</tr>\n", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "<tr>\n<td><a href=\"", r->uri, "?b=",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes balancer->name + sizeof("balancer://") - 1, "&w=",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "</td><td>", worker->s->redirect, NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td><td>", worker->s->elected);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<table><tr><td>Load factor:</td><td><input name=\"lf\" type=text ", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbfactor);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<tr><td>LB Set:</td><td><input name=\"ls\" type=text ", r);
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes ap_rprintf(r, "value=\"%d\"></td></tr>\n", wsel->s->lbset);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<tr><td>Route:</td><td><input name=\"wr\" type=text ", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<tr><td>Route Redirect:</td><td><input name=\"rr\" type=text ", r);
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin ap_rputs("<tr><td>Status:</td><td>Disabled: <input name=\"dw\" value=\"Disable\" type=radio", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("> | Enabled: <input name=\"dw\" value=\"Enable\" type=radio", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "</table>\n<input type=hidden name=\"w\" ", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "value=\"", ap_escape_uri(r->pool, wsel->name), "\">\n", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "<input type=hidden name=\"b\" ", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "<form method=\"GET\" action=\"", NULL);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_rputs("<table><tr><td>StickySession Identifier:</td><td><input name=\"ss\" type=text ", r);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_rputs("></td><tr>\n<tr><td>Timeout:</td><td><input name=\"tm\" type=text ", r);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_rprintf(r, "value=\"%" APR_TIME_T_FMT "\"></td></tr>\n",
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_rputs("<tr><td>Failover Attempts:</td><td><input name=\"fa\" type=text ", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<tr><td>LB Method:</td><td><select name=\"lm\">", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes methods = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes method = (ap_list_provider_names_t *)methods->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rprintf(r, "<option value=\"%s\" %s>%s</option>", method->provider_name,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes (!strcasecmp(bsel->lbmethod->name, method->provider_name)) ? "selected" : "",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rputs("<tr><td colspan=2><input type=submit value=\"Submit\"></td></tr>\n", r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "</table>\n<input type=hidden name=\"b\" ", NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_rvputs(r, "value=\"", bsel->name + sizeof("balancer://") - 1,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Initialize shared scoreboard data */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes balancer = (proxy_balancer *)conf->balancers->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The idea behind the find_best_byrequests scheduler is the following:
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * lbfactor is "how much we expect this worker to work", or "the worker's
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * normalized work quota".
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * lbstatus is "how urgent this worker has to work to fulfill its quota
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * We distribute each worker's work quota to the worker, and then look
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * which of them needs to work most urgently (biggest lbstatus). This
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * worker is then selected for work, and its lbstatus reduced by the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * total work quota we distributed to all workers. Thus the sum of all
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * lbstatus does not change.(*)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * If some workers are disabled, the others will
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * still be scheduled correctly.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If a balancer is configured as follows:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * worker a b c d
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * lbfactor 25 25 25 25
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * And b gets disabled, the following schedule is produced:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * a c d a c d a c d ...
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Note that the above lbfactor setting is the *exact* same as:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * worker a b c d
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * lbfactor 1 1 1 1
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * Asymmetric configurations work as one would expect. For
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * worker a b c d
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * lbfactor 1 1 1 2
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * would have a, b and c all handling about the same
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * amount of load with d handling twice what a or b
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * or c handles individually. So we could see:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * b a d c d a c d b d ...
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic proxy_worker *find_best_byrequests(proxy_balancer *balancer,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: Entering byrequests for BALANCER (%s)",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* First try to see if we have available candidate */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe for (i = 0; i < balancer->workers->nelts; i++, worker++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* If the worker is in error state run
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * retry on that worker. It will be marked as
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * operational if the retry timeout is elapsed.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The worker might still be unusable, but we try
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_proxy_retry_worker("BALANCER", worker, r->server);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Take into calculation only the workers that are
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * not in error state or not disabled.
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The idea behind the find_best_bytraffic scheduler is the following:
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * We know the amount of traffic (bytes in and out) handled by each
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * worker. We normalize that traffic by each workers' weight. So assuming
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * a setup as below:
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * worker a b c
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * lbfactor 1 1 3
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the scheduler will allow worker c to handle 3 times the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * traffic of a and b. If each request/response results in the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * same amount of traffic, then c would be accessed 3 times as
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * often as a or b. If, for example, a handled a request that
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin * resulted in a large i/o bytecount, then b and c would be
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrin * chosen more often, to even things out.
bb07ee33bce1a448bcc60ca43720b1ab1c413f87minfrinstatic proxy_worker *find_best_bytraffic(proxy_balancer *balancer,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: Entering bytraffic for BALANCER (%s)",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* First try to see if we have available candidate */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; i < balancer->workers->nelts; i++, worker++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* If the worker is in error state run
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes * retry on that worker. It will be marked as
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * operational if the retry timeout is elapsed.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * The worker might still be unusable, but we try
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_proxy_retry_worker("BALANCER", worker, r->server);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Take into calculation only the workers that are
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * not in error state or not disabled.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes mytraffic = (worker->s->transferred/worker->s->lbfactor) +
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * How to add additional lbmethods:
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes * 1. Create func which determines "best" candidate worker
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * (eg: find_best_bytraffic, above)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * 2. Register it as a provider.
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "byrequests",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes "bytraffic",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesstatic void ap_proxy_balancer_register_hook(apr_pool_t *p)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Only the mpm_winnt has child init hook handler.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * make sure that we are called after the mpm
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * initializes and after the mod_proxy
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy.c", NULL};
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* manager handler */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_register_provider(p, PROXY_LBMETHOD, "bytraffic", "0", &bytraffic);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);