Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes-entries/proxy-mutex-thread.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*) mod_proxy: Use a mutex local to each child process to protect the
run-time workers, instead of a cross-process one. The "proxy" type
of the Mutex directive is still accepted but has no effect anymore.
[Yann Ylavic]
13 changes: 13 additions & 0 deletions docs/manual/mod/core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4000,6 +4000,19 @@ or specified mutexes</description>
<td><module>mod_ldap</module></td>
<td>LDAP result cache</td>
</tr>
<tr>
<td><code>proxy</code></td>
<td><module>mod_proxy</module></td>
<td>none: accepted for compatibility, it has no effect (the
run-time workers are protected by a mutex local to each child
process)</td>
</tr>
<tr>
<td><code>proxy-balancer-shm</code></td>
<td><module>mod_proxy_balancer</module></td>
<td>workers added to a balancer at run time, in shared memory
(balancer-manager)</td>
</tr>
<tr>
<td><code>rewrite-map</code></td>
<td><module>mod_rewrite</module></td>
Expand Down
27 changes: 10 additions & 17 deletions modules/proxy/mod_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ proxy_wstat_t PROXY_DECLARE_DATA proxy_wstat_tbl[] = {
};

static const char * const proxy_id = "proxy";
apr_global_mutex_t *proxy_mutex = NULL;

/*
* A Web proxy module. Stages:
Expand Down Expand Up @@ -3159,12 +3158,11 @@ static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *main_s)
{
server_rec *s = main_s;
apr_status_t rv = ap_global_mutex_create(&proxy_mutex, NULL,
proxy_id, NULL, s, pconf, 0);
if (rv != APR_SUCCESS) {
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(02478)
"failed to create %s mutex", proxy_id);
return rv;
int rc;

rc = proxy_util_post_config(pconf, plog, ptemp, main_s);
if (rc != OK) {
return rc;
}

proxy_ssl_enable = APR_RETRIEVE_OPTIONAL_FN(ssl_proxy_enable);
Expand All @@ -3174,7 +3172,7 @@ static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
ap_proxy_strmatch_domain = apr_strmatch_precompile(pconf, "domain=", 0);

for (; s; s = s->next) {
int rc, i;
int i;
proxy_server_conf *sconf =
ap_get_module_config(s->module_config, &proxy_module);
ap_conf_vector_t **sections =
Expand Down Expand Up @@ -3326,15 +3324,6 @@ static void child_init(apr_pool_t *p, server_rec *s)
proxy_worker *forward = NULL;
proxy_worker *reverse = NULL;

apr_status_t rv = apr_global_mutex_child_init(&proxy_mutex,
apr_global_mutex_lockfile(proxy_mutex),
p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(02479)
"could not init proxy_mutex in child");
exit(1); /* Ugly, but what else? */
}

