mod_proxy_balancer.c revision 4952d93743af3632518d1c925fac5227720cf64a
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* Licensed to the Apache Software Foundation (ASF) under one or more
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * contributor license agreements. See the NOTICE file distributed with
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * this work for additional information regarding copyright ownership.
b99dbaab171d91e1b664397cc40e039d0c087c65fielding * The ASF licenses this file to You under the Apache License, Version 2.0
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * (the "License"); you may not use this file except in compliance with
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the License. You may obtain a copy of the License at
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http://www.apache.org/licenses/LICENSE-2.0
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Unless required by applicable law or agreed to in writing, software
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * distributed under the License is distributed on an "AS IS" BASIS,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * See the License for the specific language governing permissions and
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* Load balancer module for Apache proxy */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "mod_proxy.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "scoreboard.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "ap_mpm.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr_version.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr_hooks.h"
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#include "apr_date.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fieldingstatic const char *balancer_mutex_type = "proxy-balancer-shm";
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fieldingap_slotmem_provider_t *storage = NULL;
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fieldingmodule AP_MODULE_DECLARE_DATA proxy_balancer_module;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding/*
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding * Register our mutex type before the config is read so we
64185f9824e42f21ca7b9ae6c004484215c031a7rbb * can adjust the mutex settings using the Mutex directive.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fieldingstatic int balancer_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding apr_pool_t *ptemp)
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding{
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_status_t rv;
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding rv = ap_mutex_register(pconf, balancer_mutex_type, NULL,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding APR_LOCK_DEFAULT, 0);
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding if (rv != APR_SUCCESS) {
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding return rv;
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding }
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return OK;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding#if 0
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fieldingextern void proxy_update_members(proxy_balancer **balancer, request_rec *r,
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding proxy_server_conf *conf);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fieldingstatic int proxy_balancer_canon(request_rec *r, char *url)
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *host, *path;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *search = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb const char *err;
3568de757bac0b47256647504c186d17ca272f85rbb apr_port_t port = 0;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* TODO: offset of BALANCER_PREFIX ?? */
3568de757bac0b47256647504c186d17ca272f85rbb if (strncasecmp(url, "balancer:", 9) == 0) {
3568de757bac0b47256647504c186d17ca272f85rbb url += 9;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else {
3568de757bac0b47256647504c186d17ca272f85rbb return DECLINED;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: canonicalising URL %s", url);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* do syntatic check.
3568de757bac0b47256647504c186d17ca272f85rbb * We break the URL into host, port, path, search
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
3568de757bac0b47256647504c186d17ca272f85rbb if (err) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3568de757bac0b47256647504c186d17ca272f85rbb "error parsing URL %s: %s",
3568de757bac0b47256647504c186d17ca272f85rbb url, err);
3568de757bac0b47256647504c186d17ca272f85rbb return HTTP_BAD_REQUEST;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * now parse path/search args, according to rfc1738:
3568de757bac0b47256647504c186d17ca272f85rbb * process the path. With proxy-noncanon set (by
3568de757bac0b47256647504c186d17ca272f85rbb * mod_proxy) we use the raw, unparsed uri
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb if (apr_table_get(r->notes, "proxy-nocanon")) {
3568de757bac0b47256647504c186d17ca272f85rbb path = url; /* this is the raw path */
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else {
3568de757bac0b47256647504c186d17ca272f85rbb path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
3568de757bac0b47256647504c186d17ca272f85rbb r->proxyreq);
3568de757bac0b47256647504c186d17ca272f85rbb search = r->args;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb if (path == NULL)
3568de757bac0b47256647504c186d17ca272f85rbb return HTTP_BAD_REQUEST;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb r->filename = apr_pstrcat(r->pool, "proxy:", BALANCER_PREFIX, host,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
3568de757bac0b47256647504c186d17ca272f85rbb return OK;
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic void init_balancer_members(apr_pool_t *p, server_rec *s,
3568de757bac0b47256647504c186d17ca272f85rbb proxy_balancer *balancer)
db12cd62083041bf90945eeb90cc40fbd2340797trawick{
db12cd62083041bf90945eeb90cc40fbd2340797trawick int i;
db12cd62083041bf90945eeb90cc40fbd2340797trawick proxy_worker **workers;
333eac96e4fb7d6901cb75e6ca7bb22b2ccb84cetrawick
333eac96e4fb7d6901cb75e6ca7bb22b2ccb84cetrawick workers = (proxy_worker **)balancer->workers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < balancer->workers->nelts; i++) {
3568de757bac0b47256647504c186d17ca272f85rbb int worker_is_initialized;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker *worker = *workers;
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
3568de757bac0b47256647504c186d17ca272f85rbb "Looking at %s -> %s initialized?", balancer->name, worker->s->name);
3568de757bac0b47256647504c186d17ca272f85rbb worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(worker);
3568de757bac0b47256647504c186d17ca272f85rbb if (!worker_is_initialized) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_proxy_initialize_worker(worker, s, p);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb ++workers;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Set default number of attempts to the number of
3568de757bac0b47256647504c186d17ca272f85rbb * workers.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb if (!balancer->s->max_attempts_set && balancer->workers->nelts > 1) {
3568de757bac0b47256647504c186d17ca272f85rbb balancer->s->max_attempts = balancer->workers->nelts - 1;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding balancer->s->max_attempts_set = 1;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* Retrieve the parameter with the given name
8f3ec4772d2aeb347cf40e87c77627bb784dd018rbb * Something like 'JSESSIONID=12345...N'
8f3ec4772d2aeb347cf40e87c77627bb784dd018rbb */
3d96ee83babeec32482c9082c9426340cee8c44dwrowestatic char *get_path_param(apr_pool_t *pool, char *url,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *name, int scolon_sep)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb char *path = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb char *pathdelims = "?&";
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if (scolon_sep) {
3568de757bac0b47256647504c186d17ca272f85rbb pathdelims = ";?&";
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
3568de757bac0b47256647504c186d17ca272f85rbb path += strlen(name);
3568de757bac0b47256647504c186d17ca272f85rbb if (*path == '=') {
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * Session path was found, get it's value
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb ++path;
3568de757bac0b47256647504c186d17ca272f85rbb if (*path) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *q;
3568de757bac0b47256647504c186d17ca272f85rbb path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
3568de757bac0b47256647504c186d17ca272f85rbb return path;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb return NULL;
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic char *get_cookie_param(request_rec *r, const char *name)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb const char *cookies;
3568de757bac0b47256647504c186d17ca272f85rbb const char *start_cookie;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
3568de757bac0b47256647504c186d17ca272f85rbb for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
3568de757bac0b47256647504c186d17ca272f85rbb start_cookie = ap_strstr_c(start_cookie + 1, name)) {
3568de757bac0b47256647504c186d17ca272f85rbb if (start_cookie == cookies ||
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding start_cookie[-1] == ';' ||
3568de757bac0b47256647504c186d17ca272f85rbb start_cookie[-1] == ',' ||
3568de757bac0b47256647504c186d17ca272f85rbb isspace(start_cookie[-1])) {
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb start_cookie += strlen(name);
3568de757bac0b47256647504c186d17ca272f85rbb while(*start_cookie && isspace(*start_cookie))
3568de757bac0b47256647504c186d17ca272f85rbb ++start_cookie;
3568de757bac0b47256647504c186d17ca272f85rbb if (*start_cookie++ == '=' && *start_cookie) {
3568de757bac0b47256647504c186d17ca272f85rbb /*
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Session cookie was found, get it's value
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
78ae889ffe0fdfab72f56c6993b0f302cb48da55rbb char *end_cookie, *cookie;
3568de757bac0b47256647504c186d17ca272f85rbb cookie = apr_pstrdup(r->pool, start_cookie);
fc1efab92032301e317f07e1b3a00082d9d71f3frbb if ((end_cookie = strchr(cookie, ';')) != NULL)
0375f54d32af9c27de0006956d9f85d01b020351stoddard *end_cookie = '\0';
3568de757bac0b47256647504c186d17ca272f85rbb if((end_cookie = strchr(cookie, ',')) != NULL)
3568de757bac0b47256647504c186d17ca272f85rbb *end_cookie = '\0';
3568de757bac0b47256647504c186d17ca272f85rbb return cookie;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
fc1efab92032301e317f07e1b3a00082d9d71f3frbb return NULL;
3568de757bac0b47256647504c186d17ca272f85rbb}
fc1efab92032301e317f07e1b3a00082d9d71f3frbb
3568de757bac0b47256647504c186d17ca272f85rbb/* Find the worker that has the 'route' defined
fc1efab92032301e317f07e1b3a00082d9d71f3frbb */
3568de757bac0b47256647504c186d17ca272f85rbbstatic proxy_worker *find_route_worker(proxy_balancer *balancer,
fc1efab92032301e317f07e1b3a00082d9d71f3frbb const char *route, request_rec *r)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb int i;
3568de757bac0b47256647504c186d17ca272f85rbb int checking_standby;
3568de757bac0b47256647504c186d17ca272f85rbb int checked_standby;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker **workers;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb checking_standby = checked_standby = 0;
3568de757bac0b47256647504c186d17ca272f85rbb while (!checked_standby) {
3568de757bac0b47256647504c186d17ca272f85rbb workers = (proxy_worker **)balancer->workers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < balancer->workers->nelts; i++, workers++) {
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker *worker = *workers;
3568de757bac0b47256647504c186d17ca272f85rbb if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
3568de757bac0b47256647504c186d17ca272f85rbb continue;
3568de757bac0b47256647504c186d17ca272f85rbb if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
3568de757bac0b47256647504c186d17ca272f85rbb if (worker && PROXY_WORKER_IS_USABLE(worker)) {
3568de757bac0b47256647504c186d17ca272f85rbb return worker;
3568de757bac0b47256647504c186d17ca272f85rbb } else {
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * If the worker is in error state run
3568de757bac0b47256647504c186d17ca272f85rbb * retry on that worker. It will be marked as
3568de757bac0b47256647504c186d17ca272f85rbb * operational if the retry timeout is elapsed.
3568de757bac0b47256647504c186d17ca272f85rbb * The worker might still be unusable, but we try
3568de757bac0b47256647504c186d17ca272f85rbb * anyway.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb ap_proxy_retry_worker("BALANCER", worker, r->server);
3568de757bac0b47256647504c186d17ca272f85rbb if (PROXY_WORKER_IS_USABLE(worker)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return worker;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding } else {
3568de757bac0b47256647504c186d17ca272f85rbb /*
239f998fbee5ac5b114b965bb76e217cce0003edstoddard * We have a worker that is unusable.
78ae889ffe0fdfab72f56c6993b0f302cb48da55rbb * It can be in error or disabled, but in case
3568de757bac0b47256647504c186d17ca272f85rbb * it has a redirection set use that redirection worker.
3568de757bac0b47256647504c186d17ca272f85rbb * This enables to safely remove the member from the
3568de757bac0b47256647504c186d17ca272f85rbb * balancer. Of course you will need some kind of
3568de757bac0b47256647504c186d17ca272f85rbb * session replication between those two remote.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb if (*worker->s->redirect) {
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker *rworker = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb rworker = find_route_worker(balancer, worker->s->redirect, r);
3568de757bac0b47256647504c186d17ca272f85rbb /* Check if the redirect worker is usable */
3568de757bac0b47256647504c186d17ca272f85rbb if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard /*
3568de757bac0b47256647504c186d17ca272f85rbb * If the worker is in error state run
3568de757bac0b47256647504c186d17ca272f85rbb * retry on that worker. It will be marked as
3568de757bac0b47256647504c186d17ca272f85rbb * operational if the retry timeout is elapsed.
3568de757bac0b47256647504c186d17ca272f85rbb * The worker might still be unusable, but we try
3568de757bac0b47256647504c186d17ca272f85rbb * anyway.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb ap_proxy_retry_worker("BALANCER", rworker, r->server);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb if (rworker && PROXY_WORKER_IS_USABLE(rworker))
3568de757bac0b47256647504c186d17ca272f85rbb return rworker;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
ca53a74f4012a45cbad48e940eddf27d866981f9dougm checked_standby = checking_standby++;
ca53a74f4012a45cbad48e940eddf27d866981f9dougm }
ca53a74f4012a45cbad48e940eddf27d866981f9dougm return NULL;
ca53a74f4012a45cbad48e940eddf27d866981f9dougm}
239f998fbee5ac5b114b965bb76e217cce0003edstoddard
3568de757bac0b47256647504c186d17ca272f85rbbstatic proxy_worker *find_session_route(proxy_balancer *balancer,
3568de757bac0b47256647504c186d17ca272f85rbb request_rec *r,
3568de757bac0b47256647504c186d17ca272f85rbb char **route,
3568de757bac0b47256647504c186d17ca272f85rbb const char **sticky_used,
3568de757bac0b47256647504c186d17ca272f85rbb char **url)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker *worker = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!*balancer->s->sticky)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Try to find the sticky route inside url */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *route = get_path_param(r->pool, *url, balancer->s->sticky_path, balancer->s->scolonsep);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (*route) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: Found value %s for "
3568de757bac0b47256647504c186d17ca272f85rbb "stickysession %s", *route, balancer->s->sticky_path);
3568de757bac0b47256647504c186d17ca272f85rbb *sticky_used = balancer->s->sticky_path;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else {
3568de757bac0b47256647504c186d17ca272f85rbb *route = get_cookie_param(r, balancer->s->sticky);
3568de757bac0b47256647504c186d17ca272f85rbb if (*route) {
3568de757bac0b47256647504c186d17ca272f85rbb *sticky_used = balancer->s->sticky;
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: Found value %s for "
3568de757bac0b47256647504c186d17ca272f85rbb "stickysession %s", *route, balancer->s->sticky);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * If we found a value for sticksession, find the first '.' within.
3568de757bac0b47256647504c186d17ca272f85rbb * Everything after '.' (if present) is our route.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
3568de757bac0b47256647504c186d17ca272f85rbb (*route)++;
3568de757bac0b47256647504c186d17ca272f85rbb if ((*route) && (**route)) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: Found route %s", *route);
3568de757bac0b47256647504c186d17ca272f85rbb /* We have a route in path or in cookie
3568de757bac0b47256647504c186d17ca272f85rbb * Find the worker that has this route defined.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb worker = find_route_worker(balancer, *route, r);
3568de757bac0b47256647504c186d17ca272f85rbb if (worker && strcmp(*route, worker->s->route)) {
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * Notice that the route of the worker chosen is different from
3568de757bac0b47256647504c186d17ca272f85rbb * the route supplied by the client.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: BALANCER: Route changed from %s to %s",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *route, worker->s->route);
5a58a10705b7b154b53294e0c0283d64eabdcb7fstoddard }
3568de757bac0b47256647504c186d17ca272f85rbb return worker;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else
3568de757bac0b47256647504c186d17ca272f85rbb return NULL;
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic proxy_worker *find_best_worker(proxy_balancer *balancer,
3568de757bac0b47256647504c186d17ca272f85rbb request_rec *r)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker *candidate = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb apr_status_t rv;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Lock failed for find_best_worker()", balancer->name);
3568de757bac0b47256647504c186d17ca272f85rbb return NULL;
3568de757bac0b47256647504c186d17ca272f85rbb }
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard
3568de757bac0b47256647504c186d17ca272f85rbb candidate = (*balancer->s->lbmethod->finder)(balancer, r);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if (candidate)
3568de757bac0b47256647504c186d17ca272f85rbb candidate->s->elected++;
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard
3568de757bac0b47256647504c186d17ca272f85rbb if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Unlock failed for find_best_worker()", balancer->name);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if (candidate == NULL) {
3568de757bac0b47256647504c186d17ca272f85rbb /* All the workers are in error state or disabled.
3568de757bac0b47256647504c186d17ca272f85rbb * If the balancer has a timeout sleep for a while
3568de757bac0b47256647504c186d17ca272f85rbb * and try again to find the worker. The chances are
3568de757bac0b47256647504c186d17ca272f85rbb * that some other thread will release a connection.
3568de757bac0b47256647504c186d17ca272f85rbb * By default the timeout is not set, and the server
74fd6d9aeadb9022086259c5c1ae00bc0dda9c9astoddard * returns SERVER_BUSY.
74fd6d9aeadb9022086259c5c1ae00bc0dda9c9astoddard */
3568de757bac0b47256647504c186d17ca272f85rbb if (balancer->s->timeout) {
3568de757bac0b47256647504c186d17ca272f85rbb /* XXX: This can perhaps be build using some
3568de757bac0b47256647504c186d17ca272f85rbb * smarter mechanism, like tread_cond.
3568de757bac0b47256647504c186d17ca272f85rbb * But since the statuses can came from
3568de757bac0b47256647504c186d17ca272f85rbb * different childs, use the provided algo.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb apr_interval_time_t timeout = balancer->s->timeout;
3568de757bac0b47256647504c186d17ca272f85rbb apr_interval_time_t step, tval = 0;
3568de757bac0b47256647504c186d17ca272f85rbb /* Set the timeout to 0 so that we don't
3568de757bac0b47256647504c186d17ca272f85rbb * end in infinite loop
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb balancer->s->timeout = 0;
3568de757bac0b47256647504c186d17ca272f85rbb step = timeout / 100;
3568de757bac0b47256647504c186d17ca272f85rbb while (tval < timeout) {
3568de757bac0b47256647504c186d17ca272f85rbb apr_sleep(step);
3568de757bac0b47256647504c186d17ca272f85rbb /* Try again */
3568de757bac0b47256647504c186d17ca272f85rbb if ((candidate = find_best_worker(balancer, r)))
3568de757bac0b47256647504c186d17ca272f85rbb break;
3568de757bac0b47256647504c186d17ca272f85rbb tval += step;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb /* restore the timeout */
3568de757bac0b47256647504c186d17ca272f85rbb balancer->s->timeout = timeout;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb return candidate;
3568de757bac0b47256647504c186d17ca272f85rbb
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic int rewrite_url(request_rec *r, proxy_worker *worker,
3568de757bac0b47256647504c186d17ca272f85rbb char **url)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb const char *scheme = strstr(*url, "://");
3568de757bac0b47256647504c186d17ca272f85rbb const char *path = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if (scheme)
3568de757bac0b47256647504c186d17ca272f85rbb path = ap_strchr_c(scheme + 3, '/');
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* we break the URL into host, port, uri */
3568de757bac0b47256647504c186d17ca272f85rbb if (!worker) {
3568de757bac0b47256647504c186d17ca272f85rbb return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
3568de757bac0b47256647504c186d17ca272f85rbb "missing worker. URI cannot be parsed: ", *url,
3568de757bac0b47256647504c186d17ca272f85rbb NULL));
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb *url = apr_pstrcat(r->pool, worker->s->name, path, NULL);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb return OK;
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic void force_recovery(proxy_balancer *balancer, server_rec *s)
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard{
3568de757bac0b47256647504c186d17ca272f85rbb int i;
3568de757bac0b47256647504c186d17ca272f85rbb int ok = 0;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker **worker;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb worker = (proxy_worker **)balancer->workers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < balancer->workers->nelts; i++, worker++) {
3568de757bac0b47256647504c186d17ca272f85rbb if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
3568de757bac0b47256647504c186d17ca272f85rbb ok = 1;
3568de757bac0b47256647504c186d17ca272f85rbb break;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else {
3568de757bac0b47256647504c186d17ca272f85rbb /* Try if we can recover */
3568de757bac0b47256647504c186d17ca272f85rbb ap_proxy_retry_worker("BALANCER", *worker, s);
3568de757bac0b47256647504c186d17ca272f85rbb if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
3568de757bac0b47256647504c186d17ca272f85rbb ok = 1;
3568de757bac0b47256647504c186d17ca272f85rbb break;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb if (!ok) {
3568de757bac0b47256647504c186d17ca272f85rbb /* If all workers are in error state force the recovery.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb worker = (proxy_worker **)balancer->workers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < balancer->workers->nelts; i++, worker++) {
3568de757bac0b47256647504c186d17ca272f85rbb ++(*worker)->s->retries;
3568de757bac0b47256647504c186d17ca272f85rbb (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Forcing recovery for worker (%s)",
3568de757bac0b47256647504c186d17ca272f85rbb balancer->name, (*worker)->s->hostname);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic int proxy_balancer_pre_request(proxy_worker **worker,
3568de757bac0b47256647504c186d17ca272f85rbb proxy_balancer **balancer,
3568de757bac0b47256647504c186d17ca272f85rbb request_rec *r,
3568de757bac0b47256647504c186d17ca272f85rbb proxy_server_conf *conf, char **url)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb int access_status;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker *runtime;
3568de757bac0b47256647504c186d17ca272f85rbb char *route = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb const char *sticky = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb apr_status_t rv;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb *worker = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb /* Step 1: check if the url is for us
3568de757bac0b47256647504c186d17ca272f85rbb * The url we can handle starts with 'balancer://'
3568de757bac0b47256647504c186d17ca272f85rbb * If balancer is already provided skip the search
3568de757bac0b47256647504c186d17ca272f85rbb * for balancer, because this is failover attempt.
beb70d51e435dc36c56a72b6cd83556c04db9283wrowe */
fe6baec9bbcd36f932b71a355120cd7b5a685d6cfielding if (!*balancer &&
3568de757bac0b47256647504c186d17ca272f85rbb !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url)))
3568de757bac0b47256647504c186d17ca272f85rbb return DECLINED;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Step 2: Lock the LoadBalancer
3568de757bac0b47256647504c186d17ca272f85rbb * XXX: perhaps we need the process lock here
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Lock failed for pre_request",
3568de757bac0b47256647504c186d17ca272f85rbb (*balancer)->name);
3568de757bac0b47256647504c186d17ca272f85rbb return DECLINED;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Step 3: force recovery */
ce03576b2434cec77f2921db9d5b6a0510581c23rederpj force_recovery(*balancer, r->server);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Step 3.5: Update member list for the balancer */
3568de757bac0b47256647504c186d17ca272f85rbb /* TODO: Implement as provider! */
8e9734d1a4af74c141e2a0f880bb51bb061fa03atrawick ap_proxy_update_members(*balancer, r->server, conf);
8e9734d1a4af74c141e2a0f880bb51bb061fa03atrawick
3568de757bac0b47256647504c186d17ca272f85rbb /* Step 4: find the session route */
3568de757bac0b47256647504c186d17ca272f85rbb runtime = find_session_route(*balancer, r, &route, &sticky, url);
3568de757bac0b47256647504c186d17ca272f85rbb if (runtime) {
8e9734d1a4af74c141e2a0f880bb51bb061fa03atrawick if ((*balancer)->s->lbmethod && (*balancer)->s->lbmethod->updatelbstatus) {
3568de757bac0b47256647504c186d17ca272f85rbb /* Call the LB implementation */
3568de757bac0b47256647504c186d17ca272f85rbb (*balancer)->s->lbmethod->updatelbstatus(*balancer, runtime, r->server);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else { /* Use the default one */
3568de757bac0b47256647504c186d17ca272f85rbb int i, total_factor = 0;
8e9734d1a4af74c141e2a0f880bb51bb061fa03atrawick proxy_worker **workers;
3568de757bac0b47256647504c186d17ca272f85rbb /* We have a sticky load balancer
8e9734d1a4af74c141e2a0f880bb51bb061fa03atrawick * Update the workers status
3568de757bac0b47256647504c186d17ca272f85rbb * so that even session routes get
3568de757bac0b47256647504c186d17ca272f85rbb * into account.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb workers = (proxy_worker **)(*balancer)->workers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < (*balancer)->workers->nelts; i++) {
3568de757bac0b47256647504c186d17ca272f85rbb /* Take into calculation only the workers that are
3568de757bac0b47256647504c186d17ca272f85rbb * not in error state or not disabled.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb if (PROXY_WORKER_IS_USABLE(*workers)) {
3568de757bac0b47256647504c186d17ca272f85rbb (*workers)->s->lbstatus += (*workers)->s->lbfactor;
3568de757bac0b47256647504c186d17ca272f85rbb total_factor += (*workers)->s->lbfactor;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb workers++;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb runtime->s->lbstatus -= total_factor;
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard }
3568de757bac0b47256647504c186d17ca272f85rbb runtime->s->elected++;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb *worker = runtime;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb else if (route && (*balancer)->s->sticky_force) {
3568de757bac0b47256647504c186d17ca272f85rbb int i, member_of = 0;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker **workers;
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * We have a route provided that doesn't match the
3568de757bac0b47256647504c186d17ca272f85rbb * balancer name. See if the provider route is the
3568de757bac0b47256647504c186d17ca272f85rbb * member of the same balancer in which case return 503
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb workers = (proxy_worker **)(*balancer)->workers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < (*balancer)->workers->nelts; i++) {
3568de757bac0b47256647504c186d17ca272f85rbb if (*((*workers)->s->route) && strcmp((*workers)->s->route, route) == 0) {
3568de757bac0b47256647504c186d17ca272f85rbb member_of = 1;
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard break;
3568de757bac0b47256647504c186d17ca272f85rbb }
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard workers++;
3568de757bac0b47256647504c186d17ca272f85rbb }
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard if (member_of) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard "proxy: BALANCER: (%s). All workers are in error state for route (%s)",
3568de757bac0b47256647504c186d17ca272f85rbb (*balancer)->name, route);
3568de757bac0b47256647504c186d17ca272f85rbb if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Unlock failed for pre_request",
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard (*balancer)->name);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb return HTTP_SERVICE_UNAVAILABLE;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Unlock failed for pre_request",
3568de757bac0b47256647504c186d17ca272f85rbb (*balancer)->name);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb if (!*worker) {
3568de757bac0b47256647504c186d17ca272f85rbb runtime = find_best_worker(*balancer, r);
3568de757bac0b47256647504c186d17ca272f85rbb if (!runtime) {
3568de757bac0b47256647504c186d17ca272f85rbb if ((*balancer)->workers->nelts) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). All workers are in error state",
3568de757bac0b47256647504c186d17ca272f85rbb (*balancer)->name);
3568de757bac0b47256647504c186d17ca272f85rbb } else {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). No workers in balancer",
3568de757bac0b47256647504c186d17ca272f85rbb (*balancer)->name);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb return HTTP_SERVICE_UNAVAILABLE;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb if (*(*balancer)->s->sticky && runtime) {
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * This balancer has sticky sessions and the client either has not
3568de757bac0b47256647504c186d17ca272f85rbb * supplied any routing information or all workers for this route
3568de757bac0b47256647504c186d17ca272f85rbb * including possible redirect and hotstandby workers are in error
3568de757bac0b47256647504c186d17ca272f85rbb * state, but we have found another working worker for this
3568de757bac0b47256647504c186d17ca272f85rbb * balancer where we can send the request. Thus notice that we have
3568de757bac0b47256647504c186d17ca272f85rbb * changed the route to the backend.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb *worker = runtime;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb (*worker)->s->busy++;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Add balancer/worker info to env. */
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->subprocess_env,
3568de757bac0b47256647504c186d17ca272f85rbb "BALANCER_NAME", (*balancer)->name);
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->subprocess_env,
3568de757bac0b47256647504c186d17ca272f85rbb "BALANCER_WORKER_NAME", (*worker)->s->name);
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->subprocess_env,
3568de757bac0b47256647504c186d17ca272f85rbb "BALANCER_WORKER_ROUTE", (*worker)->s->route);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Rewrite the url from 'balancer://url'
3568de757bac0b47256647504c186d17ca272f85rbb * to the 'worker_scheme://worker_hostname[:worker_port]/url'
3568de757bac0b47256647504c186d17ca272f85rbb * This replaces the balancers fictional name with the
3568de757bac0b47256647504c186d17ca272f85rbb * real hostname of the elected worker.
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb access_status = rewrite_url(r, *worker, url);
3568de757bac0b47256647504c186d17ca272f85rbb /* Add the session route to request notes if present */
3568de757bac0b47256647504c186d17ca272f85rbb if (route) {
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->notes, "session-sticky", sticky);
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->notes, "session-route", route);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Add session info to env. */
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->subprocess_env,
3568de757bac0b47256647504c186d17ca272f85rbb "BALANCER_SESSION_STICKY", sticky);
3568de757bac0b47256647504c186d17ca272f85rbb apr_table_setn(r->subprocess_env,
3568de757bac0b47256647504c186d17ca272f85rbb "BALANCER_SESSION_ROUTE", route);
3568de757bac0b47256647504c186d17ca272f85rbb }
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER (%s) worker (%s) rewritten to %s",
3568de757bac0b47256647504c186d17ca272f85rbb (*balancer)->name, (*worker)->s->name, *url);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb return access_status;
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic int proxy_balancer_post_request(proxy_worker *worker,
3568de757bac0b47256647504c186d17ca272f85rbb proxy_balancer *balancer,
3568de757bac0b47256647504c186d17ca272f85rbb request_rec *r,
3568de757bac0b47256647504c186d17ca272f85rbb proxy_server_conf *conf)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb apr_status_t rv;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Lock failed for post_request",
3568de757bac0b47256647504c186d17ca272f85rbb balancer->name);
3568de757bac0b47256647504c186d17ca272f85rbb return HTTP_INTERNAL_SERVER_ERROR;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if (!apr_is_empty_array(balancer->errstatuses)) {
3568de757bac0b47256647504c186d17ca272f85rbb int i;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < balancer->errstatuses->nelts; i++) {
3568de757bac0b47256647504c186d17ca272f85rbb int val = ((int *)balancer->errstatuses->elts)[i];
3568de757bac0b47256647504c186d17ca272f85rbb if (r->status == val) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Forcing recovery for worker (%s), failonstatus %d",
3568de757bac0b47256647504c186d17ca272f85rbb balancer->name, worker->s->name, val);
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->status |= PROXY_WORKER_IN_ERROR;
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->error_time = apr_time_now();
3568de757bac0b47256647504c186d17ca272f85rbb break;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy: BALANCER: (%s). Unlock failed for post_request",
3568de757bac0b47256647504c186d17ca272f85rbb balancer->name);
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
3568de757bac0b47256647504c186d17ca272f85rbb "proxy_balancer_post_request for (%s)", balancer->name);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if (worker && worker->s->busy)
3568de757bac0b47256647504c186d17ca272f85rbb worker->s->busy--;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb return OK;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic void recalc_factors(proxy_balancer *balancer)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb int i;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker **workers;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Recalculate lbfactors */
3568de757bac0b47256647504c186d17ca272f85rbb workers = (proxy_worker **)balancer->workers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb /* Special case if there is only one worker it's
3568de757bac0b47256647504c186d17ca272f85rbb * load factor will always be 1
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb if (balancer->workers->nelts == 1) {
3568de757bac0b47256647504c186d17ca272f85rbb (*workers)->s->lbstatus = (*workers)->s->lbfactor = 1;
3568de757bac0b47256647504c186d17ca272f85rbb return;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < balancer->workers->nelts; i++) {
3568de757bac0b47256647504c186d17ca272f85rbb /* Update the status entries */
3568de757bac0b47256647504c186d17ca272f85rbb workers[i]->s->lbstatus = workers[i]->s->lbfactor;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbbstatic apr_status_t lock_remove(void *data)
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard{
3568de757bac0b47256647504c186d17ca272f85rbb int i;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_balancer *balancer;
3568de757bac0b47256647504c186d17ca272f85rbb server_rec *s = data;
3568de757bac0b47256647504c186d17ca272f85rbb void *sconf = s->module_config;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb balancer = (proxy_balancer *)conf->balancers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
3568de757bac0b47256647504c186d17ca272f85rbb if (balancer->gmutex) {
3568de757bac0b47256647504c186d17ca272f85rbb apr_global_mutex_destroy(balancer->gmutex);
3568de757bac0b47256647504c186d17ca272f85rbb balancer->gmutex = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb return(0);
3568de757bac0b47256647504c186d17ca272f85rbb}
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb/* post_config hook: */
3568de757bac0b47256647504c186d17ca272f85rbbstatic int balancer_post_config(apr_pool_t *pconf, apr_pool_t *plog,
3568de757bac0b47256647504c186d17ca272f85rbb apr_pool_t *ptemp, server_rec *s)
3568de757bac0b47256647504c186d17ca272f85rbb{
3568de757bac0b47256647504c186d17ca272f85rbb apr_status_t rv;
3568de757bac0b47256647504c186d17ca272f85rbb void *data;
3568de757bac0b47256647504c186d17ca272f85rbb void *sconf = s->module_config;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
3568de757bac0b47256647504c186d17ca272f85rbb const char *userdata_key = "mod_proxy_balancer_init";
3568de757bac0b47256647504c186d17ca272f85rbb ap_slotmem_instance_t *new = NULL;
3568de757bac0b47256647504c186d17ca272f85rbb apr_time_t tstamp;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* balancer_post_config() will be called twice during startup. So, only
3568de757bac0b47256647504c186d17ca272f85rbb * set up the static data the 1st time through. */
3568de757bac0b47256647504c186d17ca272f85rbb apr_pool_userdata_get(&data, userdata_key, s->process->pool);
3568de757bac0b47256647504c186d17ca272f85rbb if (!data) {
3568de757bac0b47256647504c186d17ca272f85rbb apr_pool_userdata_set((const void *)1, userdata_key,
3568de757bac0b47256647504c186d17ca272f85rbb apr_pool_cleanup_null, s->process->pool);
3568de757bac0b47256647504c186d17ca272f85rbb return OK;
ad83978f20c7d1a4323059d9af122e56fcd353bdstoddard }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * Get slotmem setups
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb storage = ap_lookup_provider(AP_SLOTMEM_PROVIDER_GROUP, "shared", "0");
3568de757bac0b47256647504c186d17ca272f85rbb if (!storage) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, s,
3568de757bac0b47256647504c186d17ca272f85rbb "ap_lookup_provider %s failed", AP_SLOTMEM_PROVIDER_GROUP);
3568de757bac0b47256647504c186d17ca272f85rbb return !OK;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb tstamp = apr_time_now();
3568de757bac0b47256647504c186d17ca272f85rbb /*
3568de757bac0b47256647504c186d17ca272f85rbb * Go thru each Vhost and create the shared mem slotmem for
3568de757bac0b47256647504c186d17ca272f85rbb * each balancer's workers
3568de757bac0b47256647504c186d17ca272f85rbb */
3568de757bac0b47256647504c186d17ca272f85rbb while (s) {
3568de757bac0b47256647504c186d17ca272f85rbb int i,j;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_balancer *balancer;
3568de757bac0b47256647504c186d17ca272f85rbb sconf = s->module_config;
3568de757bac0b47256647504c186d17ca272f85rbb conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb if (conf->balancers->nelts) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Doing balancers create: %d, %d",
3568de757bac0b47256647504c186d17ca272f85rbb (int)ALIGNED_PROXY_BALANCER_SHARED_SIZE,
3568de757bac0b47256647504c186d17ca272f85rbb (int)conf->balancers->nelts);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb rv = storage->create(&new, conf->id,
3568de757bac0b47256647504c186d17ca272f85rbb ALIGNED_PROXY_BALANCER_SHARED_SIZE,
3568de757bac0b47256647504c186d17ca272f85rbb conf->balancers->nelts, AP_SLOTMEM_TYPE_PREGRAB, pconf);
3568de757bac0b47256647504c186d17ca272f85rbb if (rv != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "balancer slotmem_create failed");
3568de757bac0b47256647504c186d17ca272f85rbb return !OK;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb conf->slot = new;
3568de757bac0b47256647504c186d17ca272f85rbb }
3568de757bac0b47256647504c186d17ca272f85rbb conf->storage = storage;
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Initialize shared scoreboard data */
3568de757bac0b47256647504c186d17ca272f85rbb balancer = (proxy_balancer *)conf->balancers->elts;
3568de757bac0b47256647504c186d17ca272f85rbb for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker **workers;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_worker *worker;
3568de757bac0b47256647504c186d17ca272f85rbb proxy_balancer_shared *bshm;
3568de757bac0b47256647504c186d17ca272f85rbb unsigned int index;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding balancer->max_workers = balancer->workers->nelts + balancer->growth;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* no need for the 'balancer://' prefix */
3568de757bac0b47256647504c186d17ca272f85rbb ap_pstr2_alnum(pconf, balancer->name + sizeof(BALANCER_PREFIX) - 1,
3568de757bac0b47256647504c186d17ca272f85rbb &balancer->sname);
3568de757bac0b47256647504c186d17ca272f85rbb balancer->sname = apr_pstrcat(pconf, conf->id, "_", balancer->sname, NULL);
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb /* Create global mutex */
3568de757bac0b47256647504c186d17ca272f85rbb rv = ap_global_mutex_create(&(balancer->gmutex), NULL, balancer_mutex_type,
3568de757bac0b47256647504c186d17ca272f85rbb balancer->sname, s, pconf, 0);
3568de757bac0b47256647504c186d17ca272f85rbb if (rv != APR_SUCCESS || !balancer->gmutex) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
3568de757bac0b47256647504c186d17ca272f85rbb "mutex creation of %s : %s failed", balancer_mutex_type,
3568de757bac0b47256647504c186d17ca272f85rbb balancer->sname);
3568de757bac0b47256647504c186d17ca272f85rbb return HTTP_INTERNAL_SERVER_ERROR;
ce03576b2434cec77f2921db9d5b6a0510581c23rederpj }
3568de757bac0b47256647504c186d17ca272f85rbb
3568de757bac0b47256647504c186d17ca272f85rbb apr_pool_cleanup_register(pconf, (void *)s, lock_remove,
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe apr_pool_cleanup_null);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe /* setup shm for balancers */
3568de757bac0b47256647504c186d17ca272f85rbb if ((rv = storage->grab(conf->slot, &index)) != APR_SUCCESS) {
ca53a74f4012a45cbad48e940eddf27d866981f9dougm ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "balancer slotmem_grab failed");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return !OK;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
302dc1f7b3feee23a91ad8f3cf3cb2edd95a557bmanoj if ((rv = storage->dptr(conf->slot, index, (void *)&bshm)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "balancer slotmem_dptr failed");
3568de757bac0b47256647504c186d17ca272f85rbb return !OK;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if ((rv = ap_proxy_share_balancer(balancer, bshm, index)) != APR_SUCCESS) {
3568de757bac0b47256647504c186d17ca272f85rbb ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "Cannot share balancer");
3568de757bac0b47256647504c186d17ca272f85rbb return !OK;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
3568de757bac0b47256647504c186d17ca272f85rbb
/* create slotmem slots for workers */
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Doing workers create: %s (%s), %d, %d",
balancer->name, balancer->sname,
(int)ALIGNED_PROXY_WORKER_SHARED_SIZE,
(int)balancer->max_workers);
rv = storage->create(&new, balancer->sname,
ALIGNED_PROXY_WORKER_SHARED_SIZE,
balancer->max_workers, AP_SLOTMEM_TYPE_PREGRAB, pconf);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "worker slotmem_create failed");
return !OK;
}
balancer->slot = new;
balancer->storage = storage;
/* sync all timestamps */
balancer->wupdated = balancer->s->wupdated = tstamp;
/* now go thru each worker */
workers = (proxy_worker **)balancer->workers->elts;
for (j = 0; j < balancer->workers->nelts; j++, workers++) {
proxy_worker_shared *shm;
worker = *workers;
if ((rv = storage->grab(balancer->slot, &index)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "worker slotmem_grab failed");
return !OK;
}
if ((rv = storage->dptr(balancer->slot, index, (void *)&shm)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "worker slotmem_dptr failed");
return !OK;
}
if ((rv = ap_proxy_share_worker(worker, shm, index)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "Cannot share worker");
return !OK;
}
worker->s->updated = tstamp;
}
}
s = s->next;
}
return OK;
}
static void create_radio(const char *name, unsigned int flag, request_rec *r)
{
ap_rvputs(r, "<td>On <input name='", name, "' id='", name, "' value='1' type=radio", NULL);
if (flag)
ap_rputs(" checked", r);
ap_rvputs(r, "> <br/> Off <input name='", name, "' id='", name, "' value='0' type=radio", NULL);
if (!flag)
ap_rputs(" checked", r);
ap_rputs("></td>\n", r);
}
/* Manages the loadfactors and member status
*/
static int balancer_handler(request_rec *r)
{
void *sconf;
proxy_server_conf *conf;
proxy_balancer *balancer, *bsel = NULL;
proxy_worker *worker, *wsel = NULL;
proxy_worker **workers = NULL;
apr_table_t *params;
int access_status;
int i, n;
int ok2change = 1;
const char *name;
/* is this for us? */
if (strcmp(r->handler, "balancer-manager")) {
return DECLINED;
}
r->allowed = (AP_METHOD_BIT << M_GET);
if (r->method_number != M_GET) {
return DECLINED;
}
sconf = r->server->module_config;
conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
params = apr_table_make(r->pool, 10);
balancer = (proxy_balancer *)conf->balancers->elts;
for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
apr_status_t rv;
if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Lock failed for balancer_handler",
balancer->name);
}
ap_proxy_update_members(balancer, r->server, conf);
if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Unlock failed for balancer_handler",
balancer->name);
}
}
if (r->args) {
char *args = apr_pstrdup(r->pool, r->args);
char *tok, *val;
while (args && *args) {
if ((val = ap_strchr(args, '='))) {
*val++ = '\0';
if ((tok = ap_strchr(val, '&')))
*tok++ = '\0';
/*
* Special case: workers are allowed path information
*/
if ((access_status = ap_unescape_url(val)) != OK)
if ((strcmp(args, "w") && strcmp(args, "b_nwrkr")) || (access_status != HTTP_NOT_FOUND))
return access_status;
apr_table_setn(params, args, val);
args = tok;
}
else
return HTTP_BAD_REQUEST;
}
}
if ((name = apr_table_get(params, "b")))
bsel = ap_proxy_get_balancer(r->pool, conf,
apr_pstrcat(r->pool, BALANCER_PREFIX, name, NULL));
if ((name = apr_table_get(params, "w"))) {
wsel = ap_proxy_get_worker(r->pool, bsel, conf, name);
}
/* Check that the supplied nonce matches this server's nonce;
* otherwise ignore all parameters, to prevent a CSRF attack. */
if (!bsel ||
(*bsel->s->nonce &&
(
(name = apr_table_get(params, "nonce")) == NULL ||
strcmp(bsel->s->nonce, name) != 0
)
)
) {
apr_table_clear(params);
ok2change = 0;
}
/* First set the params */
if (wsel && ok2change) {
const char *val;
if ((val = apr_table_get(params, "w_lf"))) {
int ival = atoi(val);
if (ival >= 1 && ival <= 100) {
wsel->s->lbfactor = ival;
if (bsel)
recalc_factors(bsel);
}
}
if ((val = apr_table_get(params, "w_wr"))) {
if (strlen(val) && strlen(val) < sizeof(wsel->s->route))
strcpy(wsel->s->route, val);
else
*wsel->s->route = '\0';
}
if ((val = apr_table_get(params, "w_rr"))) {
if (strlen(val) && strlen(val) < sizeof(wsel->s->redirect))
strcpy(wsel->s->redirect, val);
else
*wsel->s->redirect = '\0';
}
if ((val = apr_table_get(params, "w_status_I"))) {
ap_proxy_set_wstatus('I', atoi(val), wsel);
}
if ((val = apr_table_get(params, "w_status_N"))) {
ap_proxy_set_wstatus('N', atoi(val), wsel);
}
if ((val = apr_table_get(params, "w_status_D"))) {
ap_proxy_set_wstatus('D', atoi(val), wsel);
}
if ((val = apr_table_get(params, "w_status_H"))) {
ap_proxy_set_wstatus('H', atoi(val), wsel);
}
if ((val = apr_table_get(params, "w_ls"))) {
int ival = atoi(val);
if (ival >= 0 && ival <= 99) {
wsel->s->lbset = ival;
}
}
}
if (bsel && ok2change) {
const char *val;
int ival;
if ((val = apr_table_get(params, "b_lbm"))) {
proxy_balancer_method *lbmethod;
lbmethod = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
if (lbmethod)
bsel->s->lbmethod = lbmethod;
}
if ((val = apr_table_get(params, "b_tmo"))) {
ival = atoi(val);
if (ival >= 0 && ival <= 7200) { /* 2 hrs enuff? */
bsel->s->timeout = apr_time_from_sec(ival);
}
}
if ((val = apr_table_get(params, "b_max"))) {
ival = atoi(val);
if (ival >= 0 && ival <= 99) {
bsel->s->max_attempts = ival;
}
}
if ((val = apr_table_get(params, "b_sforce"))) {
ival = atoi(val);
bsel->s->sticky_force = (ival != 0);
}
if ((val = apr_table_get(params, "b_ss")) && *val) {
if (strlen(val) < (PROXY_BALANCER_MAX_STICKY_SIZE-1)) {
if (*val == '-' && *(val+1) == '\0')
*bsel->s->sticky_path = *bsel->s->sticky = '\0';
else {
char *path;
PROXY_STRNCPY(bsel->s->sticky_path, val);
PROXY_STRNCPY(bsel->s->sticky, val);
if ((path = strchr((char *)bsel->s->sticky, '|'))) {
*path++ = '\0';
PROXY_STRNCPY(bsel->s->sticky_path, path);
}
}
}
}
if ((val = apr_table_get(params, "b_wyes")) &&
(*val == '1' && *(val+1) == '\0') &&
(val = apr_table_get(params, "b_nwrkr"))) {
char *ret;
proxy_worker *nworker;
apr_status_t rv;
nworker = ap_proxy_get_worker(conf->pool, bsel, conf, val);
if (!nworker && storage->num_free_slots(bsel->slot)) {
if ((rv = PROXY_GLOBAL_LOCK(bsel)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Lock failed for adding worker",
bsel->name);
}
ret = ap_proxy_define_worker(conf->pool, &nworker, bsel, conf, val, 0);
if (!ret) {
unsigned int index;
proxy_worker_shared *shm;
PROXY_COPY_CONF_PARAMS(nworker, conf);
if ((rv = storage->grab(bsel->slot, &index)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, r->server, "worker slotmem_grab failed");
if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Unlock failed for adding worker",
bsel->name);
}
return HTTP_BAD_REQUEST;
}
if ((rv = storage->dptr(bsel->slot, index, (void *)&shm)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, r->server, "worker slotmem_dptr failed");
if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Unlock failed for adding worker",
bsel->name);
}
return HTTP_BAD_REQUEST;
}
if ((rv = ap_proxy_share_worker(nworker, shm, index)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, r->server, "Cannot share worker");
if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Unlock failed for adding worker",
bsel->name);
}
return HTTP_BAD_REQUEST;
}
if ((rv = ap_proxy_initialize_worker(nworker, r->server, conf->pool)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, r->server, "Cannot init worker");
if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Unlock failed for adding worker",
bsel->name);
}
return HTTP_BAD_REQUEST;
}
/* sync all timestamps */
bsel->wupdated = bsel->s->wupdated = nworker->s->updated = apr_time_now();
/* by default, all new workers are disabled */
ap_proxy_set_wstatus('D', 1, nworker);
}
if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Unlock failed for adding worker",
bsel->name);
}
}
}
}
if (apr_table_get(params, "xml")) {
ap_set_content_type(r, "text/xml");
ap_rputs("<?xml version='1.0' encoding='UTF-8' ?>\n", r);
ap_rputs("<httpd:manager xmlns:httpd='http://httpd.apache.org'>\n", r);
ap_rputs(" <httpd:balancers>\n", r);
balancer = (proxy_balancer *)conf->balancers->elts;
for (i = 0; i < conf->balancers->nelts; i++) {
ap_rputs(" <httpd:balancer>\n", r);
ap_rvputs(r, " <httpd:name>", balancer->name, "</httpd:name>\n", NULL);
ap_rputs(" <httpd:workers>\n", r);
workers = (proxy_worker **)balancer->workers->elts;
for (n = 0; n < balancer->workers->nelts; n++) {
worker = *workers;
ap_rputs(" <httpd:worker>\n", r);
ap_rvputs(r, " <httpd:scheme>", worker->s->scheme,
"</httpd:scheme>\n", NULL);
ap_rvputs(r, " <httpd:hostname>", worker->s->hostname,
"</httpd:hostname>\n", NULL);
ap_rprintf(r, " <httpd:loadfactor>%d</httpd:loadfactor>\n",
worker->s->lbfactor);
ap_rputs(" </httpd:worker>\n", r);
++workers;
}
ap_rputs(" </httpd:workers>\n", r);
ap_rputs(" </httpd:balancer>\n", r);
++balancer;
}
ap_rputs(" </httpd:balancers>\n", r);
ap_rputs("</httpd:manager>", r);
}
else {
ap_set_content_type(r, "text/html; charset=ISO-8859-1");
ap_rputs(DOCTYPE_HTML_3_2
"<html><head><title>Balancer Manager</title></head>\n", r);
ap_rputs("<body><h1>Load Balancer Manager for ", r);
ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
ap_rvputs(r, "<dl><dt>Server Version: ",
ap_get_server_description(), "</dt>\n", NULL);
ap_rvputs(r, "<dt>Server Built: ",
ap_get_server_built(), "\n</dt></dl>\n", NULL);
balancer = (proxy_balancer *)conf->balancers->elts;
for (i = 0; i < conf->balancers->nelts; i++) {
ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
ap_rvputs(r, "<a href='", r->uri, "?b=",
balancer->name + sizeof(BALANCER_PREFIX) - 1,
"&nonce=", balancer->s->nonce,
"'>", NULL);
ap_rvputs(r, balancer->name, "</a></h3>\n\n", NULL);
ap_rputs("\n\n<table border='0' style='text-align: left;'><tr>"
"<th>MaxMembers</th><th>StickySession</th><th>DisableFailover</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
"</tr>\n<tr>", r);
/* the below is a safe cast, since the number of slots total will
* never be more than max_workers, which is restricted to int */
ap_rprintf(r, "<td align='center'>%d [%d Used]</td>\n", balancer->max_workers,
balancer->max_workers - (int)storage->num_free_slots(balancer->slot));
if (*balancer->s->sticky) {
if (strcmp(balancer->s->sticky, balancer->s->sticky_path)) {
ap_rvputs(r, "<td align='center'>", balancer->s->sticky, " | ",
balancer->s->sticky_path, NULL);
}
else {
ap_rvputs(r, "<td align='center'>", balancer->s->sticky, NULL);
}
}
else {
ap_rputs("<td align='center'> (None) ", r);
}
ap_rprintf(r, "<td align='center'>%s</td>\n",
balancer->s->sticky_force ? "On" : "Off");
ap_rprintf(r, "</td><td align='center'>%" APR_TIME_T_FMT "</td>",
apr_time_sec(balancer->s->timeout));
ap_rprintf(r, "<td align='center'>%d</td>\n", balancer->s->max_attempts);
ap_rprintf(r, "<td align='center'>%s</td>\n",
balancer->s->lbmethod->name);
ap_rputs("</table>\n<br />", r);
ap_rputs("\n\n<table border='0' style='text-align: left;'><tr>"
"<th>Worker URL</th>"
"<th>Route</th><th>RouteRedir</th>"
"<th>Factor</th><th>Set</th><th align='center'>Status</th>"
"<th>Elected</th><th>To</th><th>From</th>"
"</tr>\n", r);
workers = (proxy_worker **)balancer->workers->elts;
for (n = 0; n < balancer->workers->nelts; n++) {
char fbuf[50];
worker = *workers;
ap_rvputs(r, "<tr>\n<td><a href='", r->uri, "?b=",
balancer->name + sizeof(BALANCER_PREFIX) - 1, "&w=",
ap_escape_uri(r->pool, worker->s->name),
"&nonce=", balancer->s->nonce,
"'>", NULL);
ap_rvputs(r, worker->s->name, "</a></td>", NULL);
ap_rvputs(r, "<td align='center'>", ap_escape_html(r->pool, worker->s->route),
NULL);
ap_rvputs(r, "</td><td align='center'>",
ap_escape_html(r->pool, worker->s->redirect), NULL);
ap_rprintf(r, "</td><td align='center'>%d</td>", worker->s->lbfactor);
ap_rprintf(r, "<td align='center'>%d</td><td align='center'>", worker->s->lbset);
ap_rvputs(r, ap_proxy_parse_wstatus(r->pool, worker), NULL);
ap_rputs("</td>", r);
ap_rprintf(r, "<td align='center'>%" APR_SIZE_T_FMT "</td><td align='center'>", worker->s->elected);
ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
ap_rputs("</td><td align='center'>", r);
ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
ap_rputs("</td></tr>\n", r);
++workers;
}
ap_rputs("</table>\n", r);
++balancer;
}
ap_rputs("<hr />\n", r);
if (wsel && bsel) {
ap_rputs("<h3>Edit worker settings for ", r);
ap_rvputs(r, wsel->s->name, "</h3>\n", NULL);
ap_rvputs(r, "<form method='GET' action='", NULL);
ap_rvputs(r, r->uri, "'>\n<dl>", NULL);
ap_rputs("<table><tr><td>Load factor:</td><td><input name='w_lf' id='w_lf' type=text ", r);
ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbfactor);
ap_rputs("<tr><td>LB Set:</td><td><input name='w_ls' id='w_ls' type=text ", r);
ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbset);
ap_rputs("<tr><td>Route:</td><td><input name='w_wr' id='w_wr' type=text ", r);
ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->route),
NULL);
ap_rputs("'></td></tr>\n", r);
ap_rputs("<tr><td>Route Redirect:</td><td><input name='w_rr' id='w_rr' type=text ", r);
ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->redirect),
NULL);
ap_rputs("'></td></tr>\n", r);
ap_rputs("<tr><td>Status:</td>", r);
ap_rputs("<td><table border='1'><tr><th>Ign</th><th>Drn</th><th>Dis</th><th>Stby</th></tr>\n<tr>", r);
create_radio("w_status_I", (PROXY_WORKER_IGNORE_ERRORS & wsel->s->status), r);
create_radio("w_status_N", (PROXY_WORKER_DRAIN & wsel->s->status), r);
create_radio("w_status_D", (PROXY_WORKER_DISABLED & wsel->s->status), r);
create_radio("w_status_H", (PROXY_WORKER_HOT_STANDBY & wsel->s->status), r);
ap_rputs("</tr></table>\n", r);
ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
ap_rvputs(r, "</table>\n<input type=hidden name='w' id='w' ", NULL);
ap_rvputs(r, "value='", ap_escape_uri(r->pool, wsel->s->name), "'>\n", NULL);
ap_rvputs(r, "<input type=hidden name='b' id='b' ", NULL);
ap_rvputs(r, "value='", bsel->name + sizeof(BALANCER_PREFIX) - 1,
"'>\n", NULL);
ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
bsel->s->nonce, "'>\n", NULL);
ap_rvputs(r, "</form>\n", NULL);
ap_rputs("<hr />\n", r);
} else if (bsel) {
const apr_array_header_t *provs;
const ap_list_provider_names_t *pname;
int i;
ap_rputs("<h3>Edit balancer settings for ", r);
ap_rvputs(r, bsel->name, "</h3>\n", NULL);
ap_rvputs(r, "<form method='GET' action='", NULL);
ap_rvputs(r, r->uri, "'>\n<dl>\n<table>\n", NULL);
provs = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
if (provs) {
ap_rputs("<tr><td>LBmethod:</td>", r);
ap_rputs("<td>\n<select name='b_lbm' id='b_lbm'>", r);
pname = (ap_list_provider_names_t *)provs->elts;
for (i = 0; i < provs->nelts; i++, pname++) {
ap_rvputs(r,"<option value='", pname->provider_name, "'", NULL);
if (strcmp(pname->provider_name, bsel->s->lbmethod->name) == 0)
ap_rputs(" selected ", r);
ap_rvputs(r, ">", pname->provider_name, "\n", NULL);
}
ap_rputs("</select>\n</td></tr>\n", r);
}
ap_rputs("<tr><td>Timeout:</td><td><input name='b_tmo' id='b_tmo' type=text ", r);
ap_rprintf(r, "value='%" APR_TIME_T_FMT "'></td></tr>\n", apr_time_sec(bsel->s->timeout));
ap_rputs("<tr><td>Failover Attempts:</td><td><input name='b_max' id='b_max' type=text ", r);
ap_rprintf(r, "value='%d'></td></tr>\n", bsel->s->max_attempts);
ap_rputs("<tr><td>Disable Failover:</td>", r);
create_radio("b_sforce", bsel->s->sticky_force, r);
ap_rputs("<tr><td>Sticky Session:</td><td><input name='b_ss' id='b_ss' size=64 type=text ", r);
if (strcmp(bsel->s->sticky, bsel->s->sticky_path)) {
ap_rvputs(r, "value ='", bsel->s->sticky, " | ",
bsel->s->sticky_path, NULL);
}
else {
ap_rvputs(r, "value ='", bsel->s->sticky, NULL);
}
ap_rputs("'>&nbsp;&nbsp;&nbsp;&nbsp;(Use '-' to delete)</td></tr>\n", r);
if (storage->num_free_slots(bsel->slot) != 0) {
ap_rputs("<tr><td>Add New Worker:</td><td><input name='b_nwrkr' id='b_nwrkr' size=32 type=text>"
"&nbsp;&nbsp;&nbsp;&nbsp;Are you sure? <input name='b_wyes' id='b_wyes' type=checkbox value='1'>"
"</td></tr>", r);
}
ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
ap_rvputs(r, "</table>\n<input type=hidden name='b' id='b' ", NULL);
ap_rvputs(r, "value='", bsel->name + sizeof(BALANCER_PREFIX) - 1,
"'>\n", NULL);
ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
bsel->s->nonce, "'>\n", NULL);
ap_rvputs(r, "</form>\n", NULL);
ap_rputs("<hr />\n", r);
}
ap_rputs(ap_psignature("",r), r);
ap_rputs("</body></html>\n", r);
}
return OK;
}
static void balancer_child_init(apr_pool_t *p, server_rec *s)
{
while (s) {
proxy_balancer *balancer;
int i;
void *sconf = s->module_config;
proxy_server_conf *conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
apr_status_t rv;
if (conf->balancers->nelts) {
apr_size_t size;
unsigned int num;
storage->attach(&(conf->slot), conf->id, &size, &num, p);
if (!conf->slot) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, s, "slotmem_attach failed");
exit(1); /* Ugly, but what else? */
}
}
balancer = (proxy_balancer *)conf->balancers->elts;
for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
rv = ap_proxy_initialize_balancer(balancer, s, p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
"Failed to init balancer %s in child",
balancer->name);
exit(1); /* Ugly, but what else? */
}
init_balancer_members(conf->pool, s, balancer);
}
s = s->next;
}
}
PROXY_DECLARE(apr_status_t) ap_proxy_update_members(proxy_balancer *b, server_rec *s,
proxy_server_conf *conf)
{
proxy_worker **workers;
int i;
unsigned int index;
proxy_worker_shared *shm;
if (b->s->wupdated <= b->wupdated)
return APR_SUCCESS;
/*
* Look thru the list of workers in shm
* and see which one(s) we are lacking
*/
for (index = 0; index < b->max_workers; index++) {
int found;
apr_status_t rv;
if ((rv = storage->dptr(b->slot, index, (void *)&shm)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "worker slotmem_dptr failed");
return APR_EGENERAL;
}
/* account for possible "holes" in the slotmem
* (eg: slots 0-2 are used, but 3 isn't, but 4-5 is)
*/
if (!shm->hash)
continue;
found = 0;
workers = (proxy_worker **)b->workers->elts;
for (i = 0; i < b->workers->nelts; i++, workers++) {
proxy_worker *worker = *workers;
if (worker->hash == shm->hash) {
found = 1;
break;
}
}
if (!found) {
proxy_worker **runtime;
runtime = apr_array_push(b->workers);
*runtime = apr_palloc(conf->pool, sizeof(proxy_worker));
(*runtime)->hash = shm->hash;
(*runtime)->context = NULL;
(*runtime)->cp = NULL;
(*runtime)->balancer = b;
(*runtime)->s = shm;
if ((rv = ap_proxy_initialize_worker(*runtime, s, conf->pool)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, "Cannot init worker");
return rv;
}
}
}
b->wupdated = b->s->wupdated;
return APR_SUCCESS;
}
static void ap_proxy_balancer_register_hook(apr_pool_t *p)
{
/* Only the mpm_winnt has child init hook handler.
* make sure that we are called after the mpm
* initializes
*/
static const char *const aszPred[] = { "mpm_winnt.c", "mod_slotmem_shm.c", NULL};
/* manager handler */
ap_hook_post_config(balancer_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_pre_config(balancer_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
ap_hook_child_init(balancer_child_init, aszPred, NULL, APR_HOOK_MIDDLE);
proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
}
AP_DECLARE_MODULE(proxy_balancer) = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
NULL, /* command apr_table_t */
ap_proxy_balancer_register_hook /* register hooks */
};