From 1dc40216f01dd28f6fc34f74c839b22a460d6202 Mon Sep 17 00:00:00 2001 From: phk Date: Tue, 18 Apr 2006 08:23:44 +0000 Subject: [PATCH] Add busyflag and strorage link to objects. Initialize default hasher and stevedore. Give each workerthread an object pointer to be kept populated with a template object for when lookups miss and need to insert one. Add libmd to get MD5, (choice of hash-algorithm to be revisited later) Implement lookup, begin on fetch. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@141 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/Makefile.am | 3 +- varnish-cache/bin/varnishd/cache.h | 5 ++ varnish-cache/bin/varnishd/cache_main.c | 9 +++ varnish-cache/bin/varnishd/cache_pool.c | 86 +++++++++++++++++++--- varnish-cache/bin/varnishd/cache_vcl.c | 10 ++- varnish-cache/include/vcl_lang.h | 10 ++- varnish-cache/lib/libvcl/vcl_fixed_token.c | 10 ++- 7 files changed, 115 insertions(+), 18 deletions(-) diff --git a/varnish-cache/bin/varnishd/Makefile.am b/varnish-cache/bin/varnishd/Makefile.am index 29a35f4f..f8606e92 100644 --- a/varnish-cache/bin/varnishd/Makefile.am +++ b/varnish-cache/bin/varnishd/Makefile.am @@ -29,4 +29,5 @@ varnishd_LDADD = \ $(top_builddir)/lib/libvarnish/libvarnish.la \ $(top_builddir)/lib/libvcl/libvcl.la \ $(top_builddir)/contrib/libevent/libevent.la \ - -lpthread + -lpthread \ + -lmd diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index f42ce40b..1cd7753d 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -10,6 +10,7 @@ struct worker { struct event_base *eb; struct event e1, e2; struct sbuf *sb; + struct object *nobj; }; #else struct worker; @@ -30,6 +31,8 @@ struct hash_slinger { extern struct hash_slinger hsl_slinger; +extern struct hash_slinger *hash; + /* Storage -----------------------------------------------------------*/ struct storage { @@ -52,6 +55,8 @@ struct stevedore { extern struct stevedore sma_stevedore; +extern struct stevedore *stevedore; + /* Prototypes etc ----------------------------------------------------*/ diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index b9680604..224cbf9c 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -25,6 +25,9 @@ static struct event ev_keepalive; static pthread_t vca_thread; +struct hash_slinger *hash; +struct stevedore *stevedore; + pthread_mutex_t sessmtx; /*--------------------------------------------------------------------*/ @@ -114,6 +117,12 @@ child_main(void) eb = event_init(); assert(eb != NULL); + hash = &hsl_slinger; + hash->init(); + + stevedore = &sma_stevedore; + stevedore->init(); + CVCL_Load(heritage.vcl_file, "boot"); cli = cli_setup(eb, heritage.fds[2], heritage.fds[1], 0, cli_proto); diff --git a/varnish-cache/bin/varnishd/cache_pool.c b/varnish-cache/bin/varnishd/cache_pool.c index 678b9f98..23b3e605 100644 --- a/varnish-cache/bin/varnishd/cache_pool.c +++ b/varnish-cache/bin/varnishd/cache_pool.c @@ -3,6 +3,7 @@ */ #include +#include #include #include #include @@ -10,8 +11,10 @@ #include #include #include +#include #include "libvarnish.h" +#include "shmlog.h" #include "vcl_lang.h" #include "cache.h" @@ -19,11 +22,55 @@ static TAILQ_HEAD(, sess) shd = TAILQ_HEAD_INITIALIZER(shd); static pthread_cond_t shdcnd; +static int +LookupSession(struct worker *w, struct sess *sp) +{ + struct object *o; + unsigned char key[16]; + MD5_CTX ctx; + + if (w->nobj == NULL) { + w->nobj = calloc(sizeof *w->nobj, 1); + assert(w->nobj != NULL); + w->nobj->busy = 1; + TAILQ_INIT(&w->nobj->store); + } + + MD5Init(&ctx); + MD5Update(&ctx, sp->http.url, strlen(sp->http.url)); + MD5Final(key, &ctx); + o = hash->lookup(key, w->nobj); + if (o == w->nobj) + w->nobj = NULL; + /* + * XXX: if obj is busy, park session on it + */ + + sp->obj = o; + sp->handling = HND_Unclass; + sp->vcl->lookup_func(sp); + if (sp->handling == HND_Unclass) { + if (o->valid && o->cacheable) + sp->handling = HND_Deliver; + else + sp->handling = HND_Pass; + } + return (0); +} + +static int +FetchSession(struct worker *w, struct sess *sp) +{ + + assert(w == NULL); +} + static void * CacheWorker(void *priv) { struct sess *sp; struct worker w; + int done; memset(&w, 0, sizeof w); w.eb = event_init(); @@ -47,19 +94,36 @@ CacheWorker(void *priv) HttpdAnalyze(sp, 1); sp->backend = sp->vcl->default_backend; - /* Call the VCL program */ + + /* + * Call the VCL recv function. + * Default action is to lookup + */ + sp->handling = HND_Lookup; + sp->vcl->recv_func(sp); - printf("Handling: %d\n", sp->handling); - switch(sp->handling) { - case HND_Unclass: - case HND_Handle: - case HND_Pipe: - PipeSession(&w, sp); - break; - case HND_Pass: - PassSession(&w, sp); - break; + for (done = 0; !done; ) { + printf("Handling: %d\n", sp->handling); + switch(sp->handling) { + case HND_Lookup: + done = LookupSession(&w, sp); + break; + case HND_Fetch: + done = FetchSession(&w, sp); + break; + case HND_Pipe: + PipeSession(&w, sp); + done = 1; + break; + case HND_Pass: + PassSession(&w, sp); + done = 1; + break; + case HND_Unclass: + case HND_Deliver: + assert(sp->handling == HND_Unclass); + } } AZ(pthread_mutex_lock(&sessmtx)); diff --git a/varnish-cache/bin/varnishd/cache_vcl.c b/varnish-cache/bin/varnishd/cache_vcl.c index bb8ca235..16cd0b9e 100644 --- a/varnish-cache/bin/varnishd/cache_vcl.c +++ b/varnish-cache/bin/varnishd/cache_vcl.c @@ -198,5 +198,11 @@ VCL_pass(VCL_FARGS) } void VCL_insert(VCL_FARGS) { } -void VCL_fetch(VCL_FARGS) { } -void VCL_error(VCL_FARGS, unsigned err, const char *str) { } + +void VCL_fetch(VCL_FARGS) { + sess->handling = HND_Fetch; + sess->done++; +} + +void VCL_error(VCL_FARGS, unsigned err, const char *str) { +} diff --git a/varnish-cache/include/vcl_lang.h b/varnish-cache/include/vcl_lang.h index 9b919b16..8afd4be0 100644 --- a/varnish-cache/include/vcl_lang.h +++ b/varnish-cache/include/vcl_lang.h @@ -45,6 +45,10 @@ struct object { unsigned refcnt; unsigned valid; unsigned cacheable; + + unsigned busy; + + TAILQ_HEAD(, storage) store; }; struct sess { @@ -63,9 +67,11 @@ struct sess { enum { HND_Unclass, - HND_Handle, + HND_Deliver, HND_Pass, - HND_Pipe + HND_Pipe, + HND_Lookup, + HND_Fetch } handling; char done; diff --git a/varnish-cache/lib/libvcl/vcl_fixed_token.c b/varnish-cache/lib/libvcl/vcl_fixed_token.c index 792a7be6..1b5becca 100644 --- a/varnish-cache/lib/libvcl/vcl_fixed_token.c +++ b/varnish-cache/lib/libvcl/vcl_fixed_token.c @@ -483,6 +483,10 @@ vcl_output_lang_h(FILE *f) fputs(" unsigned refcnt;\n", f); fputs(" unsigned valid;\n", f); fputs(" unsigned cacheable;\n", f); + fputs("\n", f); + fputs(" unsigned busy;\n", f); + fputs("\n", f); + fputs(" TAILQ_HEAD(, storage) store;\n", f); fputs("};\n", f); fputs("\n", f); fputs("struct sess {\n", f); @@ -501,9 +505,11 @@ vcl_output_lang_h(FILE *f) fputs("\n", f); fputs(" enum {\n", f); fputs(" HND_Unclass,\n", f); - fputs(" HND_Handle,\n", f); + fputs(" HND_Deliver,\n", f); fputs(" HND_Pass,\n", f); - fputs(" HND_Pipe\n", f); + fputs(" HND_Pipe,\n", f); + fputs(" HND_Lookup,\n", f); + fputs(" HND_Fetch\n", f); fputs(" } handling;\n", f); fputs("\n", f); fputs(" char done;\n", f); -- 2.39.5