-*- coding: utf-8 -*-
Changes with Apache 2.3.0
+ *) mod_proxy/mod_proxy_balancer: Provide a simple, functional
+ interface to add additional balancer lb selection methods
+ without requiring code changes to mod_proxy/mod_proxy_balancer;
+ these can be implemented via sub-modules now. [Jim Jagielski]
+
*) mod_cache: Fix incorrectly served 304 responses when expired cache
entity is valid, but cache is unwritable and headers cannot be
updated. [Colm MacCarthaigh <colm stdlib.net>]
return NULL;
}
-static const char *set_balancer_param(apr_pool_t *p,
+static const char *set_balancer_param(proxy_server_conf *conf,
+ apr_pool_t *p,
proxy_balancer *balancer,
const char *key,
const char *val)
balancer->max_attempts_set = 1;
}
else if (!strcasecmp(key, "lbmethod")) {
- /* Which LB scheduler method */
- if (!strcasecmp(val, "traffic"))
- balancer->lbmethod = lbmethod_traffic;
- else if (!strcasecmp(val, "requests"))
- balancer->lbmethod = lbmethod_requests;
- else
- return "lbmethod must be Traffic|Requests";
+ struct proxy_balancer_method *ent =
+ (struct proxy_balancer_method *) conf->lbmethods->elts;
+ int i;
+ for (i = 0; i < conf->lbmethods->nelts; i++) {
+ if (!strcasecmp(val, ent->name)) {
+ balancer->lbmethod = ent;
+ return NULL;
+ }
+ ent++;
+ }
+ return "unknown lbmethod";
}
else {
return "unknown Balancer parameter";
ps->allowed_connect_ports = apr_array_make(p, 10, sizeof(int));
ps->workers = apr_array_make(p, 10, sizeof(proxy_worker));
ps->balancers = apr_array_make(p, 10, sizeof(proxy_balancer));
+ ps->lbmethods = apr_array_make(p, 10, sizeof(proxy_balancer_method));
ps->forward = NULL;
ps->reverse = NULL;
ps->domain = NULL;
ps->badopt = bad_error;
ps->badopt_set = 0;
ps->pool = p;
+
+ proxy_run_load_lbmethods(ps);
+
return ps;
}
ps->allowed_connect_ports = apr_array_append(p, base->allowed_connect_ports, overrides->allowed_connect_ports);
ps->workers = apr_array_append(p, base->workers, overrides->workers);
ps->balancers = apr_array_append(p, base->balancers, overrides->balancers);
+ ps->lbmethods = apr_array_append(p, base->lbmethods, overrides->lbmethods);
ps->forward = overrides->forward ? overrides->forward : base->forward;
ps->reverse = overrides->reverse ? overrides->reverse : base->reverse;
return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
}
for (i = 0; i < arr->nelts; i++) {
- const char *err = set_balancer_param(cmd->pool, balancer, elts[i].key,
+ const char *err = set_balancer_param(conf, cmd->pool, balancer, elts[i].key,
elts[i].val);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxyPass ", err, NULL);
if (worker)
err = set_worker_param(cmd->pool, worker, word, val);
else
- err = set_balancer_param(cmd->pool, balancer, word, val);
+ err = set_balancer_param(conf, cmd->pool, balancer, word, val);
if (err)
return apr_pstrcat(cmd->temp_pool, "ProxySet ", err, " ", word, " ", name, NULL);
ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
apr_time_sec(balancer->timeout));
ap_rprintf(r, "<td>%s</td>\n",
- balancer->lbmethod == lbmethod_requests ? "Requests" :
- balancer->lbmethod == lbmethod_traffic ? "Traffic" :
- "Unknown");
+ balancer->lbmethod->name);
ap_rputs("</table>\n", r);
ap_rputs("\n\n<table border=\"0\"><tr>"
"<th>Sch</th><th>Host</th><th>Stat</th>"
APR_HOOK_LINK(canon_handler)
APR_HOOK_LINK(pre_request)
APR_HOOK_LINK(post_request)
+ APR_HOOK_LINK(load_lbmethods)
APR_HOOK_LINK(request_status)
)
request_rec *r,
proxy_server_conf *conf),(worker,
balancer,r,conf),DECLINED)
+APR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(proxy, PROXY, int, load_lbmethods,
+ (proxy_server_conf *conf),
+ (conf),
+ OK, DECLINED)
APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, fixups,
(request_rec *r), (r),
OK, DECLINED)
typedef struct proxy_balancer proxy_balancer;
typedef struct proxy_worker proxy_worker;
typedef struct proxy_conn_pool proxy_conn_pool;
+typedef struct proxy_balancer_method proxy_balancer_method;
typedef struct {
apr_array_header_t *proxies;
} proxy_status; /* Status display options */
char proxy_status_set;
apr_pool_t *pool; /* Pool used for allocating this struct */
+ apr_array_header_t *lbmethods;
} proxy_server_conf;
apr_size_t elected; /* Number of times the worker was elected */
char route[PROXY_WORKER_MAX_ROUTE_SIZ+1];
char redirect[PROXY_WORKER_MAX_ROUTE_SIZ+1];
+ void *context; /* general purpose storage */
} proxy_worker_stat;
/* Worker configuration */
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex; /* Thread lock for updating address cache */
#endif
+ void *context; /* general purpose storage */
};
struct proxy_balancer {
apr_interval_time_t timeout; /* Timeout for waiting on free connection */
int max_attempts; /* Number of attempts before failing */
char max_attempts_set;
- enum {
- lbmethod_requests = 1,
- lbmethod_traffic = 2
- } lbmethod;
+ proxy_balancer_method *lbmethod;
/* XXX: Perhaps we will need the proc mutex too.
* Altrough we are only using arithmetic operations
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex; /* Thread lock for updating lb params */
#endif
+ void *context; /* general purpose storage */
+};
+
+struct proxy_balancer_method {
+ const char *name; /* name of the load balancer method*/
+ proxy_worker *(*finder)(proxy_balancer *balancer,
+ request_rec *r);
+ void *context; /* general purpose storage */
};
#if APR_HAS_THREADS
APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r))
+/*
+ * Useful hook run within the create per-server phase which
+ * adds the required lbmethod structs, so they exist at
+ * configure time
+ */
+APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, load_lbmethods,
+ (proxy_server_conf *conf))
+
/**
* pre request hook.
* It will return the most suitable worker at the moment
#include "mod_proxy.h"
#include "ap_mpm.h"
#include "apr_version.h"
+#include "apr_hooks.h"
module AP_MODULE_DECLARE_DATA proxy_balancer_module;
* b a d c d a c d b d ...
*
*/
+
static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
- request_rec *r)
+ request_rec *r)
{
int i;
int total_factor = 0;
proxy_worker *worker = (proxy_worker *)balancer->workers->elts;
- proxy_worker *candidate = NULL;
+ proxy_worker *mycandidate = NULL;
/* First try to see if we have available candidate */
for (i = 0; i < balancer->workers->nelts; i++) {
if (PROXY_WORKER_IS_USABLE(worker)) {
worker->s->lbstatus += worker->s->lbfactor;
total_factor += worker->s->lbfactor;
- if (!candidate || worker->s->lbstatus > candidate->s->lbstatus)
- candidate = worker;
+ if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
+ mycandidate = worker;
}
worker++;
}
- if (candidate) {
- candidate->s->lbstatus -= total_factor;
- candidate->s->elected++;
+ if (mycandidate) {
+ mycandidate->s->lbstatus -= total_factor;
+ mycandidate->s->elected++;
}
- return candidate;
+ return mycandidate;
}
/*
* chosen more often, to even things out.
*/
static proxy_worker *find_best_bytraffic(proxy_balancer *balancer,
- request_rec *r)
+ request_rec *r)
{
int i;
apr_off_t mytraffic = 0;
apr_off_t curmin = 0;
proxy_worker *worker = (proxy_worker *)balancer->workers->elts;
- proxy_worker *candidate = NULL;
+ proxy_worker *mycandidate = NULL;
/* First try to see if we have available candidate */
for (i = 0; i < balancer->workers->nelts; i++) {
if (PROXY_WORKER_IS_USABLE(worker)) {
mytraffic = (worker->s->transferred/worker->s->lbfactor) +
(worker->s->read/worker->s->lbfactor);
- if (!candidate || mytraffic < curmin) {
- candidate = worker;
+ if (!mycandidate || mytraffic < curmin) {
+ mycandidate = worker;
curmin = mytraffic;
}
}
worker++;
}
- if (candidate) {
- candidate->s->elected++;
+ if (mycandidate) {
+ mycandidate->s->elected++;
}
- return candidate;
+ return mycandidate;
}
static proxy_worker *find_best_worker(proxy_balancer *balancer,
if (PROXY_THREAD_LOCK(balancer) != APR_SUCCESS)
return NULL;
- if (balancer->lbmethod == lbmethod_requests) {
- candidate = find_best_byrequests(balancer, r);
- } else if (balancer->lbmethod == lbmethod_traffic) {
- candidate = find_best_bytraffic(balancer, r);
- } else {
+ candidate = (*balancer->lbmethod->finder)(balancer, r);
+
+/*
PROXY_THREAD_UNLOCK(balancer);
return NULL;
- }
+*/
PROXY_THREAD_UNLOCK(balancer);
bsel->max_attempts_set = 1;
}
if ((val = apr_table_get(params, "lm"))) {
- int ival = atoi(val);
- switch(ival) {
- case 0:
- break;
- case lbmethod_traffic:
- bsel->lbmethod = lbmethod_traffic;
- break;
- case lbmethod_requests:
- bsel->lbmethod = lbmethod_requests;
- break;
- default:
+ struct proxy_balancer_method *ent =
+ (struct proxy_balancer_method *) conf->lbmethods->elts;
+ int i;
+ for (i = 0; i < conf->lbmethods->nelts; i++) {
+ if (!strcasecmp(val, ent->name)) {
+ bsel->lbmethod = ent;
break;
+ }
+ ent++;
}
}
}
apr_time_sec(balancer->timeout));
ap_rprintf(r, "<td>%d</td>\n", balancer->max_attempts);
ap_rprintf(r, "<td>%s</td>\n",
- balancer->lbmethod == lbmethod_requests ? "Requests" :
- balancer->lbmethod == lbmethod_traffic ? "Traffic" : "Unknown");
+ balancer->lbmethod->name);
ap_rputs("</table>\n", r);
ap_rputs("\n\n<table border=\"0\"><tr>"
"<th>Scheme</th><th>Host</th>"
ap_rprintf(r, "value=\"%d\"></td></tr>\n",
bsel->max_attempts);
ap_rputs("<tr><td>LB Method:</td><td><select name=\"lm\">", r);
- ap_rprintf(r, "<option value=\"%d\" %s>Requests</option>", lbmethod_requests,
- bsel->lbmethod == lbmethod_requests ? "selected" : "");
- ap_rprintf(r, "<option value=\"%d\" %s>Traffic</option>", lbmethod_traffic,
- bsel->lbmethod == lbmethod_traffic ? "selected" : "");
+ {
+ struct proxy_balancer_method *ent =
+ (struct proxy_balancer_method *) conf->lbmethods->elts;
+ int i;
+ for (i = 0; i < conf->lbmethods->nelts; i++) {
+ ap_rprintf(r, "<option value=\"%s\" %s>%s</option>", ent->name,
+ (!strcasecmp(bsel->lbmethod->name, ent->name)) ? "selected" : "",
+ ent->name);
+ ent++;
+ }
+ }
ap_rputs("</select></td></tr>\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=\"b\" ", NULL);
}
+/*
+ * How to add additional lbmethods:
+ * 1. Create func which determines "best" candidate worker
+ * (eg: find_best_bytraffic, above)
+ * 2. Create proxy_balancer_method struct which
+ * defines the method and add it to
+ * available server methods using
+ * the proxy_hook_load_lbmethods hook
+ * (eg: add_lbmethods below).
+ */
+static int add_lbmethods(proxy_server_conf *conf)
+{
+ proxy_balancer_method *new;
+
+ new = apr_array_push(conf->lbmethods);
+ new->name = "byrequests";
+ new->finder = find_best_byrequests;
+ new = apr_array_push(conf->lbmethods);
+ new->name = "bytraffic";
+ new->finder = find_best_bytraffic;
+
+ return OK;
+}
+
static void ap_proxy_balancer_register_hook(apr_pool_t *p)
{
/* Only the mpm_winnt has child init hook handler.
static const char *const aszPred[] = { "mpm_winnt.c", "mod_proxy.c", NULL};
/* manager handler */
ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
- ap_hook_child_init(child_init, aszPred, NULL, APR_HOOK_MIDDLE);
+ ap_hook_child_init(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);
+ proxy_hook_load_lbmethods(add_lbmethods, NULL, NULL, APR_HOOK_FIRST);
}
module AP_MODULE_DECLARE_DATA proxy_balancer_module = {
const char *url)
{
char *c, *q, *uri = apr_pstrdup(p, url);
+ int i;
+ proxy_balancer_method *lbmethod;
c = strchr(uri, ':');
if (c == NULL || c[1] != '/' || c[2] != '/' || c[3] == '\0')
*balancer = apr_array_push(conf->balancers);
memset(*balancer, 0, sizeof(proxy_balancer));
+ /*
+ * NOTE: The default method is byrequests, which we assume
+ * exists!
+ */
+ lbmethod = (proxy_balancer_method *)conf->lbmethods->elts;
+ for (i = 0; i < conf->lbmethods->nelts; i++) {
+ if (!strcasecmp(lbmethod->name, "byrequests")) {
+ break;
+ }
+ lbmethod++;
+ }
+
(*balancer)->name = uri;
- (*balancer)->lbmethod = lbmethod_requests;
+ (*balancer)->lbmethod = lbmethod;
(*balancer)->workers = apr_array_make(p, 5, sizeof(proxy_worker));
/* XXX Is this a right place to create mutex */
#if APR_HAS_THREADS