mod_proxy_balancer.c revision d46dfdce9351f52a971777948d9b02f8fc668ff8
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele/* Licensed to the Apache Software Foundation (ASF) under one or more
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * contributor license agreements. See the NOTICE file distributed with
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * this work for additional information regarding copyright ownership.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * The ASF licenses this file to You under the Apache License, Version 2.0
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * (the "License"); you may not use this file except in compliance with
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * the License. You may obtain a copy of the License at
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Unless required by applicable law or agreed to in writing, software
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * distributed under the License is distributed on an "AS IS" BASIS,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * See the License for the specific language governing permissions and
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * limitations under the License.
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive/* Load balancer module for Apache proxy */
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slivestatic char balancer_nonce[APR_UUID_FORMATTED_LENGTH + 1];
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slivestatic int proxy_balancer_canon(request_rec *r, char *url)
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive const char *err;
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele /* do syntatic check.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * We break the URL into host, port, path, search
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele "error parsing URL %s: %s",
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * now parse path/search args, according to rfc1738:
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * process the path. With proxy-noncanon set (by
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * mod_proxy) we use the raw, unparsed uri
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele r->filename = apr_pstrcat(r->pool, "proxy:balancer://", host,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabelestatic int init_balancer_members(proxy_server_conf *conf, server_rec *s,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele workers = (proxy_worker **)balancer->workers->elts;
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(*workers);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * If the worker is not initialized check whether its scoreboard
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * slot is already initialized.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele slot = (proxy_worker_stat *) ap_get_scoreboard_lb((*workers)->id);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele worker_is_initialized = slot->status & PROXY_WORKER_INITIALIZED;
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_proxy_initialize_worker_share(conf, *workers, s);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_proxy_initialize_worker(*workers, s, conf->pool);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele /* Set to the original configuration */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele (*workers)->s->lbstatus = (*workers)->s->lbfactor =
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ((*workers)->lbfactor ? (*workers)->lbfactor : 1);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele /* Set default number of attempts to the number of
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (!balancer->max_attempts_set && balancer->workers->nelts > 1) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele balancer->max_attempts = balancer->workers->nelts - 1;
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele/* Retrieve the parameter with the given name
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Something like 'JSESSIONID=12345...N'
4ab980a06412fd86f52a6d054fb7e26de155c530erikabelestatic char *get_path_param(apr_pool_t *pool, char *url,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Session path was found, get it's value
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slivestatic char *get_cookie_param(request_rec *r, const char *name)
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele const char *cookies;
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele start_cookie = ap_strstr_c(start_cookie + 1, name)) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Session cookie was found, get it's value
d94fadc8231669a4e710869f290ed57a8232a61drbowen/* Find the worker that has the 'route' defined
d94fadc8231669a4e710869f290ed57a8232a61drbowenstatic proxy_worker *find_route_worker(proxy_balancer *balancer,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele workers = (proxy_worker **)balancer->workers->elts;
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele for (i = 0; i < balancer->workers->nelts; i++, workers++) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * If the worker is in error state run
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * retry on that worker. It will be marked as
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * operational if the retry timeout is elapsed.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * The worker might still be unusable, but we try
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_proxy_retry_worker("BALANCER", worker, r->server);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * We have a worker that is unusable.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * It can be in error or disabled, but in case
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * it has a redirection set use that redirection worker.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * This enables to safely remove the member from the
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * balancer. Of course you will need some kind of
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * session replication between those two remote.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele rworker = find_route_worker(balancer, worker->s->redirect, r);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele /* Check if the redirect worker is usable */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * If the worker is in error state run
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * retry on that worker. It will be marked as
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * operational if the retry timeout is elapsed.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * The worker might still be unusable, but we try
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_proxy_retry_worker("BALANCER", rworker, r->server);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabelestatic proxy_worker *find_session_route(proxy_balancer *balancer,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele const char **sticky_used,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele /* Try to find the sticky route inside url */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele *route = get_path_param(r->pool, *url, balancer->sticky_path, balancer->scolonsep);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele "proxy: BALANCER: Found value %s for "
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele "stickysession %s", *route, balancer->sticky_path);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele "proxy: BALANCER: Found value %s for "
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * If we found a value for sticksession, find the first '.' within.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Everything after '.' (if present) is our route.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele /* We have a route in path or in cookie
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Find the worker that has this route defined.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Notice that the route of the worker chosen is different from
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * the route supplied by the client.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele "proxy: BALANCER: Route changed from %s to %s",
4ab980a06412fd86f52a6d054fb7e26de155c530erikabelestatic proxy_worker *find_best_worker(proxy_balancer *balancer,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele candidate = (*balancer->lbmethod->finder)(balancer, r);
if (candidate)
#if APR_HAS_THREADS
return candidate;
char **url)
if (scheme)
if (!worker) {
NULL));
return OK;
int ok = 0;
if (!ok) {
request_rec *r,
int access_status;
if (!*balancer &&
return DECLINED;
return DECLINED;
if (runtime) {
int i, total_factor = 0;
workers++;
int i, member_of = 0;
workers++;
if (member_of) {
return HTTP_SERVICE_UNAVAILABLE;
if (!*worker) {
if (!runtime) {
return HTTP_SERVICE_UNAVAILABLE;
if (route) {
return access_status;
request_rec *r,
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
void *data;
if (!data) {
return OK;
return OK;
int access_status;
const char *name;
return DECLINED;
return DECLINED;
if (r->args) {
return access_status;
return HTTP_BAD_REQUEST;
++workers;
if (wsel) {
const char *val;
if (bsel)
++workers;
++balancer;
NULL);
++workers;
++balancer;
NULL);
NULL);
return OK;
balancer++;
s = s->next;