Skip to content

Commit

Permalink
Add child init hook for cache backends (#290)
Browse files Browse the repository at this point in the history
* Some cache backends might need to call specific code at startup of a process. In the case of Apache mpm_event, child_init is executed after process start (fork) and before starting any threads that will do actual request processing.

* Use single noop child init function for all cache backends

* mapcache_cache_child_init needs a DLL export to not cause linking problems on Windows
  • Loading branch information
marisn committed Jan 19, 2023
1 parent 882c009 commit ac2ac10
Show file tree
Hide file tree
Showing 20 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apache/mod_mapcache.c
Expand Up @@ -307,11 +307,17 @@ static int write_http_response(mapcache_context_apache_request *ctx, mapcache_ht

static void mod_mapcache_child_init(apr_pool_t *pool, server_rec *s)
{
mapcache_context *ctx;
ctx = (mapcache_context*)create_apache_server_context(s,pool);
for( ; s ; s=s->next) {
mapcache_server_cfg* cfg = ap_get_module_config(s->module_config, &mapcache_module);
int i,rv;
for(i=0;i<cfg->aliases->nelts;i++) {
mapcache_alias_entry *alias_entry = APR_ARRAY_IDX(cfg->aliases,i,mapcache_alias_entry*);
mapcache_cache_child_init(ctx,alias_entry->cfg,pool);
if (GC_HAS_ERROR(ctx)) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, "%s", ctx->get_error_message(ctx));
}
rv = mapcache_connection_pool_create(alias_entry->cfg, &(alias_entry->cp),pool);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating a child process mapcache connection pool on server %s for alias %s", s->server_hostname, alias_entry->endpoint);
if(rv!=APR_SUCCESS) {
Expand All @@ -320,6 +326,10 @@ static void mod_mapcache_child_init(apr_pool_t *pool, server_rec *s)
}
for(i=0;i<cfg->quickaliases->nelts;i++) {
mapcache_alias_entry *alias_entry = APR_ARRAY_IDX(cfg->quickaliases,i,mapcache_alias_entry*);
mapcache_cache_child_init(ctx,alias_entry->cfg,pool);
if (GC_HAS_ERROR(ctx)) {
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, "%s", ctx->get_error_message(ctx));
}
rv = mapcache_connection_pool_create(alias_entry->cfg, &(alias_entry->cp),pool);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating a child process mapcache connection pool on server %s for alias %s", s->server_hostname, alias_entry->endpoint);
if(rv!=APR_SUCCESS) {
Expand Down
2 changes: 2 additions & 0 deletions cgi/mapcache.c
Expand Up @@ -213,6 +213,8 @@ static void load_config(mapcache_context *ctx, char *filename)
apr_pool_destroy(config_pool);
}
config_pool = tmp_config_pool;
mapcache_cache_child_init(ctx,cfg,config_pool);
if (GC_HAS_ERROR(ctx)) goto failed_load;
mapcache_connection_pool_create(cfg, &ctx->connection_pool, config_pool);

return;
Expand Down
2 changes: 2 additions & 0 deletions contrib/mapcache_detail/mapcache_detail.c
Expand Up @@ -714,6 +714,8 @@ int main(int argc, char * argv[])
mapcache_context_init(&ctx);
ctx.config = mapcache_configuration_create(ctx.pool);
ctx.log = mapcache_log;
mapcache_cache_child_init(&ctx,ctx.config,ctx.pool);
if (GC_HAS_ERROR(&ctx)) goto failure;
mapcache_connection_pool_create(ctx.config, &ctx.connection_pool, ctx.pool);


Expand Down
4 changes: 4 additions & 0 deletions include/mapcache.h
Expand Up @@ -369,6 +369,7 @@ struct mapcache_cache {

void (*configuration_parse_xml)(mapcache_context *ctx, ezxml_t xml, mapcache_cache * cache, mapcache_cfg *config);
void (*configuration_post_config)(mapcache_context *ctx, mapcache_cache * cache, mapcache_cfg *config);
void (*child_init)(mapcache_context *ctx, mapcache_cache *cache, apr_pool_t *pchild);
};

MS_DLL_EXPORT int mapcache_cache_tile_get(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile);
Expand All @@ -377,6 +378,9 @@ MS_DLL_EXPORT int mapcache_cache_tile_exists(mapcache_context *ctx, mapcache_cac
MS_DLL_EXPORT void mapcache_cache_tile_set(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile);
void mapcache_cache_tile_multi_set(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tiles, int ntiles);

MS_DLL_EXPORT void mapcache_cache_child_init(mapcache_context *ctx, mapcache_cfg *config, apr_pool_t *pchild);
static inline void mapcache_cache_child_init_noop(mapcache_context *ctx, mapcache_cache *cache, apr_pool_t *pchild) {
};


/**
Expand Down
13 changes: 13 additions & 0 deletions lib/cache.c
Expand Up @@ -174,3 +174,16 @@ void mapcache_cache_tile_multi_set(mapcache_context *ctx, mapcache_cache *cache,
}
}
}

void mapcache_cache_child_init(mapcache_context *ctx, mapcache_cfg *config, apr_pool_t *pchild)
{
apr_hash_index_t *cachei = apr_hash_first(pchild,config->caches);
while(cachei) {
mapcache_cache *cache;
const void *key;
apr_ssize_t keylen;
apr_hash_this(cachei,&key,&keylen,(void**)&cache);
cache->child_init(ctx,cache,pchild);
cachei = apr_hash_next(cachei);
}
}
1 change: 1 addition & 0 deletions lib/cache_bdb.c
Expand Up @@ -417,6 +417,7 @@ mapcache_cache* mapcache_cache_bdb_create(mapcache_context *ctx)
cache->cache._tile_multi_set = _mapcache_cache_bdb_multiset;
cache->cache.configuration_post_config = _mapcache_cache_bdb_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_bdb_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
cache->basedir = NULL;
cache->key_template = NULL;
return (mapcache_cache*)cache;
Expand Down
1 change: 1 addition & 0 deletions lib/cache_composite.c
Expand Up @@ -262,5 +262,6 @@ mapcache_cache* mapcache_cache_composite_create(mapcache_context *ctx)
cache->cache._tile_multi_set = _mapcache_cache_composite_tile_multi_set;
cache->cache.configuration_post_config = _mapcache_cache_composite_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_composite_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
return (mapcache_cache*)cache;
}
1 change: 1 addition & 0 deletions lib/cache_couchbase.c
Expand Up @@ -466,6 +466,7 @@ mapcache_cache* mapcache_cache_couchbase_create(mapcache_context *ctx) {
cache->cache.tile_delete = _mapcache_cache_couchbase_delete;
cache->cache.configuration_parse_xml = _mapcache_cache_couchbase_configuration_parse_xml;
cache->cache.configuration_post_config = _mapcache_cache_couchbase_configuration_post_config;
cache->cache.child_init = mapcache_cache_child_init_noop;
cache->host = NULL;
cache->username = NULL;
cache->password = NULL;
Expand Down
1 change: 1 addition & 0 deletions lib/cache_disk.c
Expand Up @@ -768,6 +768,7 @@ mapcache_cache* mapcache_cache_disk_create(mapcache_context *ctx)
cache->cache._tile_set = _mapcache_cache_disk_set;
cache->cache.configuration_post_config = _mapcache_cache_disk_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_disk_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
return (mapcache_cache*)cache;
}

Expand Down
1 change: 1 addition & 0 deletions lib/cache_fallback.c
Expand Up @@ -190,6 +190,7 @@ mapcache_cache* mapcache_cache_fallback_create(mapcache_context *ctx)
cache->cache._tile_multi_set = _mapcache_cache_fallback_tile_multi_set;
cache->cache.configuration_post_config = _mapcache_cache_fallback_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_fallback_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
return (mapcache_cache*)cache;
}

1 change: 1 addition & 0 deletions lib/cache_memcache.c
Expand Up @@ -370,6 +370,7 @@ mapcache_cache* mapcache_cache_memcache_create(mapcache_context *ctx)
cache->cache._tile_delete = _mapcache_cache_memcache_delete;
cache->cache.configuration_post_config = _mapcache_cache_memcache_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_memcache_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
return (mapcache_cache*)cache;
}

Expand Down
1 change: 1 addition & 0 deletions lib/cache_multitier.c
Expand Up @@ -160,6 +160,7 @@ mapcache_cache* mapcache_cache_multitier_create(mapcache_context *ctx)
cache->cache._tile_multi_set = _mapcache_cache_multitier_tile_multi_set;
cache->cache.configuration_post_config = _mapcache_cache_multitier_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_multitier_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
return (mapcache_cache*)cache;
}

1 change: 1 addition & 0 deletions lib/cache_redis.c
Expand Up @@ -297,6 +297,7 @@ mapcache_cache* mapcache_cache_redis_create(mapcache_context *ctx)
cache->cache._tile_delete = _mapcache_cache_redis_delete;
cache->cache.configuration_post_config = _mapcache_cache_redis_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_redis_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
cache->host = NULL;
cache->port = 6379;
cache->bucket_template = NULL;
Expand Down
1 change: 1 addition & 0 deletions lib/cache_rest.c
Expand Up @@ -1441,6 +1441,7 @@ void mapcache_cache_rest_init(mapcache_context *ctx, mapcache_cache_rest *cache)
cache->cache._tile_set = _mapcache_cache_rest_set;
cache->cache.configuration_post_config = _mapcache_cache_rest_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_rest_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
}
/**
* \brief creates and initializes a mapcache_rest_cache
Expand Down
1 change: 1 addition & 0 deletions lib/cache_riak.c
Expand Up @@ -424,6 +424,7 @@ mapcache_cache* mapcache_cache_riak_create(mapcache_context *ctx) {
cache->cache._tile_delete = _mapcache_cache_riak_delete;
cache->cache.configuration_parse_xml = _mapcache_cache_riak_configuration_parse_xml;
cache->cache.configuration_post_config = _mapcache_cache_riak_configuration_post_config;
cache->cache.child_init = mapcache_cache_child_init_noop;
cache->host = NULL;
cache->port = 8087; // Default RIAK port used for protobuf
cache->bucket_template = NULL;
Expand Down
2 changes: 2 additions & 0 deletions lib/cache_sqlite.c
Expand Up @@ -1086,6 +1086,7 @@ mapcache_cache* mapcache_cache_sqlite_create(mapcache_context *ctx)
cache->cache._tile_multi_set = _mapcache_cache_sqlite_multi_set;
cache->cache.configuration_post_config = _mapcache_cache_sqlite_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_sqlite_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
cache->create_stmt.sql = apr_pstrdup(ctx->pool,
"create table if not exists tiles(tileset text, grid text, x integer, y integer, z integer, data blob, dim text, ctime datetime, primary key(tileset,grid,x,y,z,dim))");
cache->exists_stmt.sql = apr_pstrdup(ctx->pool,
Expand Down Expand Up @@ -1121,6 +1122,7 @@ mapcache_cache* mapcache_cache_mbtiles_create(mapcache_context *ctx)
return NULL;
}
cache->cache.configuration_post_config = _mapcache_cache_mbtiles_configuration_post_config;
cache->cache.child_init = mapcache_cache_child_init_noop;
cache->cache._tile_set = _mapcache_cache_mbtiles_set;
cache->cache._tile_multi_set = _mapcache_cache_mbtiles_multi_set;
cache->cache._tile_delete = _mapcache_cache_mbtiles_delete;
Expand Down
1 change: 1 addition & 0 deletions lib/cache_tiff.c
Expand Up @@ -1397,6 +1397,7 @@ mapcache_cache* mapcache_cache_tiff_create(mapcache_context *ctx)
cache->cache._tile_set = _mapcache_cache_tiff_set;
cache->cache.configuration_post_config = _mapcache_cache_tiff_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_tiff_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
cache->count_x = 10;
cache->count_y = 10;
cache->x_fmt = cache->y_fmt = cache->z_fmt
Expand Down
1 change: 1 addition & 0 deletions lib/cache_tokyocabinet.c
Expand Up @@ -219,6 +219,7 @@ mapcache_cache* mapcache_cache_tc_create(mapcache_context *ctx)
cache->cache.tile_set = _mapcache_cache_tc_set;
cache->cache.configuration_post_config = _mapcache_cache_tc_configuration_post_config;
cache->cache.configuration_parse_xml = _mapcache_cache_tc_configuration_parse_xml;
cache->cache.child_init = mapcache_cache_child_init_noop;
cache->basedir = NULL;
cache->key_template = NULL;
return (mapcache_cache*)cache;
Expand Down
5 changes: 5 additions & 0 deletions nginx/ngx_http_mapcache_module.c
Expand Up @@ -300,6 +300,11 @@ ngx_http_mapcache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "no mapcache <service>s configured/enabled, no point in continuing.");
return NGX_CONF_ERROR;
}
mapcache_cache_child_init(ctx,ctx->cfg,ctx->pool);
if(GC_HAS_ERROR(ctx)) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,ctx->get_error_message(ctx));
return NGX_CONF_ERROR;
}
mapcache_connection_pool_create(ctx->config, &ctx->connection_pool,ctx->pool);
ctx->config->non_blocking = 1;

Expand Down
3 changes: 3 additions & 0 deletions util/mapcache_seed.c
Expand Up @@ -1189,6 +1189,9 @@ int main(int argc, const char **argv)
mapcache_configuration_post_config(&ctx,cfg);
if(ctx.get_error(&ctx))
return usage(argv[0],ctx.get_error_message(&ctx));
mapcache_cache_child_init(&ctx,cfg,ctx.pool);
if (GC_HAS_ERROR(&ctx))
return usage(argv[0],ctx.get_error_message(&ctx));
mapcache_connection_pool_create(cfg, &ctx.connection_pool, ctx.pool);
}

Expand Down

0 comments on commit ac2ac10

Please sign in to comment.