From: cecilihf Date: Fri, 20 Jul 2007 08:16:44 +0000 (+0000) Subject: Support for multiple cache files. The selecting of a cache file is done by a simple... X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=928bf09e9a7115aea344956986db0ef8445fdf20;p=varnish Support for multiple cache files. The selecting of a cache file is done by a simple round-robin that selects the first file in the list, and then moves it to the end. If the selected file hasn't got enough space, the next one is tried. If no file has enough space, the LRU discarding is applied to the first file in the list. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1724 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index c8d9506d..4c085c74 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -221,7 +221,7 @@ struct storage { * XXX: selected by some kind of heuristics based on size, lifetime * XXX: etc etc. For now we support only one. */ -extern struct stevedore *stevedore; +extern struct stevedore_head *stevedore_h; /* -------------------------------------------------------------------*/ diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index 92fc0b39..24a185a1 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -38,7 +38,7 @@ #include "shmlog.h" #include "cache.h" -struct stevedore *stevedore; +struct stevedore_head *stevedore_h; /*-------------------------------------------------------------------- * XXX: Think more about which order we start things @@ -47,6 +47,7 @@ struct stevedore *stevedore; void child_main(void) { + struct stevedore *st; setbuf(stdout, NULL); setbuf(stderr, NULL); @@ -66,9 +67,11 @@ child_main(void) HSH_Init(); BAN_Init(); - stevedore = heritage.stevedore; - if (stevedore->open != NULL) - stevedore->open(stevedore); + stevedore_h = &heritage.stevedore_h; + TAILQ_FOREACH(st, stevedore_h, stevedore_list) { + if (st->open != NULL) + st->open(st); + } printf("Ready\n"); VSL_stats->start_time = (time_t)TIM_real(); diff --git a/varnish-cache/bin/varnishd/heritage.h b/varnish-cache/bin/varnishd/heritage.h index ad471ec2..d70e3589 100644 --- a/varnish-cache/bin/varnishd/heritage.h +++ b/varnish-cache/bin/varnishd/heritage.h @@ -40,6 +40,7 @@ struct listen_sock { }; TAILQ_HEAD(listen_sock_head, listen_sock); +TAILQ_HEAD(stevedore_head, stevedore); struct heritage { @@ -58,7 +59,7 @@ struct heritage { unsigned vsl_size; /* Storage method */ - struct stevedore *stevedore; + struct stevedore_head stevedore_h; /* Hash method */ struct hash_slinger *hash; diff --git a/varnish-cache/bin/varnishd/stevedore.c b/varnish-cache/bin/varnishd/stevedore.c index e609346f..1ef53f42 100644 --- a/varnish-cache/bin/varnishd/stevedore.c +++ b/varnish-cache/bin/varnishd/stevedore.c @@ -28,19 +28,48 @@ * $Id$ */ +#include +#include +#include + #include "cache.h" +#include "heritage.h" + +extern struct stevedore sma_stevedore; +extern struct stevedore smf_stevedore; + struct storage * STV_alloc(size_t size) { struct storage *st; + struct stevedore *stv, *stv_first; - AN(stevedore); - AN(stevedore->alloc); + /* Simple round robin selecting of a stevedore. + */ + stv_first = TAILQ_FIRST(stevedore_h); + stv = stv_first; + do { + AN(stv->alloc); + st = stv->alloc(stv, size); + TAILQ_REMOVE(stevedore_h, stv, stevedore_list); + TAILQ_INSERT_TAIL(stevedore_h, stv, stevedore_list); + if (st != NULL) + return (st); + } while ((stv = TAILQ_FIRST(stevedore_h)) != stv_first); + + /* No stevedore with enough space is found. Make room in the first + * one in the list, and move it to the end. Ensuring the round-robin. + */ + stv = TAILQ_FIRST(stevedore_h); + TAILQ_REMOVE(stevedore_h, stv, stevedore_list); + TAILQ_INSERT_TAIL(stevedore_h, stv, stevedore_list); + do { - if ((st = stevedore->alloc(stevedore, size)) == NULL) + if ((st = stv->alloc(stv, size)) == NULL) AN(LRU_DiscardOne()); } while (st == NULL); + return (st); } @@ -58,6 +87,47 @@ STV_free(struct storage *st) { AN(st->stevedore); - AN(stevedore->free); + AN(st->stevedore->free); st->stevedore->free(st); } + +static int +cmp_storage(struct stevedore *s, const char *p, const char *q) +{ + if (strlen(s->name) != q - p) + return (1); + if (strncmp(s->name, p, q - p)) + return (1); + return (0); +} + + +void +STV_add(const char *spec) +{ + const char *p, *q; + struct stevedore *stp; + + p = strchr(spec, ','); + if (p == NULL) + q = p = strchr(spec, '\0'); + else + q = p + 1; + xxxassert(p != NULL); + xxxassert(q != NULL); + + stp = malloc(sizeof *stp); + + if (!cmp_storage(&sma_stevedore, spec, p)) { + *stp = sma_stevedore; + } else if (!cmp_storage(&smf_stevedore, spec, p)) { + *stp = smf_stevedore; + } else { + fprintf(stderr, "Unknown storage method \"%.*s\"\n", + (int)(p - spec), spec); + exit (2); + } + TAILQ_INSERT_HEAD(&heritage.stevedore_h, stp, stevedore_list); + if (stp->init != NULL) + stp->init(stp, q); +} diff --git a/varnish-cache/bin/varnishd/stevedore.h b/varnish-cache/bin/varnishd/stevedore.h index f88c11c9..10b7f17a 100644 --- a/varnish-cache/bin/varnishd/stevedore.h +++ b/varnish-cache/bin/varnishd/stevedore.h @@ -28,6 +28,9 @@ * * $Id$ */ + + +#include "queue.h" struct stevedore; struct sess; @@ -49,8 +52,10 @@ struct stevedore { /* private fields */ void *priv; + TAILQ_ENTRY(stevedore) stevedore_list; }; struct storage *STV_alloc(size_t size); void STV_trim(struct storage *st, size_t size); void STV_free(struct storage *st); +void STV_add(const char *spec); diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index aa0495a6..f9fc09ed 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -120,46 +120,6 @@ setup_hash(const char *s_arg) /*--------------------------------------------------------------------*/ -static int -cmp_storage(struct stevedore *s, const char *p, const char *q) -{ - if (strlen(s->name) != q - p) - return (1); - if (strncmp(s->name, p, q - p)) - return (1); - return (0); -} - -static void -setup_storage(const char *s_arg) -{ - const char *p, *q; - struct stevedore *stp; - - p = strchr(s_arg, ','); - if (p == NULL) - q = p = strchr(s_arg, '\0'); - else - q = p + 1; - xxxassert(p != NULL); - xxxassert(q != NULL); - if (!cmp_storage(&sma_stevedore, s_arg, p)) { - stp = &sma_stevedore; - } else if (!cmp_storage(&smf_stevedore, s_arg, p)) { - stp = &smf_stevedore; - } else { - fprintf(stderr, "Unknown storage method \"%.*s\"\n", - (int)(p - s_arg), s_arg); - exit (2); - } - heritage.stevedore = malloc(sizeof *heritage.stevedore); - *heritage.stevedore = *stp; - if (stp->init != NULL) - stp->init(heritage.stevedore, q); -} - -/*--------------------------------------------------------------------*/ - static void usage(void) { @@ -416,6 +376,7 @@ main(int argc, char *argv[]) const char *n_arg = NULL; const char *P_arg = NULL; const char *s_arg = "file"; + int s_arg_given = 0; const char *T_arg = NULL; char *p; struct cli cli[1]; @@ -431,6 +392,8 @@ main(int argc, char *argv[]) cli[0].result = CLIS_OK; TAILQ_INIT(&heritage.socks); + TAILQ_INIT(&heritage.stevedore_h); + mgt_vcc_init(); MCF_ParamInit(cli); @@ -479,7 +442,8 @@ main(int argc, char *argv[]) cli_check(cli); break; case 's': - s_arg = optarg; + s_arg_given = 1; + STV_add(optarg); break; case 't': MCF_ParamSet(cli, "default_ttl", optarg); @@ -570,7 +534,9 @@ main(int argc, char *argv[]) if (C_flag) exit (0); - setup_storage(s_arg); + if (!s_arg_given) + STV_add(s_arg); + setup_hash(h_arg); VSL_MgtInit(SHMLOG_FILENAME, 8*1024*1024);