From a369776b21077a7f64d400f14cc865c07d64f4e4 Mon Sep 17 00:00:00 2001 From: Christophe Jaillet Date: Sat, 15 Aug 2015 22:05:08 +0000 Subject: [PATCH] With the current implementation, it is likely to connect/close a socket with the memcache server for each command sent. The root cause is a too small idle timeout (600 microseconds). Add a new directive, 'MemcacheConnTTL', to control this idle connection timeout with the memcache server(s). Change the default value from 600 usec (!) to 15 sec as per Yann suggestion. I've limited accepted values from 1 to 1800 seconds (half an hour) because internaly, the value passed to 'apr_memcache_server_create' is still in mirco-seconds. PR 58091 ~~~~~~~~~~~~~~~~~~~_ Homemade measurement (on a slighly modified version of httpd) shows a +30% in number of processed requests using memcache to cache /index.html. Comparison made between the 600 usec and 15 sec TTL. Memcache config: default httpd Config: CacheEnable socache / CacheSocache memcache:127.0.0.1 LoadModule mpm_event_module modules/mod_mpm_event.so httpd compiled with: ./configure --enable-mpms-shared=all --with-included-apr --with-mysql --with-libxml2 --enable-modules=reallyall --enable-ssl-ct=no --enable-maintainer-mode --prefix=$HOME/httpd-2.5 httpd and memcache running on the same VM running under Ubuntu 15.04 Load tested using: ab -n 20000 http://127.0.0.1/index.html Creation/closing of connections beetween httpd and memcache confirmed using the telnet connection to memcache and the stats command git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1696105 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES | 5 +++ docs/manual/mod/mod_socache_memcache.xml | 21 +++++++++ modules/cache/mod_socache_memcache.c | 56 ++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index b422730966..0420ab900c 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,11 @@ -*- coding: utf-8 -*- Changes with Apache 2.5.0 + *) mod_socache_memcache: Add the 'MemcacheConnTTL' directive to control how + long to keep idle connections with the memcache server(s). + Change default value from 600 usec (!) to 15 sec. PR 58091 + [Christophe Jaillet] + *) mod_dir: Prevent the internal identifier "httpd/unix-directory" from appearing as a Content-Type response header when requests for a directory are rewritten by mod_rewrite. [Eric Covener] diff --git a/docs/manual/mod/mod_socache_memcache.xml b/docs/manual/mod/mod_socache_memcache.xml index c6c0163a41..c0f3422f27 100644 --- a/docs/manual/mod/mod_socache_memcache.xml +++ b/docs/manual/mod/mod_socache_memcache.xml @@ -52,4 +52,25 @@ + +MemcacheConnTTL +Keepalive time for idle connections +MemcacheConnTTL seconds +MemcacheConnTTL 15 + +server config +virtual host + + + + +

Set the time, in seconds, to keep idle connections with the memcache + server(s) alive (threaded platforms only).

+ +

Valid values for MemcacheConnTTL are integers + from 1 to 1800. That is to say, up to half an hour.

+ +
+
+ diff --git a/modules/cache/mod_socache_memcache.c b/modules/cache/mod_socache_memcache.c index b05b6ed36f..795e23b579 100644 --- a/modules/cache/mod_socache_memcache.c +++ b/modules/cache/mod_socache_memcache.c @@ -51,9 +51,15 @@ #endif #ifndef MC_DEFAULT_SERVER_TTL -#define MC_DEFAULT_SERVER_TTL 600 +#define MC_DEFAULT_SERVER_TTL (15*1000*1000) /* 15 seconds */ #endif +module AP_MODULE_DECLARE_DATA socache_memcache_module; + +typedef struct { + unsigned int ttl; +} socache_mc_svr_cfg; + struct ap_socache_instance_t { const char *servers; apr_memcache_t *mc; @@ -90,6 +96,9 @@ static apr_status_t socache_mc_init(ap_socache_instance_t *ctx, char *split; char *tok; + socache_mc_svr_cfg *sconf = ap_get_module_config(s->module_config, + &socache_memcache_module); + ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit); /* Find all the servers in the first run to get a total count */ @@ -140,7 +149,7 @@ static apr_status_t socache_mc_init(ap_socache_instance_t *ctx, MC_DEFAULT_SERVER_MIN, MC_DEFAULT_SERVER_SMAX, thread_limit, - MC_DEFAULT_SERVER_TTL, + sconf->ttl, &st); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(00788) @@ -310,6 +319,35 @@ static const ap_socache_provider_t socache_mc = { #endif /* HAVE_APU_MEMCACHE */ +static void *create_server_config(apr_pool_t *p, server_rec *s) +{ + socache_mc_svr_cfg *sconf = apr_pcalloc(p, sizeof(socache_mc_svr_cfg)); + + sconf->ttl = MC_DEFAULT_SERVER_TTL; + + return sconf; +} + +static const char *socache_mc_set_ttl(cmd_parms *cmd, void *dummy, + const char *arg) +{ + socache_mc_svr_cfg *sconf = ap_get_module_config(cmd->server->module_config, + &socache_memcache_module); + int i; + + i = atoi(arg); + + if ((i < 1) || (i > 1800)) { + return apr_pstrcat(cmd->pool, cmd->cmd->name, + " must be a number between 1 and 1800.", NULL); + } + + /* apr_memcache_server_create needs a ttl in usec. */ + sconf->ttl = i * 1000 * 1000; + + return NULL; +} + static void register_hooks(apr_pool_t *p) { #ifdef HAVE_APU_MEMCACHE @@ -319,8 +357,18 @@ static void register_hooks(apr_pool_t *p) #endif } +static const command_rec socache_memcache_cmds[] = { + AP_INIT_TAKE1("MemcacheConnTTL", socache_mc_set_ttl, NULL, RSRC_CONF, + "TTL used for the connection with the memcache server(s), in seconds"), + { NULL } +}; + AP_DECLARE_MODULE(socache_memcache) = { STANDARD20_MODULE_STUFF, - NULL, NULL, NULL, NULL, NULL, - register_hooks + NULL, /* create per-dir config structures */ + NULL, /* merge per-dir config structures */ + create_server_config, /* create per-server config structures */ + NULL, /* merge per-server config structures */ + socache_memcache_cmds, /* table of config file commands */ + register_hooks /* register hooks */ }; -- 2.40.0