main_conf = ap_get_module_config(s->module_config, &proxy_module);
for (; s; s = s->next) {
void *sconf = s->module_config;
Expand Down Expand Up @@ -3407,6 +3396,10 @@ static void child_init(apr_pool_t *p, server_rec *s)
static int proxy_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp)
{
/* The "proxy" mutex is a thread mutex now (see proxy_util.c), but the
* type stays registered so that existing Mutex directives naming it are
* still accepted; they have no effect.
*/
apr_status_t rv = ap_mutex_register(pconf, proxy_id, NULL,
APR_LOCK_DEFAULT, 0);
if (rv != APR_SUCCESS) {
Expand Down
12 changes: 10 additions & 2 deletions modules/proxy/mod_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,16 @@ struct proxy_balancer_method {
#define PROXY_THREAD_LOCK(x) ( (x) && (x)->tmutex ? apr_thread_mutex_lock((x)->tmutex) : APR_SUCCESS)
#define PROXY_THREAD_UNLOCK(x) ( (x) && (x)->tmutex ? apr_thread_mutex_unlock((x)->tmutex) : APR_SUCCESS)
#else
#define PROXY_THREAD_LOCK(x) (APR_SUCCESS)
#define PROXY_THREAD_UNLOCK(x) (APR_SUCCESS)
/* Without threads, a function call rather than a constant so that the macros
* can be used as statements without "statement with no effect" warnings
* (internal to these macros, not API).
*/
static APR_INLINE apr_status_t ap_proxy__nolock(void)
{
return APR_SUCCESS;
}
#define PROXY_THREAD_LOCK(x) ((void)(x), ap_proxy__nolock())
#define PROXY_THREAD_UNLOCK(x) ((void)(x), ap_proxy__nolock())
#endif

#define PROXY_GLOBAL_LOCK(x) ( (x) && (x)->gmutex ? apr_global_mutex_lock((x)->gmutex) : APR_SUCCESS)
Expand Down
6 changes: 6 additions & 0 deletions modules/proxy/mod_proxy_balancer.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ static proxy_worker *find_best_worker(proxy_balancer *balancer,
request_rec *r)
{
proxy_worker *candidate = NULL;
#if APR_HAS_THREADS
apr_status_t rv;
#endif

#if APR_HAS_THREADS
if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
Expand Down Expand Up @@ -466,7 +468,9 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
proxy_worker *runtime;
char *route = NULL;
const char *sticky = NULL;
#if APR_HAS_THREADS
apr_status_t rv;
#endif

*worker = NULL;
/* Step 1: check if the url is for us
Expand Down Expand Up @@ -640,7 +644,9 @@ static int proxy_balancer_post_request(proxy_worker *worker,
proxy_server_conf *conf)
{

#if APR_HAS_THREADS
apr_status_t rv;
#endif

#if APR_HAS_THREADS
if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
Expand Down
57 changes: 45 additions & 12 deletions modules/proxy/proxy_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,43 @@ static int lb_workers_limit = 0;
const apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_path;
const apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_domain;

extern apr_global_mutex_t *proxy_mutex;
#if APR_HAS_THREADS
/* Serializes, within a process, the definition, initialization and sync of
* the run-time workers. Those are per-process data (the workers and
* balancers, and the pools they are allocated from), so it is a thread
* mutex, created at post_config for each child to inherit unlocked.
*/
static apr_thread_mutex_t *proxy_mutex = NULL;

static APR_INLINE void proxy_mutex_lock(void)
{
apr_thread_mutex_lock(proxy_mutex);
}

static APR_INLINE void proxy_mutex_unlock(void)
{
apr_thread_mutex_unlock(proxy_mutex);
}
#else
#define proxy_mutex_lock() ((void)0)
#define proxy_mutex_unlock() ((void)0)
#endif

int proxy_util_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *main_s)
{
#if APR_HAS_THREADS
apr_status_t rv = apr_thread_mutex_create(&proxy_mutex,
APR_THREAD_MUTEX_DEFAULT,
pconf);
if (rv != APR_SUCCESS) {
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(02478)
"failed to create proxy mutex");
return HTTP_INTERNAL_SERVER_ERROR;
}
#endif
return OK;
}

static const apr_time_t *proxy_start_time; /* epoch for expiring addresses */

Expand Down Expand Up @@ -1346,9 +1382,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,

PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer, server_rec *s, apr_pool_t *p)
{
#if APR_HAS_THREADS
apr_status_t rv = APR_SUCCESS;
#endif
ap_slotmem_provider_t *storage = balancer->storage;
apr_size_t size;
unsigned int num;
Expand Down Expand Up @@ -2259,9 +2293,9 @@ PROXY_DECLARE(char *) ap_proxy_define_worker_ex(apr_pool_t *p,
if (!balancer || ap_state_query(AP_SQ_MAIN_STATE) != AP_SQ_MS_RUN_MPM) {
return define_worker(p, worker, balancer, conf, url, mask);
}
apr_global_mutex_lock(proxy_mutex);
proxy_mutex_lock();
err = define_worker(p, worker, balancer, conf, url, mask);
apr_global_mutex_unlock(proxy_mutex);
proxy_mutex_unlock();
return err;
}

Expand Down Expand Up @@ -2408,7 +2442,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
ap_proxy_worker_get_name(worker));
}
else {
apr_global_mutex_lock(proxy_mutex);
proxy_mutex_lock();
/* Check again after we got the lock if we are still uninitialized */
if (!(AP_VOLATILIZE_T(unsigned int, worker->local_status) & PROXY_WORKER_INITIALIZED)) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00927)
Expand All @@ -2421,7 +2455,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00928)
"can not create worker thread mutex");
apr_global_mutex_unlock(proxy_mutex);
proxy_mutex_unlock();
return rv;
}
}
Expand All @@ -2431,7 +2465,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
if (worker->cp == NULL) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00929)
"can not create connection pool");
apr_global_mutex_unlock(proxy_mutex);
proxy_mutex_unlock();
return APR_EGENERAL;
}

Expand Down Expand Up @@ -2468,7 +2502,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
worker->local_status |= (PROXY_WORKER_INITIALIZED);
}
}
apr_global_mutex_unlock(proxy_mutex);
proxy_mutex_unlock();

}
if (rv == APR_SUCCESS) {
Expand Down Expand Up @@ -4547,11 +4581,10 @@ PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b, server_rec
"that can't grow", b->s->name, index, shm->name);
continue;
}
/* XXX: a thread mutex is maybe enough here */
apr_global_mutex_lock(proxy_mutex);
proxy_mutex_lock();
runtime = apr_array_push(b->workers);
*runtime = apr_pcalloc(conf->pool, sizeof(proxy_worker));
apr_global_mutex_unlock(proxy_mutex);
proxy_mutex_unlock();
(*runtime)->hash = shm->hash;
(*runtime)->balancer = b;
(*runtime)->s = shm;
Expand Down
14 changes: 14 additions & 0 deletions modules/proxy/proxy_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
* walk it holding that lock. The array of an ungrowable balancer never
* changes, and is walked without it.
*/
#if APR_HAS_THREADS
#define PROXY_WORKERS_LOCK(b) ((b)->ungrowable ? APR_SUCCESS : \
PROXY_THREAD_LOCK(b))
#define PROXY_WORKERS_UNLOCK(b) ((b)->ungrowable ? APR_SUCCESS : \
PROXY_THREAD_UNLOCK(b))
#else
#define PROXY_WORKERS_LOCK(b) PROXY_THREAD_LOCK(b)
#define PROXY_WORKERS_UNLOCK(b) PROXY_THREAD_UNLOCK(b)
#endif

/**
* The workers of a balancer to walk, for a walk that does not hold the
Expand All @@ -64,6 +69,15 @@ extern PROXY_DECLARE_DATA const apr_strmatch_pattern *ap_proxy_strmatch_domain;
*/
void proxy_util_register_hooks(apr_pool_t *p);

/**
* Post-config initialization of what proxy_util.c owns (e.g. the mutex
* serializing the definition, initialization and sync of the run-time
* workers in each process), called from mod_proxy's post_config hook with
* the same arguments.
*/
int proxy_util_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *main_s);

/*
* Get the busy counter from the shared worker memory
*
Expand Down
Loading