1
0
mirror of https://github.com/apache/httpd.git synced 2025-08-07 04:02:58 +03:00

No longer depend on how fork() works when laying out segments...

function pointers in shm are... nasty ;)

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1072098 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jim Jagielski
2011-02-18 18:40:25 +00:00
parent ce73ec0484
commit aac783bdf5
4 changed files with 60 additions and 34 deletions

View File

@@ -322,9 +322,12 @@ static const char *set_balancer_param(proxy_server_conf *conf,
} }
else if (!strcasecmp(key, "lbmethod")) { else if (!strcasecmp(key, "lbmethod")) {
proxy_balancer_method *provider; proxy_balancer_method *provider;
if (strlen(val) > (sizeof(balancer->s->lbpname)-1))
return "unknown lbmethod";
provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0"); provider = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
if (provider) { if (provider) {
balancer->s->lbmethod = provider; balancer->lbmethod = provider;
PROXY_STRNCPY(balancer->s->lbpname, val);
return NULL; return NULL;
} }
return "unknown lbmethod"; return "unknown lbmethod";
@@ -2309,7 +2312,7 @@ static int proxy_status_hook(request_rec *r, int flags)
ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>", ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
apr_time_sec(balancer->s->timeout)); apr_time_sec(balancer->s->timeout));
ap_rprintf(r, "<td>%s</td>\n", ap_rprintf(r, "<td>%s</td>\n",
balancer->s->lbmethod->name); balancer->lbmethod->name);
ap_rputs("</table>\n", r); ap_rputs("</table>\n", r);
ap_rputs("\n\n<table border=\"0\"><tr>" ap_rputs("\n\n<table border=\"0\"><tr>"
"<th>Sch</th><th>Host</th><th>Stat</th>" "<th>Sch</th><th>Host</th><th>Stat</th>"

View File

@@ -296,8 +296,11 @@ PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
#define PROXY_WORKER_MAX_SCHEME_SIZE 16 #define PROXY_WORKER_MAX_SCHEME_SIZE 16
#define PROXY_WORKER_MAX_ROUTE_SIZE 64 #define PROXY_WORKER_MAX_ROUTE_SIZE 64
#define PROXY_WORKER_MAX_NAME_SIZE 96 #define PROXY_WORKER_MAX_NAME_SIZE 96
#define PROXY_WORKER_MAX_HOSTNAME_SIZE 64
#define PROXY_BALANCER_MAX_STICKY_SIZE 64 #define PROXY_BALANCER_MAX_STICKY_SIZE 64
#define PROXY_MAX_PROVIDER_NAME_SIZE 16
#define PROXY_STRNCPY(dst, src) apr_cpystrn((dst), (src), sizeof(dst)) #define PROXY_STRNCPY(dst, src) apr_cpystrn((dst), (src), sizeof(dst))
#define PROXY_COPY_CONF_PARAMS(w, c) \ #define PROXY_COPY_CONF_PARAMS(w, c) \
@@ -315,7 +318,7 @@ do { \
typedef struct { typedef struct {
char name[PROXY_WORKER_MAX_NAME_SIZE]; char name[PROXY_WORKER_MAX_NAME_SIZE];
char scheme[PROXY_WORKER_MAX_SCHEME_SIZE]; /* scheme to use ajp|http|https */ char scheme[PROXY_WORKER_MAX_SCHEME_SIZE]; /* scheme to use ajp|http|https */
char hostname[PROXY_WORKER_MAX_ROUTE_SIZE]; /* remote backend address */ char hostname[PROXY_WORKER_MAX_HOSTNAME_SIZE]; /* remote backend address */
char route[PROXY_WORKER_MAX_ROUTE_SIZE]; /* balancing route */ char route[PROXY_WORKER_MAX_ROUTE_SIZE]; /* balancing route */
char redirect[PROXY_WORKER_MAX_ROUTE_SIZE]; /* temporary balancing redirection route */ char redirect[PROXY_WORKER_MAX_ROUTE_SIZE]; /* temporary balancing redirection route */
char flusher[PROXY_WORKER_MAX_SCHEME_SIZE]; /* flush provider used by mod_proxy_fdpass */ char flusher[PROXY_WORKER_MAX_SCHEME_SIZE]; /* flush provider used by mod_proxy_fdpass */
@@ -389,10 +392,10 @@ struct proxy_worker {
typedef struct { typedef struct {
char sticky_path[PROXY_BALANCER_MAX_STICKY_SIZE]; /* URL sticky session identifier */ char sticky_path[PROXY_BALANCER_MAX_STICKY_SIZE]; /* URL sticky session identifier */
char sticky[PROXY_BALANCER_MAX_STICKY_SIZE]; /* sticky session identifier */ char sticky[PROXY_BALANCER_MAX_STICKY_SIZE]; /* sticky session identifier */
char lbpname[PROXY_MAX_PROVIDER_NAME_SIZE]; /* lbmethod provider name */
char nonce[APR_UUID_FORMATTED_LENGTH + 1]; char nonce[APR_UUID_FORMATTED_LENGTH + 1];
apr_interval_time_t timeout; /* Timeout for waiting on free connection */ apr_interval_time_t timeout; /* Timeout for waiting on free connection */
apr_time_t wupdated; /* timestamp of last change to workers list */ apr_time_t wupdated; /* timestamp of last change to workers list */
proxy_balancer_method *lbmethod;
int max_attempts; /* Number of attempts before failing */ int max_attempts; /* Number of attempts before failing */
int index; /* shm array index */ int index; /* shm array index */
unsigned int sticky_force:1; /* Disable failover for sticky sessions */ unsigned int sticky_force:1; /* Disable failover for sticky sessions */
@@ -413,6 +416,7 @@ struct proxy_balancer {
const char *name; /* name of the load balancer */ const char *name; /* name of the load balancer */
const char *sname; /* filesystem safe balancer name */ const char *sname; /* filesystem safe balancer name */
apr_time_t wupdated; /* timestamp of last change to workers list */ apr_time_t wupdated; /* timestamp of last change to workers list */
proxy_balancer_method *lbmethod;
apr_global_mutex_t *gmutex; /* global lock for updating list of workers */ apr_global_mutex_t *gmutex; /* global lock for updating list of workers */
apr_thread_mutex_t *tmutex; /* Thread lock for updating shm */ apr_thread_mutex_t *tmutex; /* Thread lock for updating shm */
void *context; /* general purpose storage */ void *context; /* general purpose storage */
@@ -857,14 +861,14 @@ PROXY_DECLARE(char *) ap_proxy_parse_wstatus(apr_pool_t *p, proxy_worker *w);
/** /**
* Create readable representation of worker status bitfield * Sync balancer and workers based on any updates w/i shm
* @param b balancer to check/update member list of * @param b balancer to check/update member list of
* @param s server rec * @param s server rec
* @param conf config * @param conf config
* @return APR_SUCCESS if all goes well * @return APR_SUCCESS if all goes well
*/ */
PROXY_DECLARE(apr_status_t) ap_proxy_update_members(proxy_balancer *b, server_rec *s, PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b, server_rec *s,
proxy_server_conf *conf); proxy_server_conf *conf);
/** /**

View File

@@ -331,7 +331,7 @@ static proxy_worker *find_best_worker(proxy_balancer *balancer,
return NULL; return NULL;
} }
candidate = (*balancer->s->lbmethod->finder)(balancer, r); candidate = (*balancer->lbmethod->finder)(balancer, r);
if (candidate) if (candidate)
candidate->s->elected++; candidate->s->elected++;
@@ -470,14 +470,14 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
/* Step 3.5: Update member list for the balancer */ /* Step 3.5: Update member list for the balancer */
/* TODO: Implement as provider! */ /* TODO: Implement as provider! */
ap_proxy_update_members(*balancer, r->server, conf); ap_proxy_sync_balancer(*balancer, r->server, conf);
/* Step 4: find the session route */ /* Step 4: find the session route */
runtime = find_session_route(*balancer, r, &route, &sticky, url); runtime = find_session_route(*balancer, r, &route, &sticky, url);
if (runtime) { if (runtime) {
if ((*balancer)->s->lbmethod && (*balancer)->s->lbmethod->updatelbstatus) { if ((*balancer)->lbmethod && (*balancer)->lbmethod->updatelbstatus) {
/* Call the LB implementation */ /* Call the LB implementation */
(*balancer)->s->lbmethod->updatelbstatus(*balancer, runtime, r->server); (*balancer)->lbmethod->updatelbstatus(*balancer, runtime, r->server);
} }
else { /* Use the default one */ else { /* Use the default one */
int i, total_factor = 0; int i, total_factor = 0;
@@ -874,7 +874,7 @@ static int balancer_handler(request_rec *r)
"proxy: BALANCER: (%s). Lock failed for balancer_handler", "proxy: BALANCER: (%s). Lock failed for balancer_handler",
balancer->name); balancer->name);
} }
ap_proxy_update_members(balancer, r->server, conf); ap_proxy_sync_balancer(balancer, r->server, conf);
if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) { if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server, ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Unlock failed for balancer_handler", "proxy: BALANCER: (%s). Unlock failed for balancer_handler",
@@ -975,10 +975,15 @@ static int balancer_handler(request_rec *r)
const char *val; const char *val;
int ival; int ival;
if ((val = apr_table_get(params, "b_lbm"))) { if ((val = apr_table_get(params, "b_lbm"))) {
proxy_balancer_method *lbmethod; if (strlen(val) < (sizeof(bsel->s->lbpname)-1)) {
lbmethod = ap_lookup_provider(PROXY_LBMETHOD, val, "0"); proxy_balancer_method *lbmethod;
if (lbmethod) lbmethod = ap_lookup_provider(PROXY_LBMETHOD, bsel->s->lbpname, "0");
bsel->s->lbmethod = lbmethod; if (lbmethod) {
PROXY_STRNCPY(bsel->s->lbpname, val);
bsel->lbmethod = lbmethod;
bsel->s->wupdated = apr_time_now();
}
}
} }
if ((val = apr_table_get(params, "b_tmo"))) { if ((val = apr_table_get(params, "b_tmo"))) {
ival = atoi(val); ival = atoi(val);
@@ -997,7 +1002,7 @@ static int balancer_handler(request_rec *r)
bsel->s->sticky_force = (ival != 0); bsel->s->sticky_force = (ival != 0);
} }
if ((val = apr_table_get(params, "b_ss")) && *val) { if ((val = apr_table_get(params, "b_ss")) && *val) {
if (strlen(val) < (PROXY_BALANCER_MAX_STICKY_SIZE-1)) { if (strlen(val) < (sizeof(bsel->s->sticky_path)-1)) {
if (*val == '-' && *(val+1) == '\0') if (*val == '-' && *(val+1) == '\0')
*bsel->s->sticky_path = *bsel->s->sticky = '\0'; *bsel->s->sticky_path = *bsel->s->sticky = '\0';
else { else {
@@ -1075,9 +1080,9 @@ static int balancer_handler(request_rec *r)
ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server, ap_log_error(APLOG_MARK, APLOG_ERR, rv, r->server,
"proxy: BALANCER: (%s). Unlock failed for adding worker", "proxy: BALANCER: (%s). Unlock failed for adding worker",
bsel->name); bsel->name);
} }
} }
} }
} }
@@ -1156,7 +1161,7 @@ static int balancer_handler(request_rec *r)
apr_time_sec(balancer->s->timeout)); 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'>%d</td>\n", balancer->s->max_attempts);
ap_rprintf(r, "<td align='center'>%s</td>\n", ap_rprintf(r, "<td align='center'>%s</td>\n",
balancer->s->lbmethod->name); balancer->s->lbpname);
ap_rputs("</table>\n<br />", r); ap_rputs("</table>\n<br />", r);
ap_rputs("\n\n<table border='0' style='text-align: left;'><tr>" ap_rputs("\n\n<table border='0' style='text-align: left;'><tr>"
"<th>Worker URL</th>" "<th>Worker URL</th>"
@@ -1244,7 +1249,7 @@ static int balancer_handler(request_rec *r)
pname = (ap_list_provider_names_t *)provs->elts; pname = (ap_list_provider_names_t *)provs->elts;
for (i = 0; i < provs->nelts; i++, pname++) { for (i = 0; i < provs->nelts; i++, pname++) {
ap_rvputs(r,"<option value='", pname->provider_name, "'", NULL); ap_rvputs(r,"<option value='", pname->provider_name, "'", NULL);
if (strcmp(pname->provider_name, bsel->s->lbmethod->name) == 0) if (strcmp(pname->provider_name, bsel->s->lbpname) == 0)
ap_rputs(" selected ", r); ap_rputs(" selected ", r);
ap_rvputs(r, ">", pname->provider_name, "\n", NULL); ap_rvputs(r, ">", pname->provider_name, "\n", NULL);
} }

View File

@@ -1381,23 +1381,24 @@ PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
(*balancer)->workers = apr_array_make(p, 5, sizeof(proxy_worker *)); (*balancer)->workers = apr_array_make(p, 5, sizeof(proxy_worker *));
(*balancer)->gmutex = NULL; (*balancer)->gmutex = NULL;
(*balancer)->tmutex = NULL; (*balancer)->tmutex = NULL;
(*balancer)->lbmethod = lbmethod;
if (do_malloc) if (do_malloc)
bshared = malloc(sizeof(proxy_balancer_shared)); bshared = malloc(sizeof(proxy_balancer_shared));
else else
bshared = apr_palloc(p, sizeof(proxy_balancer_shared)); bshared = apr_palloc(p, sizeof(proxy_balancer_shared));
memset(bshared, 0, sizeof(proxy_balancer_shared)); memset(bshared, 0, sizeof(proxy_balancer_shared));
bshared->lbmethod = lbmethod;
bshared->was_malloced = (do_malloc != 0); bshared->was_malloced = (do_malloc != 0);
PROXY_STRNCPY(bshared->lbpname, "byrequests");
/* Retrieve a UUID and store the nonce for the lifetime of /* Retrieve a UUID and store the nonce for the lifetime of
* the process. */ * the process. */
apr_uuid_get(&uuid); apr_uuid_get(&uuid);
apr_uuid_format(nonce, &uuid); apr_uuid_format(nonce, &uuid);
PROXY_STRNCPY(bshared->nonce, nonce); PROXY_STRNCPY(bshared->nonce, nonce);
(*balancer)->s = bshared; (*balancer)->s = bshared;
return NULL; return NULL;
@@ -1410,6 +1411,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
proxy_balancer_shared *shm, proxy_balancer_shared *shm,
int i) int i)
{ {
proxy_balancer_method *lbmethod;
if (!shm || !balancer->s) if (!shm || !balancer->s)
return APR_EINVAL; return APR_EINVAL;
@@ -1418,6 +1420,10 @@ PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
free(balancer->s); free(balancer->s);
balancer->s = shm; balancer->s = shm;
balancer->s->index = i; balancer->s->index = i;
/* the below should always succeed */
lbmethod = ap_lookup_provider(PROXY_LBMETHOD, balancer->s->lbpname, "0");
if (lbmethod)
balancer->lbmethod = lbmethod;
return APR_SUCCESS; return APR_SUCCESS;
} }
@@ -1442,7 +1448,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balance
"no mutex %s", balancer->name); "no mutex %s", balancer->name);
return APR_EGENERAL; return APR_EGENERAL;
} }
/* Re-open the mutex for the child. */ /* Re-open the mutex for the child. */
rv = apr_global_mutex_child_init(&(balancer->gmutex), rv = apr_global_mutex_child_init(&(balancer->gmutex),
apr_global_mutex_lockfile(balancer->gmutex), apr_global_mutex_lockfile(balancer->gmutex),
@@ -1453,16 +1459,16 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balance
balancer->name); balancer->name);
return rv; return rv;
} }
/* now attach */ /* now attach */
storage->attach(&(balancer->slot), balancer->sname, &size, &num, p); storage->attach(&(balancer->slot), balancer->sname, &size, &num, p);
if (!balancer->slot) { if (!balancer->slot) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, s, "slotmem_attach failed"); ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, s, "slotmem_attach failed");
return APR_EGENERAL; return APR_EGENERAL;
} }
if (balancer->s->lbmethod && balancer->s->lbmethod->reset) if (balancer->lbmethod && balancer->lbmethod->reset)
balancer->s->lbmethod->reset(balancer, s); balancer->lbmethod->reset(balancer, s);
if (balancer->tmutex == NULL) { if (balancer->tmutex == NULL) {
rv = apr_thread_mutex_create(&(balancer->tmutex), APR_THREAD_MUTEX_DEFAULT, p); rv = apr_thread_mutex_create(&(balancer->tmutex), APR_THREAD_MUTEX_DEFAULT, p);
if (rv != APR_SUCCESS) { if (rv != APR_SUCCESS) {
@@ -1470,7 +1476,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balance
"can not create balancer thread mutex"); "can not create balancer thread mutex");
return rv; return rv;
} }
} }
return APR_SUCCESS; return APR_SUCCESS;
} }
@@ -1897,7 +1903,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
"can not create worker thread mutex"); "can not create worker thread mutex");
return rv; return rv;
} }
} }
if (worker->cp == NULL) if (worker->cp == NULL)
init_conn_pool(p, worker); init_conn_pool(p, worker);
if (worker->cp == NULL) { if (worker->cp == NULL) {
@@ -2933,17 +2939,25 @@ PROXY_DECLARE(char *) ap_proxy_parse_wstatus(apr_pool_t *p, proxy_worker *w)
return ret; return ret;
} }
PROXY_DECLARE(apr_status_t) ap_proxy_update_members(proxy_balancer *b, server_rec *s, PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b, server_rec *s,
proxy_server_conf *conf) proxy_server_conf *conf)
{ {
proxy_worker **workers; proxy_worker **workers;
int i; int i;
int index; int index;
proxy_worker_shared *shm; proxy_worker_shared *shm;
proxy_balancer_method *lbmethod;
ap_slotmem_provider_t *storage = b->storage; ap_slotmem_provider_t *storage = b->storage;
if (b->s->wupdated <= b->wupdated) if (b->s->wupdated <= b->wupdated)
return APR_SUCCESS; return APR_SUCCESS;
/* balancer sync */
lbmethod = ap_lookup_provider(PROXY_LBMETHOD, b->s->lbpname, "0");
if (lbmethod) {
b->lbmethod = lbmethod;
}
/* worker sync */
/* /*
* Look thru the list of workers in shm * Look thru the list of workers in shm
* and see which one(s) we are lacking... * and see which one(s) we are lacking